]> git.proxmox.com Git - pmg-gui.git/blobdiff - js/QuarantineList.js
pbs remote: add namespace support
[pmg-gui.git] / js / QuarantineList.js
index ede20f1525b71d08e2d6c733be2d09adbded2aef..74163cfb3d50ac869605ea2576cb6f7b671f8020 100644 (file)
@@ -57,13 +57,12 @@ Ext.define('PMG.QuarantineList', {
                }
            });
         },
-       // extjs has no method to dynamically change the emptytext on
-       // grids, so we have to do it this way
+       // ExtJS cannot dynamically change the emptyText on grids, so implement ourself
        setEmptyText: function(emptyText) {
            let me = this;
            let view = me.getView();
            let tableview = view.getView();
-           tableview.emptyText = '<div class="x-grid-empty">'+ (emptyText || view.notFoundText) + '</div>';
+           tableview.emptyText = `<div class="x-grid-empty">${emptyText || view.notFoundText}</div>`;
        },
 
        load: function(callback) {
@@ -73,20 +72,16 @@ Ext.define('PMG.QuarantineList', {
            let store = view.getStore();
            if (view.emailSelection) {
                if (!me.lookupReference('email').getSelection()) {
-                   // if the combobox has no selection we do not reload
-                   return;
+                   return; // if the combobox has no selection we do not reload
                }
                me.setEmptyText();
            }
-           store.load(function() {
-               if (me.savedPosition !== undefined) {
-                   if (store.getCount() - 1 < me.savedPosition) {
-                       me.savedPosition = store.getCount() - 1;
-                   }
-                   view.setSelection(store.getAt(me.savedPosition));
-               } else {
-                   view.setSelection();
-               }
+           // deselect all first, else ExtJS does some funky O(n^3) comparissions as it tries
+           // to keep the selection, but we do not care for that on a new load anyway
+           view.getSelectionModel().deselectAll();
+
+           store.load(() => {
+               me.restoreSavedSelection();
                if (Ext.isFunction(callback)) {
                    callback();
                }
@@ -94,6 +89,20 @@ Ext.define('PMG.QuarantineList', {
            });
        },
 
+       restoreSavedSelection: function() {
+           let me = this;
+           let view = me.getView();
+           if (me.savedPosition !== undefined) {
+               let store = view.getStore();
+               if (store.getCount() - 1 < me.savedPosition) {
+                   me.savedPosition = store.getCount() - 1;
+               }
+               view.setSelection(store.getAt(me.savedPosition));
+           } else {
+               view.setSelection();
+           }
+       },
+
        setFrom: function(from) {
            let view = this.getView();
            let params = view.getStore().getProxy().getExtraParams();
@@ -114,7 +123,11 @@ Ext.define('PMG.QuarantineList', {
        setUser: function(user) {
            let view = this.getView();
            let params = view.getStore().getProxy().getExtraParams();
-           params.pmail = user;
+           if (user === null) {
+               delete params.pmail;
+           } else {
+               params.pmail = user;
+           }
            view.getStore().getProxy().setExtraParams(params);
            view.user = user;
        },
@@ -166,7 +179,11 @@ Ext.define('PMG.QuarantineList', {
            let me = this;
            me.savedPosition = undefined;
            me.allowPositionSave = false;
-           me.setUser(value);
+           if (value === 'all') {
+               me.setUser(null);
+           } else {
+               me.setUser(value);
+           }
            me.load();
        },
 
@@ -175,7 +192,7 @@ Ext.define('PMG.QuarantineList', {
            if (!me.allowPositionSave) {
                return;
            }
-           if (!selected.length) {
+           if (selected.length <= 0) {
                me.savedPosition = undefined;
                return;
            }
@@ -186,6 +203,66 @@ Ext.define('PMG.QuarantineList', {
            me.savedPosition = id;
        },
 
+       doFilter: function(searchValue, store, sm) {
+           const selected = sm.getSelection();
+           const selectedRecordId = selected.length === 1 ? selected[0].id : null;
+           let clearSelectedMail = true;
+           let toDeselect = [];
+           store.filterBy(function(record) {
+               let match = false;
+
+               Ext.each(['subject', 'from'], property => {
+                   if (record.data[property] === null) {
+                       return;
+                   }
+
+                   let v = record.data[property].toString();
+                   if (v !== undefined) {
+                       v = v.toLowerCase();
+                       if (v.includes(searchValue)) {
+                           match = true;
+                           if (record.id === selectedRecordId) {
+                               clearSelectedMail = false;
+                           }
+                       }
+                   }
+               });
+               if (!match && sm.isSelected(record)) {
+                   toDeselect.push(record);
+               }
+               return match;
+           });
+           if (toDeselect.length > 0) {
+               sm.deselect(toDeselect, true);
+               sm.maybeFireSelectionChange(true);
+           }
+           return selectedRecordId !== null && clearSelectedMail;
+       },
+
+       updateFilter: async function(field) {
+           let me = this;
+           let view = me.getView();
+           let store = view.getStore();
+           let sm = view.getSelectionModel();
+
+           let searchValue = field.getValue().toLowerCase();
+
+           // supress store event if not empty, let filterBy below trigger it to avoid glitches
+           store.clearFilter(searchValue.length > 0);
+           field.triggers.clear.setVisible(searchValue.length > 0);
+
+           if (searchValue.length === 0) {
+               me.setEmptyText();
+               return;
+           }
+           me.setEmptyText(gettext('No match found'));
+
+           let clearSelection = me.doFilter(searchValue, store, sm);
+
+           if (clearSelection) {
+               view.setSelection();
+           }
+       },
 
        control: {
            '#': {
@@ -221,18 +298,18 @@ Ext.define('PMG.QuarantineList', {
        },
        items: [
            {
+               xtype: 'datefield',
+               name: 'from',
                fieldLabel: gettext('Since'),
                reference: 'from',
-               xtype: 'datefield',
                format: 'Y-m-d',
-               name: 'from',
            },
            {
+               xtype: 'datefield',
+               name: 'to',
                fieldLabel: gettext('Until'),
                reference: 'to',
-               xtype: 'datefield',
                format: 'Y-m-d',
-               name: 'to',
            },
            {
                xtype: 'combobox',
@@ -256,6 +333,13 @@ Ext.define('PMG.QuarantineList', {
                            renderer: Ext.htmlEncode,
                        },
                    ],
+                   listeners: {
+                       load: function(store, records, successfull) {
+                           if (successfull && records.length > 1) {
+                               store.insert(0, { mail: 'all' });
+                           }
+                       },
+                   },
                },
                queryMode: 'local',
                editable: true,
@@ -267,6 +351,30 @@ Ext.define('PMG.QuarantineList', {
                reference: 'email',
                fieldLabel: 'E-Mail',
            },
+           {
+               xtype: 'textfield',
+               name: 'filter',
+               fieldLabel: gettext('Search'),
+               emptyText: gettext('Subject, Sender'),
+               enableKeyEvents: true,
+               triggers: {
+                   clear: {
+                       cls: 'pmx-clear-trigger',
+                       weight: -1,
+                       hidden: true,
+                       handler: function() {
+                           let me = this;
+                           me.setValue('');
+                           // setValue does not results in a keyup event, so trigger manually
+                           me.up('grid').getController().updateFilter(me);
+                       },
+                   },
+               },
+               listeners: {
+                   buffer: 500,
+                   keyup: 'updateFilter',
+               },
+           },
        ],
     },
 });