Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / examples / ux / gridfilters / menu / RangeMenu.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 Ext.ns('Ext.ux.menu');
8
9 /** 
10  * @class Ext.ux.menu.RangeMenu
11  * @extends Ext.menu.Menu
12  * Custom implementation of Ext.menu.Menu that has preconfigured
13  * items for gt, lt, eq.
14  * <p><b><u>Example Usage:</u></b></p>
15  * <pre><code>    
16
17  * </code></pre> 
18  */
19 Ext.ux.menu.RangeMenu = Ext.extend(Ext.menu.Menu, {
20
21     constructor : function (config) {
22
23         Ext.ux.menu.RangeMenu.superclass.constructor.call(this, config);
24
25         this.addEvents(
26             /**
27              * @event update
28              * Fires when a filter configuration has changed
29              * @param {Ext.ux.grid.filter.Filter} this The filter object.
30              */
31             'update'
32         );
33       
34         this.updateTask = new Ext.util.DelayedTask(this.fireUpdate, this);
35     
36         var i, len, item, cfg, Cls;
37
38         for (i = 0, len = this.menuItems.length; i < len; i++) {
39             item = this.menuItems[i];
40             if (item !== '-') {
41                 // defaults
42                 cfg = {
43                     itemId: 'range-' + item,
44                     enableKeyEvents: true,
45                     iconCls: this.iconCls[item] || 'no-icon',
46                     listeners: {
47                         scope: this,
48                         keyup: this.onInputKeyUp
49                     }
50                 };
51                 Ext.apply(
52                     cfg,
53                     // custom configs
54                     Ext.applyIf(this.fields[item] || {}, this.fieldCfg[item]),
55                     // configurable defaults
56                     this.menuItemCfgs
57                 );
58                 Cls = cfg.fieldCls || this.fieldCls;
59                 item = this.fields[item] = new Cls(cfg);
60             }
61             this.add(item);
62         }
63     },
64
65     /**
66      * @private
67      * called by this.updateTask
68      */
69     fireUpdate : function () {
70         this.fireEvent('update', this);
71     },
72     
73     /**
74      * Get and return the value of the filter.
75      * @return {String} The value of this filter
76      */
77     getValue : function () {
78         var result = {}, key, field;
79         for (key in this.fields) {
80             field = this.fields[key];
81             if (field.isValid() && String(field.getValue()).length > 0) {
82                 result[key] = field.getValue();
83             }
84         }
85         return result;
86     },
87   
88     /**
89      * Set the value of this menu and fires the 'update' event.
90      * @param {Object} data The data to assign to this menu
91      */ 
92     setValue : function (data) {
93         var key;
94         for (key in this.fields) {
95             this.fields[key].setValue(data[key] !== undefined ? data[key] : '');
96         }
97         this.fireEvent('update', this);
98     },
99
100     /**  
101      * @private
102      * Handler method called when there is a keyup event on an input
103      * item of this menu.
104      */
105     onInputKeyUp : function (field, e) {
106         var k = e.getKey();
107         if (k == e.RETURN && field.isValid()) {
108             e.stopEvent();
109             this.hide(true);
110             return;
111         }
112         
113         if (field == this.fields.eq) {
114             if (this.fields.gt) {
115                 this.fields.gt.setValue(null);
116             }
117             if (this.fields.lt) {
118                 this.fields.lt.setValue(null);
119             }
120         }
121         else {
122             this.fields.eq.setValue(null);
123         }
124         
125         // restart the timer
126         this.updateTask.delay(this.updateBuffer);
127     }
128 });