]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/Toolkit.js
refactor DnsName regex
[pve-manager.git] / www / manager6 / Toolkit.js
1 /*global IP4_match, IP4_cidr_match, IP6_match, IP6_cidr_match, IP64_match, DnsName_match*/
2 // ExtJS related things
3
4 PVE.Utils.toolkit = 'extjs';
5
6 // do not send '_dc' parameter
7 Ext.Ajax.disableCaching = false;
8
9 // custom Vtypes
10 Ext.apply(Ext.form.field.VTypes, {
11 IPAddress: function(v) {
12 return IP4_match.test(v);
13 },
14 IPAddressText: gettext('Example') + ': 192.168.1.1',
15 IPAddressMask: /[\d\.]/i,
16
17 IPCIDRAddress: function(v) {
18 var result = IP4_cidr_match.exec(v);
19 // limits according to JSON Schema see
20 // pve-common/src/PVE/JSONSchema.pm
21 return (result !== null && result[1] >= 8 && result[1] <= 32);
22 },
23 IPCIDRAddressText: gettext('Example') + ': 192.168.1.1/24' + "<br>" + gettext('Valid CIDR Range') + ': 8-32',
24 IPCIDRAddressMask: /[\d\.\/]/i,
25
26 IP6Address: function(v) {
27 return IP6_match.test(v);
28 },
29 IP6AddressText: gettext('Example') + ': 2001:DB8::42',
30 IP6AddressMask: /[A-Fa-f0-9:]/,
31
32 IP6CIDRAddress: function(v) {
33 var result = IP6_cidr_match.exec(v);
34 // limits according to JSON Schema see
35 // pve-common/src/PVE/JSONSchema.pm
36 return (result !== null && result[1] >= 8 && result[1] <= 120);
37 },
38 IP6CIDRAddressText: gettext('Example') + ': 2001:DB8::42/64' + "<br>" + gettext('Valid CIDR Range') + ': 8-120',
39 IP6CIDRAddressMask: /[A-Fa-f0-9:\/]/,
40
41 IP6PrefixLength: function(v) {
42 return v >= 0 && v <= 128;
43 },
44 IP6PrefixLengthText: gettext('Example') + ': X, where 0 <= X <= 128',
45 IP6PrefixLengthMask: /[0-9]/,
46
47 IP64Address: function(v) {
48 return IP64_match.test(v);
49 },
50 IP64AddressText: gettext('Example') + ': 192.168.1.1 2001:DB8::42',
51 IP64AddressMask: /[A-Fa-f0-9\.:]/,
52
53 MacAddress: function(v) {
54 return (/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/).test(v);
55 },
56 MacAddressMask: /[a-fA-F0-9:]/,
57 MacAddressText: gettext('Example') + ': 01:23:45:67:89:ab',
58
59 BridgeName: function(v) {
60 return (/^vmbr\d{1,4}$/).test(v);
61 },
62 BridgeNameText: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999',
63
64 BondName: function(v) {
65 return (/^bond\d{1,4}$/).test(v);
66 },
67 BondNameText: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999',
68
69 InterfaceName: function(v) {
70 return (/^[a-z][a-z0-9_]{1,20}$/).test(v);
71 },
72 InterfaceNameText: gettext('Format') + ': [a-z][a-z0-9_]{1,20}',
73
74
75 QemuStartDate: function(v) {
76 return (/^(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)$/).test(v);
77 },
78 QemuStartDateText: gettext('Format') + ': "now" or "2006-06-17T16:01:21" or "2006-06-17"',
79
80 StorageId: function(v) {
81 return (/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i).test(v);
82 },
83 StorageIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '-', '_', '.'",
84
85 ConfigId: function(v) {
86 return (/^[a-z][a-z0-9\_]+$/i).test(v);
87 },
88 ConfigIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '_'",
89
90 HttpProxy: function(v) {
91 return (/^http:\/\/.*$/).test(v);
92 },
93 HttpProxyText: gettext('Example') + ": http://username:password&#64;host:port/",
94
95 DnsName: function(v) {
96 return DnsName_match.test(v);
97 },
98 DnsNameText: gettext('This is not a valid DNS name'),
99
100 // workaround for https://www.sencha.com/forum/showthread.php?302150
101 pveMail: function(v) {
102 return (/^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/).test(v);
103 },
104 pveMailText: gettext('Example') + ": user@example.com"
105 });
106
107 // ExtJs 5-6 has an issue with caching
108 // see https://www.sencha.com/forum/showthread.php?308989
109 Ext.define('PVE.UnderlayPool', {
110 override: 'Ext.dom.UnderlayPool',
111
112 checkOut: function () {
113 var cache = this.cache,
114 len = cache.length,
115 el;
116
117 // do cleanup because some of the objects might have been destroyed
118 while (len--) {
119 if (cache[len].destroyed) {
120 cache.splice(len, 1);
121 }
122 }
123 // end do cleanup
124
125 el = cache.shift();
126
127 if (!el) {
128 el = Ext.Element.create(this.elementConfig);
129 el.setVisibilityMode(2);
130 //<debug>
131 // tell the spec runner to ignore this element when checking if the dom is clean
132 el.dom.setAttribute('data-sticky', true);
133 //</debug>
134 }
135
136 return el;
137 }
138 });
139
140 // if the order of the values are not the same in originalValue and value
141 // extjs will not overwrite value, but marks the field dirty and thus
142 // the reset button will be enabled (but clicking it changes nothing)
143 // so if the arrays are not the same after resetting, we
144 // clear and set it
145 Ext.define('PVE.form.ComboBox', {
146 override: 'Ext.form.field.ComboBox',
147
148 reset: function() {
149 // copied from combobox
150 var me = this;
151 me.callParent();
152 me.applyEmptyText();
153
154 // clear and set when not the same
155 if (Ext.isArray(me.originalValue) && !Ext.Array.equals(me.getValue(), me.originalValue)) {
156 me.clearValue();
157 me.setValue(me.originalValue);
158 }
159 }
160 });
161
162 // should be fixed with ExtJS 6.0.2, see:
163 // https://www.sencha.com/forum/showthread.php?307244-Bug-with-datefield-in-window-with-scroll
164 Ext.define('PVE.Datepicker', {
165 override: 'Ext.picker.Date',
166 hideMode: 'visibility'
167 });
168
169 // force alert boxes to be rendered with an Error Icon
170 // since Ext.Msg is an object and not a prototype, we need to override it
171 // after the framework has been initiated
172 Ext.onReady(function() {
173 /*jslint confusion: true */
174 Ext.override(Ext.Msg, {
175 alert: function(title, message, fn, scope) {
176 if (Ext.isString(title)) {
177 var config = {
178 title: title,
179 message: message,
180 icon: this.ERROR,
181 buttons: this.OK,
182 fn: fn,
183 scope : scope,
184 minWidth: this.minWidth
185 };
186 return this.show(config);
187 }
188 }
189 });
190 /*jslint confusion: false */
191 });
192 Ext.define('Ext.ux.IFrame', {
193 extend: 'Ext.Component',
194
195 alias: 'widget.uxiframe',
196
197 loadMask: 'Loading...',
198
199 src: 'about:blank',
200
201 renderTpl: [
202 '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>'
203 ],
204 childEls: ['iframeEl'],
205
206 initComponent: function () {
207 this.callParent();
208
209 this.frameName = this.frameName || this.id + '-frame';
210 },
211
212 initEvents : function() {
213 var me = this;
214 me.callParent();
215 me.iframeEl.on('load', me.onLoad, me);
216 },
217
218 initRenderData: function() {
219 return Ext.apply(this.callParent(), {
220 src: this.src,
221 frameName: this.frameName
222 });
223 },
224
225 getBody: function() {
226 var doc = this.getDoc();
227 return doc.body || doc.documentElement;
228 },
229
230 getDoc: function() {
231 try {
232 return this.getWin().document;
233 } catch (ex) {
234 return null;
235 }
236 },
237
238 getWin: function() {
239 var me = this,
240 name = me.frameName,
241 win = Ext.isIE
242 ? me.iframeEl.dom.contentWindow
243 : window.frames[name];
244 return win;
245 },
246
247 getFrame: function() {
248 var me = this;
249 return me.iframeEl.dom;
250 },
251
252 beforeDestroy: function () {
253 this.cleanupListeners(true);
254 this.callParent();
255 },
256
257 cleanupListeners: function(destroying){
258 var doc, prop;
259
260 if (this.rendered) {
261 try {
262 doc = this.getDoc();
263 if (doc) {
264 /*jslint nomen: true*/
265 Ext.get(doc).un(this._docListeners);
266 /*jslint nomen: false*/
267 if (destroying && doc.hasOwnProperty) {
268 for (prop in doc) {
269 if (doc.hasOwnProperty(prop)) {
270 delete doc[prop];
271 }
272 }
273 }
274 }
275 } catch(e) { }
276 }
277 },
278
279 onLoad: function() {
280 var me = this,
281 doc = me.getDoc(),
282 fn = me.onRelayedEvent;
283
284 if (doc) {
285 try {
286 // These events need to be relayed from the inner document (where they stop
287 // bubbling) up to the outer document. This has to be done at the DOM level so
288 // the event reaches listeners on elements like the document body. The effected
289 // mechanisms that depend on this bubbling behavior are listed to the right
290 // of the event.
291 /*jslint nomen: true*/
292 Ext.get(doc).on(
293 me._docListeners = {
294 mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
295 mousemove: fn, // window resize drag detection
296 mouseup: fn, // window resize termination
297 click: fn, // not sure, but just to be safe
298 dblclick: fn, // not sure again
299 scope: me
300 }
301 );
302 /*jslint nomen: false*/
303 } catch(e) {
304 // cannot do this xss
305 }
306
307 // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
308 Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me);
309
310 this.el.unmask();
311 this.fireEvent('load', this);
312
313 } else if (me.src) {
314
315 this.el.unmask();
316 this.fireEvent('error', this);
317 }
318
319
320 },
321
322 onRelayedEvent: function (event) {
323 // relay event from the iframe's document to the document that owns the iframe...
324
325 var iframeEl = this.iframeEl,
326
327 // Get the left-based iframe position
328 iframeXY = iframeEl.getTrueXY(),
329 originalEventXY = event.getXY(),
330
331 // Get the left-based XY position.
332 // This is because the consumer of the injected event will
333 // perform its own RTL normalization.
334 eventXY = event.getTrueXY();
335
336 // the event from the inner document has XY relative to that document's origin,
337 // so adjust it to use the origin of the iframe in the outer document:
338 event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
339
340 event.injectEvent(iframeEl); // blame the iframe for the event...
341
342 event.xy = originalEventXY; // restore the original XY (just for safety)
343 },
344
345 load: function (src) {
346 var me = this,
347 text = me.loadMask,
348 frame = me.getFrame();
349
350 if (me.fireEvent('beforeload', me, src) !== false) {
351 if (text && me.el) {
352 me.el.mask(text);
353 }
354
355 frame.src = me.src = (src || me.src);
356 }
357 }
358 });