/* * Based on: * Ext JS Library 1.1.1 * Copyright(c) 2006-2007, Ext JS, LLC. * * Originally Released Under LGPL - original licence link has changed is not relivant. * * Fork - LGPL * <script type="text/javascript"> */ /** * @class Roo.form.TriggerField * @extends Roo.form.TextField * Provides a convenient wrapper for TextFields that adds a clickable trigger button (looks like a combobox by default). * The trigger has no default action, so you must assign a function to implement the trigger click handler by * overriding {@link #onTriggerClick}. You can create a TriggerField directly, as it renders exactly like a combobox * for which you can provide a custom implementation. For example: * <pre><code> var trigger = new Roo.form.TriggerField(); trigger.onTriggerClick = myTriggerFn; trigger.applyTo('my-field'); </code></pre> * * However, in general you will most likely want to use TriggerField as the base class for a reusable component. * {@link Roo.form.DateField} and {@link Roo.form.ComboBox} are perfect examples of this. * @cfg {String} triggerClass An additional CSS class used to style the trigger button. The trigger will always get the * class 'x-form-trigger' by default and triggerClass will be <b>appended</b> if specified. * @constructor * Create a new TriggerField. * @param {Object} config Configuration options (valid {@Roo.form.TextField} config options will also be applied * to the base TextField) */ Roo.form.TriggerField = function(config){ this.mimicing = false; Roo.form.TriggerField.superclass.constructor.call(this, config); }; Roo.extend(Roo.form.TriggerField, Roo.form.TextField, { /** * @cfg {String} triggerClass A CSS class to apply to the trigger */ /** * @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to * {tag: "input", type: "text", size: "16", autocomplete: "off"}) */ defaultAutoCreate : {tag: "input", type: "text", size: "16", autocomplete: "new-password"}, /** * @cfg {Boolean} hideTrigger True to hide the trigger element and display only the base text field (defaults to false) */ hideTrigger:false, /** @cfg {Boolean} grow @hide */ /** @cfg {Number} growMin @hide */ /** @cfg {Number} growMax @hide */ /** * @hide * @method */ autoSize: Roo.emptyFn, // private monitorTab : true, // private deferHeight : true, actionMode : 'wrap', // private onResize : function(w, h){ Roo.form.TriggerField.superclass.onResize.apply(this, arguments); if(typeof w == 'number'){ var x = w - this.trigger.getWidth(); this.el.setWidth(this.adjustWidth('input', x)); this.trigger.setStyle('left', x+'px'); } }, // private adjustSize : Roo.BoxComponent.prototype.adjustSize, // private getResizeEl : function(){ return this.wrap; }, // private getPositionEl : function(){ return this.wrap; }, // private alignErrorIcon : function(){ this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]); }, // private onRender : function(ct, position){ Roo.form.TriggerField.superclass.onRender.call(this, ct, position); this.wrap = this.el.wrap({cls: "x-form-field-wrap"}); this.trigger = this.wrap.createChild(this.triggerConfig || {tag: "img", src: Roo.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.triggerClass}); if(this.hideTrigger){ this.trigger.setDisplayed(false); } this.initTrigger(); if(!this.width){ this.wrap.setWidth(this.el.getWidth()+this.trigger.getWidth()); } }, // private initTrigger : function(){ this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true}); this.trigger.addClassOnOver('x-form-trigger-over'); this.trigger.addClassOnClick('x-form-trigger-click'); }, // private onDestroy : function(){ if(this.trigger){ this.trigger.removeAllListeners(); this.trigger.remove(); } if(this.wrap){ this.wrap.remove(); } Roo.form.TriggerField.superclass.onDestroy.call(this); }, // private onFocus : function(){ Roo.form.TriggerField.superclass.onFocus.call(this); if(!this.mimicing){ this.wrap.addClass('x-trigger-wrap-focus'); this.mimicing = true; Roo.get(Roo.isIE ? document.body : document).on("mousedown", this.mimicBlur, this); if(this.monitorTab){ this.el.on("keydown", this.checkTab, this); } } }, // private checkTab : function(e){ if(e.getKey() == e.TAB){ this.triggerBlur(); } }, // private onBlur : function(){ // do nothing }, // private mimicBlur : function(e, t){ if(!this.wrap.contains(t) && this.validateBlur()){ this.triggerBlur(); } }, // private triggerBlur : function(){ this.mimicing = false; Roo.get(Roo.isIE ? document.body : document).un("mousedown", this.mimicBlur); if(this.monitorTab){ this.el.un("keydown", this.checkTab, this); } this.wrap.removeClass('x-trigger-wrap-focus'); Roo.form.TriggerField.superclass.onBlur.call(this); }, // private // This should be overriden by any subclass that needs to check whether or not the field can be blurred. validateBlur : function(e, t){ return true; }, // private onDisable : function(){ Roo.form.TriggerField.superclass.onDisable.call(this); if(this.wrap){ this.wrap.addClass('x-item-disabled'); } }, // private onEnable : function(){ Roo.form.TriggerField.superclass.onEnable.call(this); if(this.wrap){ this.wrap.removeClass('x-item-disabled'); } }, // private onShow : function(){ var ae = this.getActionEl(); if(ae){ ae.dom.style.display = ''; ae.dom.style.visibility = 'visible'; } }, // private onHide : function(){ var ae = this.getActionEl(); ae.dom.style.display = 'none'; }, /** * The function that should handle the trigger's click event. This method does nothing by default until overridden * by an implementing function. * @method * @param {EventObject} e */ onTriggerClick : Roo.emptyFn }); // TwinTriggerField is not a public class to be used directly. It is meant as an abstract base class // to be extended by an implementing class. For an example of implementing this class, see the custom // SearchField implementation here: http://extjs.com/deploy/ext/examples/form/custom.html Roo.form.TwinTriggerField = Roo.extend(Roo.form.TriggerField, { initComponent : function(){ Roo.form.TwinTriggerField.superclass.initComponent.call(this); this.triggerConfig = { tag:'span', cls:'x-form-twin-triggers', cn:[ {tag: "img", src: Roo.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger1Class}, {tag: "img", src: Roo.BLANK_IMAGE_URL, cls: "x-form-trigger " + this.trigger2Class} ]}; }, getTrigger : function(index){ return this.triggers[index]; }, initTrigger : function(){ var ts = this.trigger.select('.x-form-trigger', true); this.wrap.setStyle('overflow', 'hidden'); var triggerField = this; ts.each(function(t, all, index){ t.hide = function(){ var w = triggerField.wrap.getWidth(); this.dom.style.display = 'none'; triggerField.el.setWidth(w-triggerField.trigger.getWidth()); }; t.show = function(){ var w = triggerField.wrap.getWidth(); this.dom.style.display = ''; triggerField.el.setWidth(w-triggerField.trigger.getWidth()); }; var triggerIndex = 'Trigger'+(index+1); if(this['hide'+triggerIndex]){ t.dom.style.display = 'none'; } t.on("click", this['on'+triggerIndex+'Click'], this, {preventDefault:true}); t.addClassOnOver('x-form-trigger-over'); t.addClassOnClick('x-form-trigger-click'); }, this); this.triggers = ts.elements; }, onTrigger1Click : Roo.emptyFn, onTrigger2Click : Roo.emptyFn });