3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.form.TriggerField"></div>/**
\r
15 * @class Ext.form.TriggerField
\r
16 * @extends Ext.form.TextField
\r
17 * Provides a convenient wrapper for TextFields that adds a clickable trigger button (looks like a combobox by default).
\r
18 * The trigger has no default action, so you must assign a function to implement the trigger click handler by
\r
19 * overriding {@link #onTriggerClick}. You can create a TriggerField directly, as it renders exactly like a combobox
\r
20 * for which you can provide a custom implementation. For example:
\r
22 var trigger = new Ext.form.TriggerField();
\r
23 trigger.onTriggerClick = myTriggerFn;
\r
24 trigger.applyToMarkup('my-field');
\r
27 * However, in general you will most likely want to use TriggerField as the base class for a reusable component.
\r
28 * {@link Ext.form.DateField} and {@link Ext.form.ComboBox} are perfect examples of this.
\r
31 * Create a new TriggerField.
\r
32 * @param {Object} config Configuration options (valid {@Ext.form.TextField} config options will also be applied
\r
33 * to the base TextField)
\r
36 Ext.form.TriggerField = Ext.extend(Ext.form.TextField, {
\r
37 <div id="cfg-Ext.form.TriggerField-triggerClass"></div>/**
\r
38 * @cfg {String} triggerClass
\r
39 * An additional CSS class used to style the trigger button. The trigger will always get the
\r
40 * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.
\r
42 <div id="cfg-Ext.form.TriggerField-triggerConfig"></div>/**
\r
43 * @cfg {Mixed} triggerConfig
\r
44 * <p>A {@link Ext.DomHelper DomHelper} config object specifying the structure of the
\r
45 * trigger element for this Field. (Optional).</p>
\r
46 * <p>Specify this when you need a customized element to act as the trigger button for a TriggerField.</p>
\r
47 * <p>Note that when using this option, it is the developer's responsibility to ensure correct sizing, positioning
\r
48 * and appearance of the trigger. Defaults to:</p>
\r
49 * <pre><code>{tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.triggerClass}</code></pre>
\r
51 <div id="cfg-Ext.form.TriggerField-autoCreate"></div>/**
\r
52 * @cfg {String/Object} autoCreate <p>A {@link Ext.DomHelper DomHelper} element spec, or true for a default
\r
53 * element spec. Used to create the {@link Ext.Component#getEl Element} which will encapsulate this Component.
\r
54 * See <tt>{@link Ext.Component#autoEl autoEl}</tt> for details. Defaults to:</p>
\r
55 * <pre><code>{tag: "input", type: "text", size: "16", autocomplete: "off"}</code></pre>
\r
57 defaultAutoCreate : {tag: "input", type: "text", size: "16", autocomplete: "off"},
\r
58 <div id="cfg-Ext.form.TriggerField-hideTrigger"></div>/**
\r
59 * @cfg {Boolean} hideTrigger <tt>true</tt> to hide the trigger element and display only the base
\r
60 * text field (defaults to <tt>false</tt>)
\r
63 <div id="cfg-Ext.form.TriggerField-editable"></div>/**
\r
64 * @cfg {Boolean} editable <tt>false</tt> to prevent the user from typing text directly into the field,
\r
65 * the field will only respond to a click on the trigger to set the value. (defaults to <tt>true</tt>)
\r
68 <div id="cfg-Ext.form.TriggerField-wrapFocusClass"></div>/**
\r
69 * @cfg {String} wrapFocusClass The class added to the to the wrap of the trigger element. Defaults to
\r
70 * <tt>x-trigger-wrap-focus</tt>.
\r
72 wrapFocusClass: 'x-trigger-wrap-focus',
\r
73 <div id="method-Ext.form.TriggerField-autoSize"></div>/**
\r
77 autoSize: Ext.emptyFn,
\r
87 defaultTriggerWidth: 17,
\r
90 onResize : function(w, h){
\r
91 Ext.form.TriggerField.superclass.onResize.call(this, w, h);
\r
92 var tw = this.getTriggerWidth();
\r
93 if(Ext.isNumber(w)){
\r
94 this.el.setWidth(w - tw);
\r
96 this.wrap.setWidth(this.el.getWidth() + tw);
\r
99 getTriggerWidth: function(){
\r
100 var tw = this.trigger.getWidth();
\r
101 if(!this.hideTrigger && tw === 0){
\r
102 tw = this.defaultTriggerWidth;
\r
108 alignErrorIcon : function(){
\r
110 this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
\r
115 onRender : function(ct, position){
\r
116 this.doc = Ext.isIE ? Ext.getBody() : Ext.getDoc();
\r
117 Ext.form.TriggerField.superclass.onRender.call(this, ct, position);
\r
119 this.wrap = this.el.wrap({cls: 'x-form-field-wrap x-form-field-trigger-wrap'});
\r
120 this.trigger = this.wrap.createChild(this.triggerConfig ||
\r
121 {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.triggerClass});
\r
122 if(this.hideTrigger){
\r
123 this.trigger.setDisplayed(false);
\r
125 this.initTrigger();
\r
127 this.wrap.setWidth(this.el.getWidth()+this.trigger.getWidth());
\r
129 if(!this.editable){
\r
130 this.editable = true;
\r
131 this.setEditable(false);
\r
133 this.resizeEl = this.positionEl = this.wrap;
\r
136 afterRender : function(){
\r
137 Ext.form.TriggerField.superclass.afterRender.call(this);
\r
141 initTrigger : function(){
\r
142 this.mon(this.trigger, 'click', this.onTriggerClick, this, {preventDefault:true});
\r
143 this.trigger.addClassOnOver('x-form-trigger-over');
\r
144 this.trigger.addClassOnClick('x-form-trigger-click');
\r
148 onDestroy : function(){
\r
149 Ext.destroy(this.trigger, this.wrap);
\r
150 if (this.mimicing){
\r
151 this.doc.un('mousedown', this.mimicBlur, this);
\r
153 Ext.form.TriggerField.superclass.onDestroy.call(this);
\r
157 onFocus : function(){
\r
158 Ext.form.TriggerField.superclass.onFocus.call(this);
\r
159 if(!this.mimicing){
\r
160 this.wrap.addClass(this.wrapFocusClass);
\r
161 this.mimicing = true;
\r
162 this.doc.on('mousedown', this.mimicBlur, this, {delay: 10});
\r
163 if(this.monitorTab){
\r
164 this.on('specialkey', this.checkTab, this);
\r
170 checkTab : function(me, e){
\r
171 if(e.getKey() == e.TAB){
\r
172 this.triggerBlur();
\r
177 onBlur : Ext.emptyFn,
\r
180 mimicBlur : function(e){
\r
181 if(!this.isDestroyed && !this.wrap.contains(e.target) && this.validateBlur(e)){
\r
182 this.triggerBlur();
\r
187 triggerBlur : function(){
\r
188 this.mimicing = false;
\r
189 this.doc.un('mousedown', this.mimicBlur, this);
\r
190 if(this.monitorTab && this.el){
\r
191 this.un('specialkey', this.checkTab, this);
\r
193 Ext.form.TriggerField.superclass.onBlur.call(this);
\r
195 this.wrap.removeClass(this.wrapFocusClass);
\r
199 beforeBlur : Ext.emptyFn,
\r
201 <div id="method-Ext.form.TriggerField-setEditable"></div>/**
\r
202 * Allow or prevent the user from directly editing the field text. If false is passed,
\r
203 * the user will only be able to modify the field using the trigger. This method
\r
204 * is the runtime equivalent of setting the 'editable' config option at config time.
\r
205 * @param {Boolean} value True to allow the user to directly edit the field text
\r
207 setEditable : function(value){
\r
208 if(value == this.editable){
\r
211 this.editable = value;
\r
213 this.el.addClass('x-trigger-noedit').on('click', this.onTriggerClick, this).dom.setAttribute('readOnly', true);
\r
215 this.el.removeClass('x-trigger-noedit').un('click', this.onTriggerClick, this).dom.removeAttribute('readOnly');
\r
220 // This should be overriden by any subclass that needs to check whether or not the field can be blurred.
\r
221 validateBlur : function(e){
\r
225 <div id="method-Ext.form.TriggerField-onTriggerClick"></div>/**
\r
226 * The function that should handle the trigger's click event. This method does nothing by default
\r
227 * until overridden by an implementing function. See Ext.form.ComboBox and Ext.form.DateField for
\r
228 * sample implementations.
\r
230 * @param {EventObject} e
\r
232 onTriggerClick : Ext.emptyFn
\r
234 <div id="cfg-Ext.form.TriggerField-grow"></div>/**
\r
235 * @cfg {Boolean} grow @hide
\r
237 <div id="cfg-Ext.form.TriggerField-growMin"></div>/**
\r
238 * @cfg {Number} growMin @hide
\r
240 <div id="cfg-Ext.form.TriggerField-growMax"></div>/**
\r
241 * @cfg {Number} growMax @hide
\r
245 <div id="cls-Ext.form.TwinTriggerField"></div>/**
\r
246 * @class Ext.form.TwinTriggerField
\r
247 * @extends Ext.form.TriggerField
\r
248 * TwinTriggerField is not a public class to be used directly. It is meant as an abstract base class
\r
249 * to be extended by an implementing class. For an example of implementing this class, see the custom
\r
250 * SearchField implementation here:
\r
251 * <a href="http://extjs.com/deploy/ext/examples/form/custom.html">http://extjs.com/deploy/ext/examples/form/custom.html</a>
\r
253 Ext.form.TwinTriggerField = Ext.extend(Ext.form.TriggerField, {
\r
254 <div id="cfg-Ext.form.TwinTriggerField-triggerConfig"></div>/**
\r
255 * @cfg {Mixed} triggerConfig
\r
256 * <p>A {@link Ext.DomHelper DomHelper} config object specifying the structure of the trigger elements
\r
257 * for this Field. (Optional).</p>
\r
258 * <p>Specify this when you need a customized element to contain the two trigger elements for this Field.
\r
259 * Each trigger element must be marked by the CSS class <tt>x-form-trigger</tt> (also see
\r
260 * <tt>{@link #trigger1Class}</tt> and <tt>{@link #trigger2Class}</tt>).</p>
\r
261 * <p>Note that when using this option, it is the developer's responsibility to ensure correct sizing,
\r
262 * positioning and appearance of the triggers.</p>
\r
264 <div id="cfg-Ext.form.TwinTriggerField-trigger1Class"></div>/**
\r
265 * @cfg {String} trigger1Class
\r
266 * An additional CSS class used to style the trigger button. The trigger will always get the
\r
267 * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.
\r
269 <div id="cfg-Ext.form.TwinTriggerField-trigger2Class"></div>/**
\r
270 * @cfg {String} trigger2Class
\r
271 * An additional CSS class used to style the trigger button. The trigger will always get the
\r
272 * class <tt>'x-form-trigger'</tt> by default and <tt>triggerClass</tt> will be <b>appended</b> if specified.
\r
275 initComponent : function(){
\r
276 Ext.form.TwinTriggerField.superclass.initComponent.call(this);
\r
278 this.triggerConfig = {
\r
279 tag:'span', cls:'x-form-twin-triggers', cn:[
\r
280 {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger1Class},
\r
281 {tag: "img", src: Ext.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger2Class}
\r
285 getTrigger : function(index){
\r
286 return this.triggers[index];
\r
289 initTrigger : function(){
\r
290 var ts = this.trigger.select('.x-form-trigger', true);
\r
291 var triggerField = this;
\r
292 ts.each(function(t, all, index){
\r
293 var triggerIndex = 'Trigger'+(index+1);
\r
294 t.hide = function(){
\r
295 var w = triggerField.wrap.getWidth();
\r
296 this.dom.style.display = 'none';
\r
297 triggerField.el.setWidth(w-triggerField.trigger.getWidth());
\r
298 this['hidden' + triggerIndex] = true;
\r
300 t.show = function(){
\r
301 var w = triggerField.wrap.getWidth();
\r
302 this.dom.style.display = '';
\r
303 triggerField.el.setWidth(w-triggerField.trigger.getWidth());
\r
304 this['hidden' + triggerIndex] = false;
\r
307 if(this['hide'+triggerIndex]){
\r
308 t.dom.style.display = 'none';
\r
309 this['hidden' + triggerIndex] = true;
\r
311 this.mon(t, 'click', this['on'+triggerIndex+'Click'], this, {preventDefault:true});
\r
312 t.addClassOnOver('x-form-trigger-over');
\r
313 t.addClassOnClick('x-form-trigger-click');
\r
315 this.triggers = ts.elements;
\r
318 getTriggerWidth: function(){
\r
320 Ext.each(this.triggers, function(t, index){
\r
321 var triggerIndex = 'Trigger' + (index + 1),
\r
323 if(w === 0 && !this['hidden' + triggerIndex]){
\r
324 tw += this.defaultTriggerWidth;
\r
333 onDestroy : function() {
\r
334 Ext.destroy(this.triggers);
\r
335 Ext.form.TwinTriggerField.superclass.onDestroy.call(this);
\r
338 <div id="method-Ext.form.TwinTriggerField-onTrigger1Click"></div>/**
\r
339 * The function that should handle the trigger's click event. This method does nothing by default
\r
340 * until overridden by an implementing function. See {@link Ext.form.TriggerField#onTriggerClick}
\r
341 * for additional information.
\r
343 * @param {EventObject} e
\r
345 onTrigger1Click : Ext.emptyFn,
\r
346 <div id="method-Ext.form.TwinTriggerField-onTrigger2Click"></div>/**
\r
347 * The function that should handle the trigger's click event. This method does nothing by default
\r
348 * until overridden by an implementing function. See {@link Ext.form.TriggerField#onTriggerClick}
\r
349 * for additional information.
\r
351 * @param {EventObject} e
\r
353 onTrigger2Click : Ext.emptyFn
\r
355 Ext.reg('trigger', Ext.form.TriggerField);</pre>