Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / src / widgets / form / TriggerField.js
1 /*!
2  * Ext JS Library 3.3.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
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     defaultTriggerWidth: 17,
87
88     // private
89     onResize : function(w, h){
90         Ext.form.TriggerField.superclass.onResize.call(this, w, h);
91         var tw = this.getTriggerWidth();
92         if(Ext.isNumber(w)){
93             this.el.setWidth(w - tw);
94         }
95         this.wrap.setWidth(this.el.getWidth() + tw);
96     },
97
98     getTriggerWidth: function(){
99         var tw = this.trigger.getWidth();
100         if(!this.hideTrigger && !this.readOnly && tw === 0){
101             tw = this.defaultTriggerWidth;
102         }
103         return tw;
104     },
105
106     // private
107     alignErrorIcon : function(){
108         if(this.wrap){
109             this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
110         }
111     },
112
113     // private
114     onRender : function(ct, position){
115         this.doc = Ext.isIE ? Ext.getBody() : Ext.getDoc();
116         Ext.form.TriggerField.superclass.onRender.call(this, ct, position);
117
118         this.wrap = this.el.wrap({cls: 'x-form-field-wrap x-form-field-trigger-wrap'});
119         this.trigger = this.wrap.createChild(this.triggerConfig ||
120                 {tag: "img", src: Ext.BLANK_IMAGE_URL, alt: "", cls: "x-form-trigger " + this.triggerClass});
121         this.initTrigger();
122         if(!this.width){
123             this.wrap.setWidth(this.el.getWidth()+this.trigger.getWidth());
124         }
125         this.resizeEl = this.positionEl = this.wrap;
126     },
127
128     getWidth: function() {
129         return(this.el.getWidth() + this.trigger.getWidth());
130     },
131
132     updateEditState: function(){
133         if(this.rendered){
134             if (this.readOnly) {
135                 this.el.dom.readOnly = true;
136                 this.el.addClass('x-trigger-noedit');
137                 this.mun(this.el, 'click', this.onTriggerClick, this);
138                 this.trigger.setDisplayed(false);
139             } else {
140                 if (!this.editable) {
141                     this.el.dom.readOnly = true;
142                     this.el.addClass('x-trigger-noedit');
143                     this.mon(this.el, 'click', this.onTriggerClick, this);
144                 } else {
145                     this.el.dom.readOnly = false;
146                     this.el.removeClass('x-trigger-noedit');
147                     this.mun(this.el, 'click', this.onTriggerClick, this);
148                 }
149                 this.trigger.setDisplayed(!this.hideTrigger);
150             }
151             this.onResize(this.width || this.wrap.getWidth());
152         }
153     },
154
155     /**
156      * Changes the hidden status of the trigger.
157      * @param {Boolean} hideTrigger True to hide the trigger, false to show it.
158      */
159     setHideTrigger: function(hideTrigger){
160         if(hideTrigger != this.hideTrigger){
161             this.hideTrigger = hideTrigger;
162             this.updateEditState();
163         }
164     },
165
166     /**
167      * Allow or prevent the user from directly editing the field text.  If false is passed,
168      * the user will only be able to modify the field using the trigger.  Will also add
169      * a click event to the text field which will call the trigger. This method
170      * is the runtime equivalent of setting the {@link #editable} config option at config time.
171      * @param {Boolean} value True to allow the user to directly edit the field text.
172      */
173     setEditable: function(editable){
174         if(editable != this.editable){
175             this.editable = editable;
176             this.updateEditState();
177         }
178     },
179
180     /**
181      * Setting this to true will supersede settings {@link #editable} and {@link #hideTrigger}.
182      * Setting this to false will defer back to {@link #editable} and {@link #hideTrigger}. This method
183      * is the runtime equivalent of setting the {@link #readOnly} config option at config time.
184      * @param {Boolean} value True to prevent the user changing the field and explicitly
185      * hide the trigger.
186      */
187     setReadOnly: function(readOnly){
188         if(readOnly != this.readOnly){
189             this.readOnly = readOnly;
190             this.updateEditState();
191         }
192     },
193
194     afterRender : function(){
195         Ext.form.TriggerField.superclass.afterRender.call(this);
196         this.updateEditState();
197     },
198
199     // private
200     initTrigger : function(){
201         this.mon(this.trigger, 'click', this.onTriggerClick, this, {preventDefault:true});
202         this.trigger.addClassOnOver('x-form-trigger-over');
203         this.trigger.addClassOnClick('x-form-trigger-click');
204     },
205
206     // private
207     onDestroy : function(){
208         Ext.destroy(this.trigger, this.wrap);
209         if (this.mimicing){
210             this.doc.un('mousedown', this.mimicBlur, this);
211         }
212         delete this.doc;
213         Ext.form.TriggerField.superclass.onDestroy.call(this);
214     },
215
216     // private
217     onFocus : function(){
218         Ext.form.TriggerField.superclass.onFocus.call(this);
219         if(!this.mimicing){
220             this.wrap.addClass(this.wrapFocusClass);
221             this.mimicing = true;
222             this.doc.on('mousedown', this.mimicBlur, this, {delay: 10});
223             if(this.monitorTab){
224                 this.on('specialkey', this.checkTab, this);
225             }
226         }
227     },
228
229     // private
230     checkTab : function(me, e){
231         if(e.getKey() == e.TAB){
232             this.triggerBlur();
233         }
234     },
235
236     // private
237     onBlur : Ext.emptyFn,
238
239     // private
240     mimicBlur : function(e){
241         if(!this.isDestroyed && !this.wrap.contains(e.target) && this.validateBlur(e)){
242             this.triggerBlur();
243         }
244     },
245
246     // private
247     triggerBlur : function(){
248         this.mimicing = false;
249         this.doc.un('mousedown', this.mimicBlur, this);
250         if(this.monitorTab && this.el){
251             this.un('specialkey', this.checkTab, this);
252         }
253         Ext.form.TriggerField.superclass.onBlur.call(this);
254         if(this.wrap){
255             this.wrap.removeClass(this.wrapFocusClass);
256         }
257     },
258
259     beforeBlur : Ext.emptyFn,
260
261     // private
262     // This should be overriden by any subclass that needs to check whether or not the field can be blurred.
263     validateBlur : function(e){
264         return true;
265     },
266
267     /**
268      * The function that should handle the trigger's click event.  This method does nothing by default
269      * until overridden by an implementing function.  See Ext.form.ComboBox and Ext.form.DateField for
270      * sample implementations.
271      * @method
272      * @param {EventObject} e
273      */
274     onTriggerClick : Ext.emptyFn
275
276     /**
277      * @cfg {Boolean} grow @hide
278      */
279     /**
280      * @cfg {Number} growMin @hide
281      */
282     /**
283      * @cfg {Number} growMax @hide
284      */
285 });
286
287 /**
288  * @class Ext.form.TwinTriggerField
289  * @extends Ext.form.TriggerField
290  * TwinTriggerField is not a public class to be used directly.  It is meant as an abstract base class
291  * to be extended by an implementing class.  For an example of implementing this class, see the custom
292  * SearchField implementation here:
293  * <a href="http://extjs.com/deploy/ext/examples/form/custom.html">http://extjs.com/deploy/ext/examples/form/custom.html</a>
294  */
295 Ext.form.TwinTriggerField = Ext.extend(Ext.form.TriggerField, {
296     /**
297      * @cfg {Mixed} triggerConfig
298      * <p>A {@link Ext.DomHelper DomHelper} config object specifying the structure of the trigger elements
299      * for this Field. (Optional).</p>
300      * <p>Specify this when you need a customized element to contain the two trigger elements for this Field.
301      * Each trigger element must be marked by the CSS class <tt>x-form-trigger</tt> (also see
302      * <tt>{@link #trigger1Class}</tt> and <tt>{@link #trigger2Class}</tt>).</p>
303      * <p>Note that when using this option, it is the developer's responsibility to ensure correct sizing,
304      * positioning and appearance of the triggers.</p>
305      */
306     /**
307      * @cfg {String} trigger1Class
308      * An additional CSS class used to style the trigger button.  The trigger will always get the
309      * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.
310      */
311     /**
312      * @cfg {String} trigger2Class
313      * An additional CSS class used to style the trigger button.  The trigger will always get the
314      * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.
315      */
316
317     initComponent : function(){
318         Ext.form.TwinTriggerField.superclass.initComponent.call(this);
319
320         this.triggerConfig = {
321             tag:'span', cls:'x-form-twin-triggers', cn:[
322             {tag: "img", src: Ext.BLANK_IMAGE_URL, alt: "", cls: "x-form-trigger " + this.trigger1Class},
323             {tag: "img", src: Ext.BLANK_IMAGE_URL, alt: "", cls: "x-form-trigger " + this.trigger2Class}
324         ]};
325     },
326
327     getTrigger : function(index){
328         return this.triggers[index];
329     },
330     
331     afterRender: function(){
332         Ext.form.TwinTriggerField.superclass.afterRender.call(this);
333         var triggers = this.triggers,
334             i = 0,
335             len = triggers.length;
336             
337         for(; i < len; ++i){
338             if(this['hideTrigger' + (i + 1)]){
339                     triggers[i].hide();
340                 }
341
342         }    
343     },
344
345     initTrigger : function(){
346         var ts = this.trigger.select('.x-form-trigger', true),
347             triggerField = this;
348             
349         ts.each(function(t, all, index){
350             var triggerIndex = 'Trigger'+(index+1);
351             t.hide = function(){
352                 var w = triggerField.wrap.getWidth();
353                 this.dom.style.display = 'none';
354                 triggerField.el.setWidth(w-triggerField.trigger.getWidth());
355                 triggerField['hidden' + triggerIndex] = true;
356             };
357             t.show = function(){
358                 var w = triggerField.wrap.getWidth();
359                 this.dom.style.display = '';
360                 triggerField.el.setWidth(w-triggerField.trigger.getWidth());
361                 triggerField['hidden' + triggerIndex] = false;
362             };
363             this.mon(t, 'click', this['on'+triggerIndex+'Click'], this, {preventDefault:true});
364             t.addClassOnOver('x-form-trigger-over');
365             t.addClassOnClick('x-form-trigger-click');
366         }, this);
367         this.triggers = ts.elements;
368     },
369
370     getTriggerWidth: function(){
371         var tw = 0;
372         Ext.each(this.triggers, function(t, index){
373             var triggerIndex = 'Trigger' + (index + 1),
374                 w = t.getWidth();
375             if(w === 0 && !this['hidden' + triggerIndex]){
376                 tw += this.defaultTriggerWidth;
377             }else{
378                 tw += w;
379             }
380         }, this);
381         return tw;
382     },
383
384     // private
385     onDestroy : function() {
386         Ext.destroy(this.triggers);
387         Ext.form.TwinTriggerField.superclass.onDestroy.call(this);
388     },
389
390     /**
391      * The function that should handle the trigger's click event.  This method does nothing by default
392      * until overridden by an implementing function. See {@link Ext.form.TriggerField#onTriggerClick}
393      * for additional information.
394      * @method
395      * @param {EventObject} e
396      */
397     onTrigger1Click : Ext.emptyFn,
398     /**
399      * The function that should handle the trigger's click event.  This method does nothing by default
400      * until overridden by an implementing function. See {@link Ext.form.TriggerField#onTriggerClick}
401      * for additional information.
402      * @method
403      * @param {EventObject} e
404      */
405     onTrigger2Click : Ext.emptyFn
406 });
407 Ext.reg('trigger', Ext.form.TriggerField);