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