3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
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:
15 var trigger = new Ext.form.TriggerField();
16 trigger.onTriggerClick = myTriggerFn;
17 trigger.applyToMarkup('my-field');
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.
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)
29 Ext.form.TriggerField = Ext.extend(Ext.form.TextField, {
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.
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>
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>
50 defaultAutoCreate : {tag: "input", type: "text", size: "16", autocomplete: "off"},
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>)
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>)
62 * @cfg {String} wrapFocusClass The class added to the to the wrap of the trigger element. Defaults to
63 * <tt>x-trigger-wrap-focus</tt>.
65 wrapFocusClass: 'x-trigger-wrap-focus',
70 autoSize: Ext.emptyFn,
81 onResize : function(w, h){
82 Ext.form.TriggerField.superclass.onResize.call(this, w, h);
83 if(typeof w == 'number'){
84 this.el.setWidth(this.adjustWidth('input', w - this.trigger.getWidth()));
86 this.wrap.setWidth(this.el.getWidth()+this.trigger.getWidth());
90 adjustSize : Ext.BoxComponent.prototype.adjustSize,
93 getResizeEl : function(){
98 getPositionEl : function(){
103 alignErrorIcon : function(){
105 this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
110 onRender : function(ct, position){
111 Ext.form.TriggerField.superclass.onRender.call(this, ct, position);
113 this.wrap = this.el.wrap({cls: 'x-form-field-wrap x-form-field-trigger-wrap'});
114 this.trigger = this.wrap.createChild(this.triggerConfig ||
115 {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.triggerClass});
116 if(this.hideTrigger){
117 this.trigger.setDisplayed(false);
121 this.wrap.setWidth(this.el.getWidth()+this.trigger.getWidth());
124 this.editable = true;
125 this.setEditable(false);
129 afterRender : function(){
130 Ext.form.TriggerField.superclass.afterRender.call(this);
134 initTrigger : function(){
135 this.mon(this.trigger, 'click', this.onTriggerClick, this, {preventDefault:true});
136 this.trigger.addClassOnOver('x-form-trigger-over');
137 this.trigger.addClassOnClick('x-form-trigger-click');
141 onDestroy : function(){
142 Ext.destroy(this.trigger, this.wrap);
144 Ext.get(Ext.isIE ? document.body : document).un("mousedown", this.mimicBlur, this);
146 Ext.form.TriggerField.superclass.onDestroy.call(this);
150 onFocus : function(){
151 Ext.form.TriggerField.superclass.onFocus.call(this);
153 this.wrap.addClass(this.wrapFocusClass);
154 this.mimicing = true;
155 Ext.get(Ext.isIE ? document.body : document).on("mousedown", this.mimicBlur, this, {delay: 10});
157 this.el.on('keydown', this.checkTab, this);
163 checkTab : function(e){
164 if(e.getKey() == e.TAB){
175 mimicBlur : function(e){
176 if(!this.wrap.contains(e.target) && this.validateBlur(e)){
182 triggerBlur : function(){
183 this.mimicing = false;
184 Ext.get(Ext.isIE ? document.body : document).un("mousedown", this.mimicBlur, this);
185 if(this.monitorTab && this.el){
186 this.el.un("keydown", this.checkTab, this);
188 Ext.form.TriggerField.superclass.onBlur.call(this);
190 this.wrap.removeClass(this.wrapFocusClass);
194 beforeBlur : Ext.emptyFn,
197 * Allow or prevent the user from directly editing the field text. If false is passed,
198 * the user will only be able to modify the field using the trigger. This method
199 * is the runtime equivalent of setting the 'editable' config option at config time.
200 * @param {Boolean} value True to allow the user to directly edit the field text
202 setEditable : function(value){
203 if(value == this.editable){
206 this.editable = value;
208 this.el.addClass('x-trigger-noedit').on('click', this.onTriggerClick, this).dom.setAttribute('readOnly', true);
210 this.el.removeClass('x-trigger-noedit').un('click', this.onTriggerClick, this).dom.removeAttribute('readOnly');
215 // This should be overriden by any subclass that needs to check whether or not the field can be blurred.
216 validateBlur : function(e){
221 * The function that should handle the trigger's click event. This method does nothing by default
222 * until overridden by an implementing function. See Ext.form.ComboBox and Ext.form.DateField for
223 * sample implementations.
225 * @param {EventObject} e
227 onTriggerClick : Ext.emptyFn
230 * @cfg {Boolean} grow @hide
233 * @cfg {Number} growMin @hide
236 * @cfg {Number} growMax @hide
241 * @class Ext.form.TwinTriggerField
242 * @extends Ext.form.TriggerField
243 * TwinTriggerField is not a public class to be used directly. It is meant as an abstract base class
244 * to be extended by an implementing class. For an example of implementing this class, see the custom
245 * SearchField implementation here:
246 * <a href="http://extjs.com/deploy/ext/examples/form/custom.html">http://extjs.com/deploy/ext/examples/form/custom.html</a>
248 Ext.form.TwinTriggerField = Ext.extend(Ext.form.TriggerField, {
250 * @cfg {Mixed} triggerConfig
251 * <p>A {@link Ext.DomHelper DomHelper} config object specifying the structure of the trigger elements
252 * for this Field. (Optional).</p>
253 * <p>Specify this when you need a customized element to contain the two trigger elements for this Field.
254 * Each trigger element must be marked by the CSS class <tt>x-form-trigger</tt> (also see
255 * <tt>{@link #trigger1Class}</tt> and <tt>{@link #trigger2Class}</tt>).</p>
256 * <p>Note that when using this option, it is the developer's responsibility to ensure correct sizing,
257 * positioning and appearance of the triggers.</p>
260 * @cfg {String} trigger1Class
261 * An additional CSS class used to style the trigger button. The trigger will always get the
262 * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.
265 * @cfg {String} trigger2Class
266 * An additional CSS class used to style the trigger button. The trigger will always get the
267 * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.
270 initComponent : function(){
271 Ext.form.TwinTriggerField.superclass.initComponent.call(this);
273 this.triggerConfig = {
274 tag:'span', cls:'x-form-twin-triggers', cn:[
275 {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger1Class},
276 {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger2Class}
280 getTrigger : function(index){
281 return this.triggers[index];
284 initTrigger : function(){
285 var ts = this.trigger.select('.x-form-trigger', true);
286 this.wrap.setStyle('overflow', 'hidden');
287 var triggerField = this;
288 ts.each(function(t, all, index){
290 var w = triggerField.wrap.getWidth();
291 this.dom.style.display = 'none';
292 triggerField.el.setWidth(w-triggerField.trigger.getWidth());
295 var w = triggerField.wrap.getWidth();
296 this.dom.style.display = '';
297 triggerField.el.setWidth(w-triggerField.trigger.getWidth());
299 var triggerIndex = 'Trigger'+(index+1);
301 if(this['hide'+triggerIndex]){
302 t.dom.style.display = 'none';
304 this.mon(t, 'click', this['on'+triggerIndex+'Click'], this, {preventDefault:true});
305 t.addClassOnOver('x-form-trigger-over');
306 t.addClassOnClick('x-form-trigger-click');
308 this.triggers = ts.elements;
312 * The function that should handle the trigger's click event. This method does nothing by default
313 * until overridden by an implementing function. See {@link Ext.form.TriggerField#onTriggerClick}
314 * for additional information.
316 * @param {EventObject} e
318 onTrigger1Click : Ext.emptyFn,
320 * The function that should handle the trigger's click event. This method does nothing by default
321 * until overridden by an implementing function. See {@link Ext.form.TriggerField#onTriggerClick}
322 * for additional information.
324 * @param {EventObject} e
326 onTrigger2Click : Ext.emptyFn
328 Ext.reg('trigger', Ext.form.TriggerField);