]>
Commit | Line | Data |
---|---|---|
bb64de6e DM |
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, { | |
01031528 | 8 | IPAddress: function(v) { |
bb64de6e DM |
9 | return Proxmox.Utils.IP4_match.test(v); |
10 | }, | |
01031528 | 11 | IPAddressText: gettext('Example') + ': 192.168.1.1', |
05a977a2 | 12 | IPAddressMask: /[\d.]/i, |
bb64de6e | 13 | |
01031528 | 14 | IPCIDRAddress: function(v) { |
05a977a2 | 15 | let result = Proxmox.Utils.IP4_cidr_match.exec(v); |
bb64de6e DM |
16 | // limits according to JSON Schema see |
17 | // pve-common/src/PVE/JSONSchema.pm | |
01031528 | 18 | return result !== null && result[1] >= 8 && result[1] <= 32; |
bb64de6e | 19 | }, |
05a977a2 TL |
20 | IPCIDRAddressText: gettext('Example') + ': 192.168.1.1/24<br>' + gettext('Valid CIDR Range') + ': 8-32', |
21 | IPCIDRAddressMask: /[\d./]/i, | |
bb64de6e | 22 | |
01031528 | 23 | IP6Address: function(v) { |
bb64de6e DM |
24 | return Proxmox.Utils.IP6_match.test(v); |
25 | }, | |
01031528 | 26 | IP6AddressText: gettext('Example') + ': 2001:DB8::42', |
bb64de6e DM |
27 | IP6AddressMask: /[A-Fa-f0-9:]/, |
28 | ||
01031528 | 29 | IP6CIDRAddress: function(v) { |
05a977a2 | 30 | let result = Proxmox.Utils.IP6_cidr_match.exec(v); |
bb64de6e DM |
31 | // limits according to JSON Schema see |
32 | // pve-common/src/PVE/JSONSchema.pm | |
01031528 | 33 | return result !== null && result[1] >= 8 && result[1] <= 128; |
bb64de6e | 34 | }, |
05a977a2 TL |
35 | IP6CIDRAddressText: gettext('Example') + ': 2001:DB8::42/64<br>' + gettext('Valid CIDR Range') + ': 8-128', |
36 | IP6CIDRAddressMask: /[A-Fa-f0-9:/]/, | |
bb64de6e | 37 | |
01031528 | 38 | IP6PrefixLength: function(v) { |
bb64de6e DM |
39 | return v >= 0 && v <= 128; |
40 | }, | |
01031528 TL |
41 | IP6PrefixLengthText: gettext('Example') + ': X, where 0 <= X <= 128', |
42 | IP6PrefixLengthMask: /[0-9]/, | |
bb64de6e | 43 | |
01031528 | 44 | IP64Address: function(v) { |
bb64de6e DM |
45 | return Proxmox.Utils.IP64_match.test(v); |
46 | }, | |
01031528 | 47 | IP64AddressText: gettext('Example') + ': 192.168.1.1 2001:DB8::42', |
05a977a2 | 48 | IP64AddressMask: /[A-Fa-f0-9.:]/, |
bb64de6e | 49 | |
577b6c75 | 50 | IP64CIDRAddress: function(v) { |
05a977a2 | 51 | let result = Proxmox.Utils.IP64_cidr_match.exec(v); |
577b6c75 ML |
52 | if (result === null) { |
53 | return false; | |
54 | } | |
55 | if (result[1] !== undefined) { | |
56 | return result[1] >= 8 && result[1] <= 128; | |
57 | } else if (result[2] !== undefined) { | |
58 | return result[2] >= 8 && result[2] <= 32; | |
59 | } else { | |
60 | return false; | |
61 | } | |
62 | }, | |
63 | IP64CIDRAddressText: gettext('Example') + ': 192.168.1.1/24 2001:DB8::42/64', | |
05a977a2 | 64 | IP64CIDRAddressMask: /[A-Fa-f0-9.:/]/, |
577b6c75 | 65 | |
bb64de6e DM |
66 | MacAddress: function(v) { |
67 | return (/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/).test(v); | |
68 | }, | |
69 | MacAddressMask: /[a-fA-F0-9:]/, | |
70 | MacAddressText: gettext('Example') + ': 01:23:45:67:89:ab', | |
71 | ||
01031528 | 72 | MacPrefix: function(v) { |
add18fad | 73 | return (/^[a-f0-9][02468ace](?::[a-f0-9]{2}){0,2}:?$/i).test(v); |
8f449655 TL |
74 | }, |
75 | MacPrefixMask: /[a-fA-F0-9:]/, | |
ffe26505 | 76 | MacPrefixText: gettext('Example') + ': 02:8f - ' + gettext('only unicast addresses are allowed'), |
8f449655 | 77 | |
bb64de6e DM |
78 | BridgeName: function(v) { |
79 | return (/^vmbr\d{1,4}$/).test(v); | |
80 | }, | |
8aefd47c AD |
81 | VlanName: function(v) { |
82 | if (Proxmox.Utils.VlanInterface_match.test(v)) { | |
83 | return true; | |
84 | } else if (Proxmox.Utils.Vlan_match.test(v)) { | |
85 | return true; | |
86 | } | |
87 | return true; | |
88 | }, | |
bb64de6e DM |
89 | BridgeNameText: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999', |
90 | ||
91 | BondName: function(v) { | |
92 | return (/^bond\d{1,4}$/).test(v); | |
93 | }, | |
94 | BondNameText: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999', | |
95 | ||
96 | InterfaceName: function(v) { | |
97 | return (/^[a-z][a-z0-9_]{1,20}$/).test(v); | |
98 | }, | |
05a977a2 TL |
99 | InterfaceNameText: gettext("Allowed characters") + ": 'a-z', '0-9', '_'<br />" + |
100 | gettext("Minimum characters") + ": 2<br />" + | |
101 | gettext("Maximum characters") + ": 21<br />" + | |
bb64de6e DM |
102 | gettext("Must start with") + ": 'a-z'", |
103 | ||
01031528 | 104 | StorageId: function(v) { |
05a977a2 | 105 | return (/^[a-z][a-z0-9\-_.]*[a-z0-9]$/i).test(v); |
bb64de6e | 106 | }, |
05a977a2 TL |
107 | StorageIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '-', '_', '.'<br />" + |
108 | gettext("Minimum characters") + ": 2<br />" + | |
bb64de6e DM |
109 | gettext("Must start with") + ": 'A-Z', 'a-z'<br />" + |
110 | gettext("Must end with") + ": 'A-Z', 'a-z', '0-9'<br />", | |
111 | ||
01031528 | 112 | ConfigId: function(v) { |
05a977a2 | 113 | return (/^[a-z][a-z0-9_]+$/i).test(v); |
bb64de6e | 114 | }, |
05a977a2 TL |
115 | ConfigIdText: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '_'<br />" + |
116 | gettext("Minimum characters") + ": 2<br />" + | |
bb64de6e DM |
117 | gettext("Must start with") + ": " + gettext("letter"), |
118 | ||
01031528 | 119 | HttpProxy: function(v) { |
bb64de6e DM |
120 | return (/^http:\/\/.*$/).test(v); |
121 | }, | |
122 | HttpProxyText: gettext('Example') + ": http://username:password@host:port/", | |
123 | ||
9109c392 DB |
124 | CpuSet: function(v) { |
125 | if (!Proxmox.Utils.CpuSet_match.test(v)) { | |
126 | return false; | |
127 | } | |
128 | let groups = v.split(","); | |
129 | for (let i = 0; i < groups.length; i++) { | |
130 | if (!groups[i].includes("-")) { | |
131 | continue; | |
132 | } | |
133 | let values = groups[i].split("-"); | |
134 | let left = parseInt(values[0], 10); | |
135 | let right = parseInt(values[1], 10); | |
136 | if (left > right) { | |
137 | return false; | |
138 | } | |
139 | } | |
140 | return true; | |
141 | }, | |
142 | CpuSetText: gettext('This is not a valid CpuSet'), | |
143 | ||
bb64de6e DM |
144 | DnsName: function(v) { |
145 | return Proxmox.Utils.DnsName_match.test(v); | |
146 | }, | |
147 | DnsNameText: gettext('This is not a valid DNS name'), | |
148 | ||
66c5ceb8 SI |
149 | DnsNameOrWildcard: function(v) { |
150 | return Proxmox.Utils.DnsName_or_Wildcard_match.test(v); | |
151 | }, | |
152 | DnsNameOrWildcardText: gettext('This is not a valid DNS name'), | |
153 | ||
eaa8d084 | 154 | // email regex used by pve-common |
bb64de6e | 155 | proxmoxMail: function(v) { |
eaa8d084 | 156 | return (/^[\w+-~]+(\.[\w+-~]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/).test(v); |
bb64de6e DM |
157 | }, |
158 | proxmoxMailText: gettext('Example') + ": user@example.com", | |
159 | ||
ba916e58 DC |
160 | DnsOrIp: function(v) { |
161 | if (!Proxmox.Utils.DnsName_match.test(v) && | |
694a76f6 | 162 | !Proxmox.Utils.IP64_match.test(v)) { |
ba916e58 DC |
163 | return false; |
164 | } | |
165 | ||
166 | return true; | |
167 | }, | |
694a76f6 | 168 | DnsOrIpText: gettext('Not a valid DNS name or IP address.'), |
ba916e58 | 169 | |
5252b7f1 DC |
170 | HostPort: function(v) { |
171 | return Proxmox.Utils.HostPort_match.test(v) || | |
172 | Proxmox.Utils.HostPortBrackets_match.test(v) || | |
173 | Proxmox.Utils.IP6_dotnotation_match.test(v); | |
174 | }, | |
a9bea104 | 175 | HostPortText: gettext('Host/IP address or optional port is invalid'), |
5252b7f1 | 176 | |
bb64de6e | 177 | HostList: function(v) { |
05a977a2 TL |
178 | let list = v.split(/[ ,;]+/); |
179 | let i; | |
bb64de6e | 180 | for (i = 0; i < list.length; i++) { |
05a977a2 | 181 | if (list[i] === '') { |
bb64de6e DM |
182 | continue; |
183 | } | |
184 | ||
185 | if (!Proxmox.Utils.HostPort_match.test(list[i]) && | |
186 | !Proxmox.Utils.HostPortBrackets_match.test(list[i]) && | |
187 | !Proxmox.Utils.IP6_dotnotation_match.test(list[i])) { | |
188 | return false; | |
189 | } | |
190 | } | |
191 | ||
192 | return true; | |
193 | }, | |
36704a2f DM |
194 | HostListText: gettext('Not a valid list of hosts'), |
195 | ||
196 | password: function(val, field) { | |
197 | if (field.initialPassField) { | |
05a977a2 TL |
198 | let pwd = field.up('form').down(`[name=${field.initialPassField}]`); |
199 | return val === pwd.getValue(); | |
36704a2f DM |
200 | } |
201 | return true; | |
202 | }, | |
203 | ||
01031528 | 204 | passwordText: gettext('Passwords do not match'), |
dcd5a0c6 SI |
205 | |
206 | email: function(value) { | |
df096ae3 | 207 | let emailre = /^[\w+~-]+(\.[\w+~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/; |
dcd5a0c6 SI |
208 | return emailre.test(value); |
209 | }, | |
bb64de6e DM |
210 | }); |
211 | ||
3c158249 TL |
212 | // we always want the number in x.y format and never in, e.g., x,y |
213 | Ext.define('PVE.form.field.Number', { | |
214 | override: 'Ext.form.field.Number', | |
01031528 | 215 | submitLocaleSeparator: false, |
3c158249 TL |
216 | }); |
217 | ||
c4ac6d1c TL |
218 | // avois spamming the console and if we ever use this avoid a CORS block error too |
219 | Ext.define('PVE.draw.Container', { | |
220 | override: 'Ext.draw.Container', | |
221 | defaultDownloadServerUrl: document.location.origin, // avoid that pointing to http://svg.sencha.io | |
222 | applyDownloadServerUrl: function(url) { // avoid noisy warning, we don't really use that anyway | |
223 | url = url || this.defaultDownloadServerUrl; | |
224 | return url; | |
225 | }, | |
226 | }); | |
227 | ||
bb64de6e DM |
228 | // ExtJs 5-6 has an issue with caching |
229 | // see https://www.sencha.com/forum/showthread.php?308989 | |
230 | Ext.define('Proxmox.UnderlayPool', { | |
231 | override: 'Ext.dom.UnderlayPool', | |
232 | ||
01031528 | 233 | checkOut: function() { |
05a977a2 | 234 | let cache = this.cache, |
bb64de6e DM |
235 | len = cache.length, |
236 | el; | |
237 | ||
238 | // do cleanup because some of the objects might have been destroyed | |
239 | while (len--) { | |
240 | if (cache[len].destroyed) { | |
241 | cache.splice(len, 1); | |
242 | } | |
243 | } | |
244 | // end do cleanup | |
245 | ||
246 | el = cache.shift(); | |
247 | ||
248 | if (!el) { | |
249 | el = Ext.Element.create(this.elementConfig); | |
250 | el.setVisibilityMode(2); | |
251 | //<debug> | |
252 | // tell the spec runner to ignore this element when checking if the dom is clean | |
253 | el.dom.setAttribute('data-sticky', true); | |
254 | //</debug> | |
255 | } | |
256 | ||
257 | return el; | |
01031528 | 258 | }, |
bb64de6e DM |
259 | }); |
260 | ||
261 | // if the order of the values are not the same in originalValue and value | |
262 | // extjs will not overwrite value, but marks the field dirty and thus | |
263 | // the reset button will be enabled (but clicking it changes nothing) | |
264 | // so if the arrays are not the same after resetting, we | |
265 | // clear and set it | |
266 | Ext.define('Proxmox.form.ComboBox', { | |
267 | override: 'Ext.form.field.ComboBox', | |
268 | ||
269 | reset: function() { | |
270 | // copied from combobox | |
05a977a2 | 271 | let me = this; |
bb64de6e | 272 | me.callParent(); |
bb64de6e DM |
273 | |
274 | // clear and set when not the same | |
05a977a2 TL |
275 | let value = me.getValue(); |
276 | if (Ext.isArray(me.originalValue) && Ext.isArray(value) && | |
277 | !Ext.Array.equals(value, me.originalValue)) { | |
bb64de6e DM |
278 | me.clearValue(); |
279 | me.setValue(me.originalValue); | |
280 | } | |
50032132 DC |
281 | }, |
282 | ||
283 | // we also want to open the trigger on editable comboboxes by default | |
284 | initComponent: function() { | |
285 | let me = this; | |
286 | me.callParent(); | |
287 | ||
288 | if (me.editable) { | |
289 | // The trigger.picker causes first a focus event on the field then | |
290 | // toggles the selection picker. Thus skip expanding in this case, | |
291 | // else our focus listener expands and the picker.trigger then | |
292 | // collapses it directly afterwards. | |
293 | Ext.override(me.triggers.picker, { | |
294 | onMouseDown: function(e) { | |
295 | // copied "should we focus" check from Ext.form.trigger.Trigger | |
296 | if (e.pointerType !== 'touch' && !this.field.owns(Ext.Element.getActiveElement())) { | |
297 | me.skip_expand_on_focus = true; | |
298 | } | |
299 | this.callParent(arguments); | |
01031528 | 300 | }, |
50032132 DC |
301 | }); |
302 | ||
303 | me.on("focus", function(combobox) { | |
304 | if (!combobox.isExpanded && !combobox.skip_expand_on_focus) { | |
305 | combobox.expand(); | |
306 | } | |
307 | combobox.skip_expand_on_focus = false; | |
308 | }); | |
309 | } | |
310 | }, | |
bb64de6e DM |
311 | }); |
312 | ||
47d1c80c TL |
313 | // when refreshing a grid/tree view, restoring the focus moves the view back to |
314 | // the previously focused item. Save scroll position before refocusing. | |
315 | Ext.define(null, { | |
316 | override: 'Ext.view.Table', | |
317 | ||
318 | jumpToFocus: false, | |
319 | ||
320 | saveFocusState: function() { | |
9c1296e5 | 321 | var me = this, |
05a977a2 TL |
322 | store = me.dataSource, |
323 | actionableMode = me.actionableMode, | |
324 | navModel = me.getNavigationModel(), | |
325 | focusPosition = actionableMode ? me.actionPosition : navModel.getPosition(true), | |
9c1296e5 DC |
326 | activeElement = Ext.fly(Ext.Element.getActiveElement()), |
327 | focusCell = focusPosition && focusPosition.view === me && | |
328 | Ext.fly(focusPosition.getCell(true)), | |
329 | refocusRow, refocusCol, record; | |
330 | ||
331 | // The navModel may return a position that is in a locked partner, so check that | |
332 | // the focusPosition's cell contains the focus before going forward. | |
333 | // The skipSaveFocusState is set by Actionables which actively control | |
334 | // focus destination. See CellEditing#activateCell. | |
335 | if (!me.skipSaveFocusState && focusCell && focusCell.contains(activeElement)) { | |
05a977a2 TL |
336 | // Separate this from the instance that the nav model is using. |
337 | focusPosition = focusPosition.clone(); | |
338 | ||
9c1296e5 DC |
339 | // While we deactivate the focused element, suspend focus processing on it. |
340 | activeElement.suspendFocusEvents(); | |
341 | ||
342 | // Suspend actionable mode. | |
343 | // Each Actionable must silently save its state ready to resume when focus | |
344 | // can be restored but should only do that if the activeElement is not the cell itself, | |
345 | // this happens when the grid is refreshed while one of the actionables is being | |
346 | // deactivated (e.g. Calling view refresh inside CellEditor 'edit' event listener). | |
347 | if (actionableMode && focusCell.dom !== activeElement.dom) { | |
348 | me.suspendActionableMode(); | |
349 | } else { | |
350 | // Clear position, otherwise the setPosition on the other side | |
351 | // will be rejected as a no-op if the resumption position is logically | |
352 | // equivalent. | |
353 | actionableMode = false; | |
354 | navModel.setPosition(); | |
05a977a2 | 355 | } |
47d1c80c | 356 | |
017a6376 | 357 | // Do not leave the element in that state in case refresh fails, and restoration |
9c1296e5 DC |
358 | // closure not called. |
359 | activeElement.resumeFocusEvents(); | |
05a977a2 | 360 | |
9c1296e5 DC |
361 | // if the store is expanding or collapsing, we should never scroll the view. |
362 | if (store.isExpandingOrCollapsing) { | |
363 | return Ext.emptyFn; | |
364 | } | |
05a977a2 TL |
365 | |
366 | // The following function will attempt to refocus back in the same mode to the same cell | |
9c1296e5 DC |
367 | // as it was at before based upon the previous record (if it's still in the store), |
368 | // or the row index. | |
05a977a2 | 369 | return function() { |
9c1296e5 DC |
370 | var all; |
371 | ||
372 | // May have changed due to reconfigure | |
373 | store = me.dataSource; | |
374 | ||
05a977a2 TL |
375 | // If we still have data, attempt to refocus in the same mode. |
376 | if (store.getCount()) { | |
9c1296e5 DC |
377 | all = me.all; |
378 | ||
379 | // Adjust expectations of where we are able to refocus according to | |
380 | // what kind of destruction might have been wrought on this view's DOM | |
381 | // during focus save. | |
382 | refocusRow = | |
383 | Math.min(Math.max(focusPosition.rowIdx, all.startIndex), all.endIndex); | |
384 | ||
385 | refocusCol = Math.min( | |
386 | focusPosition.colIdx, | |
387 | me.getVisibleColumnManager().getColumns().length - 1, | |
388 | ); | |
389 | ||
390 | record = focusPosition.record; | |
391 | ||
392 | focusPosition = new Ext.grid.CellContext(me).setPosition( | |
393 | record && store.contains(record) && !record.isCollapsedPlaceholder | |
394 | ? record | |
395 | : refocusRow, | |
396 | refocusCol, | |
397 | ); | |
398 | ||
399 | // Maybe there are no cells. eg: all groups collapsed. | |
400 | if (focusPosition.getCell(true)) { | |
401 | if (actionableMode) { | |
402 | me.resumeActionableMode(focusPosition); | |
403 | } else { | |
404 | // we sometimes want to scroll back to where we are | |
405 | ||
406 | let x = me.getScrollX(); | |
407 | let y = me.getScrollY(); | |
408 | ||
409 | // Pass "preventNavigation" as true | |
410 | // so that that does not cause selection. | |
411 | navModel.setPosition(focusPosition, null, null, null, true); | |
412 | ||
413 | if (!navModel.getPosition()) { | |
414 | focusPosition.column.focus(); | |
415 | } | |
416 | ||
417 | if (!me.jumpToFocus) { | |
418 | me.scrollTo(x, y); | |
419 | } | |
47d1c80c | 420 | } |
05a977a2 TL |
421 | } |
422 | } else { // No rows - focus associated column header | |
423 | focusPosition.column.focus(); | |
424 | } | |
425 | }; | |
426 | } | |
427 | return Ext.emptyFn; | |
01031528 | 428 | }, |
47d1c80c TL |
429 | }); |
430 | ||
a55813d6 DM |
431 | // ExtJS 6.0.1 has no setSubmitValue() (although you find it in the docs). |
432 | // Note: this.submitValue is a boolean flag, whereas getSubmitValue() returns | |
433 | // data to be submitted. | |
1d9804a9 DM |
434 | Ext.define('Proxmox.form.field.Text', { |
435 | override: 'Ext.form.field.Text', | |
436 | ||
437 | setSubmitValue: function(v) { | |
438 | this.submitValue = v; | |
439 | }, | |
1d9804a9 DM |
440 | }); |
441 | ||
d78eb5ec DC |
442 | // make mousescrolling work in firefox in the containers overflowhandler, |
443 | // by using only the 'wheel' event not 'mousewheel'(fixed in 7.3) | |
444 | // also reverse the scrolldirection (fixed in 7.3) | |
445 | // and reduce the default increment | |
1fb41f2e TL |
446 | Ext.define(null, { |
447 | override: 'Ext.layout.container.boxOverflow.Scroller', | |
448 | ||
d78eb5ec DC |
449 | wheelIncrement: 1, |
450 | ||
451 | getWheelDelta: function(e) { | |
452 | return -e.getWheelDelta(e); | |
453 | }, | |
454 | ||
455 | onOwnerRender: function(owner) { | |
456 | var me = this, | |
457 | scrollable = { | |
458 | isBoxOverflowScroller: true, | |
459 | x: false, | |
460 | y: false, | |
461 | listeners: { | |
462 | scrollend: this.onScrollEnd, | |
463 | scope: this, | |
464 | }, | |
465 | }; | |
466 | ||
467 | // If no obstrusive scrollbars, allow natural scrolling on mobile touch devices | |
468 | if (!Ext.scrollbar.width() && !Ext.platformTags.desktop) { | |
469 | scrollable[owner.layout.horizontal ? 'x' : 'y'] = true; | |
1fb41f2e | 470 | } else { |
d78eb5ec DC |
471 | me.wheelListener = me.layout.innerCt.on( |
472 | 'wheel', me.onMouseWheel, me, { destroyable: true }, | |
473 | ); | |
1fb41f2e | 474 | } |
1fb41f2e | 475 | |
d78eb5ec | 476 | owner.setScrollable(scrollable); |
01031528 | 477 | }, |
d78eb5ec DC |
478 | }); |
479 | ||
480 | // extj 6.7 reversed mousewheel direction... (fixed in 7.3) | |
481 | // https://forum.sencha.com/forum/showthread.php?472517-Mousewheel-scroll-direction-in-numberfield-with-spinners | |
017a6376 | 482 | // also use the 'wheel' event instead of 'mousewheel' (fixed in 7.3) |
d78eb5ec DC |
483 | Ext.define('Proxmox.form.field.Spinner', { |
484 | override: 'Ext.form.field.Spinner', | |
485 | ||
486 | onRender: function() { | |
94d84581 | 487 | let me = this; |
1fb41f2e | 488 | |
d78eb5ec DC |
489 | me.callParent(); |
490 | ||
d78eb5ec DC |
491 | // Init mouse wheel |
492 | if (me.mouseWheelEnabled) { | |
94d84581 DT |
493 | // Unlisten Ext generated listener ('mousewheel' is deprecated anyway) |
494 | me.mun(me.bodyEl, 'mousewheel', me.onMouseWheel, me); | |
495 | ||
d78eb5ec DC |
496 | me.mon(me.bodyEl, 'wheel', me.onMouseWheel, me); |
497 | } | |
d78eb5ec DC |
498 | }, |
499 | ||
500 | onMouseWheel: function(e) { | |
501 | var me = this, | |
502 | delta; | |
503 | if (me.hasFocus) { | |
504 | delta = e.getWheelDelta(); | |
505 | if (delta > 0) { | |
506 | me.spinDown(); | |
507 | } else if (delta < 0) { | |
508 | me.spinUp(); | |
509 | } | |
510 | e.stopEvent(); | |
511 | me.onSpinEnd(); | |
512 | } | |
513 | }, | |
1fb41f2e TL |
514 | }); |
515 | ||
312310e0 DC |
516 | // add '@' to the valid id |
517 | Ext.define('Proxmox.validIdReOverride', { | |
518 | override: 'Ext.Component', | |
05a977a2 | 519 | validIdRe: /^[a-z_][a-z0-9\-_@]*$/i, |
312310e0 DC |
520 | }); |
521 | ||
98d894d8 DC |
522 | Ext.define('Proxmox.selection.CheckboxModel', { |
523 | override: 'Ext.selection.CheckboxModel', | |
524 | ||
1f628e9c | 525 | // [P] use whole checkbox cell to multiselect, not only the checkbox |
98d894d8 | 526 | checkSelector: '.x-grid-cell-row-checker', |
1f628e9c | 527 | |
ea2b3fd7 TL |
528 | // TODO: remove all optimizations below to an override for parent 'Ext.selection.Model' ?? |
529 | ||
1f628e9c TL |
530 | // [ P: optimized to remove all records at once as single remove is O(n^3) slow ] |
531 | // records can be an index, a record or an array of records | |
532 | doDeselect: function(records, suppressEvent) { | |
533 | var me = this, | |
534 | selected = me.selected, | |
535 | i = 0, | |
536 | len, record, | |
537 | commit; | |
538 | if (me.locked || !me.store) { | |
539 | return false; | |
540 | } | |
541 | if (typeof records === "number") { | |
542 | // No matching record, jump out | |
543 | record = me.store.getAt(records); | |
544 | if (!record) { | |
545 | return false; | |
546 | } | |
547 | records = [ | |
548 | record, | |
549 | ]; | |
550 | } else if (!Ext.isArray(records)) { | |
551 | records = [ | |
552 | records, | |
553 | ]; | |
554 | } | |
555 | // [P] a beforedeselection, triggered by me.onSelectChange below, can block removal by | |
556 | // returning false, thus the original implementation removed only here in the commit fn, | |
557 | // which has an abysmal performance O(n^3). As blocking removal is not the norm, go do the | |
558 | // reverse, record blocked records and remove them from the to-be-removed array before | |
559 | // applying it. A FF86 i9-9900K on 10k records goes from >40s to ~33ms for >90% deselection | |
560 | let committed = false; | |
561 | commit = function() { | |
562 | committed = true; | |
563 | if (record === me.selectionStart) { | |
564 | me.selectionStart = null; | |
565 | } | |
566 | }; | |
567 | let removalBlocked = []; | |
568 | len = records.length; | |
569 | me.suspendChanges(); | |
570 | for (; i < len; i++) { | |
571 | record = records[i]; | |
572 | if (me.isSelected(record)) { | |
573 | committed = false; | |
574 | me.onSelectChange(record, false, suppressEvent, commit); | |
575 | if (!committed) { | |
576 | removalBlocked.push(record); | |
577 | } | |
578 | if (me.destroyed) { | |
579 | return false; | |
580 | } | |
581 | } | |
582 | } | |
583 | if (removalBlocked.length > 0) { | |
584 | records.remove(removalBlocked); | |
585 | } | |
586 | selected.remove(records); // [P] FAST(er) | |
587 | me.lastSelected = selected.last(); | |
588 | me.resumeChanges(); | |
589 | // fire selchange if there was a change and there is no suppressEvent flag | |
590 | me.maybeFireSelectionChange(records.length > 0 && !suppressEvent); | |
ea2b3fd7 TL |
591 | return records.length; |
592 | }, | |
593 | ||
594 | ||
595 | doMultiSelect: function(records, keepExisting, suppressEvent) { | |
596 | var me = this, | |
597 | selected = me.selected, | |
598 | change = false, | |
599 | result, i, len, record, commit; | |
600 | ||
601 | if (me.locked) { | |
602 | return; | |
603 | } | |
604 | ||
605 | records = !Ext.isArray(records) ? [records] : records; | |
606 | len = records.length; | |
607 | if (!keepExisting && selected.getCount() > 0) { | |
608 | result = me.deselectDuringSelect(records, suppressEvent); | |
609 | if (me.destroyed) { | |
610 | return; | |
611 | } | |
612 | if (result[0]) { | |
613 | // We had a failure during selection, so jump out | |
614 | // Fire selection change if we did deselect anything | |
615 | me.maybeFireSelectionChange(result[1] > 0 && !suppressEvent); | |
616 | return; | |
617 | } else { | |
618 | // Means something has been deselected, so we've had a change | |
619 | change = result[1] > 0; | |
620 | } | |
621 | } | |
622 | ||
623 | let gotBlocked, blockedRecords = []; | |
624 | commit = function() { | |
625 | if (!selected.getCount()) { | |
626 | me.selectionStart = record; | |
627 | } | |
628 | gotBlocked = false; | |
629 | change = true; | |
630 | }; | |
631 | ||
632 | for (i = 0; i < len; i++) { | |
633 | record = records[i]; | |
634 | if (me.isSelected(record)) { | |
635 | continue; | |
636 | } | |
637 | ||
638 | gotBlocked = true; | |
639 | me.onSelectChange(record, true, suppressEvent, commit); | |
640 | if (me.destroyed) { | |
641 | return; | |
642 | } | |
643 | if (gotBlocked) { | |
644 | blockedRecords.push(record); | |
645 | } | |
646 | } | |
647 | if (blockedRecords.length > 0) { | |
648 | records.remove(blockedRecords); | |
649 | } | |
650 | selected.add(records); | |
651 | me.lastSelected = record; | |
652 | ||
653 | // fire selchange if there was a change and there is no suppressEvent flag | |
654 | me.maybeFireSelectionChange(change && !suppressEvent); | |
655 | }, | |
656 | deselectDuringSelect: function(toSelect, suppressEvent) { | |
657 | var me = this, | |
658 | selected = me.selected.getRange(), | |
659 | changed = 0, | |
660 | failed = false; | |
661 | // Prevent selection change events from firing, will happen during select | |
662 | me.suspendChanges(); | |
663 | me.deselectingDuringSelect = true; | |
664 | let toDeselect = selected.filter(item => !Ext.Array.contains(toSelect, item)); | |
665 | if (toDeselect.length > 0) { | |
666 | changed = me.doDeselect(toDeselect, suppressEvent); | |
667 | if (!changed) { | |
668 | failed = true; | |
669 | } | |
670 | if (me.destroyed) { | |
671 | failed = true; | |
672 | changed = 0; | |
673 | } | |
674 | } | |
675 | me.deselectingDuringSelect = false; | |
676 | me.resumeChanges(); | |
677 | return [ | |
678 | failed, | |
679 | changed, | |
680 | ]; | |
1f628e9c | 681 | }, |
98d894d8 DC |
682 | }); |
683 | ||
322de562 DC |
684 | // stop nulling of properties |
685 | Ext.define('Proxmox.Component', { | |
686 | override: 'Ext.Component', | |
687 | clearPropertiesOnDestroy: false, | |
688 | }); | |
689 | ||
da24e83f | 690 | // Fix drag&drop for vms and desktops that detect 'pen' pointerType |
4fecfeef | 691 | // NOTE: this part has been rewritten in ExtJS 7.4, so re-check once we can upgrade |
da24e83f DC |
692 | Ext.define('Proxmox.view.DragZone', { |
693 | override: 'Ext.view.DragZone', | |
694 | ||
695 | onItemMouseDown: function(view, record, item, index, e) { | |
696 | // Ignore touchstart. | |
697 | // For touch events, we use longpress. | |
698 | if (e.pointerType !== 'touch') { | |
699 | this.onTriggerGesture(view, record, item, index, e); | |
700 | } | |
701 | }, | |
702 | }); | |
703 | ||
bb64de6e DM |
704 | // force alert boxes to be rendered with an Error Icon |
705 | // since Ext.Msg is an object and not a prototype, we need to override it | |
706 | // after the framework has been initiated | |
707 | Ext.onReady(function() { | |
bb64de6e | 708 | Ext.override(Ext.Msg, { |
05a977a2 | 709 | alert: function(title, message, fn, scope) { // eslint-disable-line consistent-return |
bb64de6e | 710 | if (Ext.isString(title)) { |
05a977a2 | 711 | let config = { |
bb64de6e DM |
712 | title: title, |
713 | message: message, | |
714 | icon: this.ERROR, | |
715 | buttons: this.OK, | |
716 | fn: fn, | |
01031528 TL |
717 | scope: scope, |
718 | minWidth: this.minWidth, | |
bb64de6e DM |
719 | }; |
720 | return this.show(config); | |
721 | } | |
01031528 | 722 | }, |
bb64de6e | 723 | }); |
bb64de6e DM |
724 | }); |
725 | Ext.define('Ext.ux.IFrame', { | |
726 | extend: 'Ext.Component', | |
727 | ||
728 | alias: 'widget.uxiframe', | |
729 | ||
730 | loadMask: 'Loading...', | |
731 | ||
732 | src: 'about:blank', | |
733 | ||
734 | renderTpl: [ | |
01031528 | 735 | '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>', |
bb64de6e DM |
736 | ], |
737 | childEls: ['iframeEl'], | |
738 | ||
01031528 | 739 | initComponent: function() { |
bb64de6e DM |
740 | this.callParent(); |
741 | ||
742 | this.frameName = this.frameName || this.id + '-frame'; | |
743 | }, | |
744 | ||
01031528 | 745 | initEvents: function() { |
05a977a2 | 746 | let me = this; |
bb64de6e DM |
747 | me.callParent(); |
748 | me.iframeEl.on('load', me.onLoad, me); | |
749 | }, | |
750 | ||
751 | initRenderData: function() { | |
752 | return Ext.apply(this.callParent(), { | |
753 | src: this.src, | |
01031528 | 754 | frameName: this.frameName, |
bb64de6e DM |
755 | }); |
756 | }, | |
757 | ||
758 | getBody: function() { | |
05a977a2 | 759 | let doc = this.getDoc(); |
bb64de6e DM |
760 | return doc.body || doc.documentElement; |
761 | }, | |
762 | ||
763 | getDoc: function() { | |
764 | try { | |
765 | return this.getWin().document; | |
766 | } catch (ex) { | |
767 | return null; | |
768 | } | |
769 | }, | |
770 | ||
771 | getWin: function() { | |
05a977a2 | 772 | let me = this, |
bb64de6e DM |
773 | name = me.frameName, |
774 | win = Ext.isIE | |
775 | ? me.iframeEl.dom.contentWindow | |
776 | : window.frames[name]; | |
777 | return win; | |
778 | }, | |
779 | ||
780 | getFrame: function() { | |
05a977a2 | 781 | let me = this; |
bb64de6e DM |
782 | return me.iframeEl.dom; |
783 | }, | |
784 | ||
01031528 | 785 | beforeDestroy: function() { |
bb64de6e DM |
786 | this.cleanupListeners(true); |
787 | this.callParent(); | |
788 | }, | |
789 | ||
01031528 | 790 | cleanupListeners: function(destroying) { |
05a977a2 | 791 | let doc, prop; |
bb64de6e DM |
792 | |
793 | if (this.rendered) { | |
794 | try { | |
795 | doc = this.getDoc(); | |
796 | if (doc) { | |
bb64de6e | 797 | Ext.get(doc).un(this._docListeners); |
bb64de6e DM |
798 | if (destroying && doc.hasOwnProperty) { |
799 | for (prop in doc) { | |
05a977a2 | 800 | if (Object.prototype.hasOwnProperty.call(doc, prop)) { |
bb64de6e DM |
801 | delete doc[prop]; |
802 | } | |
803 | } | |
804 | } | |
805 | } | |
05a977a2 TL |
806 | } catch (e) { |
807 | // do nothing | |
808 | } | |
bb64de6e DM |
809 | } |
810 | }, | |
811 | ||
812 | onLoad: function() { | |
05a977a2 | 813 | let me = this, |
bb64de6e DM |
814 | doc = me.getDoc(), |
815 | fn = me.onRelayedEvent; | |
816 | ||
817 | if (doc) { | |
818 | try { | |
819 | // These events need to be relayed from the inner document (where they stop | |
820 | // bubbling) up to the outer document. This has to be done at the DOM level so | |
821 | // the event reaches listeners on elements like the document body. The effected | |
822 | // mechanisms that depend on this bubbling behavior are listed to the right | |
823 | // of the event. | |
bb64de6e DM |
824 | Ext.get(doc).on( |
825 | me._docListeners = { | |
826 | mousedown: fn, // menu dismisal (MenuManager) and Window onMouseDown (toFront) | |
827 | mousemove: fn, // window resize drag detection | |
01031528 TL |
828 | mouseup: fn, // window resize termination |
829 | click: fn, // not sure, but just to be safe | |
830 | dblclick: fn, // not sure again | |
831 | scope: me, | |
832 | }, | |
bb64de6e | 833 | ); |
01031528 | 834 | } catch (e) { |
bb64de6e DM |
835 | // cannot do this xss |
836 | } | |
837 | ||
838 | // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK! | |
839 | Ext.get(this.getWin()).on('beforeunload', me.cleanupListeners, me); | |
840 | ||
841 | this.el.unmask(); | |
842 | this.fireEvent('load', this); | |
bb64de6e | 843 | } else if (me.src) { |
bb64de6e DM |
844 | this.el.unmask(); |
845 | this.fireEvent('error', this); | |
846 | } | |
bb64de6e DM |
847 | }, |
848 | ||
01031528 | 849 | onRelayedEvent: function(event) { |
bb64de6e DM |
850 | // relay event from the iframe's document to the document that owns the iframe... |
851 | ||
05a977a2 | 852 | let iframeEl = this.iframeEl, |
bb64de6e DM |
853 | |
854 | // Get the left-based iframe position | |
855 | iframeXY = iframeEl.getTrueXY(), | |
856 | originalEventXY = event.getXY(), | |
857 | ||
858 | // Get the left-based XY position. | |
859 | // This is because the consumer of the injected event will | |
860 | // perform its own RTL normalization. | |
861 | eventXY = event.getTrueXY(); | |
862 | ||
863 | // the event from the inner document has XY relative to that document's origin, | |
864 | // so adjust it to use the origin of the iframe in the outer document: | |
865 | event.xy = [iframeXY[0] + eventXY[0], iframeXY[1] + eventXY[1]]; | |
866 | ||
867 | event.injectEvent(iframeEl); // blame the iframe for the event... | |
868 | ||
869 | event.xy = originalEventXY; // restore the original XY (just for safety) | |
870 | }, | |
871 | ||
01031528 | 872 | load: function(src) { |
05a977a2 | 873 | let me = this, |
bb64de6e DM |
874 | text = me.loadMask, |
875 | frame = me.getFrame(); | |
876 | ||
877 | if (me.fireEvent('beforeload', me, src) !== false) { | |
878 | if (text && me.el) { | |
879 | me.el.mask(text); | |
880 | } | |
881 | ||
01031528 | 882 | frame.src = me.src = src || me.src; |
bb64de6e | 883 | } |
01031528 | 884 | }, |
bb64de6e | 885 | }); |