set dynamic max height for bootstrap combo box
[roojs1] / Roo / bootstrap / form / ComboBox.js
1 /*
2  * - LGPL
3  * * 
4  */
5
6 /**
7  * @class Roo.bootstrap.form.ComboBox
8  * @extends Roo.bootstrap.form.TriggerField
9  * A combobox control with support for autocomplete, remote-loading, paging and many other features.
10  * @cfg {Boolean} append (true|false) default false
11  * @cfg {Boolean} autoFocus (true|false) auto focus the first item, default true
12  * @cfg {Boolean} tickable ComboBox with tickable selections (true|false), default false
13  * @cfg {Boolean} triggerList trigger show the list or not (true|false) default true
14  * @cfg {Boolean} showToggleBtn show toggle button or not (true|false) default true
15  * @cfg {String} btnPosition set the position of the trigger button (left | right) default right
16  * @cfg {Boolean} animate default true
17  * @cfg {Boolean} emptyResultText only for touch device
18  * @cfg {String} triggerText multiple combobox trigger button text default 'Select'
19  * @cfg {String} emptyTitle default ''
20  * @cfg {Number} width fixed with? experimental
21  * @constructor
22  * Create a new ComboBox.
23  * @param {Object} config Configuration options
24  */
25 Roo.bootstrap.form.ComboBox = function(config){
26     Roo.bootstrap.form.ComboBox.superclass.constructor.call(this, config);
27     this.addEvents({
28         /**
29          * @event expand
30          * Fires when the dropdown list is expanded
31         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
32         */
33         'expand' : true,
34         /**
35          * @event collapse
36          * Fires when the dropdown list is collapsed
37         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
38         */
39         'collapse' : true,
40         /**
41          * @event beforeselect
42          * Fires before a list item is selected. Return false to cancel the selection.
43         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
44         * @param {Roo.data.Record} record The data record returned from the underlying store
45         * @param {Number} index The index of the selected item in the dropdown list
46         */
47         'beforeselect' : true,
48         /**
49          * @event select
50          * Fires when a list item is selected
51         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
52         * @param {Roo.data.Record} record The data record returned from the underlying store (or false on clear)
53         * @param {Number} index The index of the selected item in the dropdown list
54         */
55         'select' : true,
56         /**
57          * @event beforequery
58          * Fires before all queries are processed. Return false to cancel the query or set cancel to true.
59          * The event object passed has these properties:
60         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
61         * @param {String} query The query
62         * @param {Boolean} forceAll true to force "all" query
63         * @param {Boolean} cancel true to cancel the query
64         * @param {Object} e The query event object
65         */
66         'beforequery': true,
67          /**
68          * @event add
69          * Fires when the 'add' icon is pressed (add a listener to enable add button)
70         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
71         */
72         'add' : true,
73         /**
74          * @event edit
75          * Fires when the 'edit' icon is pressed (add a listener to enable add button)
76         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
77         * @param {Roo.data.Record|false} record The data record returned from the underlying store (or false on nothing selected)
78         */
79         'edit' : true,
80         /**
81          * @event remove
82          * Fires when the remove value from the combobox array
83         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
84         */
85         'remove' : true,
86         /**
87          * @event afterremove
88          * Fires when the remove value from the combobox array
89         * @param {Roo.bootstrap.form.ComboBox} combo This combo box
90         */
91         'afterremove' : true,
92         /**
93          * @event specialfilter
94          * Fires when specialfilter
95             * @param {Roo.bootstrap.form.ComboBox} combo This combo box
96             */
97         'specialfilter' : true,
98         /**
99          * @event tick
100          * Fires when tick the element
101             * @param {Roo.bootstrap.form.ComboBox} combo This combo box
102             */
103         'tick' : true,
104         /**
105          * @event touchviewdisplay
106          * Fires when touch view require special display (default is using displayField)
107             * @param {Roo.bootstrap.form.ComboBox} combo This combo box
108             * @param {Object} cfg set html .
109             */
110         'touchviewdisplay' : true
111         
112     });
113     
114     this.item = [];
115     this.tickItems = [];
116     
117     this.selectedIndex = -1;
118     if(this.mode == 'local'){
119         if(config.queryDelay === undefined){
120             this.queryDelay = 10;
121         }
122         if(config.minChars === undefined){
123             this.minChars = 0;
124         }
125     }
126 };
127
128 Roo.extend(Roo.bootstrap.form.ComboBox, Roo.bootstrap.form.TriggerField, {
129      
130     /**
131      * @cfg {Boolean} lazyRender True to prevent the ComboBox from rendering until requested (should always be used when
132      * rendering into an Roo.Editor, defaults to false)
133      */
134     /**
135      * @cfg {Boolean/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to:
136      * {tag: "input", type: "text", size: "24", autocomplete: "off"})
137      */
138     /**
139      * @cfg {Roo.data.Store} store The data store to which this combo is bound (defaults to undefined)
140      */
141     /**
142      * @cfg {String} title If supplied, a header element is created containing this text and added into the top of
143      * the dropdown list (defaults to undefined, with no header element)
144      */
145
146      /**
147      * @cfg {String/Roo.Template} tpl The template to use to render the output default is  '<a class="dropdown-item" href="#">{' + this.displayField + '}</a>' 
148      */
149      
150      /**
151      * @cfg {Number} listWidth The width in pixels of the dropdown list (defaults to the width of the ComboBox field)
152      */
153     listWidth: undefined,
154     /**
155      * @cfg {String} displayField The underlying data field name to bind to this CombBox (defaults to undefined if
156      * mode = 'remote' or 'text' if mode = 'local')
157      */
158     displayField: undefined,
159     
160     /**
161      * @cfg {String} valueField The underlying data value name to bind to this CombBox (defaults to undefined if
162      * mode = 'remote' or 'value' if mode = 'local'). 
163      * Note: use of a valueField requires the user make a selection
164      * in order for a value to be mapped.
165      */
166     valueField: undefined,
167     /**
168      * @cfg {String} modalTitle The title of the dialog that pops up on mobile views.
169      */
170     modalTitle : '',
171     
172     /**
173      * @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
174      * field's data value (defaults to the underlying DOM element's name)
175      */
176     hiddenName: undefined,
177     /**
178      * @cfg {String} listClass CSS class to apply to the dropdown list element (defaults to '')
179      */
180     listClass: '',
181     /**
182      * @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list (defaults to 'x-combo-selected')
183      */
184     selectedClass: 'active',
185     
186     /**
187      * @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop" for bottom-right
188      */
189     shadow:'sides',
190     /**
191      * @cfg {String} listAlign A valid anchor position value. See {@link Roo.Element#alignTo} for details on supported
192      * anchor positions (defaults to 'tl-bl')
193      */
194     listAlign: 'tl-bl?',
195     /**
196      * @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown (defaults to 300)
197      */
198     // maxHeight: 300, // not used (change maxHeight in CSS. target the list using listClass)
199     /**
200      * @cfg {String} triggerAction The action to execute when the trigger field is activated.  Use 'all' to run the
201      * query specified by the allQuery config option (defaults to 'query')
202      */
203     triggerAction: 'query',
204     /**
205      * @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and typeahead activate
206      * (defaults to 4, does not apply if editable = false)
207      */
208     minChars : 4,
209     /**
210      * @cfg {Boolean} typeAhead True to populate and autoselect the remainder of the text being typed after a configurable
211      * delay (typeAheadDelay) if it matches a known value (defaults to false)
212      */
213     typeAhead: false,
214     /**
215      * @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and sending the
216      * query to filter the dropdown list (defaults to 500 if mode = 'remote' or 10 if mode = 'local')
217      */
218     queryDelay: 500,
219     /**
220      * @cfg {Number} pageSize If greater than 0, a paging toolbar is displayed in the footer of the dropdown list and the
221      * filter queries will execute with page start and limit parameters.  Only applies when mode = 'remote' (defaults to 0)
222      */
223     pageSize: 0,
224     /**
225      * @cfg {Boolean} selectOnFocus True to select any existing text in the field immediately on focus.  Only applies
226      * when editable = true (defaults to false)
227      */
228     selectOnFocus:false,
229     /**
230      * @cfg {String} queryParam Name of the query as it will be passed on the querystring (defaults to 'query')
231      */
232     queryParam: 'query',
233     /**
234      * @cfg {String} loadingText The text to display in the dropdown list while data is loading.  Only applies
235      * when mode = 'remote' (defaults to 'Loading...')
236      */
237     loadingText: 'Loading...',
238     /**
239      * @cfg {Boolean} resizable True to add a resize handle to the bottom of the dropdown list (defaults to false)
240      */
241     resizable: false,
242     /**
243      * @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if resizable = true (defaults to 8)
244      */
245     handleHeight : 8,
246     /**
247      * @cfg {Boolean} editable False to prevent the user from typing text directly into the field, just like a
248      * traditional select (defaults to true)
249      */
250     editable: true,
251     /**
252      * @cfg {String} allQuery The text query to send to the server to return all records for the list with no filtering (defaults to '')
253      */
254     allQuery: '',
255     /**
256      * @cfg {String} mode Set to 'local' if the ComboBox loads local data (defaults to 'remote' which loads from the server)
257      */
258     mode: 'remote',
259     /**
260      * @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to 70, will be ignored if
261      * listWidth has a higher value)
262      */
263     minListWidth : 70,
264     /**
265      * @cfg {Boolean} forceSelection True to restrict the selected value to one of the values in the list, false to
266      * allow the user to set arbitrary text into the field (defaults to false)
267      */
268     forceSelection:false,
269     /**
270      * @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
271      * if typeAhead = true (defaults to 250)
272      */
273     typeAheadDelay : 250,
274     /**
275      * @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
276      * the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined)
277      */
278     valueNotFoundText : undefined,
279     /**
280      * @cfg {Boolean} blockFocus Prevents all focus calls, so it can work with things like HTML edtor bar
281      */
282     blockFocus : false,
283     
284     /**
285      * @cfg {Boolean} disableClear Disable showing of clear button.
286      */
287     disableClear : false,
288     /**
289      * @cfg {Boolean} alwaysQuery  Disable caching of results, and always send query
290      */
291     alwaysQuery : false,
292     
293     /**
294      * @cfg {Boolean} multiple  (true|false) ComboBobArray, default false
295      */
296     multiple : false,
297     
298     /**
299      * @cfg {String} invalidClass DEPRICATED - uses BS4 is-valid now
300      */
301     invalidClass : "has-warning",
302     
303     /**
304      * @cfg {String} validClass DEPRICATED - uses BS4 is-valid now
305      */
306     validClass : "has-success",
307     
308     /**
309      * @cfg {Boolean} specialFilter (true|false) special filter default false
310      */
311     specialFilter : false,
312     
313     /**
314      * @cfg {Boolean} mobileTouchView (true|false) show mobile touch view when using a mobile default true
315      */
316     mobileTouchView : true,
317     
318     /**
319      * @cfg {Boolean} useNativeIOS (true|false) render it as classic select for ios, not support dynamic load data (default false)
320      */
321     useNativeIOS : false,
322     
323     /**
324      * @cfg {Boolean} mobile_restrict_height (true|false) restrict height for touch view
325      */
326     mobile_restrict_height : false,
327     
328     ios_options : false,
329     
330     //private
331     addicon : false,
332     editicon: false,
333     
334     page: 0,
335     hasQuery: false,
336     append: false,
337     loadNext: false,
338     autoFocus : true,
339     tickable : false,
340     btnPosition : 'right',
341     triggerList : true,
342     showToggleBtn : true,
343     animate : true,
344     emptyResultText: 'Empty',
345     triggerText : 'Select',
346     emptyTitle : '',
347     width : false,
348     
349     // element that contains real text value.. (when hidden is used..)
350     
351     getAutoCreate : function()
352     {   
353         var cfg = false;
354         //render
355         /*
356          * Render classic select for iso
357          */
358         
359         if(Roo.isIOS && this.useNativeIOS){
360             cfg = this.getAutoCreateNativeIOS();
361             return cfg;
362         }
363         
364         /*
365          * Touch Devices
366          */
367         
368         if(Roo.isTouch && this.mobileTouchView){
369             cfg = this.getAutoCreateTouchView();
370             return cfg;;
371         }
372         
373         /*
374          *  Normal ComboBox
375          */
376         if(!this.tickable){
377             cfg = Roo.bootstrap.form.ComboBox.superclass.getAutoCreate.call(this);
378             return cfg;
379         }
380         
381         /*
382          *  ComboBox with tickable selections
383          */
384              
385         var align = this.labelAlign || this.parentLabelAlign();
386         
387         cfg = {
388             cls : 'form-group roo-combobox-tickable' //input-group
389         };
390         
391         var btn_text_select = '';
392         var btn_text_done = '';
393         var btn_text_cancel = '';
394         
395         if (this.btn_text_show) {
396             btn_text_select = 'Select';
397             btn_text_done = 'Done';
398             btn_text_cancel = 'Cancel'; 
399         }
400         
401         var buttons = {
402             tag : 'div',
403             cls : 'tickable-buttons',
404             cn : [
405                 {
406                     tag : 'button',
407                     type : 'button',
408                     cls : 'btn btn-link btn-edit pull-' + this.btnPosition,
409                     //html : this.triggerText
410                     html: btn_text_select
411                 },
412                 {
413                     tag : 'button',
414                     type : 'button',
415                     name : 'ok',
416                     cls : 'btn btn-link btn-ok pull-' + this.btnPosition,
417                     //html : 'Done'
418                     html: btn_text_done
419                 },
420                 {
421                     tag : 'button',
422                     type : 'button',
423                     name : 'cancel',
424                     cls : 'btn btn-link btn-cancel pull-' + this.btnPosition,
425                     //html : 'Cancel'
426                     html: btn_text_cancel
427                 }
428             ]
429         };
430         
431         if(this.editable){
432             buttons.cn.unshift({
433                 tag: 'input',
434                 cls: 'roo-select2-search-field-input'
435             });
436         }
437         
438         var _this = this;
439         
440         Roo.each(buttons.cn, function(c){
441             if (_this.size) {
442                 c.cls += ' btn-' + _this.size;
443             }
444
445             if (_this.disabled) {
446                 c.disabled = true;
447             }
448         });
449         
450         var box = {
451             tag: 'div',
452             style : 'display: contents',
453             cn: [
454                 {
455                     tag: 'input',
456                     type : 'hidden',
457                     cls: 'form-hidden-field'
458                 },
459                 {
460                     tag: 'ul',
461                     cls: 'roo-select2-choices',
462                     cn:[
463                         {
464                             tag: 'li',
465                             cls: 'roo-select2-search-field',
466                             cn: [
467                                 buttons
468                             ]
469                         }
470                     ]
471                 }
472             ]
473         };
474         
475         var combobox = {
476             cls: 'roo-select2-container input-group roo-select2-container-multi',
477             cn: [
478                 
479                 box
480 //                {
481 //                    tag: 'ul',
482 //                    cls: 'typeahead typeahead-long dropdown-menu',
483 //                    style: 'display:none; max-height:' + this.maxHeight + 'px;'
484 //                }
485             ]
486         };
487         
488         if(this.hasFeedback && !this.allowBlank){
489             
490             var feedback = {
491                 tag: 'span',
492                 cls: 'glyphicon form-control-feedback'
493             };
494
495             combobox.cn.push(feedback);
496         }
497         
498         
499         
500         var indicator = {
501             tag : 'i',
502             cls : 'roo-required-indicator ' + (this.indicatorpos == 'right'  ? 'right' : 'left') +'-indicator text-danger fa fa-lg fa-star',
503             tooltip : 'This field is required'
504         };
505          
506         if (this.allowBlank) {
507             indicator = {
508                 tag : 'i',
509                 style : 'display:none'
510             };
511         } 
512         if (align ==='left' && this.fieldLabel.length) {
513             
514             cfg.cls += ' roo-form-group-label-left'  + (Roo.bootstrap.version == 4 ? ' row' : '');
515             
516             cfg.cn = [
517                 indicator,
518                 {
519                     tag: 'label',
520                     'for' :  id,
521                     cls : 'control-label col-form-label',
522                     html : this.fieldLabel
523
524                 },
525                 {
526                     cls : "", 
527                     cn: [
528                         combobox
529                     ]
530                 }
531
532             ];
533             
534             var labelCfg = cfg.cn[1];
535             var contentCfg = cfg.cn[2];
536             
537
538             if(this.indicatorpos == 'right'){
539                 
540                 cfg.cn = [
541                     {
542                         tag: 'label',
543                         'for' :  id,
544                         cls : 'control-label col-form-label',
545                         cn : [
546                             {
547                                 tag : 'span',
548                                 html : this.fieldLabel
549                             },
550                             indicator
551                         ]
552                     },
553                     {
554                         cls : "",
555                         cn: [
556                             combobox
557                         ]
558                     }
559
560                 ];
561                 
562                 
563                 
564                 labelCfg = cfg.cn[0];
565                 contentCfg = cfg.cn[1];
566             
567             }
568             
569             if(this.labelWidth > 12){
570                 labelCfg.style = "width: " + this.labelWidth + 'px';
571             }
572             if(this.width * 1 > 0){
573                 contentCfg.style = "width: " + this.width + 'px';
574             }
575             if(this.labelWidth < 13 && this.labelmd == 0){
576                 this.labelmd = this.labelWidth;
577             }
578             
579             if(this.labellg > 0){
580                 labelCfg.cls += ' col-lg-' + this.labellg;
581                 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
582             }
583             
584             if(this.labelmd > 0){
585                 labelCfg.cls += ' col-md-' + this.labelmd;
586                 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
587             }
588             
589             if(this.labelsm > 0){
590                 labelCfg.cls += ' col-sm-' + this.labelsm;
591                 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
592             }
593             
594             if(this.labelxs > 0){
595                 labelCfg.cls += ' col-xs-' + this.labelxs;
596                 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
597             }
598                 
599                 
600         } else if ( this.fieldLabel.length) {
601 //                Roo.log(" label");
602                  cfg.cn = [
603                    indicator,
604                     {
605                         tag: 'label',
606                         //cls : 'input-group-addon',
607                         html : this.fieldLabel
608                     },
609                     combobox
610                 ];
611                 
612                 if(this.indicatorpos == 'right'){
613                     cfg.cn = [
614                         {
615                             tag: 'label',
616                             //cls : 'input-group-addon',
617                             html : this.fieldLabel
618                         },
619                         indicator,
620                         combobox
621                     ];
622                     
623                 }
624
625         } else {
626             
627 //                Roo.log(" no label && no align");
628                 cfg = combobox
629                      
630                 
631         }
632          
633         var settings=this;
634         ['xs','sm','md','lg'].map(function(size){
635             if (settings[size]) {
636                 cfg.cls += ' col-' + size + '-' + settings[size];
637             }
638         });
639         
640         return cfg;
641         
642     },
643     
644     _initEventsCalled : false,
645     
646     // private
647     initEvents: function()
648     {   
649         if (this._initEventsCalled) { // as we call render... prevent looping...
650             return;
651         }
652         this._initEventsCalled = true;
653         
654         if (!this.store) {
655             throw "can not find store for combo";
656         }
657         
658         this.indicator = this.indicatorEl();
659         
660         this.store = Roo.factory(this.store, Roo.data);
661         this.store.parent = this;
662         
663         // if we are building from html. then this element is so complex, that we can not really
664         // use the rendered HTML.
665         // so we have to trash and replace the previous code.
666         if (Roo.XComponent.build_from_html) {
667             // remove this element....
668             var e = this.el.dom, k=0;
669             while (e ) { e = e.previousSibling;  ++k;}
670
671             this.el.remove();
672             
673             this.el=false;
674             this.rendered = false;
675             
676             this.render(this.parent().getChildContainer(true), k);
677         }
678         
679         if(Roo.isIOS && this.useNativeIOS){
680             this.initIOSView();
681             return;
682         }
683         
684         /*
685          * Touch Devices
686          */
687         
688         if(Roo.isTouch && this.mobileTouchView){
689             this.initTouchView();
690             return;
691         }
692         
693         if(this.tickable){
694             this.initTickableEvents();
695             return;
696         }
697         
698         Roo.bootstrap.form.ComboBox.superclass.initEvents.call(this);
699         
700         if(this.hiddenName){
701             
702             this.hiddenField = this.el.select('input.form-hidden-field',true).first();
703             
704             this.hiddenField.dom.value =
705                 this.hiddenValue !== undefined ? this.hiddenValue :
706                 this.value !== undefined ? this.value : '';
707
708             // prevent input submission
709             this.el.dom.removeAttribute('name');
710             this.hiddenField.dom.setAttribute('name', this.hiddenName);
711              
712              
713         }
714         //if(Roo.isGecko){
715         //    this.el.dom.setAttribute('autocomplete', 'off');
716         //}
717         
718         var cls = 'x-combo-list';
719         
720         //this.list = new Roo.Layer({
721         //    shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
722         //});
723
724         this.list.addClass(this.listClass);
725         
726         var _this = this;
727         
728         (function(){
729             var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
730             _this.list.setWidth(lw);
731         }).defer(100);
732         
733         this.list.on('mouseover', this.onViewOver, this);
734         this.list.on('mousemove', this.onViewMove, this);
735         this.list.on('scroll', this.onViewScroll, this);
736         
737         /*
738         this.list.swallowEvent('mousewheel');
739         this.assetHeight = 0;
740
741         if(this.title){
742             this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
743             this.assetHeight += this.header.getHeight();
744         }
745
746         this.innerList = this.list.createChild({cls:cls+'-inner'});
747         this.innerList.on('mouseover', this.onViewOver, this);
748         this.innerList.on('mousemove', this.onViewMove, this);
749         this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
750         
751         if(this.allowBlank && !this.pageSize && !this.disableClear){
752             this.footer = this.list.createChild({cls:cls+'-ft'});
753             this.pageTb = new Roo.Toolbar(this.footer);
754            
755         }
756         if(this.pageSize){
757             this.footer = this.list.createChild({cls:cls+'-ft'});
758             this.pageTb = new Roo.PagingToolbar(this.footer, this.store,
759                     {pageSize: this.pageSize});
760             
761         }
762         
763         if (this.pageTb && this.allowBlank && !this.disableClear) {
764             var _this = this;
765             this.pageTb.add(new Roo.Toolbar.Fill(), {
766                 cls: 'x-btn-icon x-btn-clear',
767                 text: '&#160;',
768                 handler: function()
769                 {
770                     _this.collapse();
771                     _this.clearValue();
772                     _this.onSelect(false, -1);
773                 }
774             });
775         }
776         if (this.footer) {
777             this.assetHeight += this.footer.getHeight();
778         }
779         */
780             
781         if(!this.tpl){
782             this.tpl = Roo.bootstrap.version == 4 ?
783                 '<a class="dropdown-item" href="#">{' + this.displayField + '}</a>' :  // 4 does not need <li> and it get's really confisued.
784                 '<li><a class="dropdown-item" href="#">{' + this.displayField + '}</a></li>';
785         }
786
787         this.view = new Roo.View(this.list, this.tpl, {
788             singleSelect:true, store: this.store, selectedClass: this.selectedClass
789         });
790         //this.view.wrapEl.setDisplayed(false);
791         this.view.on('click', this.onViewClick, this);
792         
793         
794         this.store.on('beforeload', this.onBeforeLoad, this);
795         this.store.on('load', this.onLoad, this);
796         this.store.on('loadexception', this.onLoadException, this);
797         /*
798         if(this.resizable){
799             this.resizer = new Roo.Resizable(this.list,  {
800                pinned:true, handles:'se'
801             });
802             this.resizer.on('resize', function(r, w, h){
803                 this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
804                 this.listWidth = w;
805                 this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
806                 this.restrictHeight();
807             }, this);
808             this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
809         }
810         */
811         if(!this.editable){
812             this.editable = true;
813             this.setEditable(false);
814         }
815         
816         /*
817         
818         if (typeof(this.events.add.listeners) != 'undefined') {
819             
820             this.addicon = this.wrap.createChild(
821                 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-add' });  
822        
823             this.addicon.on('click', function(e) {
824                 this.fireEvent('add', this);
825             }, this);
826         }
827         if (typeof(this.events.edit.listeners) != 'undefined') {
828             
829             this.editicon = this.wrap.createChild(
830                 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-combo-edit' });  
831             if (this.addicon) {
832                 this.editicon.setStyle('margin-left', '40px');
833             }
834             this.editicon.on('click', function(e) {
835                 
836                 // we fire even  if inothing is selected..
837                 this.fireEvent('edit', this, this.lastData );
838                 
839             }, this);
840         }
841         */
842         
843         this.keyNav = new Roo.KeyNav(this.inputEl(), {
844             "up" : function(e){
845                 this.inKeyMode = true;
846                 this.selectPrev();
847             },
848
849             "down" : function(e){
850                 if(!this.isExpanded()){
851                     this.onTriggerClick();
852                 }else{
853                     this.inKeyMode = true;
854                     this.selectNext();
855                 }
856             },
857
858             "enter" : function(e){
859 //                this.onViewClick();
860                 //return true;
861                 this.collapse();
862                 
863                 if(this.fireEvent("specialkey", this, e)){
864                     this.onViewClick(false);
865                 }
866                 
867                 return true;
868             },
869
870             "esc" : function(e){
871                 this.collapse();
872             },
873
874             "tab" : function(e){
875                 this.collapse();
876                 
877                 if(this.fireEvent("specialkey", this, e)){
878                     this.onViewClick(false);
879                 }
880                 
881                 return true;
882             },
883
884             scope : this,
885
886             doRelay : function(foo, bar, hname){
887                 if(hname == 'down' || this.scope.isExpanded()){
888                    return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
889                 }
890                 return true;
891             },
892
893             forceKeyDown: true
894         });
895         
896         
897         this.queryDelay = Math.max(this.queryDelay || 10,
898                 this.mode == 'local' ? 10 : 250);
899         
900         
901         this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
902         
903         if(this.typeAhead){
904             this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
905         }
906         if(this.editable !== false){
907             this.inputEl().on("keyup", this.onKeyUp, this);
908         }
909         if(this.forceSelection){
910             this.inputEl().on('blur', this.doForce, this);
911         }
912         
913         if(this.multiple){
914             this.choices = this.el.select('ul.roo-select2-choices', true).first();
915             this.searchField = this.el.select('ul li.roo-select2-search-field', true).first();
916         }
917     },
918     
919     initTickableEvents: function()
920     {   
921         this.createList();
922         
923         if(this.hiddenName){
924             
925             this.hiddenField = this.el.select('input.form-hidden-field',true).first();
926             
927             this.hiddenField.dom.value =
928                 this.hiddenValue !== undefined ? this.hiddenValue :
929                 this.value !== undefined ? this.value : '';
930
931             // prevent input submission
932             this.el.dom.removeAttribute('name');
933             this.hiddenField.dom.setAttribute('name', this.hiddenName);
934              
935              
936         }
937         
938 //        this.list = this.el.select('ul.dropdown-menu',true).first();
939         
940         this.choices = this.el.select('ul.roo-select2-choices', true).first();
941         this.searchField = this.el.select('ul li.roo-select2-search-field', true).first();
942         if(this.triggerList){
943             this.searchField.on("click", this.onSearchFieldClick, this, {preventDefault:true});
944         }
945          
946         this.trigger = this.el.select('.tickable-buttons > .btn-edit', true).first();
947         this.trigger.on("click", this.onTickableTriggerClick, this, {preventDefault:true});
948         
949         this.okBtn = this.el.select('.tickable-buttons > .btn-ok', true).first();
950         this.cancelBtn = this.el.select('.tickable-buttons > .btn-cancel', true).first();
951         
952         this.okBtn.on('click', this.onTickableFooterButtonClick, this, this.okBtn);
953         this.cancelBtn.on('click', this.onTickableFooterButtonClick, this, this.cancelBtn);
954         
955         this.trigger.setVisibilityMode(Roo.Element.DISPLAY);
956         this.okBtn.setVisibilityMode(Roo.Element.DISPLAY);
957         this.cancelBtn.setVisibilityMode(Roo.Element.DISPLAY);
958         
959         this.okBtn.hide();
960         this.cancelBtn.hide();
961         
962         var _this = this;
963         
964         (function(){
965             var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
966             _this.list.setWidth(lw);
967         }).defer(100);
968         
969         this.list.on('mouseover', this.onViewOver, this);
970         this.list.on('mousemove', this.onViewMove, this);
971         
972         this.list.on('scroll', this.onViewScroll, this);
973         
974         if(!this.tpl){
975             this.tpl = '<li class="roo-select2-result"><div class="checkbox"><input id="{roo-id}"' + 
976                 'type="checkbox" {roo-data-checked}><label for="{roo-id}"><b>{' + this.displayField + '}</b></label></div></li>';
977         }
978
979         this.view = new Roo.View(this.list, this.tpl, {
980             singleSelect:true,
981             tickable:true,
982             parent:this,
983             store: this.store,
984             selectedClass: this.selectedClass
985         });
986         
987         //this.view.wrapEl.setDisplayed(false);
988         this.view.on('click', this.onViewClick, this);
989         
990         
991         
992         this.store.on('beforeload', this.onBeforeLoad, this);
993         this.store.on('load', this.onLoad, this);
994         this.store.on('loadexception', this.onLoadException, this);
995         
996         if(this.editable){
997             this.keyNav = new Roo.KeyNav(this.tickableInputEl(), {
998                 "up" : function(e){
999                     this.inKeyMode = true;
1000                     this.selectPrev();
1001                 },
1002
1003                 "down" : function(e){
1004                     this.inKeyMode = true;
1005                     this.selectNext();
1006                 },
1007
1008                 "enter" : function(e){
1009                     if(this.fireEvent("specialkey", this, e)){
1010                         this.onViewClick(false);
1011                     }
1012                     
1013                     return true;
1014                 },
1015
1016                 "esc" : function(e){
1017                     this.onTickableFooterButtonClick(e, false, false);
1018                 },
1019
1020                 "tab" : function(e){
1021                     this.fireEvent("specialkey", this, e);
1022                     
1023                     this.onTickableFooterButtonClick(e, false, false);
1024                     
1025                     return true;
1026                 },
1027
1028                 scope : this,
1029
1030                 doRelay : function(e, fn, key){
1031                     if(this.scope.isExpanded()){
1032                        return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
1033                     }
1034                     return true;
1035                 },
1036
1037                 forceKeyDown: true
1038             });
1039         }
1040         
1041         this.queryDelay = Math.max(this.queryDelay || 10,
1042                 this.mode == 'local' ? 10 : 250);
1043         
1044         
1045         this.dqTask = new Roo.util.DelayedTask(this.initQuery, this);
1046         
1047         if(this.typeAhead){
1048             this.taTask = new Roo.util.DelayedTask(this.onTypeAhead, this);
1049         }
1050         
1051         if(this.editable !== false){
1052             this.tickableInputEl().on("keyup", this.onKeyUp, this);
1053         }
1054         
1055         this.indicator = this.indicatorEl();
1056         
1057         if(this.indicator){
1058             this.indicator.setVisibilityMode(Roo.Element.DISPLAY);
1059             this.indicator.hide();
1060         }
1061         
1062     },
1063
1064     onDestroy : function(){
1065         if(this.view){
1066             this.view.setStore(null);
1067             this.view.el.removeAllListeners();
1068             this.view.el.remove();
1069             this.view.purgeListeners();
1070         }
1071         if(this.list){
1072             this.list.dom.innerHTML  = '';
1073         }
1074         
1075         if(this.store){
1076             this.store.un('beforeload', this.onBeforeLoad, this);
1077             this.store.un('load', this.onLoad, this);
1078             this.store.un('loadexception', this.onLoadException, this);
1079         }
1080         Roo.bootstrap.form.ComboBox.superclass.onDestroy.call(this);
1081     },
1082
1083     // private
1084     fireKey : function(e){
1085         if(e.isNavKeyPress() && !this.list.isVisible()){
1086             this.fireEvent("specialkey", this, e);
1087         }
1088     },
1089
1090     // private
1091     onResize: function(w, h)
1092     {
1093         
1094         
1095 //        Roo.bootstrap.form.ComboBox.superclass.onResize.apply(this, arguments);
1096 //        
1097 //        if(typeof w != 'number'){
1098 //            // we do not handle it!?!?
1099 //            return;
1100 //        }
1101 //        var tw = this.trigger.getWidth();
1102 //       // tw += this.addicon ? this.addicon.getWidth() : 0;
1103 //       // tw += this.editicon ? this.editicon.getWidth() : 0;
1104 //        var x = w - tw;
1105 //        this.inputEl().setWidth( this.adjustWidth('input', x));
1106 //            
1107 //        //this.trigger.setStyle('left', x+'px');
1108 //        
1109 //        if(this.list && this.listWidth === undefined){
1110 //            var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
1111 //            this.list.setWidth(lw);
1112 //            this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
1113 //        }
1114         
1115     
1116         
1117     },
1118
1119     /**
1120      * Allow or prevent the user from directly editing the field text.  If false is passed,
1121      * the user will only be able to select from the items defined in the dropdown list.  This method
1122      * is the runtime equivalent of setting the 'editable' config option at config time.
1123      * @param {Boolean} value True to allow the user to directly edit the field text
1124      */
1125     setEditable : function(value){
1126         if(value == this.editable){
1127             return;
1128         }
1129         this.editable = value;
1130         if(!value){
1131             this.inputEl().dom.setAttribute('readOnly', true);
1132             this.inputEl().on('mousedown', this.onTriggerClick,  this);
1133             this.inputEl().addClass('x-combo-noedit');
1134         }else{
1135             this.inputEl().dom.removeAttribute('readOnly');
1136             this.inputEl().un('mousedown', this.onTriggerClick,  this);
1137             this.inputEl().removeClass('x-combo-noedit');
1138         }
1139     },
1140
1141     // private
1142     
1143     onBeforeLoad : function(combo,opts){
1144         if(!this.hasFocus){
1145             return;
1146         }
1147          if (!opts.add) {
1148             this.list.dom.innerHTML = '<li class="loading-indicator">'+(this.loadingText||'loading')+'</li>' ;
1149          }
1150         this.restrictHeight();
1151         this.selectedIndex = -1;
1152     },
1153
1154     // private
1155     onLoad : function(){
1156         
1157         this.hasQuery = false;
1158         
1159         if(!this.hasFocus){
1160             return;
1161         }
1162         
1163         if(typeof(this.loading) !== 'undefined' && this.loading !== null){
1164             this.loading.hide();
1165         }
1166         
1167         if(this.store.getCount() > 0){
1168             
1169             this.expand();
1170             this.restrictHeight();
1171             if(this.lastQuery == this.allQuery){
1172                 if(this.editable && !this.tickable){
1173                     this.inputEl().dom.select();
1174                 }
1175                 
1176                 if(
1177                     !this.selectByValue(this.value, true) &&
1178                     this.autoFocus && 
1179                     (
1180                         !this.store.lastOptions ||
1181                         typeof(this.store.lastOptions.add) == 'undefined' || 
1182                         this.store.lastOptions.add != true
1183                     )
1184                 ){
1185                     this.select(0, true);
1186                 }
1187             }else{
1188                 if(this.autoFocus){
1189                     this.selectNext();
1190                 }
1191                 if(this.typeAhead && this.lastKey != Roo.EventObject.BACKSPACE && this.lastKey != Roo.EventObject.DELETE){
1192                     this.taTask.delay(this.typeAheadDelay);
1193                 }
1194             }
1195         }else{
1196             this.onEmptyResults();
1197         }
1198         
1199         //this.el.focus();
1200     },
1201     // private
1202     onLoadException : function()
1203     {
1204         this.hasQuery = false;
1205         
1206         if(typeof(this.loading) !== 'undefined' && this.loading !== null){
1207             this.loading.hide();
1208         }
1209         
1210         if(this.tickable && this.editable){
1211             return;
1212         }
1213         
1214         this.collapse();
1215         // only causes errors at present
1216         //Roo.log(this.store.reader.jsonData);
1217         //if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
1218             // fixme
1219             //Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
1220         //}
1221         
1222         
1223     },
1224     // private
1225     onTypeAhead : function(){
1226         if(this.store.getCount() > 0){
1227             var r = this.store.getAt(0);
1228             var newValue = r.data[this.displayField];
1229             var len = newValue.length;
1230             var selStart = this.getRawValue().length;
1231             
1232             if(selStart != len){
1233                 this.setRawValue(newValue);
1234                 this.selectText(selStart, newValue.length);
1235             }
1236         }
1237     },
1238
1239     // private
1240     onSelect : function(record, index){
1241         
1242         if(this.fireEvent('beforeselect', this, record, index) !== false){
1243         
1244             this.setFromData(index > -1 ? record.data : false);
1245             
1246             this.collapse();
1247             this.fireEvent('select', this, record, index);
1248         }
1249     },
1250
1251     /**
1252      * Returns the currently selected field value or empty string if no value is set.
1253      * @return {String} value The selected value
1254      */
1255     getValue : function()
1256     {
1257         if(Roo.isIOS && this.useNativeIOS){
1258             return this.ios_options[this.inputEl().dom.selectedIndex].data[this.valueField];
1259         }
1260         
1261         if(this.multiple){
1262             return (this.hiddenField) ? this.hiddenField.dom.value : this.value;
1263         }
1264         
1265         if(this.valueField){
1266             return typeof this.value != 'undefined' ? this.value : '';
1267         }else{
1268             return Roo.bootstrap.form.ComboBox.superclass.getValue.call(this);
1269         }
1270     },
1271     
1272     getRawValue : function()
1273     {
1274         if(Roo.isIOS && this.useNativeIOS){
1275             return this.ios_options[this.inputEl().dom.selectedIndex].data[this.displayField];
1276         }
1277         
1278         var v = this.inputEl().getValue();
1279         
1280         return v;
1281     },
1282
1283     /**
1284      * Clears any text/value currently set in the field
1285      */
1286     clearValue : function(){
1287         
1288         if(this.hiddenField){
1289             this.hiddenField.dom.value = '';
1290         }
1291         this.value = '';
1292         this.setRawValue('');
1293         this.lastSelectionText = '';
1294         this.lastData = false;
1295         
1296         var close = this.closeTriggerEl();
1297         
1298         if(close){
1299             close.hide();
1300         }
1301         
1302         this.validate();
1303         
1304     },
1305
1306     /**
1307      * Sets the specified value into the field.  If the value finds a match, the corresponding record text
1308      * will be displayed in the field.  If the value does not match the data value of an existing item,
1309      * and the valueNotFoundText config option is defined, it will be displayed as the default field text.
1310      * Otherwise the field will be blank (although the value will still be set).
1311      * @param {String} value The value to match
1312      */
1313     setValue : function(v)
1314     {
1315         if(Roo.isIOS && this.useNativeIOS){
1316             this.setIOSValue(v);
1317             return;
1318         }
1319         
1320         if(this.multiple){
1321             this.syncValue();
1322             return;
1323         }
1324         
1325         var text = v;
1326         if(this.valueField){
1327             var r = this.findRecord(this.valueField, v);
1328             if(r){
1329                 text = r.data[this.displayField];
1330             }else if(this.valueNotFoundText !== undefined){
1331                 text = this.valueNotFoundText;
1332             }
1333         }
1334         this.lastSelectionText = text;
1335         if(this.hiddenField){
1336             this.hiddenField.dom.value = v;
1337         }
1338         Roo.bootstrap.form.ComboBox.superclass.setValue.call(this, text);
1339         this.value = v;
1340         
1341         var close = this.closeTriggerEl();
1342         
1343         if(close){
1344             (v && (v.length || v * 1 > 0)) ? close.show() : close.hide();
1345         }
1346         
1347         this.validate();
1348     },
1349     /**
1350      * @property {Object} the last set data for the element
1351      */
1352     
1353     lastData : false,
1354     /**
1355      * Sets the value of the field based on a object which is related to the record format for the store.
1356      * @param {Object} value the value to set as. or false on reset?
1357      */
1358     setFromData : function(o){
1359         
1360         if(this.multiple){
1361             this.addItem(o);
1362             return;
1363         }
1364             
1365         var dv = ''; // display value
1366         var vv = ''; // value value..
1367         this.lastData = o;
1368         if (this.displayField) {
1369             dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1370         } else {
1371             // this is an error condition!!!
1372             Roo.log('no  displayField value set for '+ (this.name ? this.name : this.id));
1373         }
1374         
1375         if(this.valueField){
1376             vv = !o || typeof(o[this.valueField]) == 'undefined' ? dv : o[this.valueField];
1377         }
1378         
1379         var close = this.closeTriggerEl();
1380         
1381         if(close){
1382             if(dv.length || vv * 1 > 0){
1383                 close.show() ;
1384                 this.blockFocus=true;
1385             } else {
1386                 close.hide();
1387             }             
1388         }
1389         
1390         if(this.hiddenField){
1391             this.hiddenField.dom.value = vv;
1392             
1393             this.lastSelectionText = dv;
1394             Roo.bootstrap.form.ComboBox.superclass.setValue.call(this, dv);
1395             this.value = vv;
1396             return;
1397         }
1398         // no hidden field.. - we store the value in 'value', but still display
1399         // display field!!!!
1400         this.lastSelectionText = dv;
1401         Roo.bootstrap.form.ComboBox.superclass.setValue.call(this, dv);
1402         this.value = vv;
1403         
1404         
1405         
1406     },
1407     // private
1408     reset : function(){
1409         // overridden so that last data is reset..
1410         
1411         if(this.multiple){
1412             this.clearItem();
1413             return;
1414         }
1415         
1416         this.setValue(this.originalValue);
1417         //this.clearInvalid();
1418         this.lastData = false;
1419         if (this.view) {
1420             this.view.clearSelections();
1421         }
1422         
1423         this.validate();
1424     },
1425     // private
1426     findRecord : function(prop, value){
1427         var record;
1428         if(this.store.getCount() > 0){
1429             this.store.each(function(r){
1430                 if(r.data[prop] == value){
1431                     record = r;
1432                     return false;
1433                 }
1434                 return true;
1435             });
1436         }
1437         return record;
1438     },
1439     
1440     getName: function()
1441     {
1442         // returns hidden if it's set..
1443         if (!this.rendered) {return ''};
1444         return !this.hiddenName && this.inputEl().dom.name  ? this.inputEl().dom.name : (this.hiddenName || '');
1445         
1446     },
1447     // private
1448     onViewMove : function(e, t){
1449         this.inKeyMode = false;
1450     },
1451
1452     // private
1453     onViewOver : function(e, t){
1454         if(this.inKeyMode){ // prevent key nav and mouse over conflicts
1455             return;
1456         }
1457         var item = this.view.findItemFromChild(t);
1458         
1459         if(item){
1460             var index = this.view.indexOf(item);
1461             this.select(index, false);
1462         }
1463     },
1464
1465     // private
1466     onViewClick : function(view, doFocus, el, e)
1467     {
1468         var index = this.view.getSelectedIndexes()[0];
1469         
1470         var r = this.store.getAt(index);
1471         
1472         if(this.tickable){
1473             
1474             if(typeof(e) != 'undefined' && e.getTarget().nodeName.toLowerCase() != 'input'){
1475                 return;
1476             }
1477             
1478             var rm = false;
1479             var _this = this;
1480             
1481             Roo.each(this.tickItems, function(v,k){
1482                 
1483                 if(typeof(v) != 'undefined' && v[_this.valueField] == r.data[_this.valueField]){
1484                     Roo.log(v);
1485                     _this.tickItems.splice(k, 1);
1486                     
1487                     if(typeof(e) == 'undefined' && view == false){
1488                         Roo.get(_this.view.getNodes(index, index)[0]).select('input', true).first().dom.checked = false;
1489                     }
1490                     
1491                     rm = true;
1492                     return;
1493                 }
1494             });
1495             
1496             if(rm){
1497                 return;
1498             }
1499             
1500             if(this.fireEvent('tick', this, r, index, Roo.get(_this.view.getNodes(index, index)[0]).select('input', true).first().dom.checked) !== false){
1501                 this.tickItems.push(r.data);
1502             }
1503             
1504             if(typeof(e) == 'undefined' && view == false){
1505                 Roo.get(_this.view.getNodes(index, index)[0]).select('input', true).first().dom.checked = true;
1506             }
1507                     
1508             return;
1509         }
1510         
1511         if(r){
1512             this.onSelect(r, index);
1513         }
1514         if(doFocus !== false && !this.blockFocus){
1515             this.inputEl().focus();
1516         }
1517     },
1518
1519     // private
1520     restrictHeight : function(){
1521         //this.innerList.dom.style.height = '';
1522         //var inner = this.innerList.dom;
1523         //var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
1524         //this.innerList.setHeight(h < this.maxHeight ? 'auto' : this.maxHeight);
1525         //this.list.beginUpdate();
1526         //this.list.setHeight(this.innerList.getHeight()+this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight);
1527         this.list.alignTo(this.inputEl(), this.listAlign);
1528         this.list.alignTo(this.inputEl(), this.listAlign);
1529         //this.list.endUpdate();
1530     },
1531
1532     // private
1533     onEmptyResults : function(){
1534         
1535         if(this.tickable && this.editable){
1536             this.hasFocus = false;
1537             this.restrictHeight();
1538             return;
1539         }
1540         
1541         this.collapse();
1542     },
1543
1544     /**
1545      * Returns true if the dropdown list is expanded, else false.
1546      */
1547     isExpanded : function(){
1548         return this.list.isVisible();
1549     },
1550
1551     /**
1552      * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
1553      * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1554      * @param {String} value The data value of the item to select
1555      * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1556      * selected item if it is not currently in view (defaults to true)
1557      * @return {Boolean} True if the value matched an item in the list, else false
1558      */
1559     selectByValue : function(v, scrollIntoView){
1560         if(v !== undefined && v !== null){
1561             var r = this.findRecord(this.valueField || this.displayField, v);
1562             if(r){
1563                 this.select(this.store.indexOf(r), scrollIntoView);
1564                 return true;
1565             }
1566         }
1567         return false;
1568     },
1569
1570     /**
1571      * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
1572      * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
1573      * @param {Number} index The zero-based index of the list item to select
1574      * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
1575      * selected item if it is not currently in view (defaults to true)
1576      */
1577     select : function(index, scrollIntoView){
1578         this.selectedIndex = index;
1579         this.view.select(index);
1580         if(scrollIntoView !== false){
1581             var el = this.view.getNode(index);
1582             /*
1583              * el && !this.multiple && !this.tickable // not sure why we disable multiple before..
1584              */
1585             if(el){
1586                 this.list.scrollChildIntoView(el, false);
1587             }
1588         }
1589     },
1590
1591     // private
1592     selectNext : function(){
1593         var ct = this.store.getCount();
1594         if(ct > 0){
1595             if(this.selectedIndex == -1){
1596                 this.select(0);
1597             }else if(this.selectedIndex < ct-1){
1598                 this.select(this.selectedIndex+1);
1599             }
1600         }
1601     },
1602
1603     // private
1604     selectPrev : function(){
1605         var ct = this.store.getCount();
1606         if(ct > 0){
1607             if(this.selectedIndex == -1){
1608                 this.select(0);
1609             }else if(this.selectedIndex != 0){
1610                 this.select(this.selectedIndex-1);
1611             }
1612         }
1613     },
1614
1615     // private
1616     onKeyUp : function(e){
1617         if(this.editable !== false && !e.isSpecialKey()){
1618             this.lastKey = e.getKey();
1619             this.dqTask.delay(this.queryDelay);
1620         }
1621     },
1622
1623     // private
1624     validateBlur : function(){
1625         return !this.list || !this.list.isVisible();   
1626     },
1627
1628     // private
1629     initQuery : function(){
1630         
1631         var v = this.getRawValue();
1632         
1633         if(this.tickable && this.editable){
1634             v = this.tickableInputEl().getValue();
1635         }
1636         
1637         this.doQuery(v);
1638     },
1639
1640     // private
1641     doForce : function(){
1642         if(this.inputEl().dom.value.length > 0){
1643             this.inputEl().dom.value =
1644                 this.lastSelectionText === undefined ? '' : this.lastSelectionText;
1645              
1646         }
1647     },
1648
1649     /**
1650      * Execute a query to filter the dropdown list.  Fires the beforequery event prior to performing the
1651      * query allowing the query action to be canceled if needed.
1652      * @param {String} query The SQL query to execute
1653      * @param {Boolean} forceAll True to force the query to execute even if there are currently fewer characters
1654      * in the field than the minimum specified by the minChars config option.  It also clears any filter previously
1655      * saved in the current store (defaults to false)
1656      */
1657     doQuery : function(q, forceAll){
1658         
1659         if(q === undefined || q === null){
1660             q = '';
1661         }
1662         var qe = {
1663             query: q,
1664             forceAll: forceAll,
1665             combo: this,
1666             cancel:false
1667         };
1668         if(this.fireEvent('beforequery', qe)===false || qe.cancel){
1669             return false;
1670         }
1671         q = qe.query;
1672         
1673         forceAll = qe.forceAll;
1674         if(forceAll === true || (q.length >= this.minChars)){
1675             
1676             this.hasQuery = true;
1677             
1678             if(this.lastQuery != q || this.alwaysQuery){
1679                 this.lastQuery = q;
1680                 if(this.mode == 'local'){
1681                     this.selectedIndex = -1;
1682                     if(forceAll){
1683                         this.store.clearFilter();
1684                     }else{
1685                         
1686                         if(this.specialFilter){
1687                             this.fireEvent('specialfilter', this);
1688                             this.onLoad();
1689                             return;
1690                         }
1691                         
1692                         this.store.filter(this.displayField, q);
1693                     }
1694                     
1695                     this.store.fireEvent("datachanged", this.store);
1696                     
1697                     this.onLoad();
1698                     
1699                     
1700                 }else{
1701                     
1702                     this.store.baseParams[this.queryParam] = q;
1703                     
1704                     var options = {params : this.getParams(q)};
1705                     
1706                     if(this.loadNext){
1707                         options.add = true;
1708                         options.params.start = this.page * this.pageSize;
1709                     }
1710                     
1711                     this.store.load(options);
1712                     
1713                     /*
1714                      *  this code will make the page width larger, at the beginning, the list not align correctly, 
1715                      *  we should expand the list on onLoad
1716                      *  so command out it
1717                      */
1718 //                    this.expand();
1719                 }
1720             }else{
1721                 this.selectedIndex = -1;
1722                 this.onLoad();   
1723             }
1724         }
1725         
1726         this.loadNext = false;
1727     },
1728     
1729     // private
1730     getParams : function(q){
1731         var p = {};
1732         //p[this.queryParam] = q;
1733         
1734         if(this.pageSize){
1735             p.start = 0;
1736             p.limit = this.pageSize;
1737         }
1738         return p;
1739     },
1740
1741     /**
1742      * Hides the dropdown list if it is currently expanded. Fires the 'collapse' event on completion.
1743      */
1744     collapse : function(){
1745         if(!this.isExpanded()){
1746             return;
1747         }
1748         
1749         this.list.hide();
1750         
1751         this.hasFocus = false;
1752         
1753         if(this.tickable){
1754             this.okBtn.hide();
1755             this.cancelBtn.hide();
1756             this.trigger.show();
1757             
1758             if(this.editable){
1759                 this.tickableInputEl().dom.value = '';
1760                 this.tickableInputEl().blur();
1761             }
1762             
1763         }
1764         
1765         Roo.get(document).un('mousedown', this.collapseIf, this);
1766         Roo.get(document).un('mousewheel', this.collapseIf, this);
1767         if (!this.editable) {
1768             Roo.get(document).un('keydown', this.listKeyPress, this);
1769         }
1770         this.fireEvent('collapse', this);
1771         
1772         this.validate();
1773     },
1774
1775     // private
1776     collapseIf : function(e){
1777         var in_combo  = e.within(this.el);
1778         var in_list =  e.within(this.list);
1779         var is_list = (Roo.get(e.getTarget()).id == this.list.id) ? true : false;
1780         
1781         if (in_combo || in_list || is_list) {
1782             //e.stopPropagation();
1783             return;
1784         }
1785         
1786         if(this.tickable){
1787             this.onTickableFooterButtonClick(e, false, false);
1788         }
1789
1790         this.collapse();
1791         
1792     },
1793
1794     /**
1795      * Expands the dropdown list if it is currently hidden. Fires the 'expand' event on completion.
1796      */
1797     expand : function(){
1798        
1799         if(this.isExpanded() || !this.hasFocus){
1800             return;
1801         }
1802         
1803         var lw = this.listWidth || Math.max(this.inputEl().getWidth(), this.minListWidth);
1804         this.list.setWidth(lw);
1805         
1806         Roo.log('expand');
1807         
1808         this.list.show();
1809         
1810         this.restrictHeight();
1811         
1812         if(this.tickable){
1813             
1814             this.tickItems = Roo.apply([], this.item);
1815             
1816             this.okBtn.show();
1817             this.cancelBtn.show();
1818             this.trigger.hide();
1819             
1820             if(this.editable){
1821                 this.tickableInputEl().focus();
1822             }
1823             
1824         }
1825         
1826         Roo.get(document).on('mousedown', this.collapseIf, this);
1827         Roo.get(document).on('mousewheel', this.collapseIf, this);
1828         if (!this.editable) {
1829             Roo.get(document).on('keydown', this.listKeyPress, this);
1830         }
1831
1832         this.list.setStyle('maxHeight', 'calc(100% - ' + this.list.getTop() + 'px - 50px)');
1833         
1834         this.fireEvent('expand', this);
1835     },
1836
1837     // private
1838     // Implements the default empty TriggerField.onTriggerClick function
1839     onTriggerClick : function(e)
1840     {
1841         Roo.log('trigger click');
1842         
1843         if(this.disabled || !this.triggerList){
1844             return;
1845         }
1846         
1847         this.page = 0;
1848         this.loadNext = false;
1849         
1850         if(this.isExpanded()){
1851             this.collapse();
1852             if (!this.blockFocus) {
1853                 this.inputEl().focus();
1854             }
1855             
1856         }else {
1857             this.hasFocus = true;
1858             if(this.triggerAction == 'all') {
1859                 this.doQuery(this.allQuery, true);
1860             } else {
1861                 this.doQuery(this.getRawValue());
1862             }
1863             if (!this.blockFocus) {
1864                 this.inputEl().focus();
1865             }
1866         }
1867     },
1868     
1869     onTickableTriggerClick : function(e)
1870     {
1871         if(this.disabled){
1872             return;
1873         }
1874         
1875         this.page = 0;
1876         this.loadNext = false;
1877         this.hasFocus = true;
1878         
1879         if(this.triggerAction == 'all') {
1880             this.doQuery(this.allQuery, true);
1881         } else {
1882             this.doQuery(this.getRawValue());
1883         }
1884     },
1885     
1886     onSearchFieldClick : function(e)
1887     {
1888         if(this.hasFocus && !this.disabled && e.getTarget().nodeName.toLowerCase() != 'button'){
1889             this.onTickableFooterButtonClick(e, false, false);
1890             return;
1891         }
1892         
1893         if(this.hasFocus || this.disabled || e.getTarget().nodeName.toLowerCase() == 'button'){
1894             return;
1895         }
1896         
1897         this.page = 0;
1898         this.loadNext = false;
1899         this.hasFocus = true;
1900         
1901         if(this.triggerAction == 'all') {
1902             this.doQuery(this.allQuery, true);
1903         } else {
1904             this.doQuery(this.getRawValue());
1905         }
1906     },
1907     
1908     listKeyPress : function(e)
1909     {
1910         //Roo.log('listkeypress');
1911         // scroll to first matching element based on key pres..
1912         if (e.isSpecialKey()) {
1913             return false;
1914         }
1915         var k = String.fromCharCode(e.getKey()).toUpperCase();
1916         //Roo.log(k);
1917         var match  = false;
1918         var csel = this.view.getSelectedNodes();
1919         var cselitem = false;
1920         if (csel.length) {
1921             var ix = this.view.indexOf(csel[0]);
1922             cselitem  = this.store.getAt(ix);
1923             if (!cselitem.get(this.displayField) || cselitem.get(this.displayField).substring(0,1).toUpperCase() != k) {
1924                 cselitem = false;
1925             }
1926             
1927         }
1928         
1929         this.store.each(function(v) { 
1930             if (cselitem) {
1931                 // start at existing selection.
1932                 if (cselitem.id == v.id) {
1933                     cselitem = false;
1934                 }
1935                 return true;
1936             }
1937                 
1938             if (v.get(this.displayField) && v.get(this.displayField).substring(0,1).toUpperCase() == k) {
1939                 match = this.store.indexOf(v);
1940                 return false;
1941             }
1942             return true;
1943         }, this);
1944         
1945         if (match === false) {
1946             return true; // no more action?
1947         }
1948         // scroll to?
1949         this.view.select(match);
1950         var sn = Roo.get(this.view.getSelectedNodes()[0]);
1951         sn.scrollIntoView(sn.dom.parentNode, false);
1952     },
1953     
1954     onViewScroll : function(e, t){
1955         
1956         if(this.view.el.getScroll().top == 0 ||this.view.el.getScroll().top < this.view.el.dom.scrollHeight - this.view.el.dom.clientHeight || !this.hasFocus || !this.append || this.hasQuery){
1957             return;
1958         }
1959         
1960         this.hasQuery = true;
1961         
1962         this.loading = this.list.select('.loading', true).first();
1963         
1964         if(this.loading === null){
1965             this.list.createChild({
1966                 tag: 'div',
1967                 cls: 'loading roo-select2-more-results roo-select2-active',
1968                 html: 'Loading more results...'
1969             });
1970             
1971             this.loading = this.list.select('.loading', true).first();
1972             
1973             this.loading.setVisibilityMode(Roo.Element.DISPLAY);
1974             
1975             this.loading.hide();
1976         }
1977         
1978         this.loading.show();
1979         
1980         var _combo = this;
1981         
1982         this.page++;
1983         this.loadNext = true;
1984         
1985         (function() { _combo.doQuery(_combo.allQuery, true); }).defer(500);
1986         
1987         return;
1988     },
1989     
1990     addItem : function(o)
1991     {   
1992         var dv = ''; // display value
1993         
1994         if (this.displayField) {
1995             dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1996         } else {
1997             // this is an error condition!!!
1998             Roo.log('no  displayField value set for '+ (this.name ? this.name : this.id));
1999         }
2000         
2001         if(!dv.length){
2002             return;
2003         }
2004         
2005         var choice = this.choices.createChild({
2006             tag: 'li',
2007             cls: 'roo-select2-search-choice',
2008             cn: [
2009                 {
2010                     tag: 'div',
2011                     html: dv
2012                 },
2013                 {
2014                     tag: 'a',
2015                     href: '#',
2016                     cls: 'roo-select2-search-choice-close fa fa-times',
2017                     tabindex: '-1'
2018                 }
2019             ]
2020             
2021         }, this.searchField);
2022         
2023         var close = choice.select('a.roo-select2-search-choice-close', true).first();
2024         
2025         close.on('click', this.onRemoveItem, this, { item : choice, data : o} );
2026         
2027         this.item.push(o);
2028         
2029         this.lastData = o;
2030         
2031         this.syncValue();
2032         
2033         this.inputEl().dom.value = '';
2034         
2035         this.validate();
2036     },
2037     
2038     onRemoveItem : function(e, _self, o)
2039     {
2040         e.preventDefault();
2041         
2042         this.lastItem = Roo.apply([], this.item);
2043         
2044         var index = this.item.indexOf(o.data) * 1;
2045         
2046         if( index < 0){
2047             Roo.log('not this item?!');
2048             return;
2049         }
2050         
2051         this.item.splice(index, 1);
2052         o.item.remove();
2053         
2054         this.syncValue();
2055         
2056         this.fireEvent('remove', this, e);
2057         
2058         this.validate();
2059         
2060     },
2061     
2062     syncValue : function()
2063     {
2064         if(!this.item.length){
2065             this.clearValue();
2066             return;
2067         }
2068             
2069         var value = [];
2070         var _this = this;
2071         Roo.each(this.item, function(i){
2072             if(_this.valueField){
2073                 value.push(i[_this.valueField]);
2074                 return;
2075             }
2076
2077             value.push(i);
2078         });
2079
2080         this.value = value.join(',');
2081
2082         if(this.hiddenField){
2083             this.hiddenField.dom.value = this.value;
2084         }
2085         
2086         this.store.fireEvent("datachanged", this.store);
2087         
2088         this.validate();
2089     },
2090     
2091     clearItem : function()
2092     {
2093         if(!this.multiple){
2094             return;
2095         }
2096         
2097         this.item = [];
2098         
2099         Roo.each(this.choices.select('>li.roo-select2-search-choice', true).elements, function(c){
2100            c.remove();
2101         });
2102         
2103         this.syncValue();
2104         
2105         this.validate();
2106         
2107         if(this.tickable && !Roo.isTouch){
2108             this.view.refresh();
2109         }
2110     },
2111     
2112     inputEl: function ()
2113     {
2114         if(Roo.isIOS && this.useNativeIOS){
2115             return this.el.select('select.roo-ios-select', true).first();
2116         }
2117         
2118         if(Roo.isTouch && this.mobileTouchView){
2119             return this.el.select('input.form-control',true).first();
2120         }
2121         
2122         if(this.tickable){
2123             return this.searchField;
2124         }
2125         
2126         return this.el.select('input.form-control',true).first();
2127     },
2128     
2129     onTickableFooterButtonClick : function(e, btn, el)
2130     {
2131         e.preventDefault();
2132         
2133         this.lastItem = Roo.apply([], this.item);
2134         
2135         if(btn && btn.name == 'cancel'){
2136             this.tickItems = Roo.apply([], this.item);
2137             this.collapse();
2138             return;
2139         }
2140         
2141         this.clearItem();
2142         
2143         var _this = this;
2144         
2145         Roo.each(this.tickItems, function(o){
2146             _this.addItem(o);
2147         });
2148         
2149         this.collapse();
2150         
2151     },
2152     
2153     validate : function()
2154     {
2155         if(this.getVisibilityEl().hasClass('hidden')){
2156             return true;
2157         }
2158         
2159         var v = this.getRawValue();
2160         
2161         if(this.multiple){
2162             v = this.getValue();
2163         }
2164         
2165         if(this.disabled || this.allowBlank || v.length){
2166             this.markValid();
2167             return true;
2168         }
2169         
2170         this.markInvalid();
2171         return false;
2172     },
2173     
2174     tickableInputEl : function()
2175     {
2176         if(!this.tickable || !this.editable){
2177             return this.inputEl();
2178         }
2179         
2180         return this.inputEl().select('.roo-select2-search-field-input', true).first();
2181     },
2182     
2183     
2184     getAutoCreateTouchView : function()
2185     {
2186         var id = Roo.id();
2187         
2188         var cfg = {
2189             cls: 'form-group' //input-group
2190         };
2191         
2192         var input =  {
2193             tag: 'input',
2194             id : id,
2195             type : this.inputType,
2196             cls : 'form-control x-combo-noedit',
2197             autocomplete: 'new-password',
2198             placeholder : this.placeholder || '',
2199             readonly : true
2200         };
2201         
2202         if (this.name) {
2203             input.name = this.name;
2204         }
2205         
2206         if (this.size) {
2207             input.cls += ' input-' + this.size;
2208         }
2209         
2210         if (this.disabled) {
2211             input.disabled = true;
2212         }
2213         
2214         var inputblock = {
2215             cls : 'roo-combobox-wrap',
2216             cn : [
2217                 input
2218             ]
2219         };
2220         
2221         if(this.before){
2222             inputblock.cls += ' input-group';
2223             
2224             inputblock.cn.unshift({
2225                 tag :'span',
2226                 cls : 'input-group-addon input-group-prepend input-group-text',
2227                 html : this.before
2228             });
2229         }
2230         
2231         if(this.removable && !this.multiple){
2232             inputblock.cls += ' roo-removable';
2233             
2234             inputblock.cn.push({
2235                 tag: 'button',
2236                 html : 'x',
2237                 cls : 'roo-combo-removable-btn close'
2238             });
2239         }
2240
2241         if(this.hasFeedback && !this.allowBlank){
2242             
2243             inputblock.cls += ' has-feedback';
2244             
2245             inputblock.cn.push({
2246                 tag: 'span',
2247                 cls: 'glyphicon form-control-feedback'
2248             });
2249             
2250         }
2251         
2252         if (this.after) {
2253             
2254             inputblock.cls += (this.before) ? '' : ' input-group';
2255             
2256             inputblock.cn.push({
2257                 tag :'span',
2258                 cls : 'input-group-addon input-group-append input-group-text',
2259                 html : this.after
2260             });
2261         }
2262
2263         
2264         var ibwrap = inputblock;
2265         
2266         if(this.multiple){
2267             ibwrap = {
2268                 tag: 'ul',
2269                 cls: 'roo-select2-choices',
2270                 cn:[
2271                     {
2272                         tag: 'li',
2273                         cls: 'roo-select2-search-field',
2274                         cn: [
2275
2276                             inputblock
2277                         ]
2278                     }
2279                 ]
2280             };
2281         
2282             
2283         }
2284         
2285         var combobox = {
2286             cls: 'roo-select2-container input-group roo-touchview-combobox ',
2287             cn: [
2288                 {
2289                     tag: 'input',
2290                     type : 'hidden',
2291                     cls: 'form-hidden-field'
2292                 },
2293                 ibwrap
2294             ]
2295         };
2296         
2297         if(!this.multiple && this.showToggleBtn){
2298             
2299             var caret = {
2300                 cls: 'caret'
2301             };
2302             
2303             if (this.caret != false) {
2304                 caret = {
2305                      tag: 'i',
2306                      cls: 'fa fa-' + this.caret
2307                 };
2308                 
2309             }
2310             
2311             combobox.cn.push({
2312                 tag :'span',
2313                 cls : 'input-group-addon input-group-append input-group-text btn dropdown-toggle',
2314                 cn : [
2315                     Roo.bootstrap.version == 3 ? caret : '',
2316                     {
2317                         tag: 'span',
2318                         cls: 'combobox-clear',
2319                         cn  : [
2320                             {
2321                                 tag : 'i',
2322                                 cls: 'icon-remove'
2323                             }
2324                         ]
2325                     }
2326                 ]
2327
2328             })
2329         }
2330         
2331         if(this.multiple){
2332             combobox.cls += ' roo-select2-container-multi';
2333         }
2334         
2335         var required =  this.allowBlank ?  {
2336                     tag : 'i',
2337                     style: 'display: none'
2338                 } : {
2339                    tag : 'i',
2340                    cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
2341                    tooltip : 'This field is required'
2342                 };
2343         
2344         var align = this.labelAlign || this.parentLabelAlign();
2345         
2346         if (align ==='left' && this.fieldLabel.length) {
2347
2348             cfg.cn = [
2349                 required,
2350                 {
2351                     tag: 'label',
2352                     cls : 'control-label col-form-label',
2353                     html : this.fieldLabel
2354
2355                 },
2356                 {
2357                     cls : 'roo-combobox-wrap ', 
2358                     cn: [
2359                         combobox
2360                     ]
2361                 }
2362             ];
2363             
2364             var labelCfg = cfg.cn[1];
2365             var contentCfg = cfg.cn[2];
2366             
2367
2368             if(this.indicatorpos == 'right'){
2369                 cfg.cn = [
2370                     {
2371                         tag: 'label',
2372                         'for' :  id,
2373                         cls : 'control-label col-form-label',
2374                         cn : [
2375                             {
2376                                 tag : 'span',
2377                                 html : this.fieldLabel
2378                             },
2379                             required
2380                         ]
2381                     },
2382                     {
2383                         cls : "roo-combobox-wrap ",
2384                         cn: [
2385                             combobox
2386                         ]
2387                     }
2388
2389                 ];
2390                 
2391                 labelCfg = cfg.cn[0];
2392                 contentCfg = cfg.cn[1];
2393             }
2394             
2395            
2396             
2397             if(this.labelWidth > 12){
2398                 labelCfg.style = "width: " + this.labelWidth + 'px';
2399             }
2400            
2401             if(this.labelWidth < 13 && this.labelmd == 0){
2402                 this.labelmd = this.labelWidth;
2403             }
2404             
2405             if(this.labellg > 0){
2406                 labelCfg.cls += ' col-lg-' + this.labellg;
2407                 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
2408             }
2409             
2410             if(this.labelmd > 0){
2411                 labelCfg.cls += ' col-md-' + this.labelmd;
2412                 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
2413             }
2414             
2415             if(this.labelsm > 0){
2416                 labelCfg.cls += ' col-sm-' + this.labelsm;
2417                 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
2418             }
2419             
2420             if(this.labelxs > 0){
2421                 labelCfg.cls += ' col-xs-' + this.labelxs;
2422                 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
2423             }
2424                 
2425                 
2426         } else if ( this.fieldLabel.length) {
2427             cfg.cn = [
2428                required,
2429                 {
2430                     tag: 'label',
2431                     cls : 'control-label',
2432                     html : this.fieldLabel
2433
2434                 },
2435                 {
2436                     cls : '', 
2437                     cn: [
2438                         combobox
2439                     ]
2440                 }
2441             ];
2442             
2443             if(this.indicatorpos == 'right'){
2444                 cfg.cn = [
2445                     {
2446                         tag: 'label',
2447                         cls : 'control-label',
2448                         html : this.fieldLabel,
2449                         cn : [
2450                             required
2451                         ]
2452                     },
2453                     {
2454                         cls : '', 
2455                         cn: [
2456                             combobox
2457                         ]
2458                     }
2459                 ];
2460             }
2461         } else {
2462             cfg.cn = combobox;    
2463         }
2464         
2465         
2466         var settings = this;
2467         
2468         ['xs','sm','md','lg'].map(function(size){
2469             if (settings[size]) {
2470                 cfg.cls += ' col-' + size + '-' + settings[size];
2471             }
2472         });
2473         
2474         return cfg;
2475     },
2476     
2477     initTouchView : function()
2478     {
2479         this.renderTouchView();
2480         
2481         this.touchViewEl.on('scroll', function(){
2482             this.el.dom.scrollTop = 0;
2483         }, this);
2484         
2485         this.originalValue = this.getValue();
2486         
2487         this.triggerEl = this.el.select('span.dropdown-toggle',true).first();
2488         
2489         this.inputEl().on("click", this.showTouchView, this);
2490         if (this.triggerEl) {
2491             this.triggerEl.on("click", this.showTouchView, this);
2492         }
2493         
2494         
2495         this.touchViewFooterEl.select('.roo-touch-view-cancel', true).first().on('click', this.hideTouchView, this);
2496         this.touchViewFooterEl.select('.roo-touch-view-ok', true).first().on('click', this.setTouchViewValue, this);
2497         
2498         this.maskEl = new Roo.LoadMask(this.touchViewEl, { store : this.store, msgCls: 'roo-el-mask-msg' });
2499         
2500         this.store.on('beforeload', this.onTouchViewBeforeLoad, this);
2501         this.store.on('load', this.onTouchViewLoad, this);
2502         this.store.on('loadexception', this.onTouchViewLoadException, this);
2503         
2504         if(this.hiddenName){
2505             
2506             this.hiddenField = this.el.select('input.form-hidden-field',true).first();
2507             
2508             this.hiddenField.dom.value =
2509                 this.hiddenValue !== undefined ? this.hiddenValue :
2510                 this.value !== undefined ? this.value : '';
2511         
2512             this.el.dom.removeAttribute('name');
2513             this.hiddenField.dom.setAttribute('name', this.hiddenName);
2514         }
2515         
2516         if(this.multiple){
2517             this.choices = this.el.select('ul.roo-select2-choices', true).first();
2518             this.searchField = this.el.select('ul li.roo-select2-search-field', true).first();
2519         }
2520         
2521         if(this.removable && !this.multiple){
2522             var close = this.closeTriggerEl();
2523             if(close){
2524                 close.setVisibilityMode(Roo.Element.DISPLAY).hide();
2525                 close.on('click', this.removeBtnClick, this, close);
2526             }
2527         }
2528         /*
2529          * fix the bug in Safari iOS8
2530          */
2531         this.inputEl().on("focus", function(e){
2532             document.activeElement.blur();
2533         }, this);
2534         
2535         this._touchViewMask = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
2536         
2537         return;
2538         
2539         
2540     },
2541     
2542     renderTouchView : function()
2543     {
2544         this.touchViewEl = Roo.get(document.body).createChild(Roo.bootstrap.form.ComboBox.touchViewTemplate);
2545         this.touchViewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2546         
2547         this.touchViewHeaderEl = this.touchViewEl.select('.modal-header', true).first();
2548         this.touchViewHeaderEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2549         
2550         this.touchViewBodyEl = this.touchViewEl.select('.modal-body', true).first();
2551         this.touchViewBodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2552         this.touchViewBodyEl.setStyle('overflow', 'auto');
2553         
2554         this.touchViewListGroup = this.touchViewBodyEl.select('.list-group', true).first();
2555         this.touchViewListGroup.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2556         
2557         this.touchViewFooterEl = this.touchViewEl.select('.modal-footer', true).first();
2558         this.touchViewFooterEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2559         
2560     },
2561     
2562     showTouchView : function()
2563     {
2564         if(this.disabled){
2565             return;
2566         }
2567         
2568         this.touchViewHeaderEl.hide();
2569
2570         if(this.modalTitle.length){
2571             this.touchViewHeaderEl.dom.innerHTML = this.modalTitle;
2572             this.touchViewHeaderEl.show();
2573         }
2574
2575         this.touchViewEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
2576         this.touchViewEl.show();
2577
2578         this.touchViewEl.select('.modal-dialog', true).first().setStyle({ margin : '0px', width : '100%'});
2579         
2580         //this.touchViewEl.select('.modal-dialog > .modal-content', true).first().setSize(
2581         //        Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
2582
2583         var bodyHeight = Roo.lib.Dom.getViewHeight() - this.touchViewFooterEl.getHeight() + this.touchViewBodyEl.getPadding('tb');
2584
2585         if(this.modalTitle.length){
2586             bodyHeight = bodyHeight - this.touchViewHeaderEl.getHeight();
2587         }
2588         
2589         this.touchViewBodyEl.setHeight(bodyHeight);
2590
2591         if(this.animate){
2592             var _this = this;
2593             (function(){ _this.touchViewEl.addClass(['in','show']); }).defer(50);
2594         }else{
2595             this.touchViewEl.addClass(['in','show']);
2596         }
2597         
2598         if(this._touchViewMask){
2599             Roo.get(document.body).addClass("x-body-masked");
2600             this._touchViewMask.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
2601             this._touchViewMask.setStyle('z-index', 10000);
2602             this._touchViewMask.addClass('show');
2603         }
2604         
2605         this.doTouchViewQuery();
2606         
2607     },
2608     
2609     hideTouchView : function()
2610     {
2611         this.touchViewEl.removeClass(['in','show']);
2612
2613         if(this.animate){
2614             var _this = this;
2615             (function(){ _this.touchViewEl.setStyle('display', 'none'); }).defer(150);
2616         }else{
2617             this.touchViewEl.setStyle('display', 'none');
2618         }
2619         
2620         if(this._touchViewMask){
2621             this._touchViewMask.removeClass('show');
2622             Roo.get(document.body).removeClass("x-body-masked");
2623         }
2624     },
2625     
2626     setTouchViewValue : function()
2627     {
2628         if(this.multiple){
2629             this.clearItem();
2630         
2631             var _this = this;
2632
2633             Roo.each(this.tickItems, function(o){
2634                 this.addItem(o);
2635             }, this);
2636         }
2637         
2638         this.hideTouchView();
2639     },
2640     
2641     doTouchViewQuery : function()
2642     {
2643         var qe = {
2644             query: '',
2645             forceAll: true,
2646             combo: this,
2647             cancel:false
2648         };
2649         
2650         if(this.fireEvent('beforequery', qe) ===false || qe.cancel){
2651             return false;
2652         }
2653         
2654         if(!this.alwaysQuery || this.mode == 'local'){
2655             this.onTouchViewLoad();
2656             return;
2657         }
2658         
2659         this.store.load();
2660     },
2661     
2662     onTouchViewBeforeLoad : function(combo,opts)
2663     {
2664         return;
2665     },
2666
2667     // private
2668     onTouchViewLoad : function()
2669     {
2670         if(this.store.getCount() < 1){
2671             this.onTouchViewEmptyResults();
2672             return;
2673         }
2674         
2675         this.clearTouchView();
2676         
2677         var rawValue = this.getRawValue();
2678         
2679         var template = (this.multiple) ? Roo.bootstrap.form.ComboBox.listItemCheckbox : Roo.bootstrap.form.ComboBox.listItemRadio;
2680         
2681         this.tickItems = [];
2682         
2683         this.store.data.each(function(d, rowIndex){
2684             var row = this.touchViewListGroup.createChild(template);
2685             
2686             if(typeof(d.data.cls) != 'undefined' && d.data.cls.length){
2687                 row.addClass(d.data.cls);
2688             }
2689             
2690             if(this.displayField && typeof(d.data[this.displayField]) != 'undefined'){
2691                 var cfg = {
2692                     data : d.data,
2693                     html : d.data[this.displayField]
2694                 };
2695                 
2696                 if(this.fireEvent('touchviewdisplay', this, cfg) !== false){
2697                     row.select('.roo-combobox-list-group-item-value', true).first().dom.innerHTML = cfg.html;
2698                 }
2699             }
2700             row.removeClass('selected');
2701             if(!this.multiple && this.valueField &&
2702                     typeof(d.data[this.valueField]) != 'undefined' && d.data[this.valueField] == this.getValue())
2703             {
2704                 // radio buttons..
2705                 row.select('.roo-combobox-list-group-item-box > input', true).first().attr('checked', true);
2706                 row.addClass('selected');
2707             }
2708             
2709             if(this.multiple && this.valueField &&
2710                     typeof(d.data[this.valueField]) != 'undefined' && this.getValue().indexOf(d.data[this.valueField]) != -1)
2711             {
2712                 
2713                 // checkboxes...
2714                 row.select('.roo-combobox-list-group-item-box > input', true).first().attr('checked', true);
2715                 this.tickItems.push(d.data);
2716             }
2717             
2718             row.on('click', this.onTouchViewClick, this, {row : row, rowIndex : rowIndex});
2719             
2720         }, this);
2721         
2722         var firstChecked = this.touchViewListGroup.select('.list-group-item > .roo-combobox-list-group-item-box > input:checked', true).first();
2723         
2724         var bodyHeight = Roo.lib.Dom.getViewHeight() - this.touchViewFooterEl.getHeight() + this.touchViewBodyEl.getPadding('tb');
2725
2726         if(this.modalTitle.length){
2727             bodyHeight = bodyHeight - this.touchViewHeaderEl.getHeight();
2728         }
2729
2730         var listHeight = this.touchViewListGroup.getHeight() + this.touchViewBodyEl.getPadding('tb') * 2;
2731         
2732         if(this.mobile_restrict_height && listHeight < bodyHeight){
2733             this.touchViewBodyEl.setHeight(listHeight);
2734         }
2735         
2736         var _this = this;
2737         
2738         if(firstChecked && listHeight > bodyHeight){
2739             (function() { firstChecked.findParent('li').scrollIntoView(_this.touchViewListGroup.dom); }).defer(500);
2740         }
2741         
2742     },
2743     
2744     onTouchViewLoadException : function()
2745     {
2746         this.hideTouchView();
2747     },
2748     
2749     onTouchViewEmptyResults : function()
2750     {
2751         this.clearTouchView();
2752         
2753         this.touchViewListGroup.createChild(Roo.bootstrap.form.ComboBox.emptyResult);
2754         
2755         this.touchViewListGroup.select('.roo-combobox-touch-view-empty-result', true).first().dom.innerHTML = this.emptyResultText;
2756         
2757     },
2758     
2759     clearTouchView : function()
2760     {
2761         this.touchViewListGroup.dom.innerHTML = '';
2762     },
2763     
2764     onTouchViewClick : function(e, el, o)
2765     {
2766         e.preventDefault();
2767         
2768         var row = o.row;
2769         var rowIndex = o.rowIndex;
2770         
2771         var r = this.store.getAt(rowIndex);
2772         
2773         if(this.fireEvent('beforeselect', this, r, rowIndex) !== false){
2774             
2775             if(!this.multiple){
2776                 Roo.each(this.touchViewListGroup.select('.list-group-item > .roo-combobox-list-group-item-box > input:checked', true).elements, function(c){
2777                     c.dom.removeAttribute('checked');
2778                 }, this);
2779
2780                 row.select('.roo-combobox-list-group-item-box > input', true).first().attr('checked', true);
2781
2782                 this.setFromData(r.data);
2783
2784                 var close = this.closeTriggerEl();
2785
2786                 if(close){
2787                     close.show();
2788                 }
2789
2790                 this.hideTouchView();
2791
2792                 this.fireEvent('select', this, r, rowIndex);
2793
2794                 return;
2795             }
2796
2797             if(this.valueField && typeof(r.data[this.valueField]) != 'undefined' && this.getValue().indexOf(r.data[this.valueField]) != -1){
2798                 row.select('.roo-combobox-list-group-item-box > input', true).first().dom.removeAttribute('checked');
2799                 this.tickItems.splice(this.tickItems.indexOf(r.data), 1);
2800                 return;
2801             }
2802
2803             row.select('.roo-combobox-list-group-item-box > input', true).first().attr('checked', true);
2804             this.addItem(r.data);
2805             this.tickItems.push(r.data);
2806         }
2807     },
2808     
2809     getAutoCreateNativeIOS : function()
2810     {
2811         var cfg = {
2812             cls: 'form-group' //input-group,
2813         };
2814         
2815         var combobox =  {
2816             tag: 'select',
2817             cls : 'roo-ios-select'
2818         };
2819         
2820         if (this.name) {
2821             combobox.name = this.name;
2822         }
2823         
2824         if (this.disabled) {
2825             combobox.disabled = true;
2826         }
2827         
2828         var settings = this;
2829         
2830         ['xs','sm','md','lg'].map(function(size){
2831             if (settings[size]) {
2832                 cfg.cls += ' col-' + size + '-' + settings[size];
2833             }
2834         });
2835         
2836         cfg.cn = combobox;
2837         
2838         return cfg;
2839         
2840     },
2841     
2842     initIOSView : function()
2843     {
2844         this.store.on('load', this.onIOSViewLoad, this);
2845         
2846         return;
2847     },
2848     
2849     onIOSViewLoad : function()
2850     {
2851         if(this.store.getCount() < 1){
2852             return;
2853         }
2854         
2855         this.clearIOSView();
2856         
2857         if(this.allowBlank) {
2858             
2859             var default_text = '-- SELECT --';
2860             
2861             if(this.placeholder.length){
2862                 default_text = this.placeholder;
2863             }
2864             
2865             if(this.emptyTitle.length){
2866                 default_text += ' - ' + this.emptyTitle + ' -';
2867             }
2868             
2869             var opt = this.inputEl().createChild({
2870                 tag: 'option',
2871                 value : 0,
2872                 html : default_text
2873             });
2874             
2875             var o = {};
2876             o[this.valueField] = 0;
2877             o[this.displayField] = default_text;
2878             
2879             this.ios_options.push({
2880                 data : o,
2881                 el : opt
2882             });
2883             
2884         }
2885         
2886         this.store.data.each(function(d, rowIndex){
2887             
2888             var html = '';
2889             
2890             if(this.displayField && typeof(d.data[this.displayField]) != 'undefined'){
2891                 html = d.data[this.displayField];
2892             }
2893             
2894             var value = '';
2895             
2896             if(this.valueField && typeof(d.data[this.valueField]) != 'undefined'){
2897                 value = d.data[this.valueField];
2898             }
2899             
2900             var option = {
2901                 tag: 'option',
2902                 value : value,
2903                 html : html
2904             };
2905             
2906             if(this.value == d.data[this.valueField]){
2907                 option['selected'] = true;
2908             }
2909             
2910             var opt = this.inputEl().createChild(option);
2911             
2912             this.ios_options.push({
2913                 data : d.data,
2914                 el : opt
2915             });
2916             
2917         }, this);
2918         
2919         this.inputEl().on('change', function(){
2920            this.fireEvent('select', this);
2921         }, this);
2922         
2923     },
2924     
2925     clearIOSView: function()
2926     {
2927         this.inputEl().dom.innerHTML = '';
2928         
2929         this.ios_options = [];
2930     },
2931     
2932     setIOSValue: function(v)
2933     {
2934         this.value = v;
2935         
2936         if(!this.ios_options){
2937             return;
2938         }
2939         
2940         Roo.each(this.ios_options, function(opts){
2941            
2942            opts.el.dom.removeAttribute('selected');
2943            
2944            if(opts.data[this.valueField] != v){
2945                return;
2946            }
2947            
2948            opts.el.dom.setAttribute('selected', true);
2949            
2950         }, this);
2951     }
2952
2953     /** 
2954     * @cfg {Boolean} grow 
2955     * @hide 
2956     */
2957     /** 
2958     * @cfg {Number} growMin 
2959     * @hide 
2960     */
2961     /** 
2962     * @cfg {Number} growMax 
2963     * @hide 
2964     */
2965     /**
2966      * @hide
2967      * @method autoSize
2968      */
2969 });
2970
2971 Roo.apply(Roo.bootstrap.form.ComboBox,  {
2972     
2973     header : {
2974         tag: 'div',
2975         cls: 'modal-header',
2976         cn: [
2977             {
2978                 tag: 'h4',
2979                 cls: 'modal-title'
2980             }
2981         ]
2982     },
2983     
2984     body : {
2985         tag: 'div',
2986         cls: 'modal-body',
2987         cn: [
2988             {
2989                 tag: 'ul',
2990                 cls: 'list-group'
2991             }
2992         ]
2993     },
2994     
2995     listItemRadio : {
2996         tag: 'li',
2997         cls: 'list-group-item',
2998         cn: [
2999             {
3000                 tag: 'span',
3001                 cls: 'roo-combobox-list-group-item-value'
3002             },
3003             {
3004                 tag: 'div',
3005                 cls: 'roo-combobox-list-group-item-box pull-xs-right radio-inline radio radio-info',
3006                 cn: [
3007                     {
3008                         tag: 'input',
3009                         type: 'radio'
3010                     },
3011                     {
3012                         tag: 'label'
3013                     }
3014                 ]
3015             }
3016         ]
3017     },
3018     
3019     listItemCheckbox : {
3020         tag: 'li',
3021         cls: 'list-group-item',
3022         cn: [
3023             {
3024                 tag: 'span',
3025                 cls: 'roo-combobox-list-group-item-value'
3026             },
3027             {
3028                 tag: 'div',
3029                 cls: 'roo-combobox-list-group-item-box pull-xs-right checkbox-inline checkbox checkbox-info',
3030                 cn: [
3031                     {
3032                         tag: 'input',
3033                         type: 'checkbox'
3034                     },
3035                     {
3036                         tag: 'label'
3037                     }
3038                 ]
3039             }
3040         ]
3041     },
3042     
3043     emptyResult : {
3044         tag: 'div',
3045         cls: 'alert alert-danger roo-combobox-touch-view-empty-result'
3046     },
3047     
3048     footer : {
3049         tag: 'div',
3050         cls: 'modal-footer',
3051         cn: [
3052             {
3053                 tag: 'div',
3054                 cls: 'row',
3055                 cn: [
3056                     {
3057                         tag: 'div',
3058                         cls: 'col-xs-6 text-left',
3059                         cn: {
3060                             tag: 'button',
3061                             cls: 'btn btn-danger roo-touch-view-cancel',
3062                             html: 'Cancel'
3063                         }
3064                     },
3065                     {
3066                         tag: 'div',
3067                         cls: 'col-xs-6 text-right',
3068                         cn: {
3069                             tag: 'button',
3070                             cls: 'btn btn-success roo-touch-view-ok',
3071                             html: 'OK'
3072                         }
3073                     }
3074                 ]
3075             }
3076         ]
3077         
3078     }
3079 });
3080
3081 Roo.apply(Roo.bootstrap.form.ComboBox,  {
3082     
3083     touchViewTemplate : {
3084         tag: 'div',
3085         cls: 'modal fade roo-combobox-touch-view',
3086         cn: [
3087             {
3088                 tag: 'div',
3089                 cls: 'modal-dialog',
3090                 style : 'position:fixed', // we have to fix position....
3091                 cn: [
3092                     {
3093                         tag: 'div',
3094                         cls: 'modal-content',
3095                         cn: [
3096                             Roo.bootstrap.form.ComboBox.header,
3097                             Roo.bootstrap.form.ComboBox.body,
3098                             Roo.bootstrap.form.ComboBox.footer
3099                         ]
3100                     }
3101                 ]
3102             }
3103         ]
3104     }
3105 });