]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Toolkit.js
toolkit: fix #1307: don\'t use language dependent separator
[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] <= 120);
34 },
35 IP6CIDRAddressText: gettext('Example') + ': 2001:DB8::42/64' + "<br>" + gettext('Valid CIDR Range') + ': 8-120',
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 MacAddress: function(v) {
51 return (/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/).test(v);
52 },
53 MacAddressMask: /[a-fA-F0-9:]/,
54 MacAddressText: gettext('Example') + ': 01:23:45:67:89:ab',
55
56 BridgeName: function(v) {
57 return (/^vmbr\d{1,4}$/).test(v);
58 },
59 BridgeNameText: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999',
60
61 BondName: function(v) {
62 return (/^bond\d{1,4}$/).test(v);
63 },
64 BondNameText: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999',
65
66 InterfaceName: function(v) {
67 return (/^[a-z][a-z0-9_]{1,20}$/).test(v);
68 },
69 InterfaceNameText: gettext("Allowed characters") + ": 'a-z', '0-9', '_'" + "<br />" +
70 gettext("Minimum characters") + ": 2" + "<br />" +
71 gettext("Maximum characters") + ": 21" + "<br />" +
72 gettext("Must start with") + ": 'a-z'",
73
74 QemuStartDate: function(v) {
75 return (/^(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)$/).test(v);
76 },
77 QemuStartDateText: gettext('Format') + ': "now" or "2006-06-17T16:01:21" or "2006-06-17"',
78
79 StorageId: function(v) {
80 return (/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i).test(v);
81 },
82 StorageIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '-', '_', '.'" + "<br />" +
83 gettext("Minimum characters") + ": 2" + "<br />" +
84 gettext("Must start with") + ": 'A-Z', 'a-z'<br />" +
85 gettext("Must end with") + ": 'A-Z', 'a-z', '0-9'<br />",
86
87 ConfigId: function(v) {
88 return (/^[a-z][a-z0-9\_]+$/i).test(v);
89 },
90 ConfigIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '_'" + "<br />" +
91 gettext("Minimum characters") + ": 2" + "<br />" +
92 gettext("Must start with") + ": " + gettext("letter"),
93
94 HttpProxy: function(v) {
95 return (/^http:\/\/.*$/).test(v);
96 },
97 HttpProxyText: gettext('Example') + ": http://username:password&#64;host:port/",
98
99 DnsName: function(v) {
100 return Proxmox.Utils.DnsName_match.test(v);
101 },
102 DnsNameText: gettext('This is not a valid DNS name'),
103
104 // workaround for https://www.sencha.com/forum/showthread.php?302150
105 proxmoxMail: function(v) {
106 return (/^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/).test(v);
107 },
108 proxmoxMailText: gettext('Example') + ": user@example.com",
109
110 HostList: function(v) {
111 var list = v.split(/[\ \,\;]+/);
112 var i;
113 for (i = 0; i < list.length; i++) {
114 if (list[i] == "") {
115 continue;
116 }
117
118 if (!Proxmox.Utils.HostPort_match.test(list[i]) &&
119 !Proxmox.Utils.HostPortBrackets_match.test(list[i]) &&
120 !Proxmox.Utils.IP6_dotnotation_match.test(list[i])) {
121 return false;
122 }
123 }
124
125 return true;
126 },
127 HostListText: gettext('Not a valid list of hosts'),
128
129 password: function(val, field) {
130 if (field.initialPassField) {
131 var pwd = field.up('form').down(
132 '[name=' + field.initialPassField + ']');
133 return (val == pwd.getValue());
134 }
135 return true;
136 },
137
138 passwordText: gettext('Passwords do not match')
139 });
140
141 // we always want the number in x.y format and never in, e.g., x,y
142 Ext.define('PVE.form.field.Number', {
143 override: 'Ext.form.field.Number',
144 submitLocaleSeparator: false
145 });
146
147 // ExtJs 5-6 has an issue with caching
148 // see https://www.sencha.com/forum/showthread.php?308989
149 Ext.define('Proxmox.UnderlayPool', {
150 override: 'Ext.dom.UnderlayPool',
151
152 checkOut: function () {
153 var cache = this.cache,
154 len = cache.length,
155 el;
156
157 // do cleanup because some of the objects might have been destroyed
158 while (len--) {
159 if (cache[len].destroyed) {
160 cache.splice(len, 1);
161 }
162 }
163 // end do cleanup
164
165 el = cache.shift();
166
167 if (!el) {
168 el = Ext.Element.create(this.elementConfig);
169 el.setVisibilityMode(2);
170 //<debug>
171 // tell the spec runner to ignore this element when checking if the dom is clean
172 el.dom.setAttribute('data-sticky', true);
173 //</debug>
174 }
175
176 return el;
177 }
178 });
179
180 // 'Enter' in Textareas and aria multiline fields should not activate the
181 // defaultbutton, fixed in extjs 6.0.2
182 Ext.define('PVE.panel.Panel', {
183 override: 'Ext.panel.Panel',
184
185 fireDefaultButton: function(e) {
186 if (e.target.getAttribute('aria-multiline') === 'true' ||
187 e.target.tagName === "TEXTAREA") {
188 return true;
189 }
190 return this.callParent(arguments);
191 }
192 });
193
194 // if the order of the values are not the same in originalValue and value
195 // extjs will not overwrite value, but marks the field dirty and thus
196 // the reset button will be enabled (but clicking it changes nothing)
197 // so if the arrays are not the same after resetting, we
198 // clear and set it
199 Ext.define('Proxmox.form.ComboBox', {
200 override: 'Ext.form.field.ComboBox',
201
202 reset: function() {
203 // copied from combobox
204 var me = this;
205 me.callParent();
206
207 // clear and set when not the same
208 var value = me.getValue();
209 if (Ext.isArray(me.originalValue) && Ext.isArray(value) && !Ext.Array.equals(value, me.originalValue)) {
210 me.clearValue();
211 me.setValue(me.originalValue);
212 }
213 }
214 });
215
216 // should be fixed with ExtJS 6.0.2, see:
217 // https://www.sencha.com/forum/showthread.php?307244-Bug-with-datefield-in-window-with-scroll
218 Ext.define('Proxmox.Datepicker', {
219 override: 'Ext.picker.Date',
220 hideMode: 'visibility'
221 });
222
223 // ExtJS 6.0.1 has no setSubmitValue() (although you find it in the docs).
224 // Note: this.submitValue is a boolean flag, whereas getSubmitValue() returns
225 // data to be submitted.
226 Ext.define('Proxmox.form.field.Text', {
227 override: 'Ext.form.field.Text',
228
229 setSubmitValue: function(v) {
230 this.submitValue = v;
231 },
232 });
233
234 // force alert boxes to be rendered with an Error Icon
235 // since Ext.Msg is an object and not a prototype, we need to override it
236 // after the framework has been initiated
237 Ext.onReady(function() {
238 /*jslint confusion: true */
239 Ext.override(Ext.Msg, {
240 alert: function(title, message, fn, scope) {
241 if (Ext.isString(title)) {
242 var config = {
243 title: title,
244 message: message,
245 icon: this.ERROR,
246 buttons: this.OK,
247 fn: fn,
248 scope : scope,
249 minWidth: this.minWidth
250 };
251 return this.show(config);
252 }
253 }
254 });
255 /*jslint confusion: false */
256 });
257 Ext.define('Ext.ux.IFrame', {
258 extend: 'Ext.Component',
259
260 alias: 'widget.uxiframe',
261
262 loadMask: 'Loading...',
263
264 src: 'about:blank',
265
266 renderTpl: [
267 '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>'
268 ],
269 childEls: ['iframeEl'],
270
271 initComponent: function () {
272 this.callParent();
273
274 this.frameName = this.frameName || this.id + '-frame';
275 },
276
277 initEvents : function() {
278 var me = this;
279 me.callParent();
280 me.iframeEl.on('load', me.onLoad, me);
281 },
282
283 initRenderData: function() {
284 return Ext.apply(this.callParent(), {
285 src: this.src,
286 frameName: this.frameName
287 });
288 },
289
290 getBody: function() {
291 var doc = this.getDoc();
292 return doc.body || doc.documentElement;
293 },
294
295 getDoc: function() {
296 try {
297 return this.getWin().document;
298 } catch (ex) {
299 return null;
300 }
301 },
302
303 getWin: function() {
304 var me = this,
305 name = me.frameName,
306 win = Ext.isIE
307 ? me.iframeEl.dom.contentWindow
308 : window.frames[name];
309 return win;
310 },
311
312 getFrame: function() {
313 var me = this;
314 return me.iframeEl.dom;
315 },
316
317 beforeDestroy: function () {
318 this.cleanupListeners(true);
319 this.callParent();
320 },
321
322 cleanupListeners: function(destroying){
323 var doc, prop;
324
325 if (this.rendered) {
326 try {
327 doc = this.getDoc();
328 if (doc) {
329 /*jslint nomen: true*/
330 Ext.get(doc).un(this._docListeners);
331 /*jslint nomen: false*/
332 if (destroying && doc.hasOwnProperty) {
333 for (prop in doc) {
334 if (doc.hasOwnProperty(prop)) {
335 delete doc[prop];
336 }
337 }
338 }
339 }
340 } catch(e) { }
341 }
342 },
343
344 onLoad: function() {
345 var me = this,
346 doc = me.getDoc(),
347 fn = me.onRelayedEvent;
348
349 if (doc) {
350 try {
351 // These events need to be relayed from the inner document (where they stop
352 // bubbling) up to the outer document. This has to be done at the DOM level so
353 // the event reaches listeners on elements like the document body. The effected
354 // mechanisms that depend on this bubbling behavior are listed to the right
355 // of the event.
356 /*jslint nomen: true*/
357 Ext.get(doc).on(
358 me._docListeners = {
359 mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
360 mousemove: fn, // window resize drag detection
361 mouseup: fn, // window resize termination
362 click: fn, // not sure, but just to be safe
363 dblclick: fn, // not sure again
364 scope: me
365 }
366 );
367 /*jslint nomen: false*/
368 } catch(e) {
369 // cannot do this xss
370 }
371
372 // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
373 Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me);
374
375 this.el.unmask();
376 this.fireEvent('load', this);
377
378 } else if (me.src) {
379
380 this.el.unmask();
381 this.fireEvent('error', this);
382 }
383
384
385 },
386
387 onRelayedEvent: function (event) {
388 // relay event from the iframe's document to the document that owns the iframe...
389
390 var iframeEl = this.iframeEl,
391
392 // Get the left-based iframe position
393 iframeXY = iframeEl.getTrueXY(),
394 originalEventXY = event.getXY(),
395
396 // Get the left-based XY position.
397 // This is because the consumer of the injected event will
398 // perform its own RTL normalization.
399 eventXY = event.getTrueXY();
400
401 // the event from the inner document has XY relative to that document's origin,
402 // so adjust it to use the origin of the iframe in the outer document:
403 event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
404
405 event.injectEvent(iframeEl); // blame the iframe for the event...
406
407 event.xy = originalEventXY; // restore the original XY (just for safety)
408 },
409
410 load: function (src) {
411 var me = this,
412 text = me.loadMask,
413 frame = me.getFrame();
414
415 if (me.fireEvent('beforeload', me, src) !== false) {
416 if (text && me.el) {
417 me.el.mask(text);
418 }
419
420 frame.src = me.src = (src || me.src);
421 }
422 }
423 });