]> git.proxmox.com Git - proxmox-widget-toolkit.git/blame - Toolkit.js
follouwp: code cleanup for bond_mode_array
[proxmox-widget-toolkit.git] / 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, {
8 IPAddress: function(v) {
9 return Proxmox.Utils.IP4_match.test(v);
10 },
11 IPAddressText: gettext('Example') + ': 192.168.1.1',
12 IPAddressMask: /[\d\.]/i,
13
14 IPCIDRAddress: function(v) {
15 var result = Proxmox.Utils.IP4_cidr_match.exec(v);
16 // limits according to JSON Schema see
17 // pve-common/src/PVE/JSONSchema.pm
18 return (result !== null && result[1] >= 8 && result[1] <= 32);
19 },
20 IPCIDRAddressText: gettext('Example') + ': 192.168.1.1/24' + "<br>" + gettext('Valid CIDR Range') + ': 8-32',
21 IPCIDRAddressMask: /[\d\.\/]/i,
22
23 IP6Address: function(v) {
24 return Proxmox.Utils.IP6_match.test(v);
25 },
26 IP6AddressText: gettext('Example') + ': 2001:DB8::42',
27 IP6AddressMask: /[A-Fa-f0-9:]/,
28
29 IP6CIDRAddress: function(v) {
30 var result = Proxmox.Utils.IP6_cidr_match.exec(v);
31 // limits according to JSON Schema see
32 // pve-common/src/PVE/JSONSchema.pm
a2fb6c87 33 return (result !== null && result[1] >= 8 && result[1] <= 128);
bb64de6e 34 },
a2fb6c87 35 IP6CIDRAddressText: gettext('Example') + ': 2001:DB8::42/64' + "<br>" + gettext('Valid CIDR Range') + ': 8-128',
bb64de6e
DM
36 IP6CIDRAddressMask: /[A-Fa-f0-9:\/]/,
37
38 IP6PrefixLength: function(v) {
39 return v >= 0 && v <= 128;
40 },
41 IP6PrefixLengthText: gettext('Example') + ': X, where 0 <= X <= 128',
42 IP6PrefixLengthMask: /[0-9]/,
43
44 IP64Address: function(v) {
45 return Proxmox.Utils.IP64_match.test(v);
46 },
47 IP64AddressText: gettext('Example') + ': 192.168.1.1 2001:DB8::42',
48 IP64AddressMask: /[A-Fa-f0-9\.:]/,
49
577b6c75
ML
50 IP64CIDRAddress: function(v) {
51 var result = Proxmox.Utils.IP64_cidr_match.exec(v);
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',
64 IP64CIDRAddressMask: /[A-Fa-f0-9\.:\/]/,
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
8f449655 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 },
81 BridgeNameText: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999',
82
83 BondName: function(v) {
84 return (/^bond\d{1,4}$/).test(v);
85 },
86 BondNameText: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999',
87
88 InterfaceName: function(v) {
89 return (/^[a-z][a-z0-9_]{1,20}$/).test(v);
90 },
91 InterfaceNameText: gettext("Allowed characters") + ": 'a-z', '0-9', '_'" + "<br />" +
92 gettext("Minimum characters") + ": 2" + "<br />" +
93 gettext("Maximum characters") + ": 21" + "<br />" +
94 gettext("Must start with") + ": 'a-z'",
95
bb64de6e
DM
96 StorageId: function(v) {
97 return (/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i).test(v);
98 },
99 StorageIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '-', '_', '.'" + "<br />" +
100 gettext("Minimum characters") + ": 2" + "<br />" +
101 gettext("Must start with") + ": 'A-Z', 'a-z'<br />" +
102 gettext("Must end with") + ": 'A-Z', 'a-z', '0-9'<br />",
103
104 ConfigId: function(v) {
105 return (/^[a-z][a-z0-9\_]+$/i).test(v);
106 },
107 ConfigIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '_'" + "<br />" +
108 gettext("Minimum characters") + ": 2" + "<br />" +
109 gettext("Must start with") + ": " + gettext("letter"),
110
111 HttpProxy: function(v) {
112 return (/^http:\/\/.*$/).test(v);
113 },
114 HttpProxyText: gettext('Example') + ": http://username:password&#64;host:port/",
115
116 DnsName: function(v) {
117 return Proxmox.Utils.DnsName_match.test(v);
118 },
119 DnsNameText: gettext('This is not a valid DNS name'),
120
121 // workaround for https://www.sencha.com/forum/showthread.php?302150
122 proxmoxMail: function(v) {
123 return (/^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/).test(v);
124 },
125 proxmoxMailText: gettext('Example') + ": user@example.com",
126
ba916e58
DC
127 DnsOrIp: function(v) {
128 if (!Proxmox.Utils.DnsName_match.test(v) &&
694a76f6 129 !Proxmox.Utils.IP64_match.test(v)) {
ba916e58
DC
130 return false;
131 }
132
133 return true;
134 },
694a76f6 135 DnsOrIpText: gettext('Not a valid DNS name or IP address.'),
ba916e58 136
bb64de6e
DM
137 HostList: function(v) {
138 var list = v.split(/[\ \,\;]+/);
139 var i;
140 for (i = 0; i < list.length; i++) {
141 if (list[i] == "") {
142 continue;
143 }
144
145 if (!Proxmox.Utils.HostPort_match.test(list[i]) &&
146 !Proxmox.Utils.HostPortBrackets_match.test(list[i]) &&
147 !Proxmox.Utils.IP6_dotnotation_match.test(list[i])) {
148 return false;
149 }
150 }
151
152 return true;
153 },
36704a2f
DM
154 HostListText: gettext('Not a valid list of hosts'),
155
156 password: function(val, field) {
157 if (field.initialPassField) {
158 var pwd = field.up('form').down(
159 '[name=' + field.initialPassField + ']');
160 return (val == pwd.getValue());
161 }
162 return true;
163 },
164
165 passwordText: gettext('Passwords do not match')
bb64de6e
DM
166});
167
aea77b2c
DC
168// Firefox 52+ Touchscreen bug
169// see https://www.sencha.com/forum/showthread.php?336762-Examples-don-t-work-in-Firefox-52-touchscreen/page2
170// and https://bugzilla.proxmox.com/show_bug.cgi?id=1223
171Ext.define('EXTJS_23846.Element', {
172 override: 'Ext.dom.Element'
173}, function(Element) {
174 var supports = Ext.supports,
175 proto = Element.prototype,
176 eventMap = proto.eventMap,
177 additiveEvents = proto.additiveEvents;
178
179 if (Ext.os.is.Desktop && supports.TouchEvents && !supports.PointerEvents) {
180 eventMap.touchstart = 'mousedown';
181 eventMap.touchmove = 'mousemove';
182 eventMap.touchend = 'mouseup';
183 eventMap.touchcancel = 'mouseup';
184
185 additiveEvents.mousedown = 'mousedown';
186 additiveEvents.mousemove = 'mousemove';
187 additiveEvents.mouseup = 'mouseup';
188 additiveEvents.touchstart = 'touchstart';
189 additiveEvents.touchmove = 'touchmove';
190 additiveEvents.touchend = 'touchend';
191 additiveEvents.touchcancel = 'touchcancel';
192
193 additiveEvents.pointerdown = 'mousedown';
194 additiveEvents.pointermove = 'mousemove';
195 additiveEvents.pointerup = 'mouseup';
196 additiveEvents.pointercancel = 'mouseup';
197 }
198});
199
200Ext.define('EXTJS_23846.Gesture', {
201 override: 'Ext.event.publisher.Gesture'
202}, function(Gesture) {
203 var me = Gesture.instance;
204
205 if (Ext.supports.TouchEvents && !Ext.isWebKit && Ext.os.is.Desktop) {
206 me.handledDomEvents.push('mousedown', 'mousemove', 'mouseup');
207 me.registerEvents();
208 }
209});
210
acfbf255
DC
211Ext.define('EXTJS_18900.Pie', {
212 override: 'Ext.chart.series.Pie',
213
214 // from 6.0.2
215 betweenAngle: function (x, a, b) {
216 var pp = Math.PI * 2,
217 offset = this.rotationOffset;
218
219 if (a === b) {
220 return false;
221 }
222
223 if (!this.getClockwise()) {
224 x *= -1;
225 a *= -1;
226 b *= -1;
227 a -= offset;
228 b -= offset;
229 } else {
230 a += offset;
231 b += offset;
232 }
233
234 x -= a;
235 b -= a;
236
237 // Normalize, so that both x and b are in the [0,360) interval.
238 x %= pp;
239 b %= pp;
240 x += pp;
241 b += pp;
242 x %= pp;
243 b %= pp;
244
245 // Because 360 * n angles will be normalized to 0,
246 // we need to treat b === 0 as a special case.
247 return x < b || b === 0;
248 },
249});
250
3c158249
TL
251// we always want the number in x.y format and never in, e.g., x,y
252Ext.define('PVE.form.field.Number', {
253 override: 'Ext.form.field.Number',
254 submitLocaleSeparator: false
255});
256
bb64de6e
DM
257// ExtJs 5-6 has an issue with caching
258// see https://www.sencha.com/forum/showthread.php?308989
259Ext.define('Proxmox.UnderlayPool', {
260 override: 'Ext.dom.UnderlayPool',
261
262 checkOut: function () {
263 var cache = this.cache,
264 len = cache.length,
265 el;
266
267 // do cleanup because some of the objects might have been destroyed
268 while (len--) {
269 if (cache[len].destroyed) {
270 cache.splice(len, 1);
271 }
272 }
273 // end do cleanup
274
275 el = cache.shift();
276
277 if (!el) {
278 el = Ext.Element.create(this.elementConfig);
279 el.setVisibilityMode(2);
280 //<debug>
281 // tell the spec runner to ignore this element when checking if the dom is clean
282 el.dom.setAttribute('data-sticky', true);
283 //</debug>
284 }
285
286 return el;
287 }
288});
289
a2d8b9a9
DC
290// 'Enter' in Textareas and aria multiline fields should not activate the
291// defaultbutton, fixed in extjs 6.0.2
292Ext.define('PVE.panel.Panel', {
293 override: 'Ext.panel.Panel',
294
295 fireDefaultButton: function(e) {
296 if (e.target.getAttribute('aria-multiline') === 'true' ||
297 e.target.tagName === "TEXTAREA") {
298 return true;
299 }
300 return this.callParent(arguments);
301 }
302});
303
bb64de6e
DM
304// if the order of the values are not the same in originalValue and value
305// extjs will not overwrite value, but marks the field dirty and thus
306// the reset button will be enabled (but clicking it changes nothing)
307// so if the arrays are not the same after resetting, we
308// clear and set it
309Ext.define('Proxmox.form.ComboBox', {
310 override: 'Ext.form.field.ComboBox',
311
312 reset: function() {
313 // copied from combobox
314 var me = this;
315 me.callParent();
bb64de6e
DM
316
317 // clear and set when not the same
318 var value = me.getValue();
319 if (Ext.isArray(me.originalValue) && Ext.isArray(value) && !Ext.Array.equals(value, me.originalValue)) {
320 me.clearValue();
321 me.setValue(me.originalValue);
322 }
323 }
324});
325
47d1c80c
TL
326// when refreshing a grid/tree view, restoring the focus moves the view back to
327// the previously focused item. Save scroll position before refocusing.
328Ext.define(null, {
329 override: 'Ext.view.Table',
330
331 jumpToFocus: false,
332
333 saveFocusState: function() {
334 var me = this,
335 store = me.dataSource,
336 actionableMode = me.actionableMode,
337 navModel = me.getNavigationModel(),
338 focusPosition = actionableMode ? me.actionPosition : navModel.getPosition(true),
339 refocusRow, refocusCol;
340
341 if (focusPosition) {
342 // Separate this from the instance that the nav model is using.
343 focusPosition = focusPosition.clone();
344
345 // Exit actionable mode.
346 // We must inform any Actionables that they must relinquish control.
347 // Tabbability must be reset.
348 if (actionableMode) {
349 me.ownerGrid.setActionableMode(false);
350 }
351
352 // Blur the focused descendant, but do not trigger focusLeave.
353 me.el.dom.focus();
354
355 // Exiting actionable mode navigates to the owning cell, so in either focus mode we must
356 // clear the navigation position
357 navModel.setPosition();
358
359 // The following function will attempt to refocus back in the same mode to the same cell
360 // as it was at before based upon the previous record (if it's still inthe store), or the row index.
361 return function() {
362 // If we still have data, attempt to refocus in the same mode.
363 if (store.getCount()) {
364
365 // Adjust expectations of where we are able to refocus according to what kind of destruction
366 // might have been wrought on this view's DOM during focus save.
367 refocusRow = Math.min(focusPosition.rowIdx, me.all.getCount() - 1);
368 refocusCol = Math.min(focusPosition.colIdx, me.getVisibleColumnManager().getColumns().length - 1);
369 focusPosition = new Ext.grid.CellContext(me).setPosition(
370 store.contains(focusPosition.record) ? focusPosition.record : refocusRow, refocusCol);
371
372 if (actionableMode) {
373 me.ownerGrid.setActionableMode(true, focusPosition);
374 } else {
375 me.cellFocused = true;
376
377 // we sometimes want to scroll back to where we were
378 var x = me.getScrollX();
379 var y = me.getScrollY();
380
381 // Pass "preventNavigation" as true so that that does not cause selection.
382 navModel.setPosition(focusPosition, null, null, null, true);
383
384 if (!me.jumpToFocus) {
385 me.scrollTo(x,y);
386 }
387 }
388 }
389 // No rows - focus associated column header
390 else {
391 focusPosition.column.focus();
392 }
393 };
394 }
395 return Ext.emptyFn;
396 }
397});
398
bb64de6e
DM
399// should be fixed with ExtJS 6.0.2, see:
400// https://www.sencha.com/forum/showthread.php?307244-Bug-with-datefield-in-window-with-scroll
401Ext.define('Proxmox.Datepicker', {
402 override: 'Ext.picker.Date',
403 hideMode: 'visibility'
404});
405
a55813d6
DM
406// ExtJS 6.0.1 has no setSubmitValue() (although you find it in the docs).
407// Note: this.submitValue is a boolean flag, whereas getSubmitValue() returns
408// data to be submitted.
1d9804a9
DM
409Ext.define('Proxmox.form.field.Text', {
410 override: 'Ext.form.field.Text',
411
412 setSubmitValue: function(v) {
413 this.submitValue = v;
414 },
1d9804a9
DM
415});
416
1fb41f2e
TL
417// this should be fixed with ExtJS 6.0.2
418// make mousescrolling work in firefox in the containers overflowhandler
419Ext.define(null, {
420 override: 'Ext.layout.container.boxOverflow.Scroller',
421
422 createWheelListener: function() {
423 var me = this;
424 if (Ext.isFirefox) {
425 me.wheelListener = me.layout.innerCt.on('wheel', me.onMouseWheelFirefox, me, {destroyable: true});
426 } else {
427 me.wheelListener = me.layout.innerCt.on('mousewheel', me.onMouseWheel, me, {destroyable: true});
428 }
429 },
430
431 // special wheel handler for firefox. differs from the default onMouseWheel
432 // handler by using deltaY instead of wheelDeltaY and no normalizing,
433 // because it is already
434 onMouseWheelFirefox: function(e) {
435 e.stopEvent();
436 var delta = e.browserEvent.deltaY || 0;
437 this.scrollBy(delta * this.wheelIncrement, false);
438 }
439
440});
441
bb64de6e
DM
442// force alert boxes to be rendered with an Error Icon
443// since Ext.Msg is an object and not a prototype, we need to override it
444// after the framework has been initiated
445Ext.onReady(function() {
446/*jslint confusion: true */
447 Ext.override(Ext.Msg, {
448 alert: function(title, message, fn, scope) {
449 if (Ext.isString(title)) {
450 var config = {
451 title: title,
452 message: message,
453 icon: this.ERROR,
454 buttons: this.OK,
455 fn: fn,
456 scope : scope,
457 minWidth: this.minWidth
458 };
459 return this.show(config);
460 }
461 }
462 });
463/*jslint confusion: false */
464});
465Ext.define('Ext.ux.IFrame', {
466 extend: 'Ext.Component',
467
468 alias: 'widget.uxiframe',
469
470 loadMask: 'Loading...',
471
472 src: 'about:blank',
473
474 renderTpl: [
475 '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>'
476 ],
477 childEls: ['iframeEl'],
478
479 initComponent: function () {
480 this.callParent();
481
482 this.frameName = this.frameName || this.id + '-frame';
483 },
484
485 initEvents : function() {
486 var me = this;
487 me.callParent();
488 me.iframeEl.on('load', me.onLoad, me);
489 },
490
491 initRenderData: function() {
492 return Ext.apply(this.callParent(), {
493 src: this.src,
494 frameName: this.frameName
495 });
496 },
497
498 getBody: function() {
499 var doc = this.getDoc();
500 return doc.body || doc.documentElement;
501 },
502
503 getDoc: function() {
504 try {
505 return this.getWin().document;
506 } catch (ex) {
507 return null;
508 }
509 },
510
511 getWin: function() {
512 var me = this,
513 name = me.frameName,
514 win = Ext.isIE
515 ? me.iframeEl.dom.contentWindow
516 : window.frames[name];
517 return win;
518 },
519
520 getFrame: function() {
521 var me = this;
522 return me.iframeEl.dom;
523 },
524
525 beforeDestroy: function () {
526 this.cleanupListeners(true);
527 this.callParent();
528 },
529
530 cleanupListeners: function(destroying){
531 var doc, prop;
532
533 if (this.rendered) {
534 try {
535 doc = this.getDoc();
536 if (doc) {
537 /*jslint nomen: true*/
538 Ext.get(doc).un(this._docListeners);
539 /*jslint nomen: false*/
540 if (destroying && doc.hasOwnProperty) {
541 for (prop in doc) {
542 if (doc.hasOwnProperty(prop)) {
543 delete doc[prop];
544 }
545 }
546 }
547 }
548 } catch(e) { }
549 }
550 },
551
552 onLoad: function() {
553 var me = this,
554 doc = me.getDoc(),
555 fn = me.onRelayedEvent;
556
557 if (doc) {
558 try {
559 // These events need to be relayed from the inner document (where they stop
560 // bubbling) up to the outer document. This has to be done at the DOM level so
561 // the event reaches listeners on elements like the document body. The effected
562 // mechanisms that depend on this bubbling behavior are listed to the right
563 // of the event.
564 /*jslint nomen: true*/
565 Ext.get(doc).on(
566 me._docListeners = {
567 mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
568 mousemove: fn, // window resize drag detection
569 mouseup: fn, // window resize termination
570 click: fn, // not sure, but just to be safe
571 dblclick: fn, // not sure again
572 scope: me
573 }
574 );
575 /*jslint nomen: false*/
576 } catch(e) {
577 // cannot do this xss
578 }
579
580 // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
581 Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me);
582
583 this.el.unmask();
584 this.fireEvent('load', this);
585
586 } else if (me.src) {
587
588 this.el.unmask();
589 this.fireEvent('error', this);
590 }
591
592
593 },
594
595 onRelayedEvent: function (event) {
596 // relay event from the iframe's document to the document that owns the iframe...
597
598 var iframeEl = this.iframeEl,
599
600 // Get the left-based iframe position
601 iframeXY = iframeEl.getTrueXY(),
602 originalEventXY = event.getXY(),
603
604 // Get the left-based XY position.
605 // This is because the consumer of the injected event will
606 // perform its own RTL normalization.
607 eventXY = event.getTrueXY();
608
609 // the event from the inner document has XY relative to that document's origin,
610 // so adjust it to use the origin of the iframe in the outer document:
611 event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
612
613 event.injectEvent(iframeEl); // blame the iframe for the event...
614
615 event.xy = originalEventXY; // restore the original XY (just for safety)
616 },
617
618 load: function (src) {
619 var me = this,
620 text = me.loadMask,
621 frame = me.getFrame();
622
623 if (me.fireEvent('beforeload', me, src) !== false) {
624 if (text && me.el) {
625 me.el.mask(text);
626 }
627
628 frame.src = me.src = (src || me.src);
629 }
630 }
631});