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