]> git.proxmox.com Git - proxmox-widget-toolkit.git/blobdiff - form/ComboGrid.js
eslint fixes all over the place
[proxmox-widget-toolkit.git] / form / ComboGrid.js
index b9fea19f02e00b65a1647ec912387e47ca068656..c9eaa5cc5c5f7dfce976a9e311998ea9b43cbbee 100644 (file)
@@ -27,11 +27,36 @@ Ext.define('Proxmox.form.ComboGrid', {
         me.callParent(arguments);
     },
 
+    config: {
+       skipEmptyText: false,
+       notFoundIsValid: false,
+       deleteEmpty: false,
+    },
+
     // needed to trigger onKeyUp etc.
     enableKeyEvents: true,
 
     editable: false,
 
+    triggers: {
+       clear: {
+           cls: 'pmx-clear-trigger',
+           weight: -1,
+           hidden: true,
+           handler: function() {
+               var me = this;
+               me.setValue('');
+           },
+       },
+    },
+
+    setValue: function(value) {
+       var me = this;
+       let empty = Ext.isArray(value) ? !value.length : !value;
+       me.triggers.clear.setVisible(!empty && me.allowBlank);
+       return me.callParent([value]);
+    },
+
     // override ExtJS method
     // if the field has multiSelect enabled, the store is not loaded, and
     // the displayfield == valuefield, it saves the rawvalue as an array
@@ -51,6 +76,39 @@ Ext.define('Proxmox.form.ComboGrid', {
        }
     },
 
+    getSubmitData: function() {
+       var me = this;
+
+       let data = null;
+       if (!me.disabled && me.submitValue) {
+           let val = me.getSubmitValue();
+           if (val !== null) {
+               data = {};
+               data[me.getName()] = val;
+           } else if (me.getDeleteEmpty()) {
+               data = {};
+               data.delete = me.getName();
+           }
+       }
+       return data;
+   },
+
+    getSubmitValue: function() {
+       var me = this;
+
+       var value = me.callParent();
+       if (value !== '') {
+           return value;
+       }
+
+       return me.getSkipEmptyText() ? null: value;
+    },
+
+    setAllowBlank: function(allowBlank) {
+       this.allowBlank = allowBlank;
+       this.validate();
+    },
+
 // override ExtJS protected method
     onBindStore: function(store, initial) {
         var me = this,
@@ -84,8 +142,8 @@ Ext.define('Proxmox.form.ComboGrid', {
             extraKeySpec = {
                 byValue: {
                     rootProperty: 'data',
-                    unique: false
-                }
+                    unique: false,
+                },
             };
             extraKeySpec.byValue.property = me.valueField;
             store.setExtraKeys(extraKeySpec);
@@ -95,7 +153,7 @@ Ext.define('Proxmox.form.ComboGrid', {
             } else {
                 extraKeySpec.byText = {
                     rootProperty: 'data',
-                    unique: false
+                    unique: false,
                 };
                 extraKeySpec.byText.property = me.displayField;
                 store.setExtraKeys(extraKeySpec);
@@ -107,20 +165,20 @@ Ext.define('Proxmox.form.ComboGrid', {
                 rootProperty: 'data',
                 extraKeys: {
                     byInternalId: {
-                        property: 'internalId'
+                        property: 'internalId',
                     },
                     byValue: {
                         property: me.valueField,
-                        rootProperty: 'data'
-                    }
+                        rootProperty: 'data',
+                    },
                 },
                 // Whenever this collection is changed by anyone, whether by this field adding to it,
                 // or the BoundList operating, we must refresh our value.
                 listeners: {
                     beginupdate: me.onValueCollectionBeginUpdate,
                     endupdate: me.onValueCollectionEndUpdate,
-                    scope: me
-                }
+                    scope: me,
+                },
             };
 
             // This becomes our collection of selected records for the Field.
@@ -145,8 +203,8 @@ Ext.define('Proxmox.form.ComboGrid', {
                 store: store,
                 listeners: {
                     scope: me,
-                    lastselectedchanged: me.updateBindSelection
-                }
+                    lastselectedchanged: me.updateBindSelection,
+                },
             });
 
             if (!initial) {
@@ -180,7 +238,7 @@ Ext.define('Proxmox.form.ComboGrid', {
                 pageSize: me.pageSize,
                 tpl: me.tpl,
                 selModel: me.pickerSelectionModel,
-                focusOnToFront: false
+                focusOnToFront: false,
             }, me.listConfig, me.defaultListConfig);
 
         picker = me.picker || Ext.widget(pickerCfg);
@@ -207,21 +265,21 @@ Ext.define('Proxmox.form.ComboGrid', {
         if (!picker.initialConfig.maxHeight) {
             picker.on({
                 beforeshow: me.onBeforePickerShow,
-                scope: me
+                scope: me,
             });
         }
         picker.getSelectionModel().on({
             beforeselect: me.onBeforeSelect,
             beforedeselect: me.onBeforeDeselect,
             focuschange: me.onFocusChange,
-            selectionChange: function (sm, selectedRecords) {
+            selectionChange: function(sm, selectedRecords) {
                 var me = this;
                 if (selectedRecords.length) {
                     me.setValue(selectedRecords);
                     me.fireEvent('select', me, selectedRecords);
                 }
             },
-            scope: me
+            scope: me,
         });
 
        // hack for extjs6
@@ -229,7 +287,7 @@ Ext.define('Proxmox.form.ComboGrid', {
        // it does not select the item
        // instead we hide the picker
        if (!me.multiSelect) {
-           picker.on('itemclick', function (sm,record) {
+           picker.on('itemclick', function(sm, record) {
                if (picker.getSelection()[0] === record) {
                    picker.hide();
                }
@@ -253,15 +311,89 @@ Ext.define('Proxmox.form.ComboGrid', {
         return picker;
     },
 
+    clearLocalFilter: function() {
+        var me = this,
+            filter = me.queryFilter;
+
+        if (filter) {
+            me.queryFilter = null;
+            me.changingFilters = true;
+            me.store.removeFilter(filter, true);
+            me.changingFilters = false;
+        }
+    },
+
+    isValueInStore: function(value) {
+       var me = this;
+       var store = me.store;
+       var found = false;
+
+       if (!store) {
+           return found;
+       }
+
+       // Make sure the current filter is removed before checking the store
+       // to prevent false negative results when iterating over a filtered store.
+       // All store.find*() method's operate on the filtered store.
+       if (me.queryFilter && me.queryMode === 'local' && me.clearFilterOnBlur) {
+           me.clearLocalFilter();
+       }
+
+       if (Ext.isArray(value)) {
+           Ext.Array.each(value, function(v) {
+               if (store.findRecord(me.valueField, v)) {
+                   found = true;
+                   return false; // break
+               }
+           });
+       } else {
+           found = !!store.findRecord(me.valueField, value);
+       }
+
+       return found;
+    },
+
+    validator: function(value) {
+       var me = this;
+
+       if (!value) {
+           return true; // handled later by allowEmpty in the getErrors call chain
+       }
+
+       // we normally get here the displayField as value, but if a valueField
+       // is configured we need to get the "actual" value, to ensure it is in
+       // the store. Below check is copied from ExtJS 6.0.2 ComboBox source
+       //
+       // we also have to get the 'real' value if the we have a mulitSelect
+       // Field but got a non array value
+       if (me.valueField && me.valueField !== me.displayField ||
+           me.multiSelect && !Ext.isArray(value)) {
+           value = me.getValue();
+       }
+
+       if (!(me.notFoundIsValid || me.isValueInStore(value))) {
+           return gettext('Invalid Value');
+       }
+
+       return true;
+    },
+
+    // validate after enabling a field, otherwise blank fields with !allowBlank
+    // are sometimes not marked as invalid
+    setDisabled: function(value) {
+       this.callParent([value]);
+       this.validate();
+    },
+
     initComponent: function() {
        var me = this;
 
        Ext.apply(me, {
            queryMode: 'local',
-           matchFieldWidth: false
+           matchFieldWidth: false,
        });
 
-       Ext.applyIf(me, { value: ''}); // hack: avoid ExtJS validate() bug
+       Ext.applyIf(me, { value: '' }); // hack: avoid ExtJS validate() bug
 
        Ext.applyIf(me.listConfig, { width: 400 });
 
@@ -303,16 +435,7 @@ Ext.define('Proxmox.form.ComboGrid', {
                }
                var found = false;
                if (def) {
-                   if (Ext.isArray(def)) {
-                       Ext.Array.each(def, function(v) {
-                           if (store.findRecord(me.valueField, v)) {
-                               found = true;
-                               return false; // break
-                           }
-                       });
-                   } else {
-                       found = store.findRecord(me.valueField, def);
-                   }
+                   found = me.isValueInStore(def);
                }
 
                if (!found) {
@@ -320,11 +443,14 @@ Ext.define('Proxmox.form.ComboGrid', {
                    if (me.autoSelect && rec && rec.data) {
                        def = rec.data[me.valueField];
                        me.setValue(def, true);
-                   } else {
-                       me.setValue(me.editable ? def : '', true);
+                   } else if (!me.allowBlank && !(Ext.isArray(def) ? def.length : def)) {
+                       me.setValue(def);
+                       if (!me.notFoundIsValid && !me.isDisabled()) {
+                           me.markInvalid(me.blankText);
+                       }
                    }
                }
            }
        });
-    }
+    },
 });