2 * ComboGrid component: a ComboBox where the dropdown menu (the
3 * "Picker") is a Grid with Rows and Columns expects a listConfig
4 * object with a columns property roughly based on the GridPicker from
5 * https://www.sencha.com/forum/showthread.php?299909
9 Ext
.define('Proxmox.form.ComboGrid', {
10 extend
: 'Ext.form.field.ComboBox',
11 alias
: ['widget.proxmoxComboGrid'],
13 // this value is used as default value after load()
14 preferredValue
: undefined,
16 // hack: allow to select empty value
17 // seems extjs does not allow that when 'editable == false'
18 onKeyUp: function(e
, t
) {
22 if (!me
.editable
&& me
.allowBlank
&& !me
.multiSelect
&&
23 (key
=== e
.BACKSPACE
|| key
=== e
.DELETE
)) {
27 me
.callParent(arguments
);
32 notFoundIsValid
: false,
36 // needed to trigger onKeyUp etc.
37 enableKeyEvents
: true,
43 cls
: 'pmx-clear-trigger',
53 setValue: function(value
) {
55 let empty
= Ext
.isArray(value
) ? !value
.length
: !value
;
56 me
.triggers
.clear
.setVisible(!empty
&& me
.allowBlank
);
57 return me
.callParent([value
]);
60 // override ExtJS method
61 // if the field has multiSelect enabled, the store is not loaded, and
62 // the displayfield == valuefield, it saves the rawvalue as an array
63 // but the getRawValue method is only defined in the textfield class
64 // (which has not to deal with arrays) an returns the string in the
65 // field (not an array)
67 // so if we have multiselect enabled, return the rawValue (which
68 // should be an array) and else we do callParent so
69 // it should not impact any other use of the class
70 getRawValue: function() {
75 return me
.callParent();
79 getSubmitData: function() {
83 if (!me
.disabled
&& me
.submitValue
) {
84 let val
= me
.getSubmitValue();
87 data
[me
.getName()] = val
;
88 } else if (me
.getDeleteEmpty()) {
90 data
.delete = me
.getName();
96 getSubmitValue: function() {
99 let value
= me
.callParent();
104 return me
.getSkipEmptyText() ? null: value
;
107 setAllowBlank: function(allowBlank
) {
108 this.allowBlank
= allowBlank
;
112 // override ExtJS protected method
113 onBindStore: function(store
, initial
) {
117 valueCollectionConfig
;
119 // We're being bound, not unbound...
121 // If store was created from a 2 dimensional array with generated field names 'field1' and 'field2'
122 if (store
.autoCreated
) {
123 me
.queryMode
= 'local';
124 me
.valueField
= me
.displayField
= 'field1';
125 if (!store
.expanded
) {
126 me
.displayField
= 'field2';
129 // displayTpl config will need regenerating with the autogenerated displayField name 'field1'
130 me
.setDisplayTpl(null);
132 if (!Ext
.isDefined(me
.valueField
)) {
133 me
.valueField
= me
.displayField
;
136 // Add a byValue index to the store so that we can efficiently look up records by the value field
137 // when setValue passes string value(s).
138 // The two indices (Ext.util.CollectionKeys) are configured unique: false, so that if duplicate keys
139 // are found, they are all returned by the get call.
140 // This is so that findByText and findByValue are able to return the *FIRST* matching value. By default,
141 // if unique is true, CollectionKey keeps the *last* matching value.
144 rootProperty
: 'data',
148 extraKeySpec
.byValue
.property
= me
.valueField
;
149 store
.setExtraKeys(extraKeySpec
);
151 if (me
.displayField
=== me
.valueField
) {
152 store
.byText
= store
.byValue
;
154 extraKeySpec
.byText
= {
155 rootProperty
: 'data',
158 extraKeySpec
.byText
.property
= me
.displayField
;
159 store
.setExtraKeys(extraKeySpec
);
162 // We hold a collection of the values which have been selected, keyed by this field's valueField.
163 // This collection also functions as the selected items collection for the BoundList's selection model
164 valueCollectionConfig
= {
165 rootProperty
: 'data',
168 property
: 'internalId',
171 property
: me
.valueField
,
172 rootProperty
: 'data',
175 // Whenever this collection is changed by anyone, whether by this field adding to it,
176 // or the BoundList operating, we must refresh our value.
178 beginupdate
: me
.onValueCollectionBeginUpdate
,
179 endupdate
: me
.onValueCollectionEndUpdate
,
184 // This becomes our collection of selected records for the Field.
185 me
.valueCollection
= new Ext
.util
.Collection(valueCollectionConfig
);
187 // We use the selected Collection as our value collection and the basis
188 // for rendering the tag list.
190 //proxmox override: since the picker is represented by a grid panel,
191 // we changed here the selection to RowModel
192 me
.pickerSelectionModel
= new Ext
.selection
.RowModel({
193 mode
: me
.multiSelect
? 'SIMPLE' : 'SINGLE',
194 // There are situations when a row is selected on mousedown but then the mouse is
195 // dragged to another row and released. In these situations, the event target for
196 // the click event won't be the row where the mouse was released but the boundview.
197 // The view will then determine that it should fire a container click, and the
198 // DataViewModel will then deselect all prior selections. Setting
199 // `deselectOnContainerClick` here will prevent the model from deselecting.
200 deselectOnContainerClick
: false,
201 enableInitialSelection
: false,
203 selected
: me
.valueCollection
,
207 lastselectedchanged
: me
.updateBindSelection
,
216 picker
.setSelectionModel(me
.pickerSelectionModel
);
217 if (picker
.getStore() !== store
) {
218 picker
.bindStore(store
);
224 // copied from ComboBox
225 createPicker: function() {
229 let pickerCfg
= Ext
.apply({
230 // proxmox overrides: display a grid for selection
237 displayField
: me
.displayField
,
238 preserveScrollOnRefresh
: true,
239 pageSize
: me
.pageSize
,
241 selModel
: me
.pickerSelectionModel
,
242 focusOnToFront
: false,
243 }, me
.listConfig
, me
.defaultListConfig
);
245 picker
= me
.picker
|| Ext
.widget(pickerCfg
);
247 if (picker
.getStore() !== me
.store
) {
248 picker
.bindStore(me
.store
);
252 picker
.pagingToolbar
.on('beforechange', me
.onPageChange
, me
);
255 // proxmox overrides: pass missing method in gridPanel to its view
256 picker
.refresh = function() {
257 picker
.getSelectionModel().select(me
.valueCollection
.getRange());
258 picker
.getView().refresh();
260 picker
.getNodeByRecord = function() {
261 picker
.getView().getNodeByRecord(arguments
);
264 // We limit the height of the picker to fit in the space above
265 // or below this field unless the picker has its own ideas about that.
266 if (!picker
.initialConfig
.maxHeight
) {
268 beforeshow
: me
.onBeforePickerShow
,
272 picker
.getSelectionModel().on({
273 beforeselect
: me
.onBeforeSelect
,
274 beforedeselect
: me
.onBeforeDeselect
,
275 focuschange
: me
.onFocusChange
,
276 selectionChange: function(sm
, selectedRecords
) {
277 if (selectedRecords
.length
) {
278 this.setValue(selectedRecords
);
279 this.fireEvent('select', me
, selectedRecords
);
286 // when the clicked item is the same as the previously selected,
287 // it does not select the item
288 // instead we hide the picker
289 if (!me
.multiSelect
) {
290 picker
.on('itemclick', function(sm
, record
) {
291 if (picker
.getSelection()[0] === record
) {
297 // when our store is not yet loaded, we increase
298 // the height of the gridpanel, so that we can see
301 // we save the minheight to reset it after the load
302 picker
.on('show', function() {
303 if (me
.enableLoadMask
) {
304 me
.savedMinHeight
= picker
.getMinHeight();
305 picker
.setMinHeight(100);
308 Proxmox
.Utils
.setErrorMask(picker
, me
.loadError
);
310 picker
.updateLayout();
314 picker
.getNavigationModel().navigateOnSpace
= false;
319 clearLocalFilter: function() {
321 filter
= me
.queryFilter
;
324 me
.queryFilter
= null;
325 me
.changingFilters
= true;
326 me
.store
.removeFilter(filter
, true);
327 me
.changingFilters
= false;
331 isValueInStore: function(value
) {
333 let store
= me
.store
;
340 // Make sure the current filter is removed before checking the store
341 // to prevent false negative results when iterating over a filtered store.
342 // All store.find*() method's operate on the filtered store.
343 if (me
.queryFilter
&& me
.queryMode
=== 'local' && me
.clearFilterOnBlur
) {
344 me
.clearLocalFilter();
347 if (Ext
.isArray(value
)) {
348 Ext
.Array
.each(value
, function(v
) {
349 if (store
.findRecord(me
.valueField
, v
, 0, false, true, true)) {
351 return false; // break
356 found
= !!store
.findRecord(me
.valueField
, value
, 0, false, true, true);
362 validator: function(value
) {
366 return true; // handled later by allowEmpty in the getErrors call chain
369 // we normally get here the displayField as value, but if a valueField
370 // is configured we need to get the "actual" value, to ensure it is in
371 // the store. Below check is copied from ExtJS 6.0.2 ComboBox source
373 // we also have to get the 'real' value if the we have a mulitSelect
374 // Field but got a non array value
375 if ((me
.valueField
&& me
.valueField
!== me
.displayField
) ||
376 (me
.multiSelect
&& !Ext
.isArray(value
))) {
377 value
= me
.getValue();
380 if (!(me
.notFoundIsValid
|| me
.isValueInStore(value
))) {
381 return gettext('Invalid Value');
387 // validate after enabling a field, otherwise blank fields with !allowBlank
388 // are sometimes not marked as invalid
389 setDisabled: function(value
) {
390 this.callParent([value
]);
394 initComponent: function() {
399 matchFieldWidth
: false,
402 Ext
.applyIf(me
, { value
: '' }); // hack: avoid ExtJS validate() bug
404 Ext
.applyIf(me
.listConfig
, { width
: 400 });
408 // Create the picker at an early stage, so it is available to store the previous selection
413 me
.mon(me
.store
, 'beforeload', function() {
414 if (!me
.isDisabled()) {
415 me
.enableLoadMask
= true;
419 // hack: autoSelect does not work
420 me
.mon(me
.store
, 'load', function(store
, r
, success
, o
) {
425 if (me
.enableLoadMask
) {
426 delete me
.enableLoadMask
;
428 // if the picker exists,
429 // we reset its minheight to the saved let/0
430 // we have to update the layout, otherwise the height
431 // gets not recalculated
433 me
.picker
.setMinHeight(me
.savedMinHeight
|| 0);
434 Proxmox
.Utils
.setErrorMask(me
.picker
);
435 delete me
.savedMinHeight
;
436 me
.picker
.updateLayout();
440 let def
= me
.getValue() || me
.preferredValue
;
442 me
.setValue(def
, true); // sync with grid
446 found
= me
.isValueInStore(def
);
450 let rec
= me
.store
.first();
451 if (me
.autoSelect
&& rec
&& rec
.data
) {
452 def
= rec
.data
[me
.valueField
];
453 me
.setValue(def
, true);
454 } else if (!me
.allowBlank
&& !(Ext
.isArray(def
) ? def
.length
: def
)) {
456 if (!me
.notFoundIsValid
&& !me
.isDisabled()) {
457 me
.markInvalid(me
.blankText
);
462 let msg
= Proxmox
.Utils
.getResponseErrorMessage(o
.getError());
464 Proxmox
.Utils
.setErrorMask(me
.picker
, msg
);