]>
Commit | Line | Data |
---|---|---|
bb64de6e DM |
1 | // ExtJS related things |
2 | ||
3 | // do not send '_dc' parameter | |
4 | Ext.Ajax.disableCaching = false; | |
5 | ||
6 | // custom Vtypes | |
7 | Ext.apply(Ext.form.field.VTypes, { | |
01031528 | 8 | IPAddress: function(v) { |
bb64de6e DM |
9 | return Proxmox.Utils.IP4_match.test(v); |
10 | }, | |
01031528 | 11 | IPAddressText: gettext('Example') + ': 192.168.1.1', |
05a977a2 | 12 | IPAddressMask: /[\d.]/i, |
bb64de6e | 13 | |
01031528 | 14 | IPCIDRAddress: function(v) { |
05a977a2 | 15 | let result = Proxmox.Utils.IP4_cidr_match.exec(v); |
bb64de6e DM |
16 | // limits according to JSON Schema see |
17 | // pve-common/src/PVE/JSONSchema.pm | |
01031528 | 18 | return result !== null && result[1] >= 8 && result[1] <= 32; |
bb64de6e | 19 | }, |
05a977a2 TL |
20 | IPCIDRAddressText: gettext('Example') + ': 192.168.1.1/24<br>' + gettext('Valid CIDR Range') + ': 8-32', |
21 | IPCIDRAddressMask: /[\d./]/i, | |
bb64de6e | 22 | |
01031528 | 23 | IP6Address: function(v) { |
bb64de6e DM |
24 | return Proxmox.Utils.IP6_match.test(v); |
25 | }, | |
01031528 | 26 | IP6AddressText: gettext('Example') + ': 2001:DB8::42', |
bb64de6e DM |
27 | IP6AddressMask: /[A-Fa-f0-9:]/, |
28 | ||
01031528 | 29 | IP6CIDRAddress: function(v) { |
05a977a2 | 30 | let result = Proxmox.Utils.IP6_cidr_match.exec(v); |
bb64de6e DM |
31 | // limits according to JSON Schema see |
32 | // pve-common/src/PVE/JSONSchema.pm | |
01031528 | 33 | return result !== null && result[1] >= 8 && result[1] <= 128; |
bb64de6e | 34 | }, |
05a977a2 TL |
35 | IP6CIDRAddressText: gettext('Example') + ': 2001:DB8::42/64<br>' + gettext('Valid CIDR Range') + ': 8-128', |
36 | IP6CIDRAddressMask: /[A-Fa-f0-9:/]/, | |
bb64de6e | 37 | |
01031528 | 38 | IP6PrefixLength: function(v) { |
bb64de6e DM |
39 | return v >= 0 && v <= 128; |
40 | }, | |
01031528 TL |
41 | IP6PrefixLengthText: gettext('Example') + ': X, where 0 <= X <= 128', |
42 | IP6PrefixLengthMask: /[0-9]/, | |
bb64de6e | 43 | |
01031528 | 44 | IP64Address: function(v) { |
bb64de6e DM |
45 | return Proxmox.Utils.IP64_match.test(v); |
46 | }, | |
01031528 | 47 | IP64AddressText: gettext('Example') + ': 192.168.1.1 2001:DB8::42', |
05a977a2 | 48 | IP64AddressMask: /[A-Fa-f0-9.:]/, |
bb64de6e | 49 | |
577b6c75 | 50 | IP64CIDRAddress: function(v) { |
05a977a2 | 51 | let result = Proxmox.Utils.IP64_cidr_match.exec(v); |
577b6c75 ML |
52 | if (result === null) { |
53 | return false; | |
54 | } | |
55 | if (result[1] !== undefined) { | |
56 | return result[1] >= 8 && result[1] <= 128; | |
57 | } else if (result[2] !== undefined) { | |
58 | return result[2] >= 8 && result[2] <= 32; | |
59 | } else { | |
60 | return false; | |
61 | } | |
62 | }, | |
63 | IP64CIDRAddressText: gettext('Example') + ': 192.168.1.1/24 2001:DB8::42/64', | |
05a977a2 | 64 | IP64CIDRAddressMask: /[A-Fa-f0-9.:/]/, |
577b6c75 | 65 | |
bb64de6e DM |
66 | MacAddress: function(v) { |
67 | return (/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/).test(v); | |
68 | }, | |
69 | MacAddressMask: /[a-fA-F0-9:]/, | |
70 | MacAddressText: gettext('Example') + ': 01:23:45:67:89:ab', | |
71 | ||
01031528 | 72 | MacPrefix: function(v) { |
add18fad | 73 | return (/^[a-f0-9][02468ace](?::[a-f0-9]{2}){0,2}:?$/i).test(v); |
8f449655 TL |
74 | }, |
75 | MacPrefixMask: /[a-fA-F0-9:]/, | |
ffe26505 | 76 | MacPrefixText: gettext('Example') + ': 02:8f - ' + gettext('only unicast addresses are allowed'), |
8f449655 | 77 | |
bb64de6e DM |
78 | BridgeName: function(v) { |
79 | return (/^vmbr\d{1,4}$/).test(v); | |
80 | }, | |
8aefd47c AD |
81 | VlanName: function(v) { |
82 | if (Proxmox.Utils.VlanInterface_match.test(v)) { | |
83 | return true; | |
84 | } else if (Proxmox.Utils.Vlan_match.test(v)) { | |
85 | return true; | |
86 | } | |
87 | return true; | |
88 | }, | |
bb64de6e DM |
89 | BridgeNameText: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999', |
90 | ||
91 | BondName: function(v) { | |
92 | return (/^bond\d{1,4}$/).test(v); | |
93 | }, | |
94 | BondNameText: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999', | |
95 | ||
96 | InterfaceName: function(v) { | |
97 | return (/^[a-z][a-z0-9_]{1,20}$/).test(v); | |
98 | }, | |
05a977a2 TL |
99 | InterfaceNameText: gettext("Allowed characters") + ": 'a-z', '0-9', '_'<br />" + |
100 | gettext("Minimum characters") + ": 2<br />" + | |
101 | gettext("Maximum characters") + ": 21<br />" + | |
bb64de6e DM |
102 | gettext("Must start with") + ": 'a-z'", |
103 | ||
01031528 | 104 | StorageId: function(v) { |
05a977a2 | 105 | return (/^[a-z][a-z0-9\-_.]*[a-z0-9]$/i).test(v); |
bb64de6e | 106 | }, |
05a977a2 TL |
107 | StorageIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '-', '_', '.'<br />" + |
108 | gettext("Minimum characters") + ": 2<br />" + | |
bb64de6e DM |
109 | gettext("Must start with") + ": 'A-Z', 'a-z'<br />" + |
110 | gettext("Must end with") + ": 'A-Z', 'a-z', '0-9'<br />", | |
111 | ||
01031528 | 112 | ConfigId: function(v) { |
05a977a2 | 113 | return (/^[a-z][a-z0-9_]+$/i).test(v); |
bb64de6e | 114 | }, |
05a977a2 TL |
115 | ConfigIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '_'<br />" + |
116 | gettext("Minimum characters") + ": 2<br />" + | |
bb64de6e DM |
117 | gettext("Must start with") + ": " + gettext("letter"), |
118 | ||
01031528 | 119 | HttpProxy: function(v) { |
bb64de6e DM |
120 | return (/^http:\/\/.*$/).test(v); |
121 | }, | |
122 | HttpProxyText: gettext('Example') + ": http://username:password@host:port/", | |
123 | ||
124 | DnsName: function(v) { | |
125 | return Proxmox.Utils.DnsName_match.test(v); | |
126 | }, | |
127 | DnsNameText: gettext('This is not a valid DNS name'), | |
128 | ||
129 | // workaround for https://www.sencha.com/forum/showthread.php?302150 | |
130 | proxmoxMail: function(v) { | |
05a977a2 | 131 | return (/^(\w+)([-+.][\w]+)*@(\w[-\w]*\.){1,5}([A-Za-z]){2,63}$/).test(v); |
bb64de6e DM |
132 | }, |
133 | proxmoxMailText: gettext('Example') + ": user@example.com", | |
134 | ||
ba916e58 DC |
135 | DnsOrIp: function(v) { |
136 | if (!Proxmox.Utils.DnsName_match.test(v) && | |
694a76f6 | 137 | !Proxmox.Utils.IP64_match.test(v)) { |
ba916e58 DC |
138 | return false; |
139 | } | |
140 | ||
141 | return true; | |
142 | }, | |
694a76f6 | 143 | DnsOrIpText: gettext('Not a valid DNS name or IP address.'), |
ba916e58 | 144 | |
5252b7f1 DC |
145 | HostPort: function(v) { |
146 | return Proxmox.Utils.HostPort_match.test(v) || | |
147 | Proxmox.Utils.HostPortBrackets_match.test(v) || | |
148 | Proxmox.Utils.IP6_dotnotation_match.test(v); | |
149 | }, | |
a9bea104 | 150 | HostPortText: gettext('Host/IP address or optional port is invalid'), |
5252b7f1 | 151 | |
bb64de6e | 152 | HostList: function(v) { |
05a977a2 TL |
153 | let list = v.split(/[ ,;]+/); |
154 | let i; | |
bb64de6e | 155 | for (i = 0; i < list.length; i++) { |
05a977a2 | 156 | if (list[i] === '') { |
bb64de6e DM |
157 | continue; |
158 | } | |
159 | ||
160 | if (!Proxmox.Utils.HostPort_match.test(list[i]) && | |
161 | !Proxmox.Utils.HostPortBrackets_match.test(list[i]) && | |
162 | !Proxmox.Utils.IP6_dotnotation_match.test(list[i])) { | |
163 | return false; | |
164 | } | |
165 | } | |
166 | ||
167 | return true; | |
168 | }, | |
36704a2f DM |
169 | HostListText: gettext('Not a valid list of hosts'), |
170 | ||
171 | password: function(val, field) { | |
172 | if (field.initialPassField) { | |
05a977a2 TL |
173 | let pwd = field.up('form').down(`[name=${field.initialPassField}]`); |
174 | return val === pwd.getValue(); | |
36704a2f DM |
175 | } |
176 | return true; | |
177 | }, | |
178 | ||
01031528 | 179 | passwordText: gettext('Passwords do not match'), |
dcd5a0c6 SI |
180 | |
181 | email: function(value) { | |
df096ae3 | 182 | let emailre = /^[\w+~-]+(\.[\w+~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/; |
dcd5a0c6 SI |
183 | return emailre.test(value); |
184 | }, | |
bb64de6e DM |
185 | }); |
186 | ||
acfbf255 DC |
187 | Ext.define('EXTJS_18900.Pie', { |
188 | override: 'Ext.chart.series.Pie', | |
189 | ||
190 | // from 6.0.2 | |
01031528 | 191 | betweenAngle: function(x, a, b) { |
05a977a2 | 192 | let pp = Math.PI * 2, |
acfbf255 DC |
193 | offset = this.rotationOffset; |
194 | ||
195 | if (a === b) { | |
196 | return false; | |
197 | } | |
198 | ||
199 | if (!this.getClockwise()) { | |
200 | x *= -1; | |
201 | a *= -1; | |
202 | b *= -1; | |
203 | a -= offset; | |
204 | b -= offset; | |
205 | } else { | |
206 | a += offset; | |
207 | b += offset; | |
208 | } | |
209 | ||
210 | x -= a; | |
211 | b -= a; | |
212 | ||
213 | // Normalize, so that both x and b are in the [0,360) interval. | |
214 | x %= pp; | |
215 | b %= pp; | |
216 | x += pp; | |
217 | b += pp; | |
218 | x %= pp; | |
219 | b %= pp; | |
220 | ||
221 | // Because 360 * n angles will be normalized to 0, | |
222 | // we need to treat b === 0 as a special case. | |
223 | return x < b || b === 0; | |
224 | }, | |
225 | }); | |
226 | ||
3c158249 TL |
227 | // we always want the number in x.y format and never in, e.g., x,y |
228 | Ext.define('PVE.form.field.Number', { | |
229 | override: 'Ext.form.field.Number', | |
01031528 | 230 | submitLocaleSeparator: false, |
3c158249 TL |
231 | }); |
232 | ||
bb64de6e DM |
233 | // ExtJs 5-6 has an issue with caching |
234 | // see https://www.sencha.com/forum/showthread.php?308989 | |
235 | Ext.define('Proxmox.UnderlayPool', { | |
236 | override: 'Ext.dom.UnderlayPool', | |
237 | ||
01031528 | 238 | checkOut: function() { |
05a977a2 | 239 | let cache = this.cache, |
bb64de6e DM |
240 | len = cache.length, |
241 | el; | |
242 | ||
243 | // do cleanup because some of the objects might have been destroyed | |
244 | while (len--) { | |
245 | if (cache[len].destroyed) { | |
246 | cache.splice(len, 1); | |
247 | } | |
248 | } | |
249 | // end do cleanup | |
250 | ||
251 | el = cache.shift(); | |
252 | ||
253 | if (!el) { | |
254 | el = Ext.Element.create(this.elementConfig); | |
255 | el.setVisibilityMode(2); | |
256 | //<debug> | |
257 | // tell the spec runner to ignore this element when checking if the dom is clean | |
258 | el.dom.setAttribute('data-sticky', true); | |
259 | //</debug> | |
260 | } | |
261 | ||
262 | return el; | |
01031528 | 263 | }, |
bb64de6e DM |
264 | }); |
265 | ||
a2d8b9a9 DC |
266 | // 'Enter' in Textareas and aria multiline fields should not activate the |
267 | // defaultbutton, fixed in extjs 6.0.2 | |
268 | Ext.define('PVE.panel.Panel', { | |
269 | override: 'Ext.panel.Panel', | |
270 | ||
271 | fireDefaultButton: function(e) { | |
272 | if (e.target.getAttribute('aria-multiline') === 'true' || | |
273 | e.target.tagName === "TEXTAREA") { | |
274 | return true; | |
275 | } | |
276 | return this.callParent(arguments); | |
01031528 | 277 | }, |
a2d8b9a9 DC |
278 | }); |
279 | ||
bb64de6e DM |
280 | // if the order of the values are not the same in originalValue and value |
281 | // extjs will not overwrite value, but marks the field dirty and thus | |
282 | // the reset button will be enabled (but clicking it changes nothing) | |
283 | // so if the arrays are not the same after resetting, we | |
284 | // clear and set it | |
285 | Ext.define('Proxmox.form.ComboBox', { | |
286 | override: 'Ext.form.field.ComboBox', | |
287 | ||
288 | reset: function() { | |
289 | // copied from combobox | |
05a977a2 | 290 | let me = this; |
bb64de6e | 291 | me.callParent(); |
bb64de6e DM |
292 | |
293 | // clear and set when not the same | |
05a977a2 TL |
294 | let value = me.getValue(); |
295 | if (Ext.isArray(me.originalValue) && Ext.isArray(value) && | |
296 | !Ext.Array.equals(value, me.originalValue)) { | |
bb64de6e DM |
297 | me.clearValue(); |
298 | me.setValue(me.originalValue); | |
299 | } | |
50032132 DC |
300 | }, |
301 | ||
302 | // we also want to open the trigger on editable comboboxes by default | |
303 | initComponent: function() { | |
304 | let me = this; | |
305 | me.callParent(); | |
306 | ||
307 | if (me.editable) { | |
308 | // The trigger.picker causes first a focus event on the field then | |
309 | // toggles the selection picker. Thus skip expanding in this case, | |
310 | // else our focus listener expands and the picker.trigger then | |
311 | // collapses it directly afterwards. | |
312 | Ext.override(me.triggers.picker, { | |
313 | onMouseDown: function(e) { | |
314 | // copied "should we focus" check from Ext.form.trigger.Trigger | |
315 | if (e.pointerType !== 'touch' && !this.field.owns(Ext.Element.getActiveElement())) { | |
316 | me.skip_expand_on_focus = true; | |
317 | } | |
318 | this.callParent(arguments); | |
01031528 | 319 | }, |
50032132 DC |
320 | }); |
321 | ||
322 | me.on("focus", function(combobox) { | |
323 | if (!combobox.isExpanded && !combobox.skip_expand_on_focus) { | |
324 | combobox.expand(); | |
325 | } | |
326 | combobox.skip_expand_on_focus = false; | |
327 | }); | |
328 | } | |
329 | }, | |
bb64de6e DM |
330 | }); |
331 | ||
47d1c80c TL |
332 | // when refreshing a grid/tree view, restoring the focus moves the view back to |
333 | // the previously focused item. Save scroll position before refocusing. | |
334 | Ext.define(null, { | |
335 | override: 'Ext.view.Table', | |
336 | ||
337 | jumpToFocus: false, | |
338 | ||
339 | saveFocusState: function() { | |
9c1296e5 | 340 | var me = this, |
05a977a2 TL |
341 | store = me.dataSource, |
342 | actionableMode = me.actionableMode, | |
343 | navModel = me.getNavigationModel(), | |
344 | focusPosition = actionableMode ? me.actionPosition : navModel.getPosition(true), | |
9c1296e5 DC |
345 | activeElement = Ext.fly(Ext.Element.getActiveElement()), |
346 | focusCell = focusPosition && focusPosition.view === me && | |
347 | Ext.fly(focusPosition.getCell(true)), | |
348 | refocusRow, refocusCol, record; | |
349 | ||
350 | // The navModel may return a position that is in a locked partner, so check that | |
351 | // the focusPosition's cell contains the focus before going forward. | |
352 | // The skipSaveFocusState is set by Actionables which actively control | |
353 | // focus destination. See CellEditing#activateCell. | |
354 | if (!me.skipSaveFocusState && focusCell && focusCell.contains(activeElement)) { | |
05a977a2 TL |
355 | // Separate this from the instance that the nav model is using. |
356 | focusPosition = focusPosition.clone(); | |
357 | ||
9c1296e5 DC |
358 | // While we deactivate the focused element, suspend focus processing on it. |
359 | activeElement.suspendFocusEvents(); | |
360 | ||
361 | // Suspend actionable mode. | |
362 | // Each Actionable must silently save its state ready to resume when focus | |
363 | // can be restored but should only do that if the activeElement is not the cell itself, | |
364 | // this happens when the grid is refreshed while one of the actionables is being | |
365 | // deactivated (e.g. Calling view refresh inside CellEditor 'edit' event listener). | |
366 | if (actionableMode && focusCell.dom !== activeElement.dom) { | |
367 | me.suspendActionableMode(); | |
368 | } else { | |
369 | // Clear position, otherwise the setPosition on the other side | |
370 | // will be rejected as a no-op if the resumption position is logically | |
371 | // equivalent. | |
372 | actionableMode = false; | |
373 | navModel.setPosition(); | |
05a977a2 | 374 | } |
47d1c80c | 375 | |
9c1296e5 DC |
376 | // Do not leave the element in tht state in case refresh fails, and restoration |
377 | // closure not called. | |
378 | activeElement.resumeFocusEvents(); | |
05a977a2 | 379 | |
9c1296e5 DC |
380 | // if the store is expanding or collapsing, we should never scroll the view. |
381 | if (store.isExpandingOrCollapsing) { | |
382 | return Ext.emptyFn; | |
383 | } | |
05a977a2 TL |
384 | |
385 | // The following function will attempt to refocus back in the same mode to the same cell | |
9c1296e5 DC |
386 | // as it was at before based upon the previous record (if it's still in the store), |
387 | // or the row index. | |
05a977a2 | 388 | return function() { |
9c1296e5 DC |
389 | var all; |
390 | ||
391 | // May have changed due to reconfigure | |
392 | store = me.dataSource; | |
393 | ||
05a977a2 TL |
394 | // If we still have data, attempt to refocus in the same mode. |
395 | if (store.getCount()) { | |
9c1296e5 DC |
396 | all = me.all; |
397 | ||
398 | // Adjust expectations of where we are able to refocus according to | |
399 | // what kind of destruction might have been wrought on this view's DOM | |
400 | // during focus save. | |
401 | refocusRow = | |
402 | Math.min(Math.max(focusPosition.rowIdx, all.startIndex), all.endIndex); | |
403 | ||
404 | refocusCol = Math.min( | |
405 | focusPosition.colIdx, | |
406 | me.getVisibleColumnManager().getColumns().length - 1, | |
407 | ); | |
408 | ||
409 | record = focusPosition.record; | |
410 | ||
411 | focusPosition = new Ext.grid.CellContext(me).setPosition( | |
412 | record && store.contains(record) && !record.isCollapsedPlaceholder | |
413 | ? record | |
414 | : refocusRow, | |
415 | refocusCol, | |
416 | ); | |
417 | ||
418 | // Maybe there are no cells. eg: all groups collapsed. | |
419 | if (focusPosition.getCell(true)) { | |
420 | if (actionableMode) { | |
421 | me.resumeActionableMode(focusPosition); | |
422 | } else { | |
423 | // we sometimes want to scroll back to where we are | |
424 | ||
425 | let x = me.getScrollX(); | |
426 | let y = me.getScrollY(); | |
427 | ||
428 | // Pass "preventNavigation" as true | |
429 | // so that that does not cause selection. | |
430 | navModel.setPosition(focusPosition, null, null, null, true); | |
431 | ||
432 | if (!navModel.getPosition()) { | |
433 | focusPosition.column.focus(); | |
434 | } | |
435 | ||
436 | if (!me.jumpToFocus) { | |
437 | me.scrollTo(x, y); | |
438 | } | |
47d1c80c | 439 | } |
05a977a2 TL |
440 | } |
441 | } else { // No rows - focus associated column header | |
442 | focusPosition.column.focus(); | |
443 | } | |
444 | }; | |
445 | } | |
446 | return Ext.emptyFn; | |
01031528 | 447 | }, |
47d1c80c TL |
448 | }); |
449 | ||
bb64de6e DM |
450 | // should be fixed with ExtJS 6.0.2, see: |
451 | // https://www.sencha.com/forum/showthread.php?307244-Bug-with-datefield-in-window-with-scroll | |
452 | Ext.define('Proxmox.Datepicker', { | |
453 | override: 'Ext.picker.Date', | |
01031528 | 454 | hideMode: 'visibility', |
bb64de6e DM |
455 | }); |
456 | ||
a55813d6 DM |
457 | // ExtJS 6.0.1 has no setSubmitValue() (although you find it in the docs). |
458 | // Note: this.submitValue is a boolean flag, whereas getSubmitValue() returns | |
459 | // data to be submitted. | |
1d9804a9 DM |
460 | Ext.define('Proxmox.form.field.Text', { |
461 | override: 'Ext.form.field.Text', | |
462 | ||
463 | setSubmitValue: function(v) { | |
464 | this.submitValue = v; | |
465 | }, | |
1d9804a9 DM |
466 | }); |
467 | ||
d78eb5ec DC |
468 | // make mousescrolling work in firefox in the containers overflowhandler, |
469 | // by using only the 'wheel' event not 'mousewheel'(fixed in 7.3) | |
470 | // also reverse the scrolldirection (fixed in 7.3) | |
471 | // and reduce the default increment | |
1fb41f2e TL |
472 | Ext.define(null, { |
473 | override: 'Ext.layout.container.boxOverflow.Scroller', | |
474 | ||
d78eb5ec DC |
475 | wheelIncrement: 1, |
476 | ||
477 | getWheelDelta: function(e) { | |
478 | return -e.getWheelDelta(e); | |
479 | }, | |
480 | ||
481 | onOwnerRender: function(owner) { | |
482 | var me = this, | |
483 | scrollable = { | |
484 | isBoxOverflowScroller: true, | |
485 | x: false, | |
486 | y: false, | |
487 | listeners: { | |
488 | scrollend: this.onScrollEnd, | |
489 | scope: this, | |
490 | }, | |
491 | }; | |
492 | ||
493 | // If no obstrusive scrollbars, allow natural scrolling on mobile touch devices | |
494 | if (!Ext.scrollbar.width() && !Ext.platformTags.desktop) { | |
495 | scrollable[owner.layout.horizontal ? 'x' : 'y'] = true; | |
1fb41f2e | 496 | } else { |
d78eb5ec DC |
497 | me.wheelListener = me.layout.innerCt.on( |
498 | 'wheel', me.onMouseWheel, me, { destroyable: true }, | |
499 | ); | |
1fb41f2e | 500 | } |
1fb41f2e | 501 | |
d78eb5ec | 502 | owner.setScrollable(scrollable); |
01031528 | 503 | }, |
d78eb5ec DC |
504 | }); |
505 | ||
506 | // extj 6.7 reversed mousewheel direction... (fixed in 7.3) | |
507 | // https://forum.sencha.com/forum/showthread.php?472517-Mousewheel-scroll-direction-in-numberfield-with-spinners | |
508 | // alse use the 'wheel' event instead of 'mousewheel' (fixed in 7.3) | |
509 | Ext.define('Proxmox.form.field.Spinner', { | |
510 | override: 'Ext.form.field.Spinner', | |
511 | ||
512 | onRender: function() { | |
513 | var me = this, | |
514 | spinnerTrigger = me.getTrigger('spinner'); | |
1fb41f2e | 515 | |
d78eb5ec DC |
516 | me.callParent(); |
517 | ||
518 | // Init up/down arrow keys | |
519 | if (me.keyNavEnabled) { | |
520 | me.spinnerKeyNav = new Ext.util.KeyNav({ | |
521 | target: me.inputEl, | |
522 | scope: me, | |
523 | up: me.spinUp, | |
524 | down: me.spinDown, | |
525 | }); | |
526 | ||
527 | me.inputEl.on({ | |
528 | keyup: me.onInputElKeyUp, | |
529 | scope: me, | |
530 | }); | |
531 | } | |
532 | ||
533 | // Init mouse wheel | |
534 | if (me.mouseWheelEnabled) { | |
535 | me.mon(me.bodyEl, 'wheel', me.onMouseWheel, me); | |
536 | } | |
537 | ||
538 | // in v4 spinUpEl/spinDownEl were childEls, now they are children of the trigger. | |
539 | // create references for compatibility | |
540 | me.spinUpEl = spinnerTrigger.upEl; | |
541 | me.spinDownEl = spinnerTrigger.downEl; | |
542 | }, | |
543 | ||
544 | onMouseWheel: function(e) { | |
545 | var me = this, | |
546 | delta; | |
547 | if (me.hasFocus) { | |
548 | delta = e.getWheelDelta(); | |
549 | if (delta > 0) { | |
550 | me.spinDown(); | |
551 | } else if (delta < 0) { | |
552 | me.spinUp(); | |
553 | } | |
554 | e.stopEvent(); | |
555 | me.onSpinEnd(); | |
556 | } | |
557 | }, | |
1fb41f2e TL |
558 | }); |
559 | ||
312310e0 DC |
560 | // add '@' to the valid id |
561 | Ext.define('Proxmox.validIdReOverride', { | |
562 | override: 'Ext.Component', | |
05a977a2 | 563 | validIdRe: /^[a-z_][a-z0-9\-_@]*$/i, |
312310e0 DC |
564 | }); |
565 | ||
98d894d8 DC |
566 | Ext.define('Proxmox.selection.CheckboxModel', { |
567 | override: 'Ext.selection.CheckboxModel', | |
568 | ||
1f628e9c | 569 | // [P] use whole checkbox cell to multiselect, not only the checkbox |
98d894d8 | 570 | checkSelector: '.x-grid-cell-row-checker', |
1f628e9c | 571 | |
ea2b3fd7 TL |
572 | // TODO: remove all optimizations below to an override for parent 'Ext.selection.Model' ?? |
573 | ||
1f628e9c TL |
574 | // [ P: optimized to remove all records at once as single remove is O(n^3) slow ] |
575 | // records can be an index, a record or an array of records | |
576 | doDeselect: function(records, suppressEvent) { | |
577 | var me = this, | |
578 | selected = me.selected, | |
579 | i = 0, | |
580 | len, record, | |
581 | commit; | |
582 | if (me.locked || !me.store) { | |
583 | return false; | |
584 | } | |
585 | if (typeof records === "number") { | |
586 | // No matching record, jump out | |
587 | record = me.store.getAt(records); | |
588 | if (!record) { | |
589 | return false; | |
590 | } | |
591 | records = [ | |
592 | record, | |
593 | ]; | |
594 | } else if (!Ext.isArray(records)) { | |
595 | records = [ | |
596 | records, | |
597 | ]; | |
598 | } | |
599 | // [P] a beforedeselection, triggered by me.onSelectChange below, can block removal by | |
600 | // returning false, thus the original implementation removed only here in the commit fn, | |
601 | // which has an abysmal performance O(n^3). As blocking removal is not the norm, go do the | |
602 | // reverse, record blocked records and remove them from the to-be-removed array before | |
603 | // applying it. A FF86 i9-9900K on 10k records goes from >40s to ~33ms for >90% deselection | |
604 | let committed = false; | |
605 | commit = function() { | |
606 | committed = true; | |
607 | if (record === me.selectionStart) { | |
608 | me.selectionStart = null; | |
609 | } | |
610 | }; | |
611 | let removalBlocked = []; | |
612 | len = records.length; | |
613 | me.suspendChanges(); | |
614 | for (; i < len; i++) { | |
615 | record = records[i]; | |
616 | if (me.isSelected(record)) { | |
617 | committed = false; | |
618 | me.onSelectChange(record, false, suppressEvent, commit); | |
619 | if (!committed) { | |
620 | removalBlocked.push(record); | |
621 | } | |
622 | if (me.destroyed) { | |
623 | return false; | |
624 | } | |
625 | } | |
626 | } | |
627 | if (removalBlocked.length > 0) { | |
628 | records.remove(removalBlocked); | |
629 | } | |
630 | selected.remove(records); // [P] FAST(er) | |
631 | me.lastSelected = selected.last(); | |
632 | me.resumeChanges(); | |
633 | // fire selchange if there was a change and there is no suppressEvent flag | |
634 | me.maybeFireSelectionChange(records.length > 0 && !suppressEvent); | |
ea2b3fd7 TL |
635 | return records.length; |
636 | }, | |
637 | ||
638 | ||
639 | doMultiSelect: function(records, keepExisting, suppressEvent) { | |
640 | var me = this, | |
641 | selected = me.selected, | |
642 | change = false, | |
643 | result, i, len, record, commit; | |
644 | ||
645 | if (me.locked) { | |
646 | return; | |
647 | } | |
648 | ||
649 | records = !Ext.isArray(records) ? [records] : records; | |
650 | len = records.length; | |
651 | if (!keepExisting && selected.getCount() > 0) { | |
652 | result = me.deselectDuringSelect(records, suppressEvent); | |
653 | if (me.destroyed) { | |
654 | return; | |
655 | } | |
656 | if (result[0]) { | |
657 | // We had a failure during selection, so jump out | |
658 | // Fire selection change if we did deselect anything | |
659 | me.maybeFireSelectionChange(result[1] > 0 && !suppressEvent); | |
660 | return; | |
661 | } else { | |
662 | // Means something has been deselected, so we've had a change | |
663 | change = result[1] > 0; | |
664 | } | |
665 | } | |
666 | ||
667 | let gotBlocked, blockedRecords = []; | |
668 | commit = function() { | |
669 | if (!selected.getCount()) { | |
670 | me.selectionStart = record; | |
671 | } | |
672 | gotBlocked = false; | |
673 | change = true; | |
674 | }; | |
675 | ||
676 | for (i = 0; i < len; i++) { | |
677 | record = records[i]; | |
678 | if (me.isSelected(record)) { | |
679 | continue; | |
680 | } | |
681 | ||
682 | gotBlocked = true; | |
683 | me.onSelectChange(record, true, suppressEvent, commit); | |
684 | if (me.destroyed) { | |
685 | return; | |
686 | } | |
687 | if (gotBlocked) { | |
688 | blockedRecords.push(record); | |
689 | } | |
690 | } | |
691 | if (blockedRecords.length > 0) { | |
692 | records.remove(blockedRecords); | |
693 | } | |
694 | selected.add(records); | |
695 | me.lastSelected = record; | |
696 | ||
697 | // fire selchange if there was a change and there is no suppressEvent flag | |
698 | me.maybeFireSelectionChange(change && !suppressEvent); | |
699 | }, | |
700 | deselectDuringSelect: function(toSelect, suppressEvent) { | |
701 | var me = this, | |
702 | selected = me.selected.getRange(), | |
703 | changed = 0, | |
704 | failed = false; | |
705 | // Prevent selection change events from firing, will happen during select | |
706 | me.suspendChanges(); | |
707 | me.deselectingDuringSelect = true; | |
708 | let toDeselect = selected.filter(item => !Ext.Array.contains(toSelect, item)); | |
709 | if (toDeselect.length > 0) { | |
710 | changed = me.doDeselect(toDeselect, suppressEvent); | |
711 | if (!changed) { | |
712 | failed = true; | |
713 | } | |
714 | if (me.destroyed) { | |
715 | failed = true; | |
716 | changed = 0; | |
717 | } | |
718 | } | |
719 | me.deselectingDuringSelect = false; | |
720 | me.resumeChanges(); | |
721 | return [ | |
722 | failed, | |
723 | changed, | |
724 | ]; | |
1f628e9c | 725 | }, |
98d894d8 DC |
726 | }); |
727 | ||
518dd0b6 DC |
728 | // override the download server url globally, for privacy reasons |
729 | Ext.draw.Container.prototype.defaultDownloadServerUrl = "-"; | |
730 | ||
322de562 DC |
731 | // stop nulling of properties |
732 | Ext.define('Proxmox.Component', { | |
733 | override: 'Ext.Component', | |
734 | clearPropertiesOnDestroy: false, | |
735 | }); | |
736 | ||
bb64de6e DM |
737 | // force alert boxes to be rendered with an Error Icon |
738 | // since Ext.Msg is an object and not a prototype, we need to override it | |
739 | // after the framework has been initiated | |
740 | Ext.onReady(function() { | |
bb64de6e | 741 | Ext.override(Ext.Msg, { |
05a977a2 | 742 | alert: function(title, message, fn, scope) { // eslint-disable-line consistent-return |
bb64de6e | 743 | if (Ext.isString(title)) { |
05a977a2 | 744 | let config = { |
bb64de6e DM |
745 | title: title, |
746 | message: message, | |
747 | icon: this.ERROR, | |
748 | buttons: this.OK, | |
749 | fn: fn, | |
01031528 TL |
750 | scope: scope, |
751 | minWidth: this.minWidth, | |
bb64de6e DM |
752 | }; |
753 | return this.show(config); | |
754 | } | |
01031528 | 755 | }, |
bb64de6e | 756 | }); |
bb64de6e DM |
757 | }); |
758 | Ext.define('Ext.ux.IFrame', { | |
759 | extend: 'Ext.Component', | |
760 | ||
761 | alias: 'widget.uxiframe', | |
762 | ||
763 | loadMask: 'Loading...', | |
764 | ||
765 | src: 'about:blank', | |
766 | ||
767 | renderTpl: [ | |
01031528 | 768 | '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>', |
bb64de6e DM |
769 | ], |
770 | childEls: ['iframeEl'], | |
771 | ||
01031528 | 772 | initComponent: function() { |
bb64de6e DM |
773 | this.callParent(); |
774 | ||
775 | this.frameName = this.frameName || this.id + '-frame'; | |
776 | }, | |
777 | ||
01031528 | 778 | initEvents: function() { |
05a977a2 | 779 | let me = this; |
bb64de6e DM |
780 | me.callParent(); |
781 | me.iframeEl.on('load', me.onLoad, me); | |
782 | }, | |
783 | ||
784 | initRenderData: function() { | |
785 | return Ext.apply(this.callParent(), { | |
786 | src: this.src, | |
01031528 | 787 | frameName: this.frameName, |
bb64de6e DM |
788 | }); |
789 | }, | |
790 | ||
791 | getBody: function() { | |
05a977a2 | 792 | let doc = this.getDoc(); |
bb64de6e DM |
793 | return doc.body || doc.documentElement; |
794 | }, | |
795 | ||
796 | getDoc: function() { | |
797 | try { | |
798 | return this.getWin().document; | |
799 | } catch (ex) { | |
800 | return null; | |
801 | } | |
802 | }, | |
803 | ||
804 | getWin: function() { | |
05a977a2 | 805 | let me = this, |
bb64de6e DM |
806 | name = me.frameName, |
807 | win = Ext.isIE | |
808 | ? me.iframeEl.dom.contentWindow | |
809 | : window.frames[name]; | |
810 | return win; | |
811 | }, | |
812 | ||
813 | getFrame: function() { | |
05a977a2 | 814 | let me = this; |
bb64de6e DM |
815 | return me.iframeEl.dom; |
816 | }, | |
817 | ||
01031528 | 818 | beforeDestroy: function() { |
bb64de6e DM |
819 | this.cleanupListeners(true); |
820 | this.callParent(); | |
821 | }, | |
822 | ||
01031528 | 823 | cleanupListeners: function(destroying) { |
05a977a2 | 824 | let doc, prop; |
bb64de6e DM |
825 | |
826 | if (this.rendered) { | |
827 | try { | |
828 | doc = this.getDoc(); | |
829 | if (doc) { | |
bb64de6e | 830 | Ext.get(doc).un(this._docListeners); |
bb64de6e DM |
831 | if (destroying && doc.hasOwnProperty) { |
832 | for (prop in doc) { | |
05a977a2 | 833 | if (Object.prototype.hasOwnProperty.call(doc, prop)) { |
bb64de6e DM |
834 | delete doc[prop]; |
835 | } | |
836 | } | |
837 | } | |
838 | } | |
05a977a2 TL |
839 | } catch (e) { |
840 | // do nothing | |
841 | } | |
bb64de6e DM |
842 | } |
843 | }, | |
844 | ||
845 | onLoad: function() { | |
05a977a2 | 846 | let me = this, |
bb64de6e DM |
847 | doc = me.getDoc(), |
848 | fn = me.onRelayedEvent; | |
849 | ||
850 | if (doc) { | |
851 | try { | |
852 | // These events need to be relayed from the inner document (where they stop | |
853 | // bubbling) up to the outer document. This has to be done at the DOM level so | |
854 | // the event reaches listeners on elements like the document body. The effected | |
855 | // mechanisms that depend on this bubbling behavior are listed to the right | |
856 | // of the event. | |
bb64de6e DM |
857 | Ext.get(doc).on( |
858 | me._docListeners = { | |
859 | mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront) | |
860 | mousemove: fn, // window resize drag detection | |
01031528 TL |
861 | mouseup: fn, // window resize termination |
862 | click: fn, // not sure, but just to be safe | |
863 | dblclick: fn, // not sure again | |
864 | scope: me, | |
865 | }, | |
bb64de6e | 866 | ); |
01031528 | 867 | } catch (e) { |
bb64de6e DM |
868 | // cannot do this xss |
869 | } | |
870 | ||
871 | // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK! | |
872 | Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me); | |
873 | ||
874 | this.el.unmask(); | |
875 | this.fireEvent('load', this); | |
bb64de6e | 876 | } else if (me.src) { |
bb64de6e DM |
877 | this.el.unmask(); |
878 | this.fireEvent('error', this); | |
879 | } | |
bb64de6e DM |
880 | }, |
881 | ||
01031528 | 882 | onRelayedEvent: function(event) { |
bb64de6e DM |
883 | // relay event from the iframe's document to the document that owns the iframe... |
884 | ||
05a977a2 | 885 | let iframeEl = this.iframeEl, |
bb64de6e DM |
886 | |
887 | // Get the left-based iframe position | |
888 | iframeXY = iframeEl.getTrueXY(), | |
889 | originalEventXY = event.getXY(), | |
890 | ||
891 | // Get the left-based XY position. | |
892 | // This is because the consumer of the injected event will | |
893 | // perform its own RTL normalization. | |
894 | eventXY = event.getTrueXY(); | |
895 | ||
896 | // the event from the inner document has XY relative to that document's origin, | |
897 | // so adjust it to use the origin of the iframe in the outer document: | |
898 | event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]]; | |
899 | ||
900 | event.injectEvent(iframeEl); // blame the iframe for the event... | |
901 | ||
902 | event.xy = originalEventXY; // restore the original XY (just for safety) | |
903 | }, | |
904 | ||
01031528 | 905 | load: function(src) { |
05a977a2 | 906 | let me = this, |
bb64de6e DM |
907 | text = me.loadMask, |
908 | frame = me.getFrame(); | |
909 | ||
910 | if (me.fireEvent('beforeload', me, src) !== false) { | |
911 | if (text && me.el) { | |
912 | me.el.mask(text); | |
913 | } | |
914 | ||
01031528 | 915 | frame.src = me.src = src || me.src; |
bb64de6e | 916 | } |
01031528 | 917 | }, |
bb64de6e | 918 | }); |