]>
Commit | Line | Data |
---|---|---|
066babdc DM |
1 | /* |
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 | |
6 | * | |
7 | */ | |
8 | ||
9 | Ext.define('Proxmox.form.ComboGrid', { | |
10 | extend: 'Ext.form.field.ComboBox', | |
11 | alias: ['widget.proxmoxComboGrid'], | |
12 | ||
13 | // this value is used as default value after load() | |
14 | preferredValue: undefined, | |
15 | ||
16 | // hack: allow to select empty value | |
17 | // seems extjs does not allow that when 'editable == false' | |
18 | onKeyUp: function(e, t) { | |
19 | var me = this; | |
20 | var key = e.getKey(); | |
21 | ||
22 | if (!me.editable && me.allowBlank && !me.multiSelect && | |
23 | (key == e.BACKSPACE || key == e.DELETE)) { | |
24 | me.setValue(''); | |
25 | } | |
26 | ||
27 | me.callParent(arguments); | |
28 | }, | |
29 | ||
227159ec TL |
30 | config: { |
31 | skipEmptyText: false, | |
1ccb53ec | 32 | notFoundIsValid: false, |
227159ec TL |
33 | deleteEmpty: false, |
34 | }, | |
35 | ||
066babdc DM |
36 | // needed to trigger onKeyUp etc. |
37 | enableKeyEvents: true, | |
38 | ||
3ab80554 DC |
39 | editable: false, |
40 | ||
94953ba8 DC |
41 | triggers: { |
42 | clear: { | |
43 | cls: 'pmx-clear-trigger', | |
44 | weight: -1, | |
45 | hidden: true, | |
46 | handler: function() { | |
47 | var me = this; | |
48 | me.setValue(''); | |
01031528 TL |
49 | }, |
50 | }, | |
94953ba8 DC |
51 | }, |
52 | ||
53 | setValue: function(value) { | |
54 | var me = this; | |
f32aa3df DC |
55 | let empty = Ext.isArray(value) ? !value.length : !value; |
56 | me.triggers.clear.setVisible(!empty && me.allowBlank); | |
94953ba8 DC |
57 | return me.callParent([value]); |
58 | }, | |
59 | ||
066babdc DM |
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) | |
66 | // | |
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() { | |
71 | var me = this; | |
72 | if (me.multiSelect) { | |
73 | return me.rawValue; | |
74 | } else { | |
75 | return me.callParent(); | |
76 | } | |
77 | }, | |
78 | ||
227159ec TL |
79 | getSubmitData: function() { |
80 | var me = this; | |
81 | ||
82 | let data = null; | |
83 | if (!me.disabled && me.submitValue) { | |
84 | let val = me.getSubmitValue(); | |
85 | if (val !== null) { | |
86 | data = {}; | |
87 | data[me.getName()] = val; | |
88 | } else if (me.getDeleteEmpty()) { | |
89 | data = {}; | |
01031528 | 90 | data.delete = me.getName(); |
227159ec TL |
91 | } |
92 | } | |
93 | return data; | |
94 | }, | |
95 | ||
96 | getSubmitValue: function() { | |
97 | var me = this; | |
98 | ||
99 | var value = me.callParent(); | |
100 | if (value !== '') { | |
101 | return value; | |
102 | } | |
103 | ||
104 | return me.getSkipEmptyText() ? null: value; | |
105 | }, | |
106 | ||
a8131b5b TL |
107 | setAllowBlank: function(allowBlank) { |
108 | this.allowBlank = allowBlank; | |
109 | this.validate(); | |
110 | }, | |
111 | ||
066babdc DM |
112 | // override ExtJS protected method |
113 | onBindStore: function(store, initial) { | |
114 | var me = this, | |
115 | picker = me.picker, | |
116 | extraKeySpec, | |
117 | valueCollectionConfig; | |
118 | ||
119 | // We're being bound, not unbound... | |
120 | if (store) { | |
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'; | |
127 | } | |
128 | ||
129 | // displayTpl config will need regenerating with the autogenerated displayField name 'field1' | |
130 | me.setDisplayTpl(null); | |
131 | } | |
132 | if (!Ext.isDefined(me.valueField)) { | |
133 | me.valueField = me.displayField; | |
134 | } | |
135 | ||
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. | |
142 | extraKeySpec = { | |
143 | byValue: { | |
144 | rootProperty: 'data', | |
01031528 TL |
145 | unique: false, |
146 | }, | |
066babdc DM |
147 | }; |
148 | extraKeySpec.byValue.property = me.valueField; | |
149 | store.setExtraKeys(extraKeySpec); | |
150 | ||
151 | if (me.displayField === me.valueField) { | |
152 | store.byText = store.byValue; | |
153 | } else { | |
154 | extraKeySpec.byText = { | |
155 | rootProperty: 'data', | |
01031528 | 156 | unique: false, |
066babdc DM |
157 | }; |
158 | extraKeySpec.byText.property = me.displayField; | |
159 | store.setExtraKeys(extraKeySpec); | |
160 | } | |
161 | ||
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', | |
166 | extraKeys: { | |
167 | byInternalId: { | |
01031528 | 168 | property: 'internalId', |
066babdc DM |
169 | }, |
170 | byValue: { | |
171 | property: me.valueField, | |
01031528 TL |
172 | rootProperty: 'data', |
173 | }, | |
066babdc DM |
174 | }, |
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. | |
177 | listeners: { | |
178 | beginupdate: me.onValueCollectionBeginUpdate, | |
179 | endupdate: me.onValueCollectionEndUpdate, | |
01031528 TL |
180 | scope: me, |
181 | }, | |
066babdc DM |
182 | }; |
183 | ||
184 | // This becomes our collection of selected records for the Field. | |
185 | me.valueCollection = new Ext.util.Collection(valueCollectionConfig); | |
186 | ||
187 | // We use the selected Collection as our value collection and the basis | |
188 | // for rendering the tag list. | |
189 | ||
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 dragged to another row | |
195 | // and released. In these situations, the event target for the click event won't be the row where the mouse | |
196 | // was released but the boundview. The view will then determine that it should fire a container click, and | |
197 | // the DataViewModel will then deselect all prior selections. Setting `deselectOnContainerClick` here will | |
198 | // prevent the model from deselecting. | |
199 | deselectOnContainerClick: false, | |
200 | enableInitialSelection: false, | |
201 | pruneRemoved: false, | |
202 | selected: me.valueCollection, | |
203 | store: store, | |
204 | listeners: { | |
205 | scope: me, | |
01031528 TL |
206 | lastselectedchanged: me.updateBindSelection, |
207 | }, | |
066babdc DM |
208 | }); |
209 | ||
210 | if (!initial) { | |
211 | me.resetToDefault(); | |
212 | } | |
213 | ||
214 | if (picker) { | |
215 | picker.setSelectionModel(me.pickerSelectionModel); | |
216 | if (picker.getStore() !== store) { | |
217 | picker.bindStore(store); | |
218 | } | |
219 | } | |
220 | } | |
221 | }, | |
222 | ||
223 | // copied from ComboBox | |
224 | createPicker: function() { | |
225 | var me = this; | |
226 | var picker; | |
227 | ||
228 | var pickerCfg = Ext.apply({ | |
229 | // proxmox overrides: display a grid for selection | |
230 | xtype: 'gridpanel', | |
231 | id: me.pickerId, | |
232 | pickerField: me, | |
233 | floating: true, | |
234 | hidden: true, | |
235 | store: me.store, | |
236 | displayField: me.displayField, | |
237 | preserveScrollOnRefresh: true, | |
238 | pageSize: me.pageSize, | |
239 | tpl: me.tpl, | |
240 | selModel: me.pickerSelectionModel, | |
01031528 | 241 | focusOnToFront: false, |
066babdc DM |
242 | }, me.listConfig, me.defaultListConfig); |
243 | ||
244 | picker = me.picker || Ext.widget(pickerCfg); | |
245 | ||
246 | if (picker.getStore() !== me.store) { | |
247 | picker.bindStore(me.store); | |
248 | } | |
249 | ||
250 | if (me.pageSize) { | |
251 | picker.pagingToolbar.on('beforechange', me.onPageChange, me); | |
252 | } | |
253 | ||
254 | // proxmox overrides: pass missing method in gridPanel to its view | |
255 | picker.refresh = function() { | |
256 | picker.getSelectionModel().select(me.valueCollection.getRange()); | |
257 | picker.getView().refresh(); | |
258 | }; | |
259 | picker.getNodeByRecord = function() { | |
260 | picker.getView().getNodeByRecord(arguments); | |
261 | }; | |
262 | ||
263 | // We limit the height of the picker to fit in the space above | |
264 | // or below this field unless the picker has its own ideas about that. | |
265 | if (!picker.initialConfig.maxHeight) { | |
266 | picker.on({ | |
267 | beforeshow: me.onBeforePickerShow, | |
01031528 | 268 | scope: me, |
066babdc DM |
269 | }); |
270 | } | |
271 | picker.getSelectionModel().on({ | |
272 | beforeselect: me.onBeforeSelect, | |
273 | beforedeselect: me.onBeforeDeselect, | |
274 | focuschange: me.onFocusChange, | |
01031528 | 275 | selectionChange: function(sm, selectedRecords) { |
066babdc DM |
276 | var me = this; |
277 | if (selectedRecords.length) { | |
278 | me.setValue(selectedRecords); | |
279 | me.fireEvent('select', me, selectedRecords); | |
280 | } | |
281 | }, | |
01031528 | 282 | scope: me, |
066babdc DM |
283 | }); |
284 | ||
285 | // hack for extjs6 | |
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) { | |
01031528 | 290 | picker.on('itemclick', function(sm, record) { |
066babdc DM |
291 | if (picker.getSelection()[0] === record) { |
292 | picker.hide(); | |
293 | } | |
294 | }); | |
295 | } | |
296 | ||
297 | // when our store is not yet loaded, we increase | |
298 | // the height of the gridpanel, so that we can see | |
299 | // the loading mask | |
300 | // | |
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); | |
306 | } | |
307 | }); | |
308 | ||
309 | picker.getNavigationModel().navigateOnSpace = false; | |
310 | ||
311 | return picker; | |
312 | }, | |
313 | ||
35eec238 TM |
314 | clearLocalFilter: function() { |
315 | var me = this, | |
316 | filter = me.queryFilter; | |
317 | ||
318 | if (filter) { | |
319 | me.queryFilter = null; | |
320 | me.changingFilters = true; | |
321 | me.store.removeFilter(filter, true); | |
322 | me.changingFilters = false; | |
323 | } | |
324 | }, | |
325 | ||
1ccb53ec TL |
326 | isValueInStore: function(value) { |
327 | var me = this; | |
328 | var store = me.store; | |
329 | var found = false; | |
330 | ||
331 | if (!store) { | |
332 | return found; | |
333 | } | |
334 | ||
35eec238 TM |
335 | // Make sure the current filter is removed before checking the store |
336 | // to prevent false negative results when iterating over a filtered store. | |
337 | // All store.find*() method's operate on the filtered store. | |
338 | if (me.queryFilter && me.queryMode === 'local' && me.clearFilterOnBlur) { | |
339 | me.clearLocalFilter(); | |
340 | } | |
341 | ||
1ccb53ec TL |
342 | if (Ext.isArray(value)) { |
343 | Ext.Array.each(value, function(v) { | |
344 | if (store.findRecord(me.valueField, v)) { | |
345 | found = true; | |
346 | return false; // break | |
347 | } | |
348 | }); | |
349 | } else { | |
350 | found = !!store.findRecord(me.valueField, value); | |
351 | } | |
352 | ||
353 | return found; | |
354 | }, | |
355 | ||
01031528 | 356 | validator: function(value) { |
1ccb53ec TL |
357 | var me = this; |
358 | ||
359 | if (!value) { | |
360 | return true; // handled later by allowEmpty in the getErrors call chain | |
361 | } | |
362 | ||
c59a0a3e TL |
363 | // we normally get here the displayField as value, but if a valueField |
364 | // is configured we need to get the "actual" value, to ensure it is in | |
365 | // the store. Below check is copied from ExtJS 6.0.2 ComboBox source | |
013cbd64 DC |
366 | // |
367 | // we also have to get the 'real' value if the we have a mulitSelect | |
368 | // Field but got a non array value | |
01031528 TL |
369 | if (me.valueField && me.valueField !== me.displayField || |
370 | me.multiSelect && !Ext.isArray(value)) { | |
c59a0a3e TL |
371 | value = me.getValue(); |
372 | } | |
373 | ||
1ccb53ec TL |
374 | if (!(me.notFoundIsValid || me.isValueInStore(value))) { |
375 | return gettext('Invalid Value'); | |
376 | } | |
377 | ||
378 | return true; | |
379 | }, | |
380 | ||
6f5e0ea6 SR |
381 | // validate after enabling a field, otherwise blank fields with !allowBlank |
382 | // are sometimes not marked as invalid | |
383 | setDisabled: function(value) { | |
384 | this.callParent([value]); | |
385 | this.validate(); | |
386 | }, | |
387 | ||
066babdc DM |
388 | initComponent: function() { |
389 | var me = this; | |
390 | ||
066babdc DM |
391 | Ext.apply(me, { |
392 | queryMode: 'local', | |
01031528 | 393 | matchFieldWidth: false, |
066babdc DM |
394 | }); |
395 | ||
01031528 | 396 | Ext.applyIf(me, { value: '' }); // hack: avoid ExtJS validate() bug |
066babdc DM |
397 | |
398 | Ext.applyIf(me.listConfig, { width: 400 }); | |
399 | ||
400 | me.callParent(); | |
401 | ||
402 | // Create the picker at an early stage, so it is available to store the previous selection | |
403 | if (!me.picker) { | |
404 | me.createPicker(); | |
405 | } | |
406 | ||
407 | me.mon(me.store, 'beforeload', function() { | |
408 | if (!me.isDisabled()) { | |
409 | me.enableLoadMask = true; | |
410 | } | |
411 | }); | |
412 | ||
413 | // hack: autoSelect does not work | |
414 | me.mon(me.store, 'load', function(store, r, success, o) { | |
415 | if (success) { | |
416 | me.clearInvalid(); | |
417 | ||
418 | if (me.enableLoadMask) { | |
419 | delete me.enableLoadMask; | |
420 | ||
421 | // if the picker exists, | |
422 | // we reset its minheight to the saved var/0 | |
423 | // we have to update the layout, otherwise the height | |
424 | // gets not recalculated | |
425 | if (me.picker) { | |
426 | me.picker.setMinHeight(me.savedMinHeight || 0); | |
427 | delete me.savedMinHeight; | |
428 | me.picker.updateLayout(); | |
429 | } | |
430 | } | |
431 | ||
432 | var def = me.getValue() || me.preferredValue; | |
433 | if (def) { | |
434 | me.setValue(def, true); // sync with grid | |
435 | } | |
436 | var found = false; | |
437 | if (def) { | |
1ccb53ec | 438 | found = me.isValueInStore(def); |
066babdc DM |
439 | } |
440 | ||
441 | if (!found) { | |
442 | var rec = me.store.first(); | |
443 | if (me.autoSelect && rec && rec.data) { | |
444 | def = rec.data[me.valueField]; | |
445 | me.setValue(def, true); | |
15206214 | 446 | } else if (!me.allowBlank && !(Ext.isArray(def) ? def.length : def)) { |
f59a7b23 | 447 | me.setValue(def); |
6f5e0ea6 | 448 | if (!me.notFoundIsValid && !me.isDisabled()) { |
15206214 | 449 | me.markInvalid(me.blankText); |
1ccb53ec | 450 | } |
066babdc DM |
451 | } |
452 | } | |
453 | } | |
454 | }); | |
01031528 | 455 | }, |
066babdc | 456 | }); |