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