3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
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
15 var trigger = new Ext.form.TriggerField();
\r
16 trigger.onTriggerClick = myTriggerFn;
\r
17 trigger.applyToMarkup('my-field');
\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
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
29 Ext.form.TriggerField = Ext.extend(Ext.form.TextField, {
\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
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
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
50 defaultAutoCreate : {tag: "input", type: "text", size: "16", autocomplete: "off"},
\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
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
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
65 wrapFocusClass: 'x-trigger-wrap-focus',
\r
70 autoSize: Ext.emptyFn,
\r
80 defaultTriggerWidth: 17,
\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
89 this.wrap.setWidth(this.el.getWidth() + tw);
\r
92 getTriggerWidth: function(){
\r
93 var tw = this.trigger.getWidth();
\r
94 if(!this.hideTrigger && tw === 0){
\r
95 tw = this.defaultTriggerWidth;
\r
101 alignErrorIcon : function(){
\r
103 this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
\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
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
118 this.initTrigger();
\r
120 this.wrap.setWidth(this.el.getWidth()+this.trigger.getWidth());
\r
122 if(!this.editable){
\r
123 this.editable = true;
\r
124 this.setEditable(false);
\r
126 this.resizeEl = this.positionEl = this.wrap;
\r
129 afterRender : function(){
\r
130 Ext.form.TriggerField.superclass.afterRender.call(this);
\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
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
146 Ext.form.TriggerField.superclass.onDestroy.call(this);
\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
163 checkTab : function(me, e){
\r
164 if(e.getKey() == e.TAB){
\r
165 this.triggerBlur();
\r
170 onBlur : Ext.emptyFn,
\r
173 mimicBlur : function(e){
\r
174 if(!this.isDestroyed && !this.wrap.contains(e.target) && this.validateBlur(e)){
\r
175 this.triggerBlur();
\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
186 Ext.form.TriggerField.superclass.onBlur.call(this);
\r
188 this.wrap.removeClass(this.wrapFocusClass);
\r
192 beforeBlur : Ext.emptyFn,
\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
200 setEditable : function(value){
\r
201 if(value == this.editable){
\r
204 this.editable = value;
\r
206 this.el.addClass('x-trigger-noedit').on('click', this.onTriggerClick, this).dom.setAttribute('readOnly', true);
\r
208 this.el.removeClass('x-trigger-noedit').un('click', this.onTriggerClick, this).dom.removeAttribute('readOnly');
\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
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
223 * @param {EventObject} e
\r
225 onTriggerClick : Ext.emptyFn
\r
228 * @cfg {Boolean} grow @hide
\r
231 * @cfg {Number} growMin @hide
\r
234 * @cfg {Number} growMax @hide
\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
246 Ext.form.TwinTriggerField = Ext.extend(Ext.form.TriggerField, {
\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
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
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
268 initComponent : function(){
\r
269 Ext.form.TwinTriggerField.superclass.initComponent.call(this);
\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
278 getTrigger : function(index){
\r
279 return this.triggers[index];
\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
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
300 if(this['hide'+triggerIndex]){
\r
301 t.dom.style.display = 'none';
\r
302 this['hidden' + triggerIndex] = true;
\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
308 this.triggers = ts.elements;
\r
311 getTriggerWidth: function(){
\r
313 Ext.each(this.triggers, function(t, index){
\r
314 var triggerIndex = 'Trigger' + (index + 1),
\r
316 if(w === 0 && !this['hidden' + triggerIndex]){
\r
317 tw += this.defaultTriggerWidth;
\r
326 onDestroy : function() {
\r
327 Ext.destroy(this.triggers);
\r
328 Ext.form.TwinTriggerField.superclass.onDestroy.call(this);
\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
336 * @param {EventObject} e
\r
338 onTrigger1Click : Ext.emptyFn,
\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
344 * @param {EventObject} e
\r
346 onTrigger2Click : Ext.emptyFn
\r
348 Ext.reg('trigger', Ext.form.TriggerField);