4 * trigger field - base class for combo..
9 * @class Roo.bootstrap.TriggerField
10 * @extends Roo.bootstrap.Input
11 * Provides a convenient wrapper for TextFields that adds a clickable trigger button (looks like a combobox by default).
12 * The trigger has no default action, so you must assign a function to implement the trigger click handler by
13 * overriding {@link #onTriggerClick}. You can create a TriggerField directly, as it renders exactly like a combobox
14 * for which you can provide a custom implementation. For example:
16 var trigger = new Roo.bootstrap.TriggerField();
17 trigger.onTriggerClick = myTriggerFn;
18 trigger.applyTo('my-field');
21 * However, in general you will most likely want to use TriggerField as the base class for a reusable component.
22 * {@link Roo.bootstrap.DateField} and {@link Roo.bootstrap.ComboBox} are perfect examples of this.
23 * @cfg {String} triggerClass An additional CSS class used to style the trigger button. The trigger will always get the
24 * class 'x-form-trigger' by default and triggerClass will be <b>appended</b> if specified.
25 * @cfg {String} caret (search|calendar) a fontawesome for the trigger icon see http://fortawesome.github.io/Font-Awesome/icons/
28 * Create a new TriggerField.
29 * @param {Object} config Configuration options (valid {@Roo.bootstrap.Input} config options will also be applied
30 * to the base TextField)
32 Roo.bootstrap.TriggerField = function(config){
33 this.mimicing = false;
34 Roo.bootstrap.TriggerField.superclass.constructor.call(this, config);
37 Roo.extend(Roo.bootstrap.TriggerField, Roo.bootstrap.Input, {
39 * @cfg {String} triggerClass A CSS class to apply to the trigger
42 * @cfg {Boolean} hideTrigger True to hide the trigger element and display only the base text field (defaults to false)
47 * @cfg {Boolean} removable (true|false) special filter default false
51 /** @cfg {Boolean} grow @hide */
52 /** @cfg {Number} growMin @hide */
53 /** @cfg {Number} growMax @hide */
59 autoSize: Roo.emptyFn,
71 getAutoCreate : function(){
73 var align = this.labelAlign || this.parentLabelAlign();
78 cls: 'form-group' //input-group
85 type : this.inputType,
87 autocomplete: 'new-password',
88 placeholder : this.placeholder || ''
92 input.name = this.name;
95 input.cls += ' input-' + this.size;
102 var inputblock = input;
104 if(this.hasFeedback && !this.allowBlank){
108 cls: 'glyphicon form-control-feedback'
111 if(this.removable && !this.editable && !this.tickable){
113 cls : 'has-feedback',
119 cls : 'roo-combo-removable-btn close'
126 cls : 'has-feedback',
135 if(this.removable && !this.editable && !this.tickable){
137 cls : 'roo-removable',
143 cls : 'roo-combo-removable-btn close'
150 if (this.before || this.after) {
159 cls : 'input-group-addon input-group-prepend input-group-text',
164 inputblock.cn.push(input);
166 if(this.hasFeedback && !this.allowBlank){
167 inputblock.cls += ' has-feedback';
168 inputblock.cn.push(feedback);
174 cls : 'input-group-addon input-group-append input-group-text',
183 var ibwrap = inputblock;
188 cls: 'roo-select2-choices',
192 cls: 'roo-select2-search-field',
204 cls: 'roo-select2-container input-group',
209 cls: 'form-hidden-field'
215 if(!this.multiple && this.showToggleBtn){
221 if (this.caret != false) {
224 cls: 'fa fa-' + this.caret
231 cls : 'input-group-addon input-group-append input-group-text btn' +
232 (Roo.bootstrap.version == 3 ? ' dropdown-toggle' : ''),
237 cls: 'combobox-clear',
251 combobox.cls += ' roo-select2-container-multi';
255 cls : 'roo-required-indicator ' + (this.indicatorpos == 'right' ? 'right' : 'left') +'-indicator text-danger fa fa-lg fa-star',
256 tooltip : 'This field is required'
258 if (Roo.bootstrap.version == 4) {
261 style : 'display:none'
266 if (align ==='left' && this.fieldLabel.length) {
268 cfg.cls += ' roo-form-group-label-left' + (Roo.bootstrap.version == 4 ? ' row' : '');
275 cls : 'control-label',
276 html : this.fieldLabel
288 var labelCfg = cfg.cn[1];
289 var contentCfg = cfg.cn[2];
291 if(this.indicatorpos == 'right'){
296 cls : 'control-label',
300 html : this.fieldLabel
314 labelCfg = cfg.cn[0];
315 contentCfg = cfg.cn[1];
318 if(this.labelWidth > 12){
319 labelCfg.style = "width: " + this.labelWidth + 'px';
322 if(this.labelWidth < 13 && this.labelmd == 0){
323 this.labelmd = this.labelWidth;
326 if(this.labellg > 0){
327 labelCfg.cls += ' col-lg-' + this.labellg;
328 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
331 if(this.labelmd > 0){
332 labelCfg.cls += ' col-md-' + this.labelmd;
333 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
336 if(this.labelsm > 0){
337 labelCfg.cls += ' col-sm-' + this.labelsm;
338 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
341 if(this.labelxs > 0){
342 labelCfg.cls += ' col-xs-' + this.labelxs;
343 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
346 } else if ( this.fieldLabel.length) {
347 // Roo.log(" label");
352 //cls : 'input-group-addon',
353 html : this.fieldLabel
361 if(this.indicatorpos == 'right'){
369 html : this.fieldLabel
383 // Roo.log(" no label && no align");
390 ['xs','sm','md','lg'].map(function(size){
391 if (settings[size]) {
392 cfg.cls += ' col-' + size + '-' + settings[size];
403 onResize : function(w, h){
404 // Roo.bootstrap.TriggerField.superclass.onResize.apply(this, arguments);
405 // if(typeof w == 'number'){
406 // var x = w - this.trigger.getWidth();
407 // this.inputEl().setWidth(this.adjustWidth('input', x));
408 // this.trigger.setStyle('left', x+'px');
413 adjustSize : Roo.BoxComponent.prototype.adjustSize,
416 getResizeEl : function(){
417 return this.inputEl();
421 getPositionEl : function(){
422 return this.inputEl();
426 alignErrorIcon : function(){
427 this.errorIcon.alignTo(this.inputEl(), 'tl-tr', [2, 0]);
431 initEvents : function(){
435 Roo.bootstrap.TriggerField.superclass.initEvents.call(this);
436 //this.wrap = this.el.wrap({cls: "x-form-field-wrap"});
437 if(!this.multiple && this.showToggleBtn){
438 this.trigger = this.el.select('span.input-group-append',true).first();
439 if(this.hideTrigger){
440 this.trigger.setDisplayed(false);
442 this.trigger.on("click", this.onTriggerClick, this, {preventDefault:true});
446 this.inputEl().on("click", this.onTriggerClick, this, {preventDefault:true});
449 if(this.removable && !this.editable && !this.tickable){
450 var close = this.closeTriggerEl();
453 close.setVisibilityMode(Roo.Element.DISPLAY).hide();
454 close.on('click', this.removeBtnClick, this, close);
458 //this.trigger.addClassOnOver('x-form-trigger-over');
459 //this.trigger.addClassOnClick('x-form-trigger-click');
462 // this.wrap.setWidth(this.el.getWidth()+this.trigger.getWidth());
466 closeTriggerEl : function()
468 var close = this.el.select('.roo-combo-removable-btn', true).first();
469 return close ? close : false;
472 removeBtnClick : function(e, h, el)
476 if(this.fireEvent("remove", this) !== false){
478 this.fireEvent("afterremove", this)
482 createList : function()
484 this.list = Roo.get(document.body).createChild({
485 tag: Roo.bootstrap.version == 4 ? 'div' : 'ul',
486 cls: 'typeahead typeahead-long dropdown-menu',
487 style: 'display:none'
490 this.list.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';;
495 initTrigger : function(){
500 onDestroy : function(){
502 this.trigger.removeAllListeners();
503 // this.trigger.remove();
506 // this.wrap.remove();
508 Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
512 onFocus : function(){
513 Roo.bootstrap.TriggerField.superclass.onFocus.call(this);
516 this.wrap.addClass('x-trigger-wrap-focus');
517 this.mimicing = true;
518 Roo.get(Roo.isIE ? document.body : document).on("mousedown", this.mimicBlur, this);
520 this.el.on("keydown", this.checkTab, this);
527 checkTab : function(e){
528 if(e.getKey() == e.TAB){
539 mimicBlur : function(e, t){
541 if(!this.wrap.contains(t) && this.validateBlur()){
548 triggerBlur : function(){
549 this.mimicing = false;
550 Roo.get(Roo.isIE ? document.body : document).un("mousedown", this.mimicBlur);
552 this.el.un("keydown", this.checkTab, this);
554 //this.wrap.removeClass('x-trigger-wrap-focus');
555 Roo.bootstrap.TriggerField.superclass.onBlur.call(this);
559 // This should be overriden by any subclass that needs to check whether or not the field can be blurred.
560 validateBlur : function(e, t){
565 onDisable : function(){
566 this.inputEl().dom.disabled = true;
567 //Roo.bootstrap.TriggerField.superclass.onDisable.call(this);
569 // this.wrap.addClass('x-item-disabled');
574 onEnable : function(){
575 this.inputEl().dom.disabled = false;
576 //Roo.bootstrap.TriggerField.superclass.onEnable.call(this);
578 // this.el.removeClass('x-item-disabled');
584 var ae = this.getActionEl();
587 ae.dom.style.display = '';
588 ae.dom.style.visibility = 'visible';
595 var ae = this.getActionEl();
596 ae.dom.style.display = 'none';
600 * The function that should handle the trigger's click event. This method does nothing by default until overridden
601 * by an implementing function.
603 * @param {EventObject} e
605 onTriggerClick : Roo.emptyFn