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