Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / form / TriggerField.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.form.TriggerField\r
9  * @extends Ext.form.TextField\r
10  * Provides a convenient wrapper for TextFields that adds a clickable trigger button (looks like a combobox by default).\r
11  * The trigger has no default action, so you must assign a function to implement the trigger click handler by\r
12  * overriding {@link #onTriggerClick}. You can create a TriggerField directly, as it renders exactly like a combobox\r
13  * for which you can provide a custom implementation.  For example:\r
14  * <pre><code>\r
15 var trigger = new Ext.form.TriggerField();\r
16 trigger.onTriggerClick = myTriggerFn;\r
17 trigger.applyToMarkup('my-field');\r
18 </code></pre>\r
19  *\r
20  * However, in general you will most likely want to use TriggerField as the base class for a reusable component.\r
21  * {@link Ext.form.DateField} and {@link Ext.form.ComboBox} are perfect examples of this.\r
22  * \r
23  * @constructor\r
24  * Create a new TriggerField.\r
25  * @param {Object} config Configuration options (valid {@Ext.form.TextField} config options will also be applied\r
26  * to the base TextField)\r
27  * @xtype trigger\r
28  */\r
29 Ext.form.TriggerField = Ext.extend(Ext.form.TextField,  {\r
30     /**\r
31      * @cfg {String} triggerClass\r
32      * An additional CSS class used to style the trigger button.  The trigger will always get the\r
33      * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.\r
34      */\r
35     /**\r
36      * @cfg {Mixed} triggerConfig\r
37      * <p>A {@link Ext.DomHelper DomHelper} config object specifying the structure of the\r
38      * trigger element for this Field. (Optional).</p>\r
39      * <p>Specify this when you need a customized element to act as the trigger button for a TriggerField.</p>\r
40      * <p>Note that when using this option, it is the developer's responsibility to ensure correct sizing, positioning\r
41      * and appearance of the trigger.  Defaults to:</p>\r
42      * <pre><code>{tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.triggerClass}</code></pre>\r
43      */\r
44     /**\r
45      * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or true for a default\r
46      * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.\r
47      * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details.  Defaults to:</p>\r
48      * <pre><code>{tag: "input", type: "text", size: "16", autocomplete: "off"}</code></pre>\r
49      */\r
50     defaultAutoCreate : {tag: "input", type: "text", size: "16", autocomplete: "off"},\r
51     /**\r
52      * @cfg {Boolean} hideTrigger <tt>true</tt> to hide the trigger element and display only the base\r
53      * text field (defaults to <tt>false</tt>)\r
54      */\r
55     hideTrigger:false,\r
56     /**\r
57      * @cfg {Boolean} editable <tt>false</tt> to prevent the user from typing text directly into the field,\r
58      * the field will only respond to a click on the trigger to set the value. (defaults to <tt>true</tt>)\r
59      */\r
60     editable: true,\r
61     /**\r
62      * @cfg {String} wrapFocusClass The class added to the to the wrap of the trigger element. Defaults to\r
63      * <tt>x-trigger-wrap-focus</tt>.\r
64      */\r
65     wrapFocusClass: 'x-trigger-wrap-focus',\r
66     /**\r
67      * @hide \r
68      * @method autoSize\r
69      */\r
70     autoSize: Ext.emptyFn,\r
71     // private\r
72     monitorTab : true,\r
73     // private\r
74     deferHeight : true,\r
75     // private\r
76     mimicing : false,\r
77     \r
78     actionMode: 'wrap',\r
79     \r
80     defaultTriggerWidth: 17,\r
81 \r
82     // private\r
83     onResize : function(w, h){\r
84         Ext.form.TriggerField.superclass.onResize.call(this, w, h);\r
85         var tw = this.getTriggerWidth();\r
86         if(Ext.isNumber(w)){\r
87             this.el.setWidth(w - tw);\r
88         }\r
89         this.wrap.setWidth(this.el.getWidth() + tw);\r
90     },\r
91     \r
92     getTriggerWidth: function(){\r
93         var tw = this.trigger.getWidth();\r
94         if(!this.hideTrigger && tw === 0){\r
95             tw = this.defaultTriggerWidth;\r
96         }\r
97         return tw;\r
98     },\r
99 \r
100     // private\r
101     alignErrorIcon : function(){\r
102         if(this.wrap){\r
103             this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);\r
104         }\r
105     },\r
106 \r
107     // private\r
108     onRender : function(ct, position){\r
109         this.doc = Ext.isIE ? Ext.getBody() : Ext.getDoc();\r
110         Ext.form.TriggerField.superclass.onRender.call(this, ct, position);\r
111 \r
112         this.wrap = this.el.wrap({cls: 'x-form-field-wrap x-form-field-trigger-wrap'});\r
113         this.trigger = this.wrap.createChild(this.triggerConfig ||\r
114                 {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.triggerClass});\r
115         if(this.hideTrigger){\r
116             this.trigger.setDisplayed(false);\r
117         }\r
118         this.initTrigger();\r
119         if(!this.width){\r
120             this.wrap.setWidth(this.el.getWidth()+this.trigger.getWidth());\r
121         }\r
122         if(!this.editable){\r
123             this.editable = true;\r
124             this.setEditable(false);\r
125         }\r
126         this.resizeEl = this.positionEl = this.wrap;\r
127     },\r
128 \r
129     afterRender : function(){\r
130         Ext.form.TriggerField.superclass.afterRender.call(this);\r
131     },\r
132 \r
133     // private\r
134     initTrigger : function(){\r
135         this.mon(this.trigger, 'click', this.onTriggerClick, this, {preventDefault:true});\r
136         this.trigger.addClassOnOver('x-form-trigger-over');\r
137         this.trigger.addClassOnClick('x-form-trigger-click');\r
138     },\r
139 \r
140     // private\r
141     onDestroy : function(){\r
142         Ext.destroy(this.trigger, this.wrap);\r
143         if (this.mimicing){\r
144             this.doc.un('mousedown', this.mimicBlur, this);\r
145         }\r
146         Ext.form.TriggerField.superclass.onDestroy.call(this);\r
147     },\r
148 \r
149     // private\r
150     onFocus : function(){\r
151         Ext.form.TriggerField.superclass.onFocus.call(this);\r
152         if(!this.mimicing){\r
153             this.wrap.addClass(this.wrapFocusClass);\r
154             this.mimicing = true;\r
155             this.doc.on('mousedown', this.mimicBlur, this, {delay: 10});\r
156             if(this.monitorTab){\r
157                 this.on('specialkey', this.checkTab, this);\r
158             }\r
159         }\r
160     },\r
161 \r
162     // private\r
163     checkTab : function(me, e){\r
164         if(e.getKey() == e.TAB){\r
165             this.triggerBlur();\r
166         }\r
167     },\r
168 \r
169     // private\r
170     onBlur : Ext.emptyFn,\r
171 \r
172     // private\r
173     mimicBlur : function(e){\r
174         if(!this.isDestroyed && !this.wrap.contains(e.target) && this.validateBlur(e)){\r
175             this.triggerBlur();\r
176         }\r
177     },\r
178 \r
179     // private\r
180     triggerBlur : function(){\r
181         this.mimicing = false;\r
182         this.doc.un('mousedown', this.mimicBlur, this);\r
183         if(this.monitorTab && this.el){\r
184             this.un('specialkey', this.checkTab, this);\r
185         }\r
186         Ext.form.TriggerField.superclass.onBlur.call(this);\r
187         if(this.wrap){\r
188             this.wrap.removeClass(this.wrapFocusClass);\r
189         }\r
190     },\r
191 \r
192     beforeBlur : Ext.emptyFn, \r
193     \r
194     /**\r
195      * Allow or prevent the user from directly editing the field text.  If false is passed,\r
196      * the user will only be able to modify the field using the trigger.  This method\r
197      * is the runtime equivalent of setting the 'editable' config option at config time.\r
198      * @param {Boolean} value True to allow the user to directly edit the field text\r
199      */\r
200     setEditable : function(value){\r
201         if(value == this.editable){\r
202             return;\r
203         }\r
204         this.editable = value;\r
205         if(!value){\r
206             this.el.addClass('x-trigger-noedit').on('click', this.onTriggerClick, this).dom.setAttribute('readOnly', true);\r
207         }else{\r
208             this.el.removeClass('x-trigger-noedit').un('click', this.onTriggerClick,  this).dom.removeAttribute('readOnly');\r
209         }\r
210     },\r
211 \r
212     // private\r
213     // This should be overriden by any subclass that needs to check whether or not the field can be blurred.\r
214     validateBlur : function(e){\r
215         return true;\r
216     },\r
217 \r
218     /**\r
219      * The function that should handle the trigger's click event.  This method does nothing by default\r
220      * until overridden by an implementing function.  See Ext.form.ComboBox and Ext.form.DateField for\r
221      * sample implementations.\r
222      * @method\r
223      * @param {EventObject} e\r
224      */\r
225     onTriggerClick : Ext.emptyFn\r
226 \r
227     /**\r
228      * @cfg {Boolean} grow @hide\r
229      */\r
230     /**\r
231      * @cfg {Number} growMin @hide\r
232      */\r
233     /**\r
234      * @cfg {Number} growMax @hide\r
235      */\r
236 });\r
237 \r
238 /**\r
239  * @class Ext.form.TwinTriggerField\r
240  * @extends Ext.form.TriggerField\r
241  * TwinTriggerField is not a public class to be used directly.  It is meant as an abstract base class\r
242  * to be extended by an implementing class.  For an example of implementing this class, see the custom\r
243  * SearchField implementation here:\r
244  * <a href="http://extjs.com/deploy/ext/examples/form/custom.html">http://extjs.com/deploy/ext/examples/form/custom.html</a>\r
245  */\r
246 Ext.form.TwinTriggerField = Ext.extend(Ext.form.TriggerField, {\r
247     /**\r
248      * @cfg {Mixed} triggerConfig\r
249      * <p>A {@link Ext.DomHelper DomHelper} config object specifying the structure of the trigger elements\r
250      * for this Field. (Optional).</p>\r
251      * <p>Specify this when you need a customized element to contain the two trigger elements for this Field.\r
252      * Each trigger element must be marked by the CSS class <tt>x-form-trigger</tt> (also see\r
253      * <tt>{@link #trigger1Class}</tt> and <tt>{@link #trigger2Class}</tt>).</p>\r
254      * <p>Note that when using this option, it is the developer's responsibility to ensure correct sizing,\r
255      * positioning and appearance of the triggers.</p>\r
256      */\r
257     /**\r
258      * @cfg {String} trigger1Class\r
259      * An additional CSS class used to style the trigger button.  The trigger will always get the\r
260      * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.\r
261      */\r
262     /**\r
263      * @cfg {String} trigger2Class\r
264      * An additional CSS class used to style the trigger button.  The trigger will always get the\r
265      * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.\r
266      */\r
267 \r
268     initComponent : function(){\r
269         Ext.form.TwinTriggerField.superclass.initComponent.call(this);\r
270 \r
271         this.triggerConfig = {\r
272             tag:'span', cls:'x-form-twin-triggers', cn:[\r
273             {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger1Class},\r
274             {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger2Class}\r
275         ]};\r
276     },\r
277 \r
278     getTrigger : function(index){\r
279         return this.triggers[index];\r
280     },\r
281 \r
282     initTrigger : function(){\r
283         var ts = this.trigger.select('.x-form-trigger', true);\r
284         var triggerField = this;\r
285         ts.each(function(t, all, index){\r
286             var triggerIndex = 'Trigger'+(index+1);\r
287             t.hide = function(){\r
288                 var w = triggerField.wrap.getWidth();\r
289                 this.dom.style.display = 'none';\r
290                 triggerField.el.setWidth(w-triggerField.trigger.getWidth());\r
291                 this['hidden' + triggerIndex] = true;\r
292             };\r
293             t.show = function(){\r
294                 var w = triggerField.wrap.getWidth();\r
295                 this.dom.style.display = '';\r
296                 triggerField.el.setWidth(w-triggerField.trigger.getWidth());\r
297                 this['hidden' + triggerIndex] = false;\r
298             };\r
299             \r
300             if(this['hide'+triggerIndex]){\r
301                 t.dom.style.display = 'none';\r
302                 this['hidden' + triggerIndex] = true;\r
303             }\r
304             this.mon(t, 'click', this['on'+triggerIndex+'Click'], this, {preventDefault:true});\r
305             t.addClassOnOver('x-form-trigger-over');\r
306             t.addClassOnClick('x-form-trigger-click');\r
307         }, this);\r
308         this.triggers = ts.elements;\r
309     },\r
310     \r
311     getTriggerWidth: function(){\r
312         var tw = 0;\r
313         Ext.each(this.triggers, function(t, index){\r
314             var triggerIndex = 'Trigger' + (index + 1),\r
315                 w = t.getWidth();\r
316             if(w === 0 && !this['hidden' + triggerIndex]){\r
317                 tw += this.defaultTriggerWidth;\r
318             }else{\r
319                 tw += w;\r
320             }\r
321         }, this);\r
322         return tw;\r
323     },\r
324     \r
325     // private\r
326     onDestroy : function() {\r
327         Ext.destroy(this.triggers);\r
328         Ext.form.TwinTriggerField.superclass.onDestroy.call(this);\r
329     },\r
330 \r
331     /**\r
332      * The function that should handle the trigger's click event.  This method does nothing by default\r
333      * until overridden by an implementing function. See {@link Ext.form.TriggerField#onTriggerClick}\r
334      * for additional information.  \r
335      * @method\r
336      * @param {EventObject} e\r
337      */\r
338     onTrigger1Click : Ext.emptyFn,\r
339     /**\r
340      * The function that should handle the trigger's click event.  This method does nothing by default\r
341      * until overridden by an implementing function. See {@link Ext.form.TriggerField#onTriggerClick}\r
342      * for additional information.  \r
343      * @method\r
344      * @param {EventObject} e\r
345      */\r
346     onTrigger2Click : Ext.emptyFn\r
347 });\r
348 Ext.reg('trigger', Ext.form.TriggerField);