From 511013376b7c95d46ad0fb1798760af1f1bb29d3 Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <legac@cppm.in2p3.fr>
Date: Mon, 10 Dec 2012 16:38:25 +0100
Subject: [PATCH] Add the configuration parameter nRows, the method reset and
 the event resetgrid.

---
 static/plugin_dbui/src/grid.js | 58 ++++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 2 deletions(-)

diff --git a/static/plugin_dbui/src/grid.js b/static/plugin_dbui/src/grid.js
index 4d1ac9b3..2cecddb2 100644
--- a/static/plugin_dbui/src/grid.js
+++ b/static/plugin_dbui/src/grid.js
@@ -14,6 +14,18 @@ Ext.namespace('App.grid');
 
 App.grid.Grid = Ext.extend(Ext.grid.GridPanel, {
 
+    /**
+     * @cfg nRows integer
+     * Default number of rows display in the grid.
+     * This parameter is only use when running with a paging plugin. 
+     */
+    nRows: 10,
+    
+    /**
+     * @event resetgrid
+     * Can be used to reset underlying plugin when the reset 
+     * method is triggered
+     */
     /**
      * private attributes used by plugins
      */
@@ -49,18 +61,60 @@ App.grid.Grid = Ext.extend(Ext.grid.GridPanel, {
         // empty toolBar requires by the plugins
         this.bbar = new Ext.Toolbar();
 
-        // initialize the underlying class. DON'T MOVE
+        // initialise the underlying class. DON'T MOVE
         App.grid.Grid.superclass.initComponent.call(this);        
 
         // load the store if not yet done
         // if the grid paging is used load only the first 10 rows
         if (!this.store.getTotalCount()) {
             if (App.isPlugin(this, 'pGridPaging')) {
-                this.store.load({params: {start: 0, limit: 10}});
+                this.store.load({params: {start: 0, limit: this.nRows}});
             } else {
                 this.store.load();    
             }
         }
+    },
+    /**
+     * reset the grid
+     * The store is load with fresh data and the event 'resetgrid' is emitted. 
+     * Paging and slider plugin might be reset using the 'resetgrid' event.
+     * GridFilter object can be also reset using the same mechanism.
+     * 
+     * The scope is the grid
+     * 
+     */
+    reset: function () {
+
+        var action,
+            i;
+
+        // private function to emitt the resetgrid event
+        function fireReset() {
+            this.fireEvent('resetgrid');
+        }
+
+        // parameters for the store load action
+        action = {callback: fireReset, scope: this};
+        if (this.getBottomToolbar() instanceof Ext.PagingToolbar) {
+            action.params = {start: 0, limit: this.nRows};
+        }
+
+        // restore initial where condition of the store
+        // in order to remove filter condition
+        if ('where' in this.store.baseParams) {
+            
+            if (this.store.initialWhere) {
+                this.store.baseParams.where = [];
+                for (i = 0; i < this.store.initialWhere.length; i += 1) {
+                    this.store.baseParams.where.push(this.store.initialWhere[i]);
+                }
+            } else {
+                delete this.store.baseParams.where;  
+            }
+        }
+        
+        // reload the store and fire the resetgrid event
+        this.store.load(action);
     }
 });
 
-- 
GitLab