]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/Toolkit.js
Move globally defined regexpes into PVE.Utils
[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('Format') + ': [a-z][a-z0-9_]{1,20}',
72
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', '-', '_', '.'",
83
84 ConfigId: function(v) {
85 return (/^[a-z][a-z0-9\_]+$/i).test(v);
86 },
87 ConfigIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '_'",
88
89 HttpProxy: function(v) {
90 return (/^http:\/\/.*$/).test(v);
91 },
92 HttpProxyText: gettext('Example') + ": http://username:password&#64;host:port/",
93
94 DnsName: function(v) {
95 return PVE.Utils.DnsName_match.test(v);
96 },
97 DnsNameText: gettext('This is not a valid DNS name'),
98
99 // workaround for https://www.sencha.com/forum/showthread.php?302150
100 pveMail: function(v) {
101 return (/^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/).test(v);
102 },
103 pveMailText: gettext('Example') + ": user@example.com",
104
105 HostList: function(v) {
106 var list = v.split(/[\ \,\;]+/);
107 var i;
108 for (i = 0; i < list.length; i++) {
109 if (list[i] == "") {
110 continue;
111 }
112
113 if (!PVE.Utils.HostPort_match.test(list[i]) &&
114 !PVE.Utils.HostPortBrackets_match.test(list[i]) &&
115 !PVE.Utils.IP6_dotnotation_match.test(list[i])) {
116 return false;
117 }
118 }
119
120 return true;
121 },
122 HostListText: gettext('Not a valid list of hosts')
123 });
124
125 // ExtJs 5-6 has an issue with caching
126 // see https://www.sencha.com/forum/showthread.php?308989
127 Ext.define('PVE.UnderlayPool', {
128 override: 'Ext.dom.UnderlayPool',
129
130 checkOut: function () {
131 var cache = this.cache,
132 len = cache.length,
133 el;
134
135 // do cleanup because some of the objects might have been destroyed
136 while (len--) {
137 if (cache[len].destroyed) {
138 cache.splice(len, 1);
139 }
140 }
141 // end do cleanup
142
143 el = cache.shift();
144
145 if (!el) {
146 el = Ext.Element.create(this.elementConfig);
147 el.setVisibilityMode(2);
148 //<debug>
149 // tell the spec runner to ignore this element when checking if the dom is clean
150 el.dom.setAttribute('data-sticky', true);
151 //</debug>
152 }
153
154 return el;
155 }
156 });
157
158 // if the order of the values are not the same in originalValue and value
159 // extjs will not overwrite value, but marks the field dirty and thus
160 // the reset button will be enabled (but clicking it changes nothing)
161 // so if the arrays are not the same after resetting, we
162 // clear and set it
163 Ext.define('PVE.form.ComboBox', {
164 override: 'Ext.form.field.ComboBox',
165
166 reset: function() {
167 // copied from combobox
168 var me = this;
169 me.callParent();
170 me.applyEmptyText();
171
172 // clear and set when not the same
173 var value = me.getValue();
174 if (Ext.isArray(me.originalValue) && Ext.isArray(value) && !Ext.Array.equals(value, me.originalValue)) {
175 me.clearValue();
176 me.setValue(me.originalValue);
177 }
178 }
179 });
180
181 // should be fixed with ExtJS 6.0.2, see:
182 // https://www.sencha.com/forum/showthread.php?307244-Bug-with-datefield-in-window-with-scroll
183 Ext.define('PVE.Datepicker', {
184 override: 'Ext.picker.Date',
185 hideMode: 'visibility'
186 });
187
188 // force alert boxes to be rendered with an Error Icon
189 // since Ext.Msg is an object and not a prototype, we need to override it
190 // after the framework has been initiated
191 Ext.onReady(function() {
192 /*jslint confusion: true */
193 Ext.override(Ext.Msg, {
194 alert: function(title, message, fn, scope) {
195 if (Ext.isString(title)) {
196 var config = {
197 title: title,
198 message: message,
199 icon: this.ERROR,
200 buttons: this.OK,
201 fn: fn,
202 scope : scope,
203 minWidth: this.minWidth
204 };
205 return this.show(config);
206 }
207 }
208 });
209 /*jslint confusion: false */
210 });
211 Ext.define('Ext.ux.IFrame', {
212 extend: 'Ext.Component',
213
214 alias: 'widget.uxiframe',
215
216 loadMask: 'Loading...',
217
218 src: 'about:blank',
219
220 renderTpl: [
221 '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>'
222 ],
223 childEls: ['iframeEl'],
224
225 initComponent: function () {
226 this.callParent();
227
228 this.frameName = this.frameName || this.id + '-frame';
229 },
230
231 initEvents : function() {
232 var me = this;
233 me.callParent();
234 me.iframeEl.on('load', me.onLoad, me);
235 },
236
237 initRenderData: function() {
238 return Ext.apply(this.callParent(), {
239 src: this.src,
240 frameName: this.frameName
241 });
242 },
243
244 getBody: function() {
245 var doc = this.getDoc();
246 return doc.body || doc.documentElement;
247 },
248
249 getDoc: function() {
250 try {
251 return this.getWin().document;
252 } catch (ex) {
253 return null;
254 }
255 },
256
257 getWin: function() {
258 var me = this,
259 name = me.frameName,
260 win = Ext.isIE
261 ? me.iframeEl.dom.contentWindow
262 : window.frames[name];
263 return win;
264 },
265
266 getFrame: function() {
267 var me = this;
268 return me.iframeEl.dom;
269 },
270
271 beforeDestroy: function () {
272 this.cleanupListeners(true);
273 this.callParent();
274 },
275
276 cleanupListeners: function(destroying){
277 var doc, prop;
278
279 if (this.rendered) {
280 try {
281 doc = this.getDoc();
282 if (doc) {
283 /*jslint nomen: true*/
284 Ext.get(doc).un(this._docListeners);
285 /*jslint nomen: false*/
286 if (destroying && doc.hasOwnProperty) {
287 for (prop in doc) {
288 if (doc.hasOwnProperty(prop)) {
289 delete doc[prop];
290 }
291 }
292 }
293 }
294 } catch(e) { }
295 }
296 },
297
298 onLoad: function() {
299 var me = this,
300 doc = me.getDoc(),
301 fn = me.onRelayedEvent;
302
303 if (doc) {
304 try {
305 // These events need to be relayed from the inner document (where they stop
306 // bubbling) up to the outer document. This has to be done at the DOM level so
307 // the event reaches listeners on elements like the document body. The effected
308 // mechanisms that depend on this bubbling behavior are listed to the right
309 // of the event.
310 /*jslint nomen: true*/
311 Ext.get(doc).on(
312 me._docListeners = {
313 mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
314 mousemove: fn, // window resize drag detection
315 mouseup: fn, // window resize termination
316 click: fn, // not sure, but just to be safe
317 dblclick: fn, // not sure again
318 scope: me
319 }
320 );
321 /*jslint nomen: false*/
322 } catch(e) {
323 // cannot do this xss
324 }
325
326 // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
327 Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me);
328
329 this.el.unmask();
330 this.fireEvent('load', this);
331
332 } else if (me.src) {
333
334 this.el.unmask();
335 this.fireEvent('error', this);
336 }
337
338
339 },
340
341 onRelayedEvent: function (event) {
342 // relay event from the iframe's document to the document that owns the iframe...
343
344 var iframeEl = this.iframeEl,
345
346 // Get the left-based iframe position
347 iframeXY = iframeEl.getTrueXY(),
348 originalEventXY = event.getXY(),
349
350 // Get the left-based XY position.
351 // This is because the consumer of the injected event will
352 // perform its own RTL normalization.
353 eventXY = event.getTrueXY();
354
355 // the event from the inner document has XY relative to that document's origin,
356 // so adjust it to use the origin of the iframe in the outer document:
357 event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
358
359 event.injectEvent(iframeEl); // blame the iframe for the event...
360
361 event.xy = originalEventXY; // restore the original XY (just for safety)
362 },
363
364 load: function (src) {
365 var me = this,
366 text = me.loadMask,
367 frame = me.getFrame();
368
369 if (me.fireEvent('beforeload', me, src) !== false) {
370 if (text && me.el) {
371 me.el.mask(text);
372 }
373
374 frame.src = me.src = (src || me.src);
375 }
376 }
377 });