From 1a5a91a62156d564a611d05da250b521871a88fe Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <legac@cppm.in2p3.fr>
Date: Tue, 26 Nov 2013 18:11:36 +0100
Subject: [PATCH] ExtJS 4.2: Draft implementation of the grid filtering running
 with buffered store.

---
 modules/plugin_dbui/dbsvc.py         | 16 +++++-
 modules/plugin_dbui/storemodifier.py |  2 +
 static/plugin_dbui/src/grid.js       | 25 +++++++++-
 static/plugin_dbui/src/gridfilter.js | 75 ++++++++++------------------
 4 files changed, 66 insertions(+), 52 deletions(-)

diff --git a/modules/plugin_dbui/dbsvc.py b/modules/plugin_dbui/dbsvc.py
index 34453b35..d4678e3f 100644
--- a/modules/plugin_dbui/dbsvc.py
+++ b/modules/plugin_dbui/dbsvc.py
@@ -690,7 +690,21 @@ class DbSvc(BaseSvc):
                 if dir == 'DESC':
                     el = ~el
                 kwargs['orderby'].append(el)
-                     
+        
+        if 'sort' in arg:
+            if 'oderby' not in kwargs:
+                kwargs['orderby'] = []
+            
+            for el in arg['sort']:
+                if 'property' in el:
+                    tablename, fieldname = decode_field(el['property'])
+                    field = db[tablename][fieldname]
+            
+                if 'direction' in el and el['direction'] == "DESC":
+                    field = ~field
+            
+                kwargs['orderby'].append(field)
+                   
         # interrogate the database and serialize the records as a list:
         # [{TableField: value, }, {...}, ...]
         rows = db(query).select(*fields, **kwargs)
diff --git a/modules/plugin_dbui/storemodifier.py b/modules/plugin_dbui/storemodifier.py
index b3d897ec..17c6e7e6 100644
--- a/modules/plugin_dbui/storemodifier.py
+++ b/modules/plugin_dbui/storemodifier.py
@@ -103,6 +103,7 @@ class StoreModifier(Modifier):
         
             - autoLoad = False
             - buffered = True
+            - clearOnPageLoad = True
             - pageSize = 25
             - leadingBufferZone = 0
             - trailingBufferZone = 0
