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
);
30 // needed to trigger onKeyUp etc.
31 enableKeyEvents
: true,
35 // override ExtJS method
36 // if the field has multiSelect enabled, the store is not loaded, and
37 // the displayfield == valuefield, it saves the rawvalue as an array
38 // but the getRawValue method is only defined in the textfield class
39 // (which has not to deal with arrays) an returns the string in the
40 // field (not an array)
42 // so if we have multiselect enabled, return the rawValue (which
43 // should be an array) and else we do callParent so
44 // it should not impact any other use of the class
45 getRawValue: function() {
50 return me
.callParent();
54 // override ExtJS protected method
55 onBindStore: function(store
, initial
) {
59 valueCollectionConfig
;
61 // We're being bound, not unbound...
63 // If store was created from a 2 dimensional array with generated field names 'field1' and 'field2'
64 if (store
.autoCreated
) {
65 me
.queryMode
= 'local';
66 me
.valueField
= me
.displayField
= 'field1';
67 if (!store
.expanded
) {
68 me
.displayField
= 'field2';
71 // displayTpl config will need regenerating with the autogenerated displayField name 'field1'
72 me
.setDisplayTpl(null);
74 if (!Ext
.isDefined(me
.valueField
)) {
75 me
.valueField
= me
.displayField
;
78 // Add a byValue index to the store so that we can efficiently look up records by the value field
79 // when setValue passes string value(s).
80 // The two indices (Ext.util.CollectionKeys) are configured unique: false, so that if duplicate keys
81 // are found, they are all returned by the get call.
82 // This is so that findByText and findByValue are able to return the *FIRST* matching value. By default,
83 // if unique is true, CollectionKey keeps the *last* matching value.
90 extraKeySpec
.byValue
.property
= me
.valueField
;
91 store
.setExtraKeys(extraKeySpec
);
93 if (me
.displayField
=== me
.valueField
) {
94 store
.byText
= store
.byValue
;
96 extraKeySpec
.byText
= {
100 extraKeySpec
.byText
.property
= me
.displayField
;
101 store
.setExtraKeys(extraKeySpec
);
104 // We hold a collection of the values which have been selected, keyed by this field's valueField.
105 // This collection also functions as the selected items collection for the BoundList's selection model
106 valueCollectionConfig
= {
107 rootProperty
: 'data',
110 property
: 'internalId'
113 property
: me
.valueField
,
117 // Whenever this collection is changed by anyone, whether by this field adding to it,
118 // or the BoundList operating, we must refresh our value.
120 beginupdate
: me
.onValueCollectionBeginUpdate
,
121 endupdate
: me
.onValueCollectionEndUpdate
,
126 // This becomes our collection of selected records for the Field.
127 me
.valueCollection
= new Ext
.util
.Collection(valueCollectionConfig
);
129 // We use the selected Collection as our value collection and the basis
130 // for rendering the tag list.
132 //proxmox override: since the picker is represented by a grid panel,
133 // we changed here the selection to RowModel
134 me
.pickerSelectionModel
= new Ext
.selection
.RowModel({
135 mode
: me
.multiSelect
? 'SIMPLE' : 'SINGLE',
136 // There are situations when a row is selected on mousedown but then the mouse is dragged to another row
137 // and released. In these situations, the event target for the click event won't be the row where the mouse
138 // was released but the boundview. The view will then determine that it should fire a container click, and
139 // the DataViewModel will then deselect all prior selections. Setting `deselectOnContainerClick` here will
140 // prevent the model from deselecting.
141 deselectOnContainerClick
: false,
142 enableInitialSelection
: false,
144 selected
: me
.valueCollection
,
148 lastselectedchanged
: me
.updateBindSelection
157 picker
.setSelectionModel(me
.pickerSelectionModel
);
158 if (picker
.getStore() !== store
) {
159 picker
.bindStore(store
);
165 // copied from ComboBox
166 createPicker: function() {
170 var pickerCfg
= Ext
.apply({
171 // proxmox overrides: display a grid for selection
178 displayField
: me
.displayField
,
179 preserveScrollOnRefresh
: true,
180 pageSize
: me
.pageSize
,
182 selModel
: me
.pickerSelectionModel
,
183 focusOnToFront
: false
184 }, me
.listConfig
, me
.defaultListConfig
);
186 picker
= me
.picker
|| Ext
.widget(pickerCfg
);
188 if (picker
.getStore() !== me
.store
) {
189 picker
.bindStore(me
.store
);
193 picker
.pagingToolbar
.on('beforechange', me
.onPageChange
, me
);
196 // proxmox overrides: pass missing method in gridPanel to its view
197 picker
.refresh = function() {
198 picker
.getSelectionModel().select(me
.valueCollection
.getRange());
199 picker
.getView().refresh();
201 picker
.getNodeByRecord = function() {
202 picker
.getView().getNodeByRecord(arguments
);
205 // We limit the height of the picker to fit in the space above
206 // or below this field unless the picker has its own ideas about that.
207 if (!picker
.initialConfig
.maxHeight
) {
209 beforeshow
: me
.onBeforePickerShow
,
213 picker
.getSelectionModel().on({
214 beforeselect
: me
.onBeforeSelect
,
215 beforedeselect
: me
.onBeforeDeselect
,
216 focuschange
: me
.onFocusChange
,
217 selectionChange: function (sm
, selectedRecords
) {
219 if (selectedRecords
.length
) {
220 me
.setValue(selectedRecords
);
221 me
.fireEvent('select', me
, selectedRecords
);
228 // when the clicked item is the same as the previously selected,
229 // it does not select the item
230 // instead we hide the picker
231 if (!me
.multiSelect
) {
232 picker
.on('itemclick', function (sm
,record
) {
233 if (picker
.getSelection()[0] === record
) {
239 // when our store is not yet loaded, we increase
240 // the height of the gridpanel, so that we can see
243 // we save the minheight to reset it after the load
244 picker
.on('show', function() {
245 if (me
.enableLoadMask
) {
246 me
.savedMinHeight
= picker
.getMinHeight();
247 picker
.setMinHeight(100);
251 picker
.getNavigationModel().navigateOnSpace
= false;
256 initComponent: function() {
261 matchFieldWidth
: false
264 Ext
.applyIf(me
, { value
: ''}); // hack: avoid ExtJS validate() bug
266 Ext
.applyIf(me
.listConfig
, { width
: 400 });
270 // Create the picker at an early stage, so it is available to store the previous selection
276 // The trigger.picker causes first a focus event on the field then
277 // toggles the selection picker. Thus skip expanding in this case,
278 // else our focus listner expands and the picker.trigger then
279 // collapses it directly afterwards.
280 Ext
.override(me
.triggers
.picker
, {
281 onMouseDown : function (e
) {
282 // copied "should we focus" check from Ext.form.trigger.Trigger
283 if (e
.pointerType
!== 'touch' && !this.field
.owns(Ext
.Element
.getActiveElement())) {
284 me
.skip_expand_on_focus
= true;
286 this.callParent(arguments
);
290 me
.on("focus", function(me
) {
291 if (!me
.isExpanded
&& !me
.skip_expand_on_focus
) {
294 me
.skip_expand_on_focus
= false;
298 me
.mon(me
.store
, 'beforeload', function() {
299 if (!me
.isDisabled()) {
300 me
.enableLoadMask
= true;
304 // hack: autoSelect does not work
305 me
.mon(me
.store
, 'load', function(store
, r
, success
, o
) {
309 if (me
.enableLoadMask
) {
310 delete me
.enableLoadMask
;
312 // if the picker exists,
313 // we reset its minheight to the saved var/0
314 // we have to update the layout, otherwise the height
315 // gets not recalculated
317 me
.picker
.setMinHeight(me
.savedMinHeight
|| 0);
318 delete me
.savedMinHeight
;
319 me
.picker
.updateLayout();
323 var def
= me
.getValue() || me
.preferredValue
;
325 me
.setValue(def
, true); // sync with grid
329 if (Ext
.isArray(def
)) {
330 Ext
.Array
.each(def
, function(v
) {
331 if (store
.findRecord(me
.valueField
, v
)) {
333 return false; // break
337 found
= store
.findRecord(me
.valueField
, def
);
342 var rec
= me
.store
.first();
343 if (me
.autoSelect
&& rec
&& rec
.data
) {
344 def
= rec
.data
[me
.valueField
];
345 me
.setValue(def
, true);
347 me
.setValue(me
.editable
? def
: '', true);