]> git.proxmox.com Git - proxmox-widget-toolkit.git/blame - src/Toolkit.js
api-viewer: eslint fixes, code cleanups
[proxmox-widget-toolkit.git] / src / Toolkit.js
CommitLineData
bb64de6e
DM
1// ExtJS related things
2
3 // do not send '_dc' parameter
4Ext.Ajax.disableCaching = false;
5
6// custom Vtypes
7Ext.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&#64;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
3c158249
TL
187// we always want the number in x.y format and never in, e.g., x,y
188Ext.define('PVE.form.field.Number', {
189 override: 'Ext.form.field.Number',
01031528 190 submitLocaleSeparator: false,
3c158249
TL
191});
192
bb64de6e
DM
193// ExtJs 5-6 has an issue with caching
194// see https://www.sencha.com/forum/showthread.php?308989
195Ext.define('Proxmox.UnderlayPool', {
196 override: 'Ext.dom.UnderlayPool',
197
01031528 198 checkOut: function() {
05a977a2 199 let cache = this.cache,
bb64de6e
DM
200 len = cache.length,
201 el;
202
203 // do cleanup because some of the objects might have been destroyed
204 while (len--) {
205 if (cache[len].destroyed) {
206 cache.splice(len, 1);
207 }
208 }
209 // end do cleanup
210
211 el = cache.shift();
212
213 if (!el) {
214 el = Ext.Element.create(this.elementConfig);
215 el.setVisibilityMode(2);
216 //<debug>
217 // tell the spec runner to ignore this element when checking if the dom is clean
218 el.dom.setAttribute('data-sticky', true);
219 //</debug>
220 }
221
222 return el;
01031528 223 },
bb64de6e
DM
224});
225
226// if the order of the values are not the same in originalValue and value
227// extjs will not overwrite value, but marks the field dirty and thus
228// the reset button will be enabled (but clicking it changes nothing)
229// so if the arrays are not the same after resetting, we
230// clear and set it
231Ext.define('Proxmox.form.ComboBox', {
232 override: 'Ext.form.field.ComboBox',
233
234 reset: function() {
235 // copied from combobox
05a977a2 236 let me = this;
bb64de6e 237 me.callParent();
bb64de6e
DM
238
239 // clear and set when not the same
05a977a2
TL
240 let value = me.getValue();
241 if (Ext.isArray(me.originalValue) && Ext.isArray(value) &&
242 !Ext.Array.equals(value, me.originalValue)) {
bb64de6e
DM
243 me.clearValue();
244 me.setValue(me.originalValue);
245 }
50032132
DC
246 },
247
248 // we also want to open the trigger on editable comboboxes by default
249 initComponent: function() {
250 let me = this;
251 me.callParent();
252
253 if (me.editable) {
254 // The trigger.picker causes first a focus event on the field then
255 // toggles the selection picker. Thus skip expanding in this case,
256 // else our focus listener expands and the picker.trigger then
257 // collapses it directly afterwards.
258 Ext.override(me.triggers.picker, {
259 onMouseDown: function(e) {
260 // copied "should we focus" check from Ext.form.trigger.Trigger
261 if (e.pointerType !== 'touch' && !this.field.owns(Ext.Element.getActiveElement())) {
262 me.skip_expand_on_focus = true;
263 }
264 this.callParent(arguments);
01031528 265 },
50032132
DC
266 });
267
268 me.on("focus", function(combobox) {
269 if (!combobox.isExpanded && !combobox.skip_expand_on_focus) {
270 combobox.expand();
271 }
272 combobox.skip_expand_on_focus = false;
273 });
274 }
275 },
bb64de6e
DM
276});
277
47d1c80c
TL
278// when refreshing a grid/tree view, restoring the focus moves the view back to
279// the previously focused item. Save scroll position before refocusing.
280Ext.define(null, {
281 override: 'Ext.view.Table',
282
283 jumpToFocus: false,
284
285 saveFocusState: function() {
9c1296e5 286 var me = this,
05a977a2
TL
287 store = me.dataSource,
288 actionableMode = me.actionableMode,
289 navModel = me.getNavigationModel(),
290 focusPosition = actionableMode ? me.actionPosition : navModel.getPosition(true),
9c1296e5
DC
291 activeElement = Ext.fly(Ext.Element.getActiveElement()),
292 focusCell = focusPosition && focusPosition.view === me &&
293 Ext.fly(focusPosition.getCell(true)),
294 refocusRow, refocusCol, record;
295
296 // The navModel may return a position that is in a locked partner, so check that
297 // the focusPosition's cell contains the focus before going forward.
298 // The skipSaveFocusState is set by Actionables which actively control
299 // focus destination. See CellEditing#activateCell.
300 if (!me.skipSaveFocusState && focusCell && focusCell.contains(activeElement)) {
05a977a2
TL
301 // Separate this from the instance that the nav model is using.
302 focusPosition = focusPosition.clone();
303
9c1296e5
DC
304 // While we deactivate the focused element, suspend focus processing on it.
305 activeElement.suspendFocusEvents();
306
307 // Suspend actionable mode.
308 // Each Actionable must silently save its state ready to resume when focus
309 // can be restored but should only do that if the activeElement is not the cell itself,
310 // this happens when the grid is refreshed while one of the actionables is being
311 // deactivated (e.g. Calling view refresh inside CellEditor 'edit' event listener).
312 if (actionableMode && focusCell.dom !== activeElement.dom) {
313 me.suspendActionableMode();
314 } else {
315 // Clear position, otherwise the setPosition on the other side
316 // will be rejected as a no-op if the resumption position is logically
317 // equivalent.
318 actionableMode = false;
319 navModel.setPosition();
05a977a2 320 }
47d1c80c 321
9c1296e5
DC
322 // Do not leave the element in tht state in case refresh fails, and restoration
323 // closure not called.
324 activeElement.resumeFocusEvents();
05a977a2 325
9c1296e5
DC
326 // if the store is expanding or collapsing, we should never scroll the view.
327 if (store.isExpandingOrCollapsing) {
328 return Ext.emptyFn;
329 }
05a977a2
TL
330
331 // The following function will attempt to refocus back in the same mode to the same cell
9c1296e5
DC
332 // as it was at before based upon the previous record (if it's still in the store),
333 // or the row index.
05a977a2 334 return function() {
9c1296e5
DC
335 var all;
336
337 // May have changed due to reconfigure
338 store = me.dataSource;
339
05a977a2
TL
340 // If we still have data, attempt to refocus in the same mode.
341 if (store.getCount()) {
9c1296e5
DC
342 all = me.all;
343
344 // Adjust expectations of where we are able to refocus according to
345 // what kind of destruction might have been wrought on this view's DOM
346 // during focus save.
347 refocusRow =
348 Math.min(Math.max(focusPosition.rowIdx, all.startIndex), all.endIndex);
349
350 refocusCol = Math.min(
351 focusPosition.colIdx,
352 me.getVisibleColumnManager().getColumns().length - 1,
353 );
354
355 record = focusPosition.record;
356
357 focusPosition = new Ext.grid.CellContext(me).setPosition(
358 record && store.contains(record) && !record.isCollapsedPlaceholder
359 ? record
360 : refocusRow,
361 refocusCol,
362 );
363
364 // Maybe there are no cells. eg: all groups collapsed.
365 if (focusPosition.getCell(true)) {
366 if (actionableMode) {
367 me.resumeActionableMode(focusPosition);
368 } else {
369 // we sometimes want to scroll back to where we are
370
371 let x = me.getScrollX();
372 let y = me.getScrollY();
373
374 // Pass "preventNavigation" as true
375 // so that that does not cause selection.
376 navModel.setPosition(focusPosition, null, null, null, true);
377
378 if (!navModel.getPosition()) {
379 focusPosition.column.focus();
380 }
381
382 if (!me.jumpToFocus) {
383 me.scrollTo(x, y);
384 }
47d1c80c 385 }
05a977a2
TL
386 }
387 } else { // No rows - focus associated column header
388 focusPosition.column.focus();
389 }
390 };
391 }
392 return Ext.emptyFn;
01031528 393 },
47d1c80c
TL
394});
395
a55813d6
DM
396// ExtJS 6.0.1 has no setSubmitValue() (although you find it in the docs).
397// Note: this.submitValue is a boolean flag, whereas getSubmitValue() returns
398// data to be submitted.
1d9804a9
DM
399Ext.define('Proxmox.form.field.Text', {
400 override: 'Ext.form.field.Text',
401
402 setSubmitValue: function(v) {
403 this.submitValue = v;
404 },
1d9804a9
DM
405});
406
d78eb5ec
DC
407// make mousescrolling work in firefox in the containers overflowhandler,
408// by using only the 'wheel' event not 'mousewheel'(fixed in 7.3)
409// also reverse the scrolldirection (fixed in 7.3)
410// and reduce the default increment
1fb41f2e
TL
411Ext.define(null, {
412 override: 'Ext.layout.container.boxOverflow.Scroller',
413
d78eb5ec
DC
414 wheelIncrement: 1,
415
416 getWheelDelta: function(e) {
417 return -e.getWheelDelta(e);
418 },
419
420 onOwnerRender: function(owner) {
421 var me = this,
422 scrollable = {
423 isBoxOverflowScroller: true,
424 x: false,
425 y: false,
426 listeners: {
427 scrollend: this.onScrollEnd,
428 scope: this,
429 },
430 };
431
432 // If no obstrusive scrollbars, allow natural scrolling on mobile touch devices
433 if (!Ext.scrollbar.width() && !Ext.platformTags.desktop) {
434 scrollable[owner.layout.horizontal ? 'x' : 'y'] = true;
1fb41f2e 435 } else {
d78eb5ec
DC
436 me.wheelListener = me.layout.innerCt.on(
437 'wheel', me.onMouseWheel, me, { destroyable: true },
438 );
1fb41f2e 439 }
1fb41f2e 440
d78eb5ec 441 owner.setScrollable(scrollable);
01031528 442 },
d78eb5ec
DC
443});
444
445// extj 6.7 reversed mousewheel direction... (fixed in 7.3)
446// https://forum.sencha.com/forum/showthread.php?472517-Mousewheel-scroll-direction-in-numberfield-with-spinners
447// alse use the 'wheel' event instead of 'mousewheel' (fixed in 7.3)
448Ext.define('Proxmox.form.field.Spinner', {
449 override: 'Ext.form.field.Spinner',
450
451 onRender: function() {
452 var me = this,
453 spinnerTrigger = me.getTrigger('spinner');
1fb41f2e 454
d78eb5ec
DC
455 me.callParent();
456
457 // Init up/down arrow keys
458 if (me.keyNavEnabled) {
459 me.spinnerKeyNav = new Ext.util.KeyNav({
460 target: me.inputEl,
461 scope: me,
462 up: me.spinUp,
463 down: me.spinDown,
464 });
465
466 me.inputEl.on({
467 keyup: me.onInputElKeyUp,
468 scope: me,
469 });
470 }
471
472 // Init mouse wheel
473 if (me.mouseWheelEnabled) {
474 me.mon(me.bodyEl, 'wheel', me.onMouseWheel, me);
475 }
476
477 // in v4 spinUpEl/spinDownEl were childEls, now they are children of the trigger.
478 // create references for compatibility
479 me.spinUpEl = spinnerTrigger.upEl;
480 me.spinDownEl = spinnerTrigger.downEl;
481 },
482
483 onMouseWheel: function(e) {
484 var me = this,
485 delta;
486 if (me.hasFocus) {
487 delta = e.getWheelDelta();
488 if (delta > 0) {
489 me.spinDown();
490 } else if (delta < 0) {
491 me.spinUp();
492 }
493 e.stopEvent();
494 me.onSpinEnd();
495 }
496 },
1fb41f2e
TL
497});
498
312310e0
DC
499// add '@' to the valid id
500Ext.define('Proxmox.validIdReOverride', {
501 override: 'Ext.Component',
05a977a2 502 validIdRe: /^[a-z_][a-z0-9\-_@]*$/i,
312310e0
DC
503});
504
98d894d8
DC
505Ext.define('Proxmox.selection.CheckboxModel', {
506 override: 'Ext.selection.CheckboxModel',
507
1f628e9c 508 // [P] use whole checkbox cell to multiselect, not only the checkbox
98d894d8 509 checkSelector: '.x-grid-cell-row-checker',
1f628e9c 510
ea2b3fd7
TL
511 // TODO: remove all optimizations below to an override for parent 'Ext.selection.Model' ??
512
1f628e9c
TL
513 // [ P: optimized to remove all records at once as single remove is O(n^3) slow ]
514 // records can be an index, a record or an array of records
515 doDeselect: function(records, suppressEvent) {
516 var me = this,
517 selected = me.selected,
518 i = 0,
519 len, record,
520 commit;
521 if (me.locked || !me.store) {
522 return false;
523 }
524 if (typeof records === "number") {
525 // No matching record, jump out
526 record = me.store.getAt(records);
527 if (!record) {
528 return false;
529 }
530 records = [
531 record,
532 ];
533 } else if (!Ext.isArray(records)) {
534 records = [
535 records,
536 ];
537 }
538 // [P] a beforedeselection, triggered by me.onSelectChange below, can block removal by
539 // returning false, thus the original implementation removed only here in the commit fn,
540 // which has an abysmal performance O(n^3). As blocking removal is not the norm, go do the
541 // reverse, record blocked records and remove them from the to-be-removed array before
542 // applying it. A FF86 i9-9900K on 10k records goes from >40s to ~33ms for >90% deselection
543 let committed = false;
544 commit = function() {
545 committed = true;
546 if (record === me.selectionStart) {
547 me.selectionStart = null;
548 }
549 };
550 let removalBlocked = [];
551 len = records.length;
552 me.suspendChanges();
553 for (; i < len; i++) {
554 record = records[i];
555 if (me.isSelected(record)) {
556 committed = false;
557 me.onSelectChange(record, false, suppressEvent, commit);
558 if (!committed) {
559 removalBlocked.push(record);
560 }
561 if (me.destroyed) {
562 return false;
563 }
564 }
565 }
566 if (removalBlocked.length > 0) {
567 records.remove(removalBlocked);
568 }
569 selected.remove(records); // [P] FAST(er)
570 me.lastSelected = selected.last();
571 me.resumeChanges();
572 // fire selchange if there was a change and there is no suppressEvent flag
573 me.maybeFireSelectionChange(records.length > 0 && !suppressEvent);
ea2b3fd7
TL
574 return records.length;
575 },
576
577
578 doMultiSelect: function(records, keepExisting, suppressEvent) {
579 var me = this,
580 selected = me.selected,
581 change = false,
582 result, i, len, record, commit;
583
584 if (me.locked) {
585 return;
586 }
587
588 records = !Ext.isArray(records) ? [records] : records;
589 len = records.length;
590 if (!keepExisting && selected.getCount() > 0) {
591 result = me.deselectDuringSelect(records, suppressEvent);
592 if (me.destroyed) {
593 return;
594 }
595 if (result[0]) {
596 // We had a failure during selection, so jump out
597 // Fire selection change if we did deselect anything
598 me.maybeFireSelectionChange(result[1] > 0 && !suppressEvent);
599 return;
600 } else {
601 // Means something has been deselected, so we've had a change
602 change = result[1] > 0;
603 }
604 }
605
606 let gotBlocked, blockedRecords = [];
607 commit = function() {
608 if (!selected.getCount()) {
609 me.selectionStart = record;
610 }
611 gotBlocked = false;
612 change = true;
613 };
614
615 for (i = 0; i < len; i++) {
616 record = records[i];
617 if (me.isSelected(record)) {
618 continue;
619 }
620
621 gotBlocked = true;
622 me.onSelectChange(record, true, suppressEvent, commit);
623 if (me.destroyed) {
624 return;
625 }
626 if (gotBlocked) {
627 blockedRecords.push(record);
628 }
629 }
630 if (blockedRecords.length > 0) {
631 records.remove(blockedRecords);
632 }
633 selected.add(records);
634 me.lastSelected = record;
635
636 // fire selchange if there was a change and there is no suppressEvent flag
637 me.maybeFireSelectionChange(change && !suppressEvent);
638 },
639 deselectDuringSelect: function(toSelect, suppressEvent) {
640 var me = this,
641 selected = me.selected.getRange(),
642 changed = 0,
643 failed = false;
644 // Prevent selection change events from firing, will happen during select
645 me.suspendChanges();
646 me.deselectingDuringSelect = true;
647 let toDeselect = selected.filter(item => !Ext.Array.contains(toSelect, item));
648 if (toDeselect.length > 0) {
649 changed = me.doDeselect(toDeselect, suppressEvent);
650 if (!changed) {
651 failed = true;
652 }
653 if (me.destroyed) {
654 failed = true;
655 changed = 0;
656 }
657 }
658 me.deselectingDuringSelect = false;
659 me.resumeChanges();
660 return [
661 failed,
662 changed,
663 ];
1f628e9c 664 },
98d894d8
DC
665});
666
322de562
DC
667// stop nulling of properties
668Ext.define('Proxmox.Component', {
669 override: 'Ext.Component',
670 clearPropertiesOnDestroy: false,
671});
672
bb64de6e
DM
673// force alert boxes to be rendered with an Error Icon
674// since Ext.Msg is an object and not a prototype, we need to override it
675// after the framework has been initiated
676Ext.onReady(function() {
bb64de6e 677 Ext.override(Ext.Msg, {
05a977a2 678 alert: function(title, message, fn, scope) { // eslint-disable-line consistent-return
bb64de6e 679 if (Ext.isString(title)) {
05a977a2 680 let config = {
bb64de6e
DM
681 title: title,
682 message: message,
683 icon: this.ERROR,
684 buttons: this.OK,
685 fn: fn,
01031528
TL
686 scope: scope,
687 minWidth: this.minWidth,
bb64de6e
DM
688 };
689 return this.show(config);
690 }
01031528 691 },
bb64de6e 692 });
bb64de6e
DM
693});
694Ext.define('Ext.ux.IFrame', {
695 extend: 'Ext.Component',
696
697 alias: 'widget.uxiframe',
698
699 loadMask: 'Loading...',
700
701 src: 'about:blank',
702
703 renderTpl: [
01031528 704 '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>',
bb64de6e
DM
705 ],
706 childEls: ['iframeEl'],
707
01031528 708 initComponent: function() {
bb64de6e
DM
709 this.callParent();
710
711 this.frameName = this.frameName || this.id + '-frame';
712 },
713
01031528 714 initEvents: function() {
05a977a2 715 let me = this;
bb64de6e
DM
716 me.callParent();
717 me.iframeEl.on('load', me.onLoad, me);
718 },
719
720 initRenderData: function() {
721 return Ext.apply(this.callParent(), {
722 src: this.src,
01031528 723 frameName: this.frameName,
bb64de6e
DM
724 });
725 },
726
727 getBody: function() {
05a977a2 728 let doc = this.getDoc();
bb64de6e
DM
729 return doc.body || doc.documentElement;
730 },
731
732 getDoc: function() {
733 try {
734 return this.getWin().document;
735 } catch (ex) {
736 return null;
737 }
738 },
739
740 getWin: function() {
05a977a2 741 let me = this,
bb64de6e
DM
742 name = me.frameName,
743 win = Ext.isIE
744 ? me.iframeEl.dom.contentWindow
745 : window.frames[name];
746 return win;
747 },
748
749 getFrame: function() {
05a977a2 750 let me = this;
bb64de6e
DM
751 return me.iframeEl.dom;
752 },
753
01031528 754 beforeDestroy: function() {
bb64de6e
DM
755 this.cleanupListeners(true);
756 this.callParent();
757 },
758
01031528 759 cleanupListeners: function(destroying) {
05a977a2 760 let doc, prop;
bb64de6e
DM
761
762 if (this.rendered) {
763 try {
764 doc = this.getDoc();
765 if (doc) {
bb64de6e 766 Ext.get(doc).un(this._docListeners);
bb64de6e
DM
767 if (destroying && doc.hasOwnProperty) {
768 for (prop in doc) {
05a977a2 769 if (Object.prototype.hasOwnProperty.call(doc, prop)) {
bb64de6e
DM
770 delete doc[prop];
771 }
772 }
773 }
774 }
05a977a2
TL
775 } catch (e) {
776 // do nothing
777 }
bb64de6e
DM
778 }
779 },
780
781 onLoad: function() {
05a977a2 782 let me = this,
bb64de6e
DM
783 doc = me.getDoc(),
784 fn = me.onRelayedEvent;
785
786 if (doc) {
787 try {
788 // These events need to be relayed from the inner document (where they stop
789 // bubbling) up to the outer document. This has to be done at the DOM level so
790 // the event reaches listeners on elements like the document body. The effected
791 // mechanisms that depend on this bubbling behavior are listed to the right
792 // of the event.
bb64de6e
DM
793 Ext.get(doc).on(
794 me._docListeners = {
795 mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
796 mousemove: fn, // window resize drag detection
01031528
TL
797 mouseup: fn, // window resize termination
798 click: fn, // not sure, but just to be safe
799 dblclick: fn, // not sure again
800 scope: me,
801 },
bb64de6e 802 );
01031528 803 } catch (e) {
bb64de6e
DM
804 // cannot do this xss
805 }
806
807 // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
808 Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me);
809
810 this.el.unmask();
811 this.fireEvent('load', this);
bb64de6e 812 } else if (me.src) {
bb64de6e
DM
813 this.el.unmask();
814 this.fireEvent('error', this);
815 }
bb64de6e
DM
816 },
817
01031528 818 onRelayedEvent: function(event) {
bb64de6e
DM
819 // relay event from the iframe's document to the document that owns the iframe...
820
05a977a2 821 let iframeEl = this.iframeEl,
bb64de6e
DM
822
823 // Get the left-based iframe position
824 iframeXY = iframeEl.getTrueXY(),
825 originalEventXY = event.getXY(),
826
827 // Get the left-based XY position.
828 // This is because the consumer of the injected event will
829 // perform its own RTL normalization.
830 eventXY = event.getTrueXY();
831
832 // the event from the inner document has XY relative to that document's origin,
833 // so adjust it to use the origin of the iframe in the outer document:
834 event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
835
836 event.injectEvent(iframeEl); // blame the iframe for the event...
837
838 event.xy = originalEventXY; // restore the original XY (just for safety)
839 },
840
01031528 841 load: function(src) {
05a977a2 842 let me = this,
bb64de6e
DM
843 text = me.loadMask,
844 frame = me.getFrame();
845
846 if (me.fireEvent('beforeload', me, src) !== false) {
847 if (text && me.el) {
848 me.el.mask(text);
849 }
850
01031528 851 frame.src = me.src = src || me.src;
bb64de6e 852 }
01031528 853 },
bb64de6e 854});