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