Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / examples / ux / gridfilters / filter / Filter.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 Ext.namespace('Ext.ux.grid.filter');\r
8 \r
9 /** \r
10  * @class Ext.ux.grid.filter.Filter\r
11  * @extends Ext.util.Observable\r
12  * Abstract base class for filter implementations.\r
13  */\r
14 Ext.ux.grid.filter.Filter = Ext.extend(Ext.util.Observable, {\r
15     /**\r
16      * @cfg {Boolean} active\r
17      * Indicates the initial status of the filter (defaults to false).\r
18      */\r
19     active : false,\r
20     /**\r
21      * True if this filter is active.  Use setActive() to alter after configuration.\r
22      * @type Boolean\r
23      * @property active\r
24      */\r
25     /**\r
26      * @cfg {String} dataIndex \r
27      * The {@link Ext.data.Store} dataIndex of the field this filter represents.\r
28      * The dataIndex does not actually have to exist in the store.\r
29      */\r
30     dataIndex : null,\r
31     /**\r
32      * The filter configuration menu that will be installed into the filter submenu of a column menu.\r
33      * @type Ext.menu.Menu\r
34      * @property\r
35      */\r
36     menu : null,\r
37     /**\r
38      * @cfg {Number} updateBuffer\r
39      * Number of milliseconds to wait after user interaction to fire an update. Only supported \r
40      * by filters: 'list', 'numeric', and 'string'. Defaults to 500.\r
41      */\r
42     updateBuffer : 500,\r
43 \r
44     constructor : function (config) {\r
45         Ext.apply(this, config);\r
46             \r
47         this.addEvents(\r
48             /**\r
49              * @event activate\r
50              * Fires when an inactive filter becomes active\r
51              * @param {Ext.ux.grid.filter.Filter} this\r
52              */\r
53             'activate',\r
54             /**\r
55              * @event deactivate\r
56              * Fires when an active filter becomes inactive\r
57              * @param {Ext.ux.grid.filter.Filter} this\r
58              */\r
59             'deactivate',\r
60             /**\r
61              * @event serialize\r
62              * Fires after the serialization process. Use this to attach additional parameters to serialization\r
63              * data before it is encoded and sent to the server.\r
64              * @param {Array/Object} data A map or collection of maps representing the current filter configuration.\r
65              * @param {Ext.ux.grid.filter.Filter} filter The filter being serialized.\r
66              */\r
67             'serialize',\r
68             /**\r
69              * @event update\r
70              * Fires when a filter configuration has changed\r
71              * @param {Ext.ux.grid.filter.Filter} this The filter object.\r
72              */\r
73             'update'\r
74         );\r
75         Ext.ux.grid.filter.Filter.superclass.constructor.call(this);\r
76 \r
77         this.menu = new Ext.menu.Menu();\r
78         this.init(config);\r
79         if(config && config.value){\r
80             this.setValue(config.value);\r
81             this.setActive(config.active !== false, true);\r
82             delete config.value;\r
83         }\r
84     },\r
85 \r
86     /**\r
87      * Destroys this filter by purging any event listeners, and removing any menus.\r
88      */\r
89     destroy : function(){\r
90         if (this.menu){\r
91             this.menu.destroy();\r
92         }\r
93         this.purgeListeners();\r
94     },\r
95 \r
96     /**\r
97      * Template method to be implemented by all subclasses that is to\r
98      * initialize the filter and install required menu items.\r
99      * Defaults to Ext.emptyFn.\r
100      */\r
101     init : Ext.emptyFn,\r
102     \r
103     /**\r
104      * Template method to be implemented by all subclasses that is to\r
105      * get and return the value of the filter.\r
106      * Defaults to Ext.emptyFn.\r
107      * @return {Object} The 'serialized' form of this filter\r
108      * @methodOf Ext.ux.grid.filter.Filter\r
109      */\r
110     getValue : Ext.emptyFn,\r
111     \r
112     /**\r
113      * Template method to be implemented by all subclasses that is to\r
114      * set the value of the filter and fire the 'update' event.\r
115      * Defaults to Ext.emptyFn.\r
116      * @param {Object} data The value to set the filter\r
117      * @methodOf Ext.ux.grid.filter.Filter\r
118      */ \r
119     setValue : Ext.emptyFn,\r
120     \r
121     /**\r
122      * Template method to be implemented by all subclasses that is to\r
123      * return <tt>true</tt> if the filter has enough configuration information to be activated.\r
124      * Defaults to <tt>return true</tt>.\r
125      * @return {Boolean}\r
126      */\r
127     isActivatable : function(){\r
128         return true;\r
129     },\r
130     \r
131     /**\r
132      * Template method to be implemented by all subclasses that is to\r
133      * get and return serialized filter data for transmission to the server.\r
134      * Defaults to Ext.emptyFn.\r
135      */\r
136     getSerialArgs : Ext.emptyFn,\r
137 \r
138     /**\r
139      * Template method to be implemented by all subclasses that is to\r
140      * validates the provided Ext.data.Record against the filters configuration.\r
141      * Defaults to <tt>return true</tt>.\r
142      * @param {Ext.data.Record} record The record to validate\r
143      * @return {Boolean} true if the record is valid within the bounds\r
144      * of the filter, false otherwise.\r
145      */\r
146     validateRecord : function(){\r
147         return true;\r
148     },\r
149 \r
150     /**\r
151      * Returns the serialized filter data for transmission to the server\r
152      * and fires the 'serialize' event.\r
153      * @return {Object/Array} An object or collection of objects containing\r
154      * key value pairs representing the current configuration of the filter.\r
155      * @methodOf Ext.ux.grid.filter.Filter\r
156      */\r
157     serialize : function(){\r
158         var args = this.getSerialArgs();\r
159         this.fireEvent('serialize', args, this);\r
160         return args;\r
161     },\r
162 \r
163     /** @private */\r
164     fireUpdate : function(){\r
165         if (this.active) {\r
166             this.fireEvent('update', this);\r
167         }\r
168         this.setActive(this.isActivatable());\r
169     },\r
170     \r
171     /**\r
172      * Sets the status of the filter and fires the appropriate events.\r
173      * @param {Boolean} active        The new filter state.\r
174      * @param {Boolean} suppressEvent True to prevent events from being fired.\r
175      * @methodOf Ext.ux.grid.filter.Filter\r
176      */\r
177     setActive : function(active, suppressEvent){\r
178         if(this.active != active){\r
179             this.active = active;\r
180             if (suppressEvent !== true) {\r
181                 this.fireEvent(active ? 'activate' : 'deactivate', this);\r
182             }\r
183         }\r
184     }    \r
185 });