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