]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - src/Toolkit.js
6f1aee9f6288dce36930d7f270dbdfadfd55c958
1 // ExtJS related things
3 // do not send '_dc' parameter
4 Ext
.Ajax
.disableCaching
= false;
7 Ext
.apply(Ext
.form
.field
.VTypes
, {
8 IPAddress: function(v
) {
9 return Proxmox
.Utils
.IP4_match
.test(v
);
11 IPAddressText
: gettext('Example') + ': 192.168.1.1',
12 IPAddressMask
: /[\d.]/i,
14 IPCIDRAddress: function(v
) {
15 let 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;
20 IPCIDRAddressText
: gettext('Example') + ': 192.168.1.1/24<br>' + gettext('Valid CIDR Range') + ': 8-32',
21 IPCIDRAddressMask
: /[\d./]/i
,
23 IP6Address: function(v
) {
24 return Proxmox
.Utils
.IP6_match
.test(v
);
26 IP6AddressText
: gettext('Example') + ': 2001:DB8::42',
27 IP6AddressMask
: /[A-Fa-f0-9:]/,
29 IP6CIDRAddress: function(v
) {
30 let 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] <= 128;
35 IP6CIDRAddressText
: gettext('Example') + ': 2001:DB8::42/64<br>' + gettext('Valid CIDR Range') + ': 8-128',
36 IP6CIDRAddressMask
: /[A-Fa-f0-9:/]/,
38 IP6PrefixLength: function(v
) {
39 return v
>= 0 && v
<= 128;
41 IP6PrefixLengthText
: gettext('Example') + ': X, where 0 <= X <= 128',
42 IP6PrefixLengthMask
: /[0-9]/,
44 IP64Address: function(v
) {
45 return Proxmox
.Utils
.IP64_match
.test(v
);
47 IP64AddressText
: gettext('Example') + ': 192.168.1.1 2001:DB8::42',
48 IP64AddressMask
: /[A-Fa-f0-9.:]/,
50 IP64CIDRAddress: function(v
) {
51 let result
= Proxmox
.Utils
.IP64_cidr_match
.exec(v
);
52 if (result
=== null) {
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;
63 IP64CIDRAddressText
: gettext('Example') + ': 192.168.1.1/24 2001:DB8::42/64',
64 IP64CIDRAddressMask
: /[A-Fa-f0-9.:/]/,
66 MacAddress: function(v
) {
67 return (/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/).test(v
);
69 MacAddressMask
: /[a-fA-F0-9:]/,
70 MacAddressText
: gettext('Example') + ': 01:23:45:67:89:ab',
72 MacPrefix: function(v
) {
73 return (/^[a-f0-9][02468ace](?::[a-f0-9]{2}){0,2}:?$/i).test(v
);
75 MacPrefixMask
: /[a-fA-F0-9:]/,
76 MacPrefixText
: gettext('Example') + ': 02:8f - ' + gettext('only unicast addresses are allowed'),
78 BridgeName: function(v
) {
79 return (/^vmbr\d{1,4}$/).test(v
);
81 VlanName: function(v
) {
82 if (Proxmox
.Utils
.VlanInterface_match
.test(v
)) {
84 } else if (Proxmox
.Utils
.Vlan_match
.test(v
)) {
89 BridgeNameText
: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999',
91 BondName: function(v
) {
92 return (/^bond\d{1,4}$/).test(v
);
94 BondNameText
: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999',
96 InterfaceName: function(v
) {
97 return (/^[a-z][a-z0-9_]{1,20}$/).test(v
);
99 InterfaceNameText
: gettext("Allowed characters") + ": 'a-z', '0-9', '_'<br />" +
100 gettext("Minimum characters") + ": 2<br />" +
101 gettext("Maximum characters") + ": 21<br />" +
102 gettext("Must start with") + ": 'a-z'",
104 StorageId: function(v
) {
105 return (/^[a-z][a-z0-9\-_.]*[a-z0-9]$/i).test(v
);
107 StorageIdText
: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '-', '_', '.'<br />" +
108 gettext("Minimum characters") + ": 2<br />" +
109 gettext("Must start with") + ": 'A-Z', 'a-z'<br />" +
110 gettext("Must end with") + ": 'A-Z', 'a-z', '0-9'<br />",
112 ConfigId: function(v
) {
113 return (/^[a-z][a-z0-9_]+$/i).test(v
);
115 ConfigIdText
: gettext("Allowed characters") + ": 'A-Z', 'a-z', '0-9', '_'<br />" +
116 gettext("Minimum characters") + ": 2<br />" +
117 gettext("Must start with") + ": " + gettext("letter"),
119 HttpProxy: function(v
) {
120 return (/^http:\/\/.*$/).test(v
);
122 HttpProxyText
: gettext('Example') + ": http://username:password@host:port/",
124 DnsName: function(v
) {
125 return Proxmox
.Utils
.DnsName_match
.test(v
);
127 DnsNameText
: gettext('This is not a valid DNS name'),
129 // workaround for https://www.sencha.com/forum/showthread.php?302150
130 proxmoxMail: function(v
) {
131 return (/^(\w+)([-+.][\w]+)*@(\w[-\w]*\.){1,5}([A-Za-z]){2,63}$/).test(v
);
133 proxmoxMailText
: gettext('Example') + ": user@example.com",
135 DnsOrIp: function(v
) {
136 if (!Proxmox
.Utils
.DnsName_match
.test(v
) &&
137 !Proxmox
.Utils
.IP64_match
.test(v
)) {
143 DnsOrIpText
: gettext('Not a valid DNS name or IP address.'),
145 HostPort: function(v
) {
146 return Proxmox
.Utils
.HostPort_match
.test(v
) ||
147 Proxmox
.Utils
.HostPortBrackets_match
.test(v
) ||
148 Proxmox
.Utils
.IP6_dotnotation_match
.test(v
);
150 HostPortText
: gettext('Host/IP address or optional port is invalid'),
152 HostList: function(v
) {
153 let list
= v
.split(/[ ,;]+/);
155 for (i
= 0; i
< list
.length
; i
++) {
156 if (list
[i
] === '') {
160 if (!Proxmox
.Utils
.HostPort_match
.test(list
[i
]) &&
161 !Proxmox
.Utils
.HostPortBrackets_match
.test(list
[i
]) &&
162 !Proxmox
.Utils
.IP6_dotnotation_match
.test(list
[i
])) {
169 HostListText
: gettext('Not a valid list of hosts'),
171 password: function(val
, field
) {
172 if (field
.initialPassField
) {
173 let pwd
= field
.up('form').down(`[name=${field.initialPassField}]`);
174 return val
=== pwd
.getValue();
179 passwordText
: gettext('Passwords do not match'),
181 email: function(value
) {
182 let emailre
= /^[\w+~-]+(\.[\w+~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/;
183 return emailre
.test(value
);
187 // we always want the number in x.y format and never in, e.g., x,y
188 Ext
.define('PVE.form.field.Number', {
189 override
: 'Ext.form.field.Number',
190 submitLocaleSeparator
: false,
193 // ExtJs 5-6 has an issue with caching
194 // see https://www.sencha.com/forum/showthread.php?308989
195 Ext
.define('Proxmox.UnderlayPool', {
196 override
: 'Ext.dom.UnderlayPool',
198 checkOut: function() {
199 let cache
= this.cache
,
203 // do cleanup because some of the objects might have been destroyed
205 if (cache
[len
].destroyed
) {
206 cache
.splice(len
, 1);
214 el
= Ext
.Element
.create(this.elementConfig
);
215 el
.setVisibilityMode(2);
217 // tell the spec runner to ignore this element when checking if the dom is clean
218 el
.dom
.setAttribute('data-sticky', true);
226 // if the order of the values are not the same in originalValue and value
227 // extjs will not overwrite value, but marks the field dirty and thus
228 // the reset button will be enabled (but clicking it changes nothing)
229 // so if the arrays are not the same after resetting, we
231 Ext
.define('Proxmox.form.ComboBox', {
232 override
: 'Ext.form.field.ComboBox',
235 // copied from combobox
239 // clear and set when not the same
240 let value
= me
.getValue();
241 if (Ext
.isArray(me
.originalValue
) && Ext
.isArray(value
) &&
242 !Ext
.Array
.equals(value
, me
.originalValue
)) {
244 me
.setValue(me
.originalValue
);
248 // we also want to open the trigger on editable comboboxes by default
249 initComponent: function() {
254 // The trigger.picker causes first a focus event on the field then
255 // toggles the selection picker. Thus skip expanding in this case,
256 // else our focus listener expands and the picker.trigger then
257 // collapses it directly afterwards.
258 Ext
.override(me
.triggers
.picker
, {
259 onMouseDown: function(e
) {
260 // copied "should we focus" check from Ext.form.trigger.Trigger
261 if (e
.pointerType
!== 'touch' && !this.field
.owns(Ext
.Element
.getActiveElement())) {
262 me
.skip_expand_on_focus
= true;
264 this.callParent(arguments
);
268 me
.on("focus", function(combobox
) {
269 if (!combobox
.isExpanded
&& !combobox
.skip_expand_on_focus
) {
272 combobox
.skip_expand_on_focus
= false;
278 // when refreshing a grid/tree view, restoring the focus moves the view back to
279 // the previously focused item. Save scroll position before refocusing.
281 override
: 'Ext.view.Table',
285 saveFocusState: function() {
287 store
= me
.dataSource
,
288 actionableMode
= me
.actionableMode
,
289 navModel
= me
.getNavigationModel(),
290 focusPosition
= actionableMode
? me
.actionPosition
: navModel
.getPosition(true),
291 activeElement
= Ext
.fly(Ext
.Element
.getActiveElement()),
292 focusCell
= focusPosition
&& focusPosition
.view
=== me
&&
293 Ext
.fly(focusPosition
.getCell(true)),
294 refocusRow
, refocusCol
, record
;
296 // The navModel may return a position that is in a locked partner, so check that
297 // the focusPosition's cell contains the focus before going forward.
298 // The skipSaveFocusState is set by Actionables which actively control
299 // focus destination. See CellEditing#activateCell.
300 if (!me
.skipSaveFocusState
&& focusCell
&& focusCell
.contains(activeElement
)) {
301 // Separate this from the instance that the nav model is using.
302 focusPosition
= focusPosition
.clone();
304 // While we deactivate the focused element, suspend focus processing on it.
305 activeElement
.suspendFocusEvents();
307 // Suspend actionable mode.
308 // Each Actionable must silently save its state ready to resume when focus
309 // can be restored but should only do that if the activeElement is not the cell itself,
310 // this happens when the grid is refreshed while one of the actionables is being
311 // deactivated (e.g. Calling view refresh inside CellEditor 'edit' event listener).
312 if (actionableMode
&& focusCell
.dom
!== activeElement
.dom
) {
313 me
.suspendActionableMode();
315 // Clear position, otherwise the setPosition on the other side
316 // will be rejected as a no-op if the resumption position is logically
318 actionableMode
= false;
319 navModel
.setPosition();
322 // Do not leave the element in tht state in case refresh fails, and restoration
323 // closure not called.
324 activeElement
.resumeFocusEvents();
326 // if the store is expanding or collapsing, we should never scroll the view.
327 if (store
.isExpandingOrCollapsing
) {
331 // The following function will attempt to refocus back in the same mode to the same cell
332 // as it was at before based upon the previous record (if it's still in the store),
337 // May have changed due to reconfigure
338 store
= me
.dataSource
;
340 // If we still have data, attempt to refocus in the same mode.
341 if (store
.getCount()) {
344 // Adjust expectations of where we are able to refocus according to
345 // what kind of destruction might have been wrought on this view's DOM
346 // during focus save.
348 Math
.min(Math
.max(focusPosition
.rowIdx
, all
.startIndex
), all
.endIndex
);
350 refocusCol
= Math
.min(
351 focusPosition
.colIdx
,
352 me
.getVisibleColumnManager().getColumns().length
- 1,
355 record
= focusPosition
.record
;
357 focusPosition
= new Ext
.grid
.CellContext(me
).setPosition(
358 record
&& store
.contains(record
) && !record
.isCollapsedPlaceholder
364 // Maybe there are no cells. eg: all groups collapsed.
365 if (focusPosition
.getCell(true)) {
366 if (actionableMode
) {
367 me
.resumeActionableMode(focusPosition
);
369 // we sometimes want to scroll back to where we are
371 let x
= me
.getScrollX();
372 let y
= me
.getScrollY();
374 // Pass "preventNavigation" as true
375 // so that that does not cause selection.
376 navModel
.setPosition(focusPosition
, null, null, null, true);
378 if (!navModel
.getPosition()) {
379 focusPosition
.column
.focus();
382 if (!me
.jumpToFocus
) {
387 } else { // No rows - focus associated column header
388 focusPosition
.column
.focus();
396 // ExtJS 6.0.1 has no setSubmitValue() (although you find it in the docs).
397 // Note: this.submitValue is a boolean flag, whereas getSubmitValue() returns
398 // data to be submitted.
399 Ext
.define('Proxmox.form.field.Text', {
400 override
: 'Ext.form.field.Text',
402 setSubmitValue: function(v
) {
403 this.submitValue
= v
;
407 // make mousescrolling work in firefox in the containers overflowhandler,
408 // by using only the 'wheel' event not 'mousewheel'(fixed in 7.3)
409 // also reverse the scrolldirection (fixed in 7.3)
410 // and reduce the default increment
412 override
: 'Ext.layout.container.boxOverflow.Scroller',
416 getWheelDelta: function(e
) {
417 return -e
.getWheelDelta(e
);
420 onOwnerRender: function(owner
) {
423 isBoxOverflowScroller
: true,
427 scrollend
: this.onScrollEnd
,
432 // If no obstrusive scrollbars, allow natural scrolling on mobile touch devices
433 if (!Ext
.scrollbar
.width() && !Ext
.platformTags
.desktop
) {
434 scrollable
[owner
.layout
.horizontal
? 'x' : 'y'] = true;
436 me
.wheelListener
= me
.layout
.innerCt
.on(
437 'wheel', me
.onMouseWheel
, me
, { destroyable
: true },
441 owner
.setScrollable(scrollable
);
445 // extj 6.7 reversed mousewheel direction... (fixed in 7.3)
446 // https://forum.sencha.com/forum/showthread.php?472517-Mousewheel-scroll-direction-in-numberfield-with-spinners
447 // alse use the 'wheel' event instead of 'mousewheel' (fixed in 7.3)
448 Ext
.define('Proxmox.form.field.Spinner', {
449 override
: 'Ext.form.field.Spinner',
451 onRender: function() {
453 spinnerTrigger
= me
.getTrigger('spinner');
457 // Init up/down arrow keys
458 if (me
.keyNavEnabled
) {
459 me
.spinnerKeyNav
= new Ext
.util
.KeyNav({
467 keyup
: me
.onInputElKeyUp
,
473 if (me
.mouseWheelEnabled
) {
474 me
.mon(me
.bodyEl
, 'wheel', me
.onMouseWheel
, me
);
477 // in v4 spinUpEl/spinDownEl were childEls, now they are children of the trigger.
478 // create references for compatibility
479 me
.spinUpEl
= spinnerTrigger
.upEl
;
480 me
.spinDownEl
= spinnerTrigger
.downEl
;
483 onMouseWheel: function(e
) {
487 delta
= e
.getWheelDelta();
490 } else if (delta
< 0) {
499 // add '@' to the valid id
500 Ext
.define('Proxmox.validIdReOverride', {
501 override
: 'Ext.Component',
502 validIdRe
: /^[a-z_][a-z0-9\-_@]*$/i,
505 Ext
.define('Proxmox.selection.CheckboxModel', {
506 override
: 'Ext.selection.CheckboxModel',
508 // [P] use whole checkbox cell to multiselect, not only the checkbox
509 checkSelector
: '.x-grid-cell-row-checker',
511 // TODO: remove all optimizations below to an override for parent 'Ext.selection.Model' ??
513 // [ P: optimized to remove all records at once as single remove is O(n^3) slow ]
514 // records can be an index, a record or an array of records
515 doDeselect: function(records
, suppressEvent
) {
517 selected
= me
.selected
,
521 if (me
.locked
|| !me
.store
) {
524 if (typeof records
=== "number") {
525 // No matching record, jump out
526 record
= me
.store
.getAt(records
);
533 } else if (!Ext
.isArray(records
)) {
538 // [P] a beforedeselection, triggered by me.onSelectChange below, can block removal by
539 // returning false, thus the original implementation removed only here in the commit fn,
540 // which has an abysmal performance O(n^3). As blocking removal is not the norm, go do the
541 // reverse, record blocked records and remove them from the to-be-removed array before
542 // applying it. A FF86 i9-9900K on 10k records goes from >40s to ~33ms for >90% deselection
543 let committed
= false;
544 commit = function() {
546 if (record
=== me
.selectionStart
) {
547 me
.selectionStart
= null;
550 let removalBlocked
= [];
551 len
= records
.length
;
553 for (; i
< len
; i
++) {
555 if (me
.isSelected(record
)) {
557 me
.onSelectChange(record
, false, suppressEvent
, commit
);
559 removalBlocked
.push(record
);
566 if (removalBlocked
.length
> 0) {
567 records
.remove(removalBlocked
);
569 selected
.remove(records
); // [P] FAST(er)
570 me
.lastSelected
= selected
.last();
572 // fire selchange if there was a change and there is no suppressEvent flag
573 me
.maybeFireSelectionChange(records
.length
> 0 && !suppressEvent
);
574 return records
.length
;
578 doMultiSelect: function(records
, keepExisting
, suppressEvent
) {
580 selected
= me
.selected
,
582 result
, i
, len
, record
, commit
;
588 records
= !Ext
.isArray(records
) ? [records
] : records
;
589 len
= records
.length
;
590 if (!keepExisting
&& selected
.getCount() > 0) {
591 result
= me
.deselectDuringSelect(records
, suppressEvent
);
596 // We had a failure during selection, so jump out
597 // Fire selection change if we did deselect anything
598 me
.maybeFireSelectionChange(result
[1] > 0 && !suppressEvent
);
601 // Means something has been deselected, so we've had a change
602 change
= result
[1] > 0;
606 let gotBlocked
, blockedRecords
= [];
607 commit = function() {
608 if (!selected
.getCount()) {
609 me
.selectionStart
= record
;
615 for (i
= 0; i
< len
; i
++) {
617 if (me
.isSelected(record
)) {
622 me
.onSelectChange(record
, true, suppressEvent
, commit
);
627 blockedRecords
.push(record
);
630 if (blockedRecords
.length
> 0) {
631 records
.remove(blockedRecords
);
633 selected
.add(records
);
634 me
.lastSelected
= record
;
636 // fire selchange if there was a change and there is no suppressEvent flag
637 me
.maybeFireSelectionChange(change
&& !suppressEvent
);
639 deselectDuringSelect: function(toSelect
, suppressEvent
) {
641 selected
= me
.selected
.getRange(),
644 // Prevent selection change events from firing, will happen during select
646 me
.deselectingDuringSelect
= true;
647 let toDeselect
= selected
.filter(item
=> !Ext
.Array
.contains(toSelect
, item
));
648 if (toDeselect
.length
> 0) {
649 changed
= me
.doDeselect(toDeselect
, suppressEvent
);
658 me
.deselectingDuringSelect
= false;
667 // override the download server url globally, for privacy reasons
668 Ext
.draw
.Container
.prototype.defaultDownloadServerUrl
= "-";
670 // stop nulling of properties
671 Ext
.define('Proxmox.Component', {
672 override
: 'Ext.Component',
673 clearPropertiesOnDestroy
: false,
676 // force alert boxes to be rendered with an Error Icon
677 // since Ext.Msg is an object and not a prototype, we need to override it
678 // after the framework has been initiated
679 Ext
.onReady(function() {
680 Ext
.override(Ext
.Msg
, {
681 alert: function(title
, message
, fn
, scope
) { // eslint-disable-line consistent-return
682 if (Ext
.isString(title
)) {
690 minWidth
: this.minWidth
,
692 return this.show(config
);
697 Ext
.define('Ext.ux.IFrame', {
698 extend
: 'Ext.Component',
700 alias
: 'widget.uxiframe',
702 loadMask
: 'Loading...',
707 '<iframe src="{src}" id="{id}-iframeEl" data-ref="iframeEl" name="{frameName}" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe>',
709 childEls
: ['iframeEl'],
711 initComponent: function() {
714 this.frameName
= this.frameName
|| this.id
+ '-frame';
717 initEvents: function() {
720 me
.iframeEl
.on('load', me
.onLoad
, me
);
723 initRenderData: function() {
724 return Ext
.apply(this.callParent(), {
726 frameName
: this.frameName
,
730 getBody: function() {
731 let doc
= this.getDoc();
732 return doc
.body
|| doc
.documentElement
;
737 return this.getWin().document
;
747 ? me
.iframeEl
.dom
.contentWindow
748 : window
.frames
[name
];
752 getFrame: function() {
754 return me
.iframeEl
.dom
;
757 beforeDestroy: function() {
758 this.cleanupListeners(true);
762 cleanupListeners: function(destroying
) {
769 Ext
.get(doc
).un(this._docListeners
);
770 if (destroying
&& doc
.hasOwnProperty
) {
772 if (Object
.prototype.hasOwnProperty
.call(doc
, prop
)) {
787 fn
= me
.onRelayedEvent
;
791 // These events need to be relayed from the inner document (where they stop
792 // bubbling) up to the outer document. This has to be done at the DOM level so
793 // the event reaches listeners on elements like the document body. The effected
794 // mechanisms that depend on this bubbling behavior are listed to the right
798 mousedown
: fn
, // menu dismisal (MenuManager) and Window onMouseDown (toFront)
799 mousemove
: fn
, // window resize drag detection
800 mouseup
: fn
, // window resize termination
801 click
: fn
, // not sure, but just to be safe
802 dblclick
: fn
, // not sure again
807 // cannot do this xss
810 // We need to be sure we remove all our events from the iframe on unload or we're going to LEAK!
811 Ext
.get(this.getWin()).on('beforeunload', me
.cleanupListeners
, me
);
814 this.fireEvent('load', this);
817 this.fireEvent('error', this);
821 onRelayedEvent: function(event
) {
822 // relay event from the iframe's document to the document that owns the iframe...
824 let iframeEl
= this.iframeEl
,
826 // Get the left-based iframe position
827 iframeXY
= iframeEl
.getTrueXY(),
828 originalEventXY
= event
.getXY(),
830 // Get the left-based XY position.
831 // This is because the consumer of the injected event will
832 // perform its own RTL normalization.
833 eventXY
= event
.getTrueXY();
835 // the event from the inner document has XY relative to that document's origin,
836 // so adjust it to use the origin of the iframe in the outer document:
837 event
.xy
= [iframeXY
[0] + eventXY
[0], iframeXY
[1] + eventXY
[1]];
839 event
.injectEvent(iframeEl
); // blame the iframe for the event...
841 event
.xy
= originalEventXY
; // restore the original XY (just for safety)
844 load: function(src
) {
847 frame
= me
.getFrame();
849 if (me
.fireEvent('beforeload', me
, src
) !== false) {
854 frame
.src
= me
.src
= src
|| me
.src
;