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,
41 // override ExtJS method
42 // if the field has multiSelect enabled, the store is not loaded, and
43 // the displayfield == valuefield, it saves the rawvalue as an array
44 // but the getRawValue method is only defined in the textfield class
45 // (which has not to deal with arrays) an returns the string in the
46 // field (not an array)
48 // so if we have multiselect enabled, return the rawValue (which
49 // should be an array) and else we do callParent so
50 // it should not impact any other use of the class
51 getRawValue: function() {
56 return me
.callParent();
60 getSubmitData: function() {
64 if (!me
.disabled
&& me
.submitValue
) {
65 let val
= me
.getSubmitValue();
68 data
[me
.getName()] = val
;
69 } else if (me
.getDeleteEmpty()) {
71 data
['delete'] = me
.getName();
77 getSubmitValue: function() {
80 var value
= me
.callParent();
85 return me
.getSkipEmptyText() ? null: value
;
88 setAllowBlank: function(allowBlank
) {
89 this.allowBlank
= allowBlank
;
93 // override ExtJS protected method
94 onBindStore: function(store
, initial
) {
98 valueCollectionConfig
;
100 // We're being bound, not unbound...
102 // If store was created from a 2 dimensional array with generated field names 'field1' and 'field2'
103 if (store
.autoCreated
) {
104 me
.queryMode
= 'local';
105 me
.valueField
= me
.displayField
= 'field1';
106 if (!store
.expanded
) {
107 me
.displayField
= 'field2';
110 // displayTpl config will need regenerating with the autogenerated displayField name 'field1'
111 me
.setDisplayTpl(null);
113 if (!Ext
.isDefined(me
.valueField
)) {
114 me
.valueField
= me
.displayField
;
117 // Add a byValue index to the store so that we can efficiently look up records by the value field
118 // when setValue passes string value(s).
119 // The two indices (Ext.util.CollectionKeys) are configured unique: false, so that if duplicate keys
120 // are found, they are all returned by the get call.
121 // This is so that findByText and findByValue are able to return the *FIRST* matching value. By default,
122 // if unique is true, CollectionKey keeps the *last* matching value.
125 rootProperty
: 'data',
129 extraKeySpec
.byValue
.property
= me
.valueField
;
130 store
.setExtraKeys(extraKeySpec
);
132 if (me
.displayField
=== me
.valueField
) {
133 store
.byText
= store
.byValue
;
135 extraKeySpec
.byText
= {
136 rootProperty
: 'data',
139 extraKeySpec
.byText
.property
= me
.displayField
;
140 store
.setExtraKeys(extraKeySpec
);
143 // We hold a collection of the values which have been selected, keyed by this field's valueField.
144 // This collection also functions as the selected items collection for the BoundList's selection model
145 valueCollectionConfig
= {
146 rootProperty
: 'data',
149 property
: 'internalId'
152 property
: me
.valueField
,
156 // Whenever this collection is changed by anyone, whether by this field adding to it,
157 // or the BoundList operating, we must refresh our value.
159 beginupdate
: me
.onValueCollectionBeginUpdate
,
160 endupdate
: me
.onValueCollectionEndUpdate
,
165 // This becomes our collection of selected records for the Field.
166 me
.valueCollection
= new Ext
.util
.Collection(valueCollectionConfig
);
168 // We use the selected Collection as our value collection and the basis
169 // for rendering the tag list.
171 //proxmox override: since the picker is represented by a grid panel,
172 // we changed here the selection to RowModel
173 me
.pickerSelectionModel
= new Ext
.selection
.RowModel({
174 mode
: me
.multiSelect
? 'SIMPLE' : 'SINGLE',
175 // There are situations when a row is selected on mousedown but then the mouse is dragged to another row
176 // and released. In these situations, the event target for the click event won't be the row where the mouse
177 // was released but the boundview. The view will then determine that it should fire a container click, and
178 // the DataViewModel will then deselect all prior selections. Setting `deselectOnContainerClick` here will
179 // prevent the model from deselecting.
180 deselectOnContainerClick
: false,
181 enableInitialSelection
: false,
183 selected
: me
.valueCollection
,
187 lastselectedchanged
: me
.updateBindSelection
196 picker
.setSelectionModel(me
.pickerSelectionModel
);
197 if (picker
.getStore() !== store
) {
198 picker
.bindStore(store
);
204 // copied from ComboBox
205 createPicker: function() {
209 var pickerCfg
= Ext
.apply({
210 // proxmox overrides: display a grid for selection
217 displayField
: me
.displayField
,
218 preserveScrollOnRefresh
: true,
219 pageSize
: me
.pageSize
,
221 selModel
: me
.pickerSelectionModel
,
222 focusOnToFront
: false
223 }, me
.listConfig
, me
.defaultListConfig
);
225 picker
= me
.picker
|| Ext
.widget(pickerCfg
);
227 if (picker
.getStore() !== me
.store
) {
228 picker
.bindStore(me
.store
);
232 picker
.pagingToolbar
.on('beforechange', me
.onPageChange
, me
);
235 // proxmox overrides: pass missing method in gridPanel to its view
236 picker
.refresh = function() {
237 picker
.getSelectionModel().select(me
.valueCollection
.getRange());
238 picker
.getView().refresh();
240 picker
.getNodeByRecord = function() {
241 picker
.getView().getNodeByRecord(arguments
);
244 // We limit the height of the picker to fit in the space above
245 // or below this field unless the picker has its own ideas about that.
246 if (!picker
.initialConfig
.maxHeight
) {
248 beforeshow
: me
.onBeforePickerShow
,
252 picker
.getSelectionModel().on({
253 beforeselect
: me
.onBeforeSelect
,
254 beforedeselect
: me
.onBeforeDeselect
,
255 focuschange
: me
.onFocusChange
,
256 selectionChange: function (sm
, selectedRecords
) {
258 if (selectedRecords
.length
) {
259 me
.setValue(selectedRecords
);
260 me
.fireEvent('select', me
, selectedRecords
);
267 // when the clicked item is the same as the previously selected,
268 // it does not select the item
269 // instead we hide the picker
270 if (!me
.multiSelect
) {
271 picker
.on('itemclick', function (sm
,record
) {
272 if (picker
.getSelection()[0] === record
) {
278 // when our store is not yet loaded, we increase
279 // the height of the gridpanel, so that we can see
282 // we save the minheight to reset it after the load
283 picker
.on('show', function() {
284 if (me
.enableLoadMask
) {
285 me
.savedMinHeight
= picker
.getMinHeight();
286 picker
.setMinHeight(100);
290 picker
.getNavigationModel().navigateOnSpace
= false;
295 isValueInStore: function(value
) {
297 var store
= me
.store
;
304 if (Ext
.isArray(value
)) {
305 Ext
.Array
.each(value
, function(v
) {
306 if (store
.findRecord(me
.valueField
, v
)) {
308 return false; // break
312 found
= !!store
.findRecord(me
.valueField
, value
);
318 validator: function (value
) {
322 return true; // handled later by allowEmpty in the getErrors call chain
325 if (!(me
.notFoundIsValid
|| me
.isValueInStore(value
))) {
326 return gettext('Invalid Value');
332 initComponent: function() {
337 matchFieldWidth
: false
340 Ext
.applyIf(me
, { value
: ''}); // hack: avoid ExtJS validate() bug
342 Ext
.applyIf(me
.listConfig
, { width
: 400 });
346 // Create the picker at an early stage, so it is available to store the previous selection
352 // The trigger.picker causes first a focus event on the field then
353 // toggles the selection picker. Thus skip expanding in this case,
354 // else our focus listner expands and the picker.trigger then
355 // collapses it directly afterwards.
356 Ext
.override(me
.triggers
.picker
, {
357 onMouseDown : function (e
) {
358 // copied "should we focus" check from Ext.form.trigger.Trigger
359 if (e
.pointerType
!== 'touch' && !this.field
.owns(Ext
.Element
.getActiveElement())) {
360 me
.skip_expand_on_focus
= true;
362 this.callParent(arguments
);
366 me
.on("focus", function(me
) {
367 if (!me
.isExpanded
&& !me
.skip_expand_on_focus
) {
370 me
.skip_expand_on_focus
= false;
374 me
.mon(me
.store
, 'beforeload', function() {
375 if (!me
.isDisabled()) {
376 me
.enableLoadMask
= true;
380 // hack: autoSelect does not work
381 me
.mon(me
.store
, 'load', function(store
, r
, success
, o
) {
385 if (me
.enableLoadMask
) {
386 delete me
.enableLoadMask
;
388 // if the picker exists,
389 // we reset its minheight to the saved var/0
390 // we have to update the layout, otherwise the height
391 // gets not recalculated
393 me
.picker
.setMinHeight(me
.savedMinHeight
|| 0);
394 delete me
.savedMinHeight
;
395 me
.picker
.updateLayout();
399 var def
= me
.getValue() || me
.preferredValue
;
401 me
.setValue(def
, true); // sync with grid
405 found
= me
.isValueInStore(def
);
409 var rec
= me
.store
.first();
410 if (me
.autoSelect
&& rec
&& rec
.data
) {
411 def
= rec
.data
[me
.valueField
];
412 me
.setValue(def
, true);
415 if (!me
.notFoundIsValid
) {
416 me
.markInvalid(gettext('Invalid Value'));