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