1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-form.field.Trigger-method-constructor'><span id='Ext-form.field.Trigger'>/**
2 </span></span> * @class Ext.form.field.Trigger
3 * @extends Ext.form.field.Text
4 * <p>Provides a convenient wrapper for TextFields that adds a clickable trigger button (looks like a combobox by default).
5 * The trigger has no default action, so you must assign a function to implement the trigger click handler by
6 * overriding {@link #onTriggerClick}. You can create a Trigger field directly, as it renders exactly like a combobox
7 * for which you can provide a custom implementation.
8 * {@img Ext.form.field.Trigger/Ext.form.field.Trigger.png Ext.form.field.Trigger component}
9 * For example:</p>
10 * <pre><code>
11 Ext.define('Ext.ux.CustomTrigger', {
12 extend: 'Ext.form.field.Trigger',
13 alias: 'widget.customtrigger',
15 // override onTriggerClick
16 onTriggerClick: function() {
17 Ext.Msg.alert('Status', 'You clicked my trigger!');
21 Ext.create('Ext.form.FormPanel', {
22 title: 'Form with TriggerField',
25 renderTo: Ext.getBody(),
27 xtype: 'customtrigger',
28 fieldLabel: 'Sample Trigger',
29 emptyText: 'click the trigger',
32 </code></pre>
34 * <p>However, in general you will most likely want to use Trigger as the base class for a reusable component.
35 * {@link Ext.form.field.Date} and {@link Ext.form.field.ComboBox} are perfect examples of this.</p>
38 * Create a new Trigger field.
39 * @param {Object} config Configuration options (valid {@Ext.form.field.Text} config options will also be applied
40 * to the base Text field)
43 Ext.define('Ext.form.field.Trigger', {
44 extend:'Ext.form.field.Text',
45 alias: ['widget.triggerfield', 'widget.trigger'],
46 requires: ['Ext.core.DomHelper', 'Ext.util.ClickRepeater', 'Ext.layout.component.field.Trigger'],
47 alternateClassName: ['Ext.form.TriggerField', 'Ext.form.TwinTriggerField', 'Ext.form.Trigger'],
50 '<input id="{id}" type="{type}" ',
51 '<tpl if="name">name="{name}" </tpl>',
52 '<tpl if="size">size="{size}" </tpl>',
53 '<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
54 'class="{fieldCls} {typeCls}" autocomplete="off" />',
55 '<div class="{triggerWrapCls}" role="presentation">',
57 '<div class="{clearCls}" role="presentation"></div>',
65 <span id='Ext-form.field.Trigger-cfg-triggerCls'> /**
66 </span> * @cfg {String} triggerCls
67 * An additional CSS class used to style the trigger button. The trigger will always get the
68 * {@link #triggerBaseCls} by default and <tt>triggerCls</tt> will be <b>appended</b> if specified.
69 * Defaults to undefined.
72 <span id='Ext-form.field.Trigger-cfg-triggerBaseCls'> /**
73 </span> * @cfg {String} triggerBaseCls
74 * The base CSS class that is always added to the trigger button. The {@link #triggerCls} will be
75 * appended in addition to this class.
77 triggerBaseCls: Ext.baseCSSPrefix + 'form-trigger',
79 <span id='Ext-form.field.Trigger-cfg-triggerWrapCls'> /**
80 </span> * @cfg {String} triggerWrapCls
81 * The CSS class that is added to the div wrapping the trigger button(s).
83 triggerWrapCls: Ext.baseCSSPrefix + 'form-trigger-wrap',
85 <span id='Ext-form.field.Trigger-cfg-hideTrigger'> /**
86 </span> * @cfg {Boolean} hideTrigger <tt>true</tt> to hide the trigger element and display only the base
87 * text field (defaults to <tt>false</tt>)
91 <span id='Ext-form.field.Trigger-cfg-editable'> /**
92 </span> * @cfg {Boolean} editable <tt>false</tt> to prevent the user from typing text directly into the field;
93 * the field can only have its value set via an action invoked by the trigger. (defaults to <tt>true</tt>).
97 <span id='Ext-form.field.Trigger-cfg-readOnly'> /**
98 </span> * @cfg {Boolean} readOnly <tt>true</tt> to prevent the user from changing the field, and
99 * hides the trigger. Supercedes the editable and hideTrigger options if the value is true.
100 * (defaults to <tt>false</tt>)
104 <span id='Ext-form.field.Trigger-cfg-selectOnFocus'> /**
105 </span> * @cfg {Boolean} selectOnFocus <tt>true</tt> to select any existing text in the field immediately on focus.
106 * Only applies when <tt>{@link #editable editable} = true</tt> (defaults to <tt>false</tt>).
109 <span id='Ext-form.field.Trigger-cfg-repeatTriggerClick'> /**
110 </span> * @cfg {Boolean} repeatTriggerClick <tt>true</tt> to attach a {@link Ext.util.ClickRepeater click repeater}
111 * to the trigger. Defaults to <tt>false</tt>.
113 repeatTriggerClick: false,
116 <span id='Ext-form.field.Trigger-method-autoSize'> /**
120 autoSize: Ext.emptyFn,
126 triggerIndexRe: /trigger-index-(\d+)/,
128 componentLayout: 'triggerfield',
130 initComponent: function() {
131 this.wrapFocusCls = this.triggerWrapCls + '-focus';
132 this.callParent(arguments);
136 onRender: function(ct, position) {
139 triggerBaseCls = me.triggerBaseCls,
140 triggerWrapCls = me.triggerWrapCls,
144 // triggerCls is a synonym for trigger1Cls, so copy it.
145 // TODO this trigger<n>Cls API design doesn't feel clean, especially where it butts up against the
146 // single triggerCls config. Should rethink this, perhaps something more structured like a list of
147 // trigger config objects that hold cls, handler, etc.
148 if (!me.trigger1Cls) {
149 me.trigger1Cls = me.triggerCls;
152 // Create as many trigger elements as we have trigger<n>Cls configs, but always at least one
153 for (i = 0; (triggerCls = me['trigger' + (i + 1) + 'Cls']) || i < 1; i++) {
154 triggerConfigs.push({
155 cls: [Ext.baseCSSPrefix + 'trigger-index-' + i, triggerBaseCls, triggerCls].join(' '),
159 triggerConfigs[i - 1].cls += ' ' + triggerBaseCls + '-last';
161 Ext.applyIf(me.renderSelectors, {
162 <span id='Ext-form.field.Trigger-property-triggerWrap'> /**
163 </span> * @property triggerWrap
164 * @type Ext.core.Element
165 * A reference to the div element wrapping the trigger button(s). Only set after the field has been rendered.
167 triggerWrap: '.' + triggerWrapCls
169 Ext.applyIf(me.subTplData, {
170 triggerWrapCls: triggerWrapCls,
171 triggerEl: Ext.core.DomHelper.markup(triggerConfigs),
172 clearCls: me.clearCls
175 me.callParent(arguments);
177 <span id='Ext-form.field.Trigger-property-triggerEl'> /**
178 </span> * @property triggerEl
179 * @type Ext.CompositeElement
180 * A composite of all the trigger button elements. Only set after the field has been rendered.
182 me.triggerEl = Ext.select('.' + triggerBaseCls, true, me.triggerWrap.dom);
184 me.doc = Ext.isIE ? Ext.getBody() : Ext.getDoc();
188 onEnable: function() {
190 this.triggerWrap.unmask();
193 onDisable: function() {
195 this.triggerWrap.mask();
198 afterRender: function() {
200 this.updateEditState();
203 updateEditState: function() {
205 inputEl = me.inputEl,
206 triggerWrap = me.triggerWrap,
207 noeditCls = Ext.baseCSSPrefix + 'trigger-noedit',
213 inputEl.addCls(noeditCls);
218 inputEl.removeCls(noeditCls);
221 inputEl.addCls(noeditCls);
224 displayed = !me.hideTrigger;
227 triggerWrap.setDisplayed(displayed);
228 inputEl.dom.readOnly = readOnly;
229 me.doComponentLayout();
233 <span id='Ext-form.field.Trigger-method-getTriggerWidth'> /**
234 </span> * Get the total width of the trigger button area. Only useful after the field has been rendered.
235 * @return {Number} The trigger width
237 getTriggerWidth: function() {
239 triggerWrap = me.triggerWrap,
240 totalTriggerWidth = 0;
241 if (triggerWrap && !me.hideTrigger && !me.readOnly) {
242 me.triggerEl.each(function(trigger) {
243 totalTriggerWidth += trigger.getWidth();
245 totalTriggerWidth += me.triggerWrap.getFrameWidth('lr');
247 return totalTriggerWidth;
250 setHideTrigger: function(hideTrigger) {
251 if (hideTrigger != this.hideTrigger) {
252 this.hideTrigger = hideTrigger;
253 this.updateEditState();
257 <span id='Ext-form.field.Trigger-method-setEditable'> /**
258 </span> * @param {Boolean} editable True to allow the user to directly edit the field text
259 * Allow or prevent the user from directly editing the field text. If false is passed,
260 * the user will only be able to modify the field using the trigger. Will also add
261 * a click event to the text field which will call the trigger. This method
262 * is the runtime equivalent of setting the 'editable' config option at config time.
264 setEditable: function(editable) {
265 if (editable != this.editable) {
266 this.editable = editable;
267 this.updateEditState();
271 <span id='Ext-form.field.Trigger-method-setReadOnly'> /**
272 </span> * @param {Boolean} readOnly True to prevent the user changing the field and explicitly
274 * Setting this to true will superceed settings editable and hideTrigger.
275 * Setting this to false will defer back to editable and hideTrigger. This method
276 * is the runtime equivalent of setting the 'readOnly' config option at config time.
278 setReadOnly: function(readOnly) {
279 if (readOnly != this.readOnly) {
280 this.readOnly = readOnly;
281 this.updateEditState();
286 initTrigger: function() {
288 triggerWrap = me.triggerWrap,
289 triggerEl = me.triggerEl;
291 if (me.repeatTriggerClick) {
292 me.triggerRepeater = Ext.create('Ext.util.ClickRepeater', triggerWrap, {
293 preventDefault: true,
294 handler: function(cr, e) {
295 me.onTriggerWrapClick(e);
299 me.mon(me.triggerWrap, 'click', me.onTriggerWrapClick, me);
302 triggerEl.addClsOnOver(me.triggerBaseCls + '-over');
303 triggerEl.each(function(el, c, i) {
304 el.addClsOnOver(me['trigger' + (i + 1) + 'Cls'] + '-over');
306 triggerEl.addClsOnClick(me.triggerBaseCls + '-click');
307 triggerEl.each(function(el, c, i) {
308 el.addClsOnClick(me['trigger' + (i + 1) + 'Cls'] + '-click');
313 onDestroy: function() {
315 Ext.destroyMembers(me, 'triggerRepeater', 'triggerWrap', 'triggerEl');
321 onFocus: function() {
325 me.bodyEl.addCls(me.wrapFocusCls);
327 me.mon(me.doc, 'mousedown', me.mimicBlur, me, {
331 me.on('specialkey', me.checkTab, me);
337 checkTab: function(me, e) {
338 if (!this.ignoreMonitorTab && e.getKey() == e.TAB) {
347 mimicBlur: function(e) {
348 if (!this.isDestroyed && !this.bodyEl.contains(e.target) && this.validateBlur(e)) {
354 triggerBlur: function() {
357 me.mun(me.doc, 'mousedown', me.mimicBlur, me);
358 if (me.monitorTab && me.inputEl) {
359 me.un('specialkey', me.checkTab, me);
361 Ext.form.field.Trigger.superclass.onBlur.call(me);
363 me.bodyEl.removeCls(me.wrapFocusCls);
367 beforeBlur: Ext.emptyFn,
370 // This should be overridden by any subclass that needs to check whether or not the field can be blurred.
371 validateBlur: function(e) {
376 // process clicks upon triggers.
377 // determine which trigger index, and dispatch to the appropriate click handler
378 onTriggerWrapClick: function(e) {
380 t = e && e.getTarget('.' + Ext.baseCSSPrefix + 'form-trigger', null),
381 match = t && t.className.match(me.triggerIndexRe),
385 if (match && !me.readOnly) {
386 idx = parseInt(match[1], 10);
387 triggerClickMethod = me['onTrigger' + (idx + 1) + 'Click'] || me.onTriggerClick;
388 if (triggerClickMethod) {
389 triggerClickMethod.call(me, e);
394 <span id='Ext-form.field.Trigger-method-onTriggerClick'> /**
395 </span> * The function that should handle the trigger's click event. This method does nothing by default
396 * until overridden by an implementing function. See Ext.form.field.ComboBox and Ext.form.field.Date for
397 * sample implementations.
399 * @param {Ext.EventObject} e
401 onTriggerClick: Ext.emptyFn
403 <span id='Ext-form.field.Trigger-cfg-grow'> /**
404 </span> * @cfg {Boolean} grow @hide
406 <span id='Ext-form.field.Trigger-cfg-growMin'> /**
407 </span> * @cfg {Number} growMin @hide
409 <span id='Ext-form.field.Trigger-cfg-growMax'> /**
410 </span> * @cfg {Number} growMax @hide
413 </pre></pre></body></html>