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