@@ -121,6 +122,7 @@ class StoreModifier(Modifier):
         """
         di = dict(autoLoad=False,
                   buffered=True,
+                  clearOnPageLoad=True,
                   pageSize=25,
                   leadingBufferZone=0,
                   trailingBufferZone=0)
diff --git a/static/plugin_dbui/src/grid.js b/static/plugin_dbui/src/grid.js
index 5c009505..87b2a59e 100644
--- a/static/plugin_dbui/src/grid.js
+++ b/static/plugin_dbui/src/grid.js
@@ -49,8 +49,16 @@ Ext.define('App.grid.Grid', {
             this.store.getProxy().pageParam = 'page';
         }
 
-        // load the first page when the buffering is on
-        if (this.findPlugin('bufferedrenderer')) {
+        var plug = this.findPlugin('bufferedrenderer');
+        /*
+        plug.leadingBufferZone = 8;
+        plug.trailingBufferZone = 8;
+        plug.numFromEdge = 0;
+        console.log(plug);
+        */
+
+        // load the first page when the store is buffered
+        if (this.store.buffered) {
             this.store.loadPage(1);
 
         // load the full data set
@@ -99,6 +107,18 @@ Ext.define('App.grid.Grid', {
 
         "use strict";
 
+        var store = this.getStore();
+
+        store.restoreWhere();
+
+        if (this.getStore().buffered) {
+            store.loadPage(1);
+
+        } else {
+            store.load();
+        }
+
+        /*
         var action,
             bbar = this.getDockedItems('toolbar[dock=bottom]')[0];
 
@@ -121,5 +141,6 @@ Ext.define('App.grid.Grid', {
 
         // reload the store and fire the resetgrid event
         this.store.load(action);
+        */
     }
 });
diff --git a/static/plugin_dbui/src/gridfilter.js b/static/plugin_dbui/src/gridfilter.js
index dc759c60..6034aa43 100644
--- a/static/plugin_dbui/src/gridfilter.js
+++ b/static/plugin_dbui/src/gridfilter.js
@@ -12,9 +12,6 @@ Ext.define('App.grid.GridFilter', {
     // Private object containing where clause related to the filter request
     filterConditions: {},
 
-    // Private reference to the paging toolBar
-    pagingToolbar: null,
-
     // Private reference to the grid store
     store: null,
 
@@ -35,7 +32,7 @@ Ext.define('App.grid.GridFilter', {
         this.callParent(arguments);
 
         // associate handlers to fields
-        this.items.each(function (item, index, len) {
+        this.items.each(function (item) {
 
             if (!item.isFormField) {
                 return;
@@ -66,7 +63,7 @@ Ext.define('App.grid.GridFilter', {
     beforeDestroy: function () {
 
         //purge handlers associate to fields defined in the initComponent
-        this.items.each(function (item, index, len) {
+        this.items.each(function (item) {
 
             if (!item.isFormField) {
                 return;
@@ -93,7 +90,6 @@ Ext.define('App.grid.GridFilter', {
         }, this);
 
         // remove private reference
-        this.pagingToolbar = null;
         this.store = null;
 
         // base class
@@ -110,17 +106,32 @@ Ext.define('App.grid.GridFilter', {
 
         this.store = grid.getStore();
 
-        // paging toolbar ?
-        if (App.isPlugin(grid, 'pGridPaging')) {
-            this.pagingToolbar = grid.getBottomToolbar();
-        }
-
         // reset filter when receiving resetgrid event
         // The main user case is when the pGridExpertMenu is triggered
         // to reset the grid. In that case, the filter has to be reset too.
         grid.on('resetgrid', this.reset, this);
     },
 
+    // private method to filter the store applying filter conditions
+    filterStore: function () {
+
+        "use strict";
+
+
+        if (this.store.buffered) {
+            // NOTE
+            // Clear the page cache to fix some overlap problem (ExtJS 4.2.1)
+            // Some records are not over written and correspond to
+            // the previous filtering. Clearing the buffer solve this issue.
+            // Copy from the method Ext.data.Store.reload
+            this.store.data.clear(true);
+            this.store.loadPage(1);
+
+        } else {
+            this.store.load();
+        }
+    },
+
     /**
      * Handler to catch a change in field value and to filter the store.
      *
@@ -132,11 +143,8 @@ Ext.define('App.grid.GridFilter', {
 
         "use strict";
 
-        // setup the filter conditions
         this.setupCondition(field);
-
-        // update the store
-        this.updateStore();
+        this.filterStore();
     },
 
     /**
@@ -152,8 +160,8 @@ Ext.define('App.grid.GridFilter', {
         // restore initial where condition of the store
         this.store.restoreWhere();
 
-        // update the store
-        this.updateStore();
+        // filter the store
+        this.filterStore();
     },
 
     /**
@@ -164,7 +172,7 @@ Ext.define('App.grid.GridFilter', {
         "use strict";
 
         // reset the field values
-        this.items.each(function (item, index, len) {
+        this.items.each(function (item) {
             if (item.isFormField) {
                 item.reset();
             }
@@ -230,36 +238,5 @@ Ext.define('App.grid.GridFilter', {
         } else {
             this.store.restoreWhere();
         }
-    },
-
-    // private method to load the store applying filter conditions
-    updateStore: function () {
-
-        "use strict";
-
-        var bbar = this.pagingToolbar,
-            nRows,
-            slider;
-
-        if (bbar) {
-            nRows = bbar.pageSize;
-            slider = bbar.findByType('slider')[0];
-
-            this.store.load({params: {start: 0, limit: nRows}, callback: function () {
-
-                // protection against null store
-                // this happen in some destroy sequence ! why ?
-                if (this.store) {
-                    bbar.moveFirst();
-                    slider.setMaxValue(this.store.getTotalCount());
-                    slider.setValue(nRows);
-                }
-
-            }, scope: this});
-
-
-        } else {
-            this.store.load();
-        }
     }
 });
\ No newline at end of file
-- 
GitLab