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