2 * @class Ext.ux.grid.filter.DateFilter
3 * @extends Ext.ux.grid.filter.Filter
4 * Filter by a configurable Ext.picker.DatePicker menu
5 * <p><b><u>Example Usage:</u></b></p>
7 var filters = Ext.create('Ext.ux.grid.GridFilters', {
12 dataIndex: 'dateAdded',
15 dateFormat: 'm/d/Y', // default
16 beforeText: 'Before', // default
17 afterText: 'After', // default
18 onText: 'On', // default
20 // any DatePicker configs
23 active: true // default is false
28 Ext.define('Ext.ux.grid.filter.DateFilter', {
29 extend: 'Ext.ux.grid.filter.Filter',
30 alias: 'gridfilter.date',
31 uses: ['Ext.picker.Date', 'Ext.menu.Menu'],
34 * @cfg {String} afterText
35 * Defaults to 'After'.
39 * @cfg {String} beforeText
40 * Defaults to 'Before'.
42 beforeText : 'Before',
44 * @cfg {Object} compareMap
45 * Map for assigning the comparison values used in serialization.
53 * @cfg {String} dateFormat
54 * The date format to return when using getValue.
55 * Defaults to 'm/d/Y'.
61 * Allowable date as passed to the Ext.DatePicker
62 * Defaults to undefined.
66 * Allowable date as passed to the Ext.DatePicker
67 * Defaults to undefined.
70 * @cfg {Array} menuItems
71 * The items to be shown in this menu
73 * menuItems : ['before', 'after', '-', 'on'],
76 menuItems : ['before', 'after', '-', 'on'],
79 * @cfg {Object} menuItemCfgs
80 * Default configuration options for each menu item
88 * @cfg {String} onText
94 * @cfg {Object} pickerOpts
95 * Configuration options for the date picker associated with each field.
101 * Template method that is to initialize the filter and install required menu items.
103 init : function (config) {
105 pickerCfg, i, len, item, cfg;
107 pickerCfg = Ext.apply(me.pickerOpts, {
111 format: me.dateFormat,
114 select: me.onMenuSelect
119 for (i = 0, len = me.menuItems.length; i < len; i++) {
120 item = me.menuItems[i];
123 itemId: 'range-' + item,
124 text: me[item + 'Text'],
125 menu: Ext.create('Ext.menu.Menu', {
127 Ext.apply(pickerCfg, {
134 checkchange: me.onCheckChange
137 item = me.fields[item] = Ext.create('Ext.menu.CheckItem', cfg);
144 onCheckChange : function () {
145 this.setActive(this.isActivatable());
146 this.fireEvent('update', this);
151 * Handler method called when there is a keyup event on an input
154 onInputKeyUp : function (field, e) {
156 if (k == e.RETURN && field.isValid()) {
163 * Handler for when the DatePicker for a field fires the 'select' event
164 * @param {Ext.picker.Date} picker
165 * @param {Object} picker
166 * @param {Object} date
168 onMenuSelect : function (picker, date) {
169 var fields = this.fields,
170 field = this.fields[picker.itemId];
172 field.setChecked(true);
174 if (field == fields.on) {
175 fields.before.setChecked(false, true);
176 fields.after.setChecked(false, true);
178 fields.on.setChecked(false, true);
179 if (field == fields.after && this.getFieldValue('before') < date) {
180 fields.before.setChecked(false, true);
181 } else if (field == fields.before && this.getFieldValue('after') > date) {
182 fields.after.setChecked(false, true);
185 this.fireEvent('update', this);
187 picker.up('menu').hide();
192 * Template method that is to get and return the value of the filter.
193 * @return {String} The value of this filter
195 getValue : function () {
196 var key, result = {};
197 for (key in this.fields) {
198 if (this.fields[key].checked) {
199 result[key] = this.getFieldValue(key);
207 * Template method that is to set the value of the filter.
208 * @param {Object} value The value to set the filter
209 * @param {Boolean} preserve true to preserve the checked status
210 * of the other fields. Defaults to false, unchecking the
213 setValue : function (value, preserve) {
215 for (key in this.fields) {
217 this.getPicker(key).setValue(value[key]);
218 this.fields[key].setChecked(true);
219 } else if (!preserve) {
220 this.fields[key].setChecked(false);
223 this.fireEvent('update', this);
228 * Template method that is to return <tt>true</tt> if the filter
229 * has enough configuration information to be activated.
232 isActivatable : function () {
234 for (key in this.fields) {
235 if (this.fields[key].checked) {
244 * Template method that is to get and return serialized filter data for
245 * transmission to the server.
246 * @return {Object/Array} An object or collection of objects containing
247 * key value pairs representing the current configuration of the filter.
249 getSerialArgs : function () {
251 for (var key in this.fields) {
252 if(this.fields[key].checked){
255 comparison: this.compareMap[key],
256 value: Ext.Date.format(this.getFieldValue(key), this.dateFormat)
264 * Get and return the date menu picker value
265 * @param {String} item The field identifier ('before', 'after', 'on')
266 * @return {Date} Gets the current selected value of the date field
268 getFieldValue : function(item){
269 return this.getPicker(item).getValue();
273 * Gets the menu picker associated with the passed field
274 * @param {String} item The field identifier ('before', 'after', 'on')
275 * @return {Object} The menu picker
277 getPicker : function(item){
278 return this.fields[item].menu.items.first();
282 * Template method that is to validate the provided Ext.data.Record
283 * against the filters configuration.
284 * @param {Ext.data.Record} record The record to validate
285 * @return {Boolean} true if the record is valid within the bounds
286 * of the filter, false otherwise.
288 validateRecord : function (record) {
291 val = record.get(this.dataIndex),
292 clearTime = Ext.Date.clearTime;
294 if(!Ext.isDate(val)){
297 val = clearTime(val, true).getTime();
299 for (key in this.fields) {
300 if (this.fields[key].checked) {
301 pickerValue = clearTime(this.getFieldValue(key), true).getTime();
302 if (key == 'before' && pickerValue <= val) {
305 if (key == 'after' && pickerValue >= val) {
308 if (key == 'on' && pickerValue != val) {