From 8ec1ab4536171e883796a21b7534d8707a987b44 Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <renaud.legac@free.fr>
Date: Tue, 14 Dec 2010 20:50:27 +0000
Subject: [PATCH] The GridFilter is a separate object not a plugin anymore.

---
 static/plugin_dbui/lib/appgrid.js       |  10 --
 static/plugin_dbui/lib/appgridfilter.js | 145 ++++++++++++++++--------
 2 files changed, 97 insertions(+), 58 deletions(-)

diff --git a/static/plugin_dbui/lib/appgrid.js b/static/plugin_dbui/lib/appgrid.js
index 780d35ee..b647c1b1 100644
--- a/static/plugin_dbui/lib/appgrid.js
+++ b/static/plugin_dbui/lib/appgrid.js
@@ -43,13 +43,6 @@ App.grid.Grid = Ext.extend(Ext.grid.GridPanel, {
 	 */
 	editor: null,
 
-	/**
-	 * private
-	 * keep track of the grid filter 
-	 * this link is used by the pGridFilter plugin.
-	 */
-	filter: null,
-	
 	/**
 	 * private method requests by the component model of Extjs
 	 */
@@ -72,9 +65,6 @@ App.grid.Grid = Ext.extend(Ext.grid.GridPanel, {
 		// the columns model
 		this.columns = this.model.colModel;
 
-		// the filter
-		this.filter = Ext.ComponentMgr.create(this.model.filterModel);
-		
 		// empty top toolbar/bottomnar
 		this.tbar = [];
 		
diff --git a/static/plugin_dbui/lib/appgridfilter.js b/static/plugin_dbui/lib/appgridfilter.js
index 7678ad61..80453290 100644
--- a/static/plugin_dbui/lib/appgridfilter.js
+++ b/static/plugin_dbui/lib/appgridfilter.js
@@ -1,6 +1,8 @@
 /**
- * grid filter plugin
- * It contains all the logic to filter the content of a grid
+ * The GridFilter class
+ * 
+ * Associate to grid, it contains all the logic to filter the content of a grid
+ * according to the value define in the form.
  * 
  * @version $Id$
  *
@@ -8,80 +10,121 @@
 
 Ext.namespace('App.grid');
 
-App.grid.GridFilter = Ext.extend(Object, {
+App.grid.GridFilter = Ext.extend(Ext.form.FieldSet, {
 
 	/**
-	 * Private 
-	 * a reference to the grid and to its store
-	 */
-	grid: null,
-	store: null,
-
+	 * Private
+	 * Dictionary with additional where clause
+	 */	
+	filterConditions: {},
+	
 	/**
 	 * Private
-	 * Initial baseParams of the store
+	 * Initial where clause
 	 */
-	initialFilterConditions: null,
+	initialFilterConditions: [],
 	
 	/**
 	 * Private
-	 * Dictionary of additional where clause
-	 */	
-	filterConditions: {},
+	 * Reference to the paging toolbar
+	 */
+	pagingToolbar: null,
+
+	/**
+	 * Private 
+	 * a reference to the grid store
+	 */
+	store: null,
 	
 	/**
-	 * Private
-	 * Plugin initialization
+	 * Predefined setting
 	 */
-	init: function(grid){
-		
-		// link the grid and its store
-		this.grid = grid;
-		this.store = grid.getStore();
-		
-		// Keep track of the initial filter conditions
-		this.initialFilterConditions = [];
-		for( var i = 0; i < this.store.baseParams.where.length; i++){
-			this.initialFilterConditions.push(this.store.baseParams.where[i]);
-		}
-				
-		// associate handlers to form field
-		var fields = grid.filter.findByType('field');
+	defaults: {anchor: '100%'},
+	
+	/**
+	 * private method requests by the component model of Ext JS
+	 */
+	initComponent: function(){
+
+		// initialize the underlying class.
+		App.grid.GridFilter.superclass.initComponent.call(this);
+						
+		// associate handlers to fields
+		var fields = this.findByType('field');
 		for (var i = 0; i < fields.length; i++){
 			field = fields[i];
 			
 			// catch a change of value for combo box
 			if (field.xtype == 'xcombobox')
-				field.on('select', this.onSelect, this);
+				field.on('select', this.onComboSelect, this);
 			
 			// catch the key pressed enter for other fields
 			else 
-				field.on('specialkey', this.onSpecialKey, this);
+				field.on('specialkey', this.onKeyPressedEnter, this);
 		}
 	},
 	
+	/**
+	 * Bind the grid filter to the grid
+	 * @param {Ext.grid.GridPanel} grid
+	 */
+	bind: function(grid){
+
+		this.store = grid.getStore();
+		
+		// paging toolbar ?
+		if(grid.pagingInitialized)
+			this.pagingToolbar = grid.getBottomToolbar();
+
+		// initial filter conditions
+		var baseParams = this.store.baseParams;
+		for( var i = 0; i < baseParams.where.length; i++){
+			this.initialFilterConditions.push(baseParams.where[i]);
+		}
+				
+	},
+
 	/**
 	 * Handler to catch a new selection in combobox
 	 */
-	onSelect: function(combo, record, index){
-		this.filter(combo);
+	onComboSelect: function(combo, record, index){
+		this.setupCondition(combo);
 	},
-	
+
 	/**
 	 * Handler to catch the ENTER
 	 * @param {Object} field
 	 * @param {Object} e
 	 */
-	onSpecialKey: function(field,e){
+	onKeyPressedEnter: function(field, e){
 		if (e.getKey() == e.ENTER) 
-			this.filter(field);
+			this.setupCondition(field);
+	},
+	
+	/**
+	 * Handler to reset the filter
+	 */
+	onReset: function(){
+		
+		// reset the field value
+		var fields = this.findByType('field');
+		for (var i = 0; i < fields.length; i++) {
+			fields[i].reset();
+		};
+		
+		// reset filter conditions
+		this.filterConditions = {};
+		this.store.baseParams.where = this.initialFilterConditions;
+		
+		// update the store
+		this.updateStore();
 	},
 	
 	/**
-	 * Filter the data
+	 * Setup condition
 	 * @param {Ext.form.Field}
 	 */
-	filter: function(field){
+	setupCondition: function(field){
 
 		// get the field value and update the condition dictionary
 		var value = field.getValue();
@@ -102,18 +145,24 @@ App.grid.GridFilter = Ext.extend(Object, {
 			conditions.push(this.filterConditions[k]);
 		}
 		
-		// update the store
+		// push new condtion in the where clause of the store
 		this.store.baseParams.where = conditions;
 		
-		// update the store dealing with paging
-		if (this.grid.pagingInitialized) {
-			var bbar = this.grid.getBottomToolbar();
-			bbar.doRefresh();
-		} else {
-			this.store.load();		
-		}
+		// update the store
+		this.updateStore();
+	},
+	
+	/**
+	 * private
+	 * Helper function to load the store applying filter conditions
+	 */
+	updateStore: function(){
 		
+		if (this.pagingToolbar)
+			this.pagingToolbar.doRefresh();
+		else
+			this.store.load();		
 	}
 });
 
-Ext.preg('pGridFilter', App.grid.GridFilter);
+Ext.reg('xgridfilter', App.grid.GridFilter);
\ No newline at end of file
-- 
GitLab