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