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