3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.ux.grid.filter.DateFilter
17 * @extends Ext.ux.grid.filter.Filter
18 * Filter by a configurable Ext.picker.DatePicker menu
19 * <p><b><u>Example Usage:</u></b></p>
21 var filters = Ext.create('Ext.ux.grid.GridFilters', {
26 dataIndex: 'dateAdded',
29 dateFormat: 'm/d/Y', // default
30 beforeText: 'Before', // default
31 afterText: 'After', // default
32 onText: 'On', // default
34 // any DatePicker configs
37 active: true // default is false
42 Ext.define('Ext.ux.grid.filter.DateFilter', {
43 extend: 'Ext.ux.grid.filter.Filter',
44 alias: 'gridfilter.date',
45 uses: ['Ext.picker.Date', 'Ext.menu.Menu'],
48 * @cfg {String} afterText
49 * Defaults to 'After'.
53 * @cfg {String} beforeText
54 * Defaults to 'Before'.
56 beforeText : 'Before',
58 * @cfg {Object} compareMap
59 * Map for assigning the comparison values used in serialization.
67 * @cfg {String} dateFormat
68 * The date format to return when using getValue.
69 * Defaults to 'm/d/Y'.
75 * Allowable date as passed to the Ext.DatePicker
76 * Defaults to undefined.
80 * Allowable date as passed to the Ext.DatePicker
81 * Defaults to undefined.
84 * @cfg {Array} menuItems
85 * The items to be shown in this menu
87 * menuItems : ['before', 'after', '-', 'on'],
90 menuItems : ['before', 'after', '-', 'on'],
93 * @cfg {Object} menuItemCfgs
94 * Default configuration options for each menu item
102 * @cfg {String} onText
108 * @cfg {Object} pickerOpts
109 * Configuration options for the date picker associated with each field.
115 * Template method that is to initialize the filter and install required menu items.
117 init : function (config) {
119 pickerCfg, i, len, item, cfg;
121 pickerCfg = Ext.apply(me.pickerOpts, {
125 format: me.dateFormat,
128 select: me.onMenuSelect
133 for (i = 0, len = me.menuItems.length; i < len; i++) {
134 item = me.menuItems[i];
137 itemId: 'range-' + item,
138 text: me[item + 'Text'],
139 menu: Ext.create('Ext.menu.Menu', {
141 Ext.apply(pickerCfg, {
148 checkchange: me.onCheckChange
151 item = me.fields[item] = Ext.create('Ext.menu.CheckItem', cfg);
158 onCheckChange : function () {
159 this.setActive(this.isActivatable());
160 this.fireEvent('update', this);
165 * Handler method called when there is a keyup event on an input
168 onInputKeyUp : function (field, e) {
170 if (k == e.RETURN && field.isValid()) {
177 * Handler for when the DatePicker for a field fires the 'select' event
178 * @param {Ext.picker.Date} picker
179 * @param {Object} picker
180 * @param {Object} date
182 onMenuSelect : function (picker, date) {
183 var fields = this.fields,
184 field = this.fields[picker.itemId];
186 field.setChecked(true);
188 if (field == fields.on) {
189 fields.before.setChecked(false, true);
190 fields.after.setChecked(false, true);
192 fields.on.setChecked(false, true);
193 if (field == fields.after && this.getFieldValue('before') < date) {
194 fields.before.setChecked(false, true);
195 } else if (field == fields.before && this.getFieldValue('after') > date) {
196 fields.after.setChecked(false, true);
199 this.fireEvent('update', this);
201 picker.up('menu').hide();
206 * Template method that is to get and return the value of the filter.
207 * @return {String} The value of this filter
209 getValue : function () {
210 var key, result = {};
211 for (key in this.fields) {
212 if (this.fields[key].checked) {
213 result[key] = this.getFieldValue(key);
221 * Template method that is to set the value of the filter.
222 * @param {Object} value The value to set the filter
223 * @param {Boolean} preserve true to preserve the checked status
224 * of the other fields. Defaults to false, unchecking the
227 setValue : function (value, preserve) {
229 for (key in this.fields) {
231 this.getPicker(key).setValue(value[key]);
232 this.fields[key].setChecked(true);
233 } else if (!preserve) {
234 this.fields[key].setChecked(false);
237 this.fireEvent('update', this);
242 * Template method that is to return <tt>true</tt> if the filter
243 * has enough configuration information to be activated.
246 isActivatable : function () {
248 for (key in this.fields) {
249 if (this.fields[key].checked) {
258 * Template method that is to get and return serialized filter data for
259 * transmission to the server.
260 * @return {Object/Array} An object or collection of objects containing
261 * key value pairs representing the current configuration of the filter.
263 getSerialArgs : function () {
265 for (var key in this.fields) {
266 if(this.fields[key].checked){
269 comparison: this.compareMap[key],
270 value: Ext.Date.format(this.getFieldValue(key), this.dateFormat)
278 * Get and return the date menu picker value
279 * @param {String} item The field identifier ('before', 'after', 'on')
280 * @return {Date} Gets the current selected value of the date field
282 getFieldValue : function(item){
283 return this.getPicker(item).getValue();
287 * Gets the menu picker associated with the passed field
288 * @param {String} item The field identifier ('before', 'after', 'on')
289 * @return {Object} The menu picker
291 getPicker : function(item){
292 return this.fields[item].menu.items.first();
296 * Template method that is to validate the provided Ext.data.Record
297 * against the filters configuration.
298 * @param {Ext.data.Record} record The record to validate
299 * @return {Boolean} true if the record is valid within the bounds
300 * of the filter, false otherwise.
302 validateRecord : function (record) {
305 val = record.get(this.dataIndex),
306 clearTime = Ext.Date.clearTime;
308 if(!Ext.isDate(val)){
311 val = clearTime(val, true).getTime();
313 for (key in this.fields) {
314 if (this.fields[key].checked) {
315 pickerValue = clearTime(this.getFieldValue(key), true).getTime();
316 if (key == 'before' && pickerValue <= val) {
319 if (key == 'after' && pickerValue >= val) {
322 if (key == 'on' && pickerValue != val) {