]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/Toolkit.js
jslint: add global entries where necessary
[pve-manager.git] / www / manager6 / Toolkit.js
1 /*global IP4_match, IP4_cidr_match, IP6_match, IP6_cidr_match, IP64_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 (/^(([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)\.)*([A-Za-z0-9]([A-Za-z0-9\-]*[A-Za-z0-9])?)$/).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 // should be fixed with ExtJS 6.0.2, see:
141 // https://www.sencha.com/forum/showthread.php?307244-Bug-with-datefield-in-window-with-scroll
142 Ext.define('PVE.Datepicker', {
143 override: 'Ext.picker.Date',
144 hideMode: 'visibility'
145 });
146
147 // force alert boxes to be rendered with an Error Icon
148 // since Ext.Msg is an object and not a prototype, we need to override it
149 // after the framework has been initiated
150 Ext.onReady(function() {
151 /*jslint confusion: true */
152 Ext.override(Ext.Msg, {
153 alert: function(title, message, fn, scope) {
154 if (Ext.isString(title)) {
155 var config = {
156 title: title,
157 message: message,
158 icon: this.ERROR,
159 buttons: this.OK,
160 fn: fn,
161 scope : scope,
162 minWidth: this.minWidth
163 };
164 return this.show(config);
165 }
166 }
167 });
168 /*jslint confusion: false */
169 });
170 Ext.define('Ext.ux.IFrame', {
171 extend: 'Ext.Component',
172
173 alias: 'widget.uxiframe',
174
175 loadMask: 'Loading...',
176
177 src: 'about:blank',
178
179 renderTpl: [
180 '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>'
181 ],
182 childEls: ['iframeEl'],
183
184 initComponent: function () {
185 this.callParent();
186
187 this.frameName = this.frameName || this.id + '-frame';
188 },
189
190 initEvents : function() {
191 var me = this;
192 me.callParent();
193 me.iframeEl.on('load', me.onLoad, me);
194 },
195
196 initRenderData: function() {
197 return Ext.apply(this.callParent(), {
198 src: this.src,
199 frameName: this.frameName
200 });
201 },
202
203 getBody: function() {
204 var doc = this.getDoc();
205 return doc.body || doc.documentElement;
206 },
207
208 getDoc: function() {
209 try {
210 return this.getWin().document;
211 } catch (ex) {
212 return null;
213 }
214 },
215
216 getWin: function() {
217 var me = this,
218 name = me.frameName,
219 win = Ext.isIE
220 ? me.iframeEl.dom.contentWindow
221 : window.frames[name];
222 return win;
223 },
224
225 getFrame: function() {
226 var me = this;
227 return me.iframeEl.dom;
228 },
229
230 beforeDestroy: function () {
231 this.cleanupListeners(true);
232 this.callParent();
233 },
234
235 cleanupListeners: function(destroying){
236 var doc, prop;
237
238 if (this.rendered) {
239 try {
240 doc = this.getDoc();
241 if (doc) {
242 Ext.get(doc).un(this._docListeners);
243 if (destroying) {
244 for (prop in doc) {
245 if (doc.hasOwnProperty && doc.hasOwnProperty(prop)) {
246 delete doc[prop];
247 }
248 }
249 }
250 }
251 } catch(e) { }
252 }
253 },
254
255 onLoad: function() {
256 var me = this,
257 doc = me.getDoc(),
258 fn = me.onRelayedEvent;
259
260 if (doc) {
261 try {
262 // These events need to be relayed from the inner document (where they stop
263 // bubbling) up to the outer document. This has to be done at the DOM level so
264 // the event reaches listeners on elements like the document body. The effected
265 // mechanisms that depend on this bubbling behavior are listed to the right
266 // of the event.
267 Ext.get(doc).on(
268 me._docListeners = {
269 mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
270 mousemove: fn, // window resize drag detection
271 mouseup: fn, // window resize termination
272 click: fn, // not sure, but just to be safe
273 dblclick: fn, // not sure again
274 scope: me
275 }
276 );
277 } catch(e) {
278 // cannot do this xss
279 }
280
281 // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
282 Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me);
283
284 this.el.unmask();
285 this.fireEvent('load', this);
286
287 } else if (me.src) {
288
289 this.el.unmask();
290 this.fireEvent('error', this);
291 }
292
293
294 },
295
296 onRelayedEvent: function (event) {
297 // relay event from the iframe's document to the document that owns the iframe...
298
299 var iframeEl = this.iframeEl,
300
301 // Get the left-based iframe position
302 iframeXY = iframeEl.getTrueXY(),
303 originalEventXY = event.getXY(),
304
305 // Get the left-based XY position.
306 // This is because the consumer of the injected event will
307 // perform its own RTL normalization.
308 eventXY = event.getTrueXY();
309
310 // the event from the inner document has XY relative to that document's origin,
311 // so adjust it to use the origin of the iframe in the outer document:
312 event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]];
313
314 event.injectEvent(iframeEl); // blame the iframe for the event...
315
316 event.xy = originalEventXY; // restore the original XY (just for safety)
317 },
318
319 load: function (src) {
320 var me = this,
321 text = me.loadMask,
322 frame = me.getFrame();
323
324 if (me.fireEvent('beforeload', me, src) !== false) {
325 if (text && me.el) {
326 me.el.mask(text);
327 }
328
329 frame.src = me.src = (src || me.src);
330 }
331 }
332 });