]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Toolkit.js
bump version to 4.2.3
[proxmox-widget-toolkit.git] / Toolkit.js
1 // ExtJS related things
2
3 // do not send '_dc' parameter
4 Ext.Ajax.disableCaching = false;
5
6 // custom Vtypes
7 Ext.apply(Ext.form.field.VTypes, {
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
33 return (result !== null && result[1] >= 8 && result[1] <= 128);
34 },
35 IP6CIDRAddressText: gettext('Example') + ': 2001:DB8::42/64' + "<br>" + gettext('Valid CIDR Range') + ': 8-128',
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
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
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
72 MacPrefix: function(v) {
73 return (/^[a-f0-9][02468ace](?::[a-f0-9]{2}){0,2}:?$/i).test(v);
74 },
75 MacPrefixMask: /[a-fA-F0-9:]/,
76 MacPrefixText: gettext('Example') + ': 02:8f - ' + gettext('only unicast addresses are allowed'),
77
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
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
127 DnsOrIp: function(v) {
128 if (!Proxmox.Utils.DnsName_match.test(v) &&
129 !Proxmox.Utils.IP64_match.test(v)) {
130 return false;
131 }
132
133 return true;
134 },
135 DnsOrIpText: gettext('Not a valid DNS name or IP address.'),
136
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 },
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')
166 });
167
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
171 Ext.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
200 Ext.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
211 // we always want the number in x.y format and never in, e.g., x,y
212 Ext.define('PVE.form.field.Number', {
213 override: 'Ext.form.field.Number',
214 submitLocaleSeparator: false
215 });
216
217 // ExtJs 5-6 has an issue with caching
218 // see https://www.sencha.com/forum/showthread.php?308989
219 Ext.define('Proxmox.UnderlayPool', {
220 override: 'Ext.dom.UnderlayPool',
221
222 checkOut: function () {
223 var cache = this.cache,
224 len = cache.length,
225 el;
226
227 // do cleanup because some of the objects might have been destroyed
228 while (len--) {
229 if (cache[len].destroyed) {
230 cache.splice(len, 1);
231 }
232 }
233 // end do cleanup
234
235 el = cache.shift();
236
237 if (!el) {
238 el = Ext.Element.create(this.elementConfig);
239 el.setVisibilityMode(2);
240 //<debug>
241 // tell the spec runner to ignore this element when checking if the dom is clean
242 el.dom.setAttribute('data-sticky', true);
243 //</debug>
244 }
245
246 return el;
247 }
248 });
249
250 // 'Enter' in Textareas and aria multiline fields should not activate the
251 // defaultbutton, fixed in extjs 6.0.2
252 Ext.define('PVE.panel.Panel', {
253 override: 'Ext.panel.Panel',
254
255 fireDefaultButton: function(e) {
256 if (e.target.getAttribute('aria-multiline') === 'true' ||
257 e.target.tagName === "TEXTAREA") {
258 return true;
259 }
260 return this.callParent(arguments);
261 }
262 });
263
264 // if the order of the values are not the same in originalValue and value
265 // extjs will not overwrite value, but marks the field dirty and thus
266 // the reset button will be enabled (but clicking it changes nothing)
267 // so if the arrays are not the same after resetting, we
268 // clear and set it
269 Ext.define('Proxmox.form.ComboBox', {
270 override: 'Ext.form.field.ComboBox',
271
272 reset: function() {
273 // copied from combobox
274 var me = this;
275 me.callParent();
276
277 // clear and set when not the same
278 var value = me.getValue();
279 if (Ext.isArray(me.originalValue) && Ext.isArray(value) && !Ext.Array.equals(value, me.originalValue)) {
280 me.clearValue();
281 me.setValue(me.originalValue);
282 }
283 }
284 });
285
286 // when refreshing a grid/tree view, restoring the focus moves the view back to
287 // the previously focused item. Save scroll position before refocusing.
288 Ext.define(null, {
289 override: 'Ext.view.Table',
290
291 jumpToFocus: false,
292
293 saveFocusState: function() {
294 var me = this,
295 store = me.dataSource,
296 actionableMode = me.actionableMode,
297 navModel = me.getNavigationModel(),
298 focusPosition = actionableMode ? me.actionPosition : navModel.getPosition(true),
299 refocusRow, refocusCol;
300
301 if (focusPosition) {
302 // Separate this from the instance that the nav model is using.
303 focusPosition = focusPosition.clone();
304
305 // Exit actionable mode.
306 // We must inform any Actionables that they must relinquish control.
307 // Tabbability must be reset.
308 if (actionableMode) {
309 me.ownerGrid.setActionableMode(false);
310 }
311
312 // Blur the focused descendant, but do not trigger focusLeave.
313 me.el.dom.focus();
314
315 // Exiting actionable mode navigates to the owning cell, so in either focus mode we must
316 // clear the navigation position
317 navModel.setPosition();
318
319 // The following function will attempt to refocus back in the same mode to the same cell
320 // as it was at before based upon the previous record (if it's still inthe store), or the row index.
321 return function() {
322 // If we still have data, attempt to refocus in the same mode.
323 if (store.getCount()) {
324
325 // Adjust expectations of where we are able to refocus according to what kind of destruction
326 // might have been wrought on this view's DOM during focus save.
327 refocusRow = Math.min(focusPosition.rowIdx, me.all.getCount() - 1);
328 refocusCol = Math.min(focusPosition.colIdx, me.getVisibleColumnManager().getColumns().length - 1);
329 focusPosition = new Ext.grid.CellContext(me).setPosition(
330 store.contains(focusPosition.record) ? focusPosition.record : refocusRow, refocusCol);
331
332 if (actionableMode) {
333 me.ownerGrid.setActionableMode(true, focusPosition);
334 } else {
335 me.cellFocused = true;
336
337 // we sometimes want to scroll back to where we were
338 var x = me.getScrollX();
339 var y = me.getScrollY();
340
341 // Pass "preventNavigation" as true so that that does not cause selection.
342 navModel.setPosition(focusPosition, null, null, null, true);
343
344 if (!me.jumpToFocus) {
345 me.scrollTo(x,y);
346 }
347 }
348 }
349 // No rows - focus associated column header
350 else {
351 focusPosition.column.focus();
352 }
353 };
354 }
355 return Ext.emptyFn;
356 }
357 });
358
359 // should be fixed with ExtJS 6.0.2, see:
360 // https://www.sencha.com/forum/showthread.php?307244-Bug-with-datefield-in-window-with-scroll
361 Ext.define('Proxmox.Datepicker', {
362 override: 'Ext.picker.Date',
363 hideMode: 'visibility'
364 });
365
366 // ExtJS 6.0.1 has no setSubmitValue() (although you find it in the docs).
367 // Note: this.submitValue is a boolean flag, whereas getSubmitValue() returns
368 // data to be submitted.
369 Ext.define('Proxmox.form.field.Text', {
370 override: 'Ext.form.field.Text',
371
372 setSubmitValue: function(v) {
373 this.submitValue = v;
374 },
375 });
376
377 // this should be fixed with ExtJS 6.0.2
378 // make mousescrolling work in firefox in the containers overflowhandler
379 Ext.define(null, {
380 override: 'Ext.layout.container.boxOverflow.Scroller',
381
382 createWheelListener: function() {
383 var me = this;
384 if (Ext.isFirefox) {
385 me.wheelListener = me.layout.innerCt.on('wheel', me.onMouseWheelFirefox, me, {destroyable: true});
386 } else {
387 me.wheelListener = me.layout.innerCt.on('mousewheel', me.onMouseWheel, me, {destroyable: true});
388 }
389 },
390
391 // special wheel handler for firefox. differs from the default onMouseWheel
392 // handler by using deltaY instead of wheelDeltaY and no normalizing,
393 // because it is already
394 onMouseWheelFirefox: function(e) {
395 e.stopEvent();
396 var delta = e.browserEvent.deltaY || 0;
397 this.scrollBy(delta * this.wheelIncrement, false);
398 }
399
400 });
401
402 // force alert boxes to be rendered with an Error Icon
403 // since Ext.Msg is an object and not a prototype, we need to override it
404 // after the framework has been initiated
405 Ext.onReady(function() {
406 /*jslint confusion: true */
407 Ext.override(Ext.Msg, {
408 alert: function(title, message, fn, scope) {
409 if (Ext.isString(title)) {
410 var config = {
411 title: title,
412 message: message,
413 icon: this.ERROR,
414 buttons: this.OK,
415 fn: fn,
416 scope : scope,
417 minWidth: this.minWidth
418 };
419 return this.show(config);
420 }
421 }
422 });
423 /*jslint confusion: false */
424 });
425 Ext.define('Ext.ux.IFrame', {
426 extend: 'Ext.Component',
427
428 alias: 'widget.uxiframe',
429
430 loadMask: 'Loading...',
431
432 src: 'about:blank',
433
434 renderTpl: [
435 '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>'
436 ],
437 childEls: ['iframeEl'],
438
439 initComponent: function () {
440 this.callParent();
441
442 this.frameName = this.frameName || this.id + '-frame';
443 },
444
445 initEvents : function() {
446 var me = this;
447 me.callParent();
448 me.iframeEl.on('load', me.onLoad, me);
449 },
450
451 initRenderData: function() {
452 return Ext.apply(this.callParent(), {
453 src: this.src,
454 frameName: this.frameName
455 });
456 },
457
458 getBody: function() {
459 var doc = this.getDoc();
460 return doc.body || doc.documentElement;
461 },
462
463 getDoc: function() {
464 try {
465 return this.getWin().document;
466 } catch (ex) {
467 return null;
468 }
469 },
470
471 getWin: function() {
472 var me = this,
473 name = me.frameName,
474 win = Ext.isIE
475 ? me.iframeEl.dom.contentWindow
476 : window.frames[name];
477 return win;
478 },
479
480 getFrame: function() {
481 var me = this;
482 return me.iframeEl.dom;
483 },
484
485 beforeDestroy: function () {
486 this.cleanupListeners(true);
487 this.callParent();
488 },
489
490 cleanupListeners: function(destroying){
491 var doc, prop;
492
493 if (this.rendered) {
494 try {
495 doc = this.getDoc();
496 if (doc) {
497 /*jslint nomen: true*/
498 Ext.get(doc).un(this._docListeners);
499 /*jslint nomen: false*/
500 if (destroying && doc.hasOwnProperty) {
501 for (prop in doc) {
502 if (doc.hasOwnProperty(prop)) {
503 delete doc[prop];
504 }
505 }
506 }
507 }
508 } catch(e) { }
509 }
510 },
511
512 onLoad: function() {
513 var me = this,
514 doc = me.getDoc(),
515 fn = me.onRelayedEvent;
516
517 if (doc) {
518 try {
519 // These events need to be relayed from the inner document (where they stop
520 // bubbling) up to the outer document. This has to be done at the DOM level so
521 // the event reaches listeners on elements like the document body. The effected
522 // mechanisms that depend on this bubbling behavior are listed to the right
523 // of the event.
524 /*jslint nomen: true*/
525 Ext.get(doc).on(
526 me._docListeners = {
527 mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
528 mousemove: fn, // window resize drag detection
529 mouseup: fn, // window resize termination
530 click: fn, // not sure, but just to be safe
531 dblclick: fn, // not sure again
532 scope: me
533 }
534 );
535 /*jslint nomen: false*/
536 } catch(e) {
537 // cannot do this xss
538 }
539
540 // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
541 Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me);
542
543 this.el.unmask();
544 this.fireEvent('load', this);
545
546 } else if (me.src) {
547
548 this.el.unmask();
549 this.fireEvent('error', this);
550 }
551
552
553 },
554
555 onRelayedEvent: function (event) {
556 // relay event from the iframe's document to the document that owns the iframe...
557
558 var iframeEl = this.iframeEl,
559
560 // Get the left-based iframe position
561 iframeXY = iframeEl.getTrueXY(),
562 originalEventXY = event.getXY(),
563
564 // Get the left-based XY position.
565 // This is because the consumer of the injected event will
566 // perform its own RTL normalization.
567 eventXY = event.getTrueXY();
568
569 // the event from the inner document has XY relative to that document's origin,
570 // so adjust it to use the origin of the iframe in the outer document:
571 event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
572
573 event.injectEvent(iframeEl); // blame the iframe for the event...
574
575 event.xy = originalEventXY; // restore the original XY (just for safety)
576 },
577
578 load: function (src) {
579 var me = this,
580 text = me.loadMask,
581 frame = me.getFrame();
582
583 if (me.fireEvent('beforeload', me, src) !== false) {
584 if (text && me.el) {
585 me.el.mask(text);
586 }
587
588 frame.src = me.src = (src || me.src);
589 }
590 }
591 });