support listClass for bootstrap combobox
[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.fireEvent('expand', this);
1833     },
1834
1835     // private
1836     // Implements the default empty TriggerField.onTriggerClick function
1837     onTriggerClick : function(e)
1838     {
1839         Roo.log('trigger click');
1840         
1841         if(this.disabled || !this.triggerList){
1842             return;
1843         }
1844         
1845         this.page = 0;
1846         this.loadNext = false;
1847         
1848         if(this.isExpanded()){
1849             this.collapse();
1850             if (!this.blockFocus) {
1851                 this.inputEl().focus();
1852             }
1853             
1854         }else {
1855             this.hasFocus = true;
1856             if(this.triggerAction == 'all') {
1857                 this.doQuery(this.allQuery, true);
1858             } else {
1859                 this.doQuery(this.getRawValue());
1860             }
1861             if (!this.blockFocus) {
1862                 this.inputEl().focus();
1863             }
1864         }
1865     },
1866     
1867     onTickableTriggerClick : function(e)
1868     {
1869         if(this.disabled){
1870             return;
1871         }
1872         
1873         this.page = 0;
1874         this.loadNext = false;
1875         this.hasFocus = true;
1876         
1877         if(this.triggerAction == 'all') {
1878             this.doQuery(this.allQuery, true);
1879         } else {
1880             this.doQuery(this.getRawValue());
1881         }
1882     },
1883     
1884     onSearchFieldClick : function(e)
1885     {
1886         if(this.hasFocus && !this.disabled && e.getTarget().nodeName.toLowerCase() != 'button'){
1887             this.onTickableFooterButtonClick(e, false, false);
1888             return;
1889         }
1890         
1891         if(this.hasFocus || this.disabled || e.getTarget().nodeName.toLowerCase() == 'button'){
1892             return;
1893         }
1894         
1895         this.page = 0;
1896         this.loadNext = false;
1897         this.hasFocus = true;
1898         
1899         if(this.triggerAction == 'all') {
1900             this.doQuery(this.allQuery, true);
1901         } else {
1902             this.doQuery(this.getRawValue());
1903         }
1904     },
1905     
1906     listKeyPress : function(e)
1907     {
1908         //Roo.log('listkeypress');
1909         // scroll to first matching element based on key pres..
1910         if (e.isSpecialKey()) {
1911             return false;
1912         }
1913         var k = String.fromCharCode(e.getKey()).toUpperCase();
1914         //Roo.log(k);
1915         var match  = false;
1916         var csel = this.view.getSelectedNodes();
1917         var cselitem = false;
1918         if (csel.length) {
1919             var ix = this.view.indexOf(csel[0]);
1920             cselitem  = this.store.getAt(ix);
1921             if (!cselitem.get(this.displayField) || cselitem.get(this.displayField).substring(0,1).toUpperCase() != k) {
1922                 cselitem = false;
1923             }
1924             
1925         }
1926         
1927         this.store.each(function(v) { 
1928             if (cselitem) {
1929                 // start at existing selection.
1930                 if (cselitem.id == v.id) {
1931                     cselitem = false;
1932                 }
1933                 return true;
1934             }
1935                 
1936             if (v.get(this.displayField) && v.get(this.displayField).substring(0,1).toUpperCase() == k) {
1937                 match = this.store.indexOf(v);
1938                 return false;
1939             }
1940             return true;
1941         }, this);
1942         
1943         if (match === false) {
1944             return true; // no more action?
1945         }
1946         // scroll to?
1947         this.view.select(match);
1948         var sn = Roo.get(this.view.getSelectedNodes()[0]);
1949         sn.scrollIntoView(sn.dom.parentNode, false);
1950     },
1951     
1952     onViewScroll : function(e, t){
1953         
1954         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){
1955             return;
1956         }
1957         
1958         this.hasQuery = true;
1959         
1960         this.loading = this.list.select('.loading', true).first();
1961         
1962         if(this.loading === null){
1963             this.list.createChild({
1964                 tag: 'div',
1965                 cls: 'loading roo-select2-more-results roo-select2-active',
1966                 html: 'Loading more results...'
1967             });
1968             
1969             this.loading = this.list.select('.loading', true).first();
1970             
1971             this.loading.setVisibilityMode(Roo.Element.DISPLAY);
1972             
1973             this.loading.hide();
1974         }
1975         
1976         this.loading.show();
1977         
1978         var _combo = this;
1979         
1980         this.page++;
1981         this.loadNext = true;
1982         
1983         (function() { _combo.doQuery(_combo.allQuery, true); }).defer(500);
1984         
1985         return;
1986     },
1987     
1988     addItem : function(o)
1989     {   
1990         var dv = ''; // display value
1991         
1992         if (this.displayField) {
1993             dv = !o || typeof(o[this.displayField]) == 'undefined' ? '' : o[this.displayField];
1994         } else {
1995             // this is an error condition!!!
1996             Roo.log('no  displayField value set for '+ (this.name ? this.name : this.id));
1997         }
1998         
1999         if(!dv.length){
2000             return;
2001         }
2002         
2003         var choice = this.choices.createChild({
2004             tag: 'li',
2005             cls: 'roo-select2-search-choice',
2006             cn: [
2007                 {
2008                     tag: 'div',
2009                     html: dv
2010                 },
2011                 {
2012                     tag: 'a',
2013                     href: '#',
2014                     cls: 'roo-select2-search-choice-close fa fa-times',
2015                     tabindex: '-1'
2016                 }
2017             ]
2018             
2019         }, this.searchField);
2020         
2021         var close = choice.select('a.roo-select2-search-choice-close', true).first();
2022         
2023         close.on('click', this.onRemoveItem, this, { item : choice, data : o} );
2024         
2025         this.item.push(o);
2026         
2027         this.lastData = o;
2028         
2029         this.syncValue();
2030         
2031         this.inputEl().dom.value = '';
2032         
2033         this.validate();
2034     },
2035     
2036     onRemoveItem : function(e, _self, o)
2037     {
2038         e.preventDefault();
2039         
2040         this.lastItem = Roo.apply([], this.item);
2041         
2042         var index = this.item.indexOf(o.data) * 1;
2043         
2044         if( index < 0){
2045             Roo.log('not this item?!');
2046             return;
2047         }
2048         
2049         this.item.splice(index, 1);
2050         o.item.remove();
2051         
2052         this.syncValue();
2053         
2054         this.fireEvent('remove', this, e);
2055         
2056         this.validate();
2057         
2058     },
2059     
2060     syncValue : function()
2061     {
2062         if(!this.item.length){
2063             this.clearValue();
2064             return;
2065         }
2066             
2067         var value = [];
2068         var _this = this;
2069         Roo.each(this.item, function(i){
2070             if(_this.valueField){
2071                 value.push(i[_this.valueField]);
2072                 return;
2073             }
2074
2075             value.push(i);
2076         });
2077
2078         this.value = value.join(',');
2079
2080         if(this.hiddenField){
2081             this.hiddenField.dom.value = this.value;
2082         }
2083         
2084         this.store.fireEvent("datachanged", this.store);
2085         
2086         this.validate();
2087     },
2088     
2089     clearItem : function()
2090     {
2091         if(!this.multiple){
2092             return;
2093         }
2094         
2095         this.item = [];
2096         
2097         Roo.each(this.choices.select('>li.roo-select2-search-choice', true).elements, function(c){
2098            c.remove();
2099         });
2100         
2101         this.syncValue();
2102         
2103         this.validate();
2104         
2105         if(this.tickable && !Roo.isTouch){
2106             this.view.refresh();
2107         }
2108     },
2109     
2110     inputEl: function ()
2111     {
2112         if(Roo.isIOS && this.useNativeIOS){
2113             return this.el.select('select.roo-ios-select', true).first();
2114         }
2115         
2116         if(Roo.isTouch && this.mobileTouchView){
2117             return this.el.select('input.form-control',true).first();
2118         }
2119         
2120         if(this.tickable){
2121             return this.searchField;
2122         }
2123         
2124         return this.el.select('input.form-control',true).first();
2125     },
2126     
2127     onTickableFooterButtonClick : function(e, btn, el)
2128     {
2129         e.preventDefault();
2130         
2131         this.lastItem = Roo.apply([], this.item);
2132         
2133         if(btn && btn.name == 'cancel'){
2134             this.tickItems = Roo.apply([], this.item);
2135             this.collapse();
2136             return;
2137         }
2138         
2139         this.clearItem();
2140         
2141         var _this = this;
2142         
2143         Roo.each(this.tickItems, function(o){
2144             _this.addItem(o);
2145         });
2146         
2147         this.collapse();
2148         
2149     },
2150     
2151     validate : function()
2152     {
2153         if(this.getVisibilityEl().hasClass('hidden')){
2154             return true;
2155         }
2156         
2157         var v = this.getRawValue();
2158         
2159         if(this.multiple){
2160             v = this.getValue();
2161         }
2162         
2163         if(this.disabled || this.allowBlank || v.length){
2164             this.markValid();
2165             return true;
2166         }
2167         
2168         this.markInvalid();
2169         return false;
2170     },
2171     
2172     tickableInputEl : function()
2173     {
2174         if(!this.tickable || !this.editable){
2175             return this.inputEl();
2176         }
2177         
2178         return this.inputEl().select('.roo-select2-search-field-input', true).first();
2179     },
2180     
2181     
2182     getAutoCreateTouchView : function()
2183     {
2184         var id = Roo.id();
2185         
2186         var cfg = {
2187             cls: 'form-group' //input-group
2188         };
2189         
2190         var input =  {
2191             tag: 'input',
2192             id : id,
2193             type : this.inputType,
2194             cls : 'form-control x-combo-noedit',
2195             autocomplete: 'new-password',
2196             placeholder : this.placeholder || '',
2197             readonly : true
2198         };
2199         
2200         if (this.name) {
2201             input.name = this.name;
2202         }
2203         
2204         if (this.size) {
2205             input.cls += ' input-' + this.size;
2206         }
2207         
2208         if (this.disabled) {
2209             input.disabled = true;
2210         }
2211         
2212         var inputblock = {
2213             cls : 'roo-combobox-wrap',
2214             cn : [
2215                 input
2216             ]
2217         };
2218         
2219         if(this.before){
2220             inputblock.cls += ' input-group';
2221             
2222             inputblock.cn.unshift({
2223                 tag :'span',
2224                 cls : 'input-group-addon input-group-prepend input-group-text',
2225                 html : this.before
2226             });
2227         }
2228         
2229         if(this.removable && !this.multiple){
2230             inputblock.cls += ' roo-removable';
2231             
2232             inputblock.cn.push({
2233                 tag: 'button',
2234                 html : 'x',
2235                 cls : 'roo-combo-removable-btn close'
2236             });
2237         }
2238
2239         if(this.hasFeedback && !this.allowBlank){
2240             
2241             inputblock.cls += ' has-feedback';
2242             
2243             inputblock.cn.push({
2244                 tag: 'span',
2245                 cls: 'glyphicon form-control-feedback'
2246             });
2247             
2248         }
2249         
2250         if (this.after) {
2251             
2252             inputblock.cls += (this.before) ? '' : ' input-group';
2253             
2254             inputblock.cn.push({
2255                 tag :'span',
2256                 cls : 'input-group-addon input-group-append input-group-text',
2257                 html : this.after
2258             });
2259         }
2260
2261         
2262         var ibwrap = inputblock;
2263         
2264         if(this.multiple){
2265             ibwrap = {
2266                 tag: 'ul',
2267                 cls: 'roo-select2-choices',
2268                 cn:[
2269                     {
2270                         tag: 'li',
2271                         cls: 'roo-select2-search-field',
2272                         cn: [
2273
2274                             inputblock
2275                         ]
2276                     }
2277                 ]
2278             };
2279         
2280             
2281         }
2282         
2283         var combobox = {
2284             cls: 'roo-select2-container input-group roo-touchview-combobox ',
2285             cn: [
2286                 {
2287                     tag: 'input',
2288                     type : 'hidden',
2289                     cls: 'form-hidden-field'
2290                 },
2291                 ibwrap
2292             ]
2293         };
2294         
2295         if(!this.multiple && this.showToggleBtn){
2296             
2297             var caret = {
2298                 cls: 'caret'
2299             };
2300             
2301             if (this.caret != false) {
2302                 caret = {
2303                      tag: 'i',
2304                      cls: 'fa fa-' + this.caret
2305                 };
2306                 
2307             }
2308             
2309             combobox.cn.push({
2310                 tag :'span',
2311                 cls : 'input-group-addon input-group-append input-group-text btn dropdown-toggle',
2312                 cn : [
2313                     Roo.bootstrap.version == 3 ? caret : '',
2314                     {
2315                         tag: 'span',
2316                         cls: 'combobox-clear',
2317                         cn  : [
2318                             {
2319                                 tag : 'i',
2320                                 cls: 'icon-remove'
2321                             }
2322                         ]
2323                     }
2324                 ]
2325
2326             })
2327         }
2328         
2329         if(this.multiple){
2330             combobox.cls += ' roo-select2-container-multi';
2331         }
2332         
2333         var required =  this.allowBlank ?  {
2334                     tag : 'i',
2335                     style: 'display: none'
2336                 } : {
2337                    tag : 'i',
2338                    cls : 'roo-required-indicator left-indicator text-danger fa fa-lg fa-star',
2339                    tooltip : 'This field is required'
2340                 };
2341         
2342         var align = this.labelAlign || this.parentLabelAlign();
2343         
2344         if (align ==='left' && this.fieldLabel.length) {
2345
2346             cfg.cn = [
2347                 required,
2348                 {
2349                     tag: 'label',
2350                     cls : 'control-label col-form-label',
2351                     html : this.fieldLabel
2352
2353                 },
2354                 {
2355                     cls : 'roo-combobox-wrap ', 
2356                     cn: [
2357                         combobox
2358                     ]
2359                 }
2360             ];
2361             
2362             var labelCfg = cfg.cn[1];
2363             var contentCfg = cfg.cn[2];
2364             
2365
2366             if(this.indicatorpos == 'right'){
2367                 cfg.cn = [
2368                     {
2369                         tag: 'label',
2370                         'for' :  id,
2371                         cls : 'control-label col-form-label',
2372                         cn : [
2373                             {
2374                                 tag : 'span',
2375                                 html : this.fieldLabel
2376                             },
2377                             required
2378                         ]
2379                     },
2380                     {
2381                         cls : "roo-combobox-wrap ",
2382                         cn: [
2383                             combobox
2384                         ]
2385                     }
2386
2387                 ];
2388                 
2389                 labelCfg = cfg.cn[0];
2390                 contentCfg = cfg.cn[1];
2391             }
2392             
2393            
2394             
2395             if(this.labelWidth > 12){
2396                 labelCfg.style = "width: " + this.labelWidth + 'px';
2397             }
2398            
2399             if(this.labelWidth < 13 && this.labelmd == 0){
2400                 this.labelmd = this.labelWidth;
2401             }
2402             
2403             if(this.labellg > 0){
2404                 labelCfg.cls += ' col-lg-' + this.labellg;
2405                 contentCfg.cls += ' col-lg-' + (12 - this.labellg);
2406             }
2407             
2408             if(this.labelmd > 0){
2409                 labelCfg.cls += ' col-md-' + this.labelmd;
2410                 contentCfg.cls += ' col-md-' + (12 - this.labelmd);
2411             }
2412             
2413             if(this.labelsm > 0){
2414                 labelCfg.cls += ' col-sm-' + this.labelsm;
2415                 contentCfg.cls += ' col-sm-' + (12 - this.labelsm);
2416             }
2417             
2418             if(this.labelxs > 0){
2419                 labelCfg.cls += ' col-xs-' + this.labelxs;
2420                 contentCfg.cls += ' col-xs-' + (12 - this.labelxs);
2421             }
2422                 
2423                 
2424         } else if ( this.fieldLabel.length) {
2425             cfg.cn = [
2426                required,
2427                 {
2428                     tag: 'label',
2429                     cls : 'control-label',
2430                     html : this.fieldLabel
2431
2432                 },
2433                 {
2434                     cls : '', 
2435                     cn: [
2436                         combobox
2437                     ]
2438                 }
2439             ];
2440             
2441             if(this.indicatorpos == 'right'){
2442                 cfg.cn = [
2443                     {
2444                         tag: 'label',
2445                         cls : 'control-label',
2446                         html : this.fieldLabel,
2447                         cn : [
2448                             required
2449                         ]
2450                     },
2451                     {
2452                         cls : '', 
2453                         cn: [
2454                             combobox
2455                         ]
2456                     }
2457                 ];
2458             }
2459         } else {
2460             cfg.cn = combobox;    
2461         }
2462         
2463         
2464         var settings = this;
2465         
2466         ['xs','sm','md','lg'].map(function(size){
2467             if (settings[size]) {
2468                 cfg.cls += ' col-' + size + '-' + settings[size];
2469             }
2470         });
2471         
2472         return cfg;
2473     },
2474     
2475     initTouchView : function()
2476     {
2477         this.renderTouchView();
2478         
2479         this.touchViewEl.on('scroll', function(){
2480             this.el.dom.scrollTop = 0;
2481         }, this);
2482         
2483         this.originalValue = this.getValue();
2484         
2485         this.triggerEl = this.el.select('span.dropdown-toggle',true).first();
2486         
2487         this.inputEl().on("click", this.showTouchView, this);
2488         if (this.triggerEl) {
2489             this.triggerEl.on("click", this.showTouchView, this);
2490         }
2491         
2492         
2493         this.touchViewFooterEl.select('.roo-touch-view-cancel', true).first().on('click', this.hideTouchView, this);
2494         this.touchViewFooterEl.select('.roo-touch-view-ok', true).first().on('click', this.setTouchViewValue, this);
2495         
2496         this.maskEl = new Roo.LoadMask(this.touchViewEl, { store : this.store, msgCls: 'roo-el-mask-msg' });
2497         
2498         this.store.on('beforeload', this.onTouchViewBeforeLoad, this);
2499         this.store.on('load', this.onTouchViewLoad, this);
2500         this.store.on('loadexception', this.onTouchViewLoadException, this);
2501         
2502         if(this.hiddenName){
2503             
2504             this.hiddenField = this.el.select('input.form-hidden-field',true).first();
2505             
2506             this.hiddenField.dom.value =
2507                 this.hiddenValue !== undefined ? this.hiddenValue :
2508                 this.value !== undefined ? this.value : '';
2509         
2510             this.el.dom.removeAttribute('name');
2511             this.hiddenField.dom.setAttribute('name', this.hiddenName);
2512         }
2513         
2514         if(this.multiple){
2515             this.choices = this.el.select('ul.roo-select2-choices', true).first();
2516             this.searchField = this.el.select('ul li.roo-select2-search-field', true).first();
2517         }
2518         
2519         if(this.removable && !this.multiple){
2520             var close = this.closeTriggerEl();
2521             if(close){
2522                 close.setVisibilityMode(Roo.Element.DISPLAY).hide();
2523                 close.on('click', this.removeBtnClick, this, close);
2524             }
2525         }
2526         /*
2527          * fix the bug in Safari iOS8
2528          */
2529         this.inputEl().on("focus", function(e){
2530             document.activeElement.blur();
2531         }, this);
2532         
2533         this._touchViewMask = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
2534         
2535         return;
2536         
2537         
2538     },
2539     
2540     renderTouchView : function()
2541     {
2542         this.touchViewEl = Roo.get(document.body).createChild(Roo.bootstrap.form.ComboBox.touchViewTemplate);
2543         this.touchViewEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2544         
2545         this.touchViewHeaderEl = this.touchViewEl.select('.modal-header', true).first();
2546         this.touchViewHeaderEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2547         
2548         this.touchViewBodyEl = this.touchViewEl.select('.modal-body', true).first();
2549         this.touchViewBodyEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2550         this.touchViewBodyEl.setStyle('overflow', 'auto');
2551         
2552         this.touchViewListGroup = this.touchViewBodyEl.select('.list-group', true).first();
2553         this.touchViewListGroup.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2554         
2555         this.touchViewFooterEl = this.touchViewEl.select('.modal-footer', true).first();
2556         this.touchViewFooterEl.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
2557         
2558     },
2559     
2560     showTouchView : function()
2561     {
2562         if(this.disabled){
2563             return;
2564         }
2565         
2566         this.touchViewHeaderEl.hide();
2567
2568         if(this.modalTitle.length){
2569             this.touchViewHeaderEl.dom.innerHTML = this.modalTitle;
2570             this.touchViewHeaderEl.show();
2571         }
2572
2573         this.touchViewEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
2574         this.touchViewEl.show();
2575
2576         this.touchViewEl.select('.modal-dialog', true).first().setStyle({ margin : '0px', width : '100%'});
2577         
2578         //this.touchViewEl.select('.modal-dialog > .modal-content', true).first().setSize(
2579         //        Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
2580
2581         var bodyHeight = Roo.lib.Dom.getViewHeight() - this.touchViewFooterEl.getHeight() + this.touchViewBodyEl.getPadding('tb');
2582
2583         if(this.modalTitle.length){
2584             bodyHeight = bodyHeight - this.touchViewHeaderEl.getHeight();
2585         }
2586         
2587         this.touchViewBodyEl.setHeight(bodyHeight);
2588
2589         if(this.animate){
2590             var _this = this;
2591             (function(){ _this.touchViewEl.addClass(['in','show']); }).defer(50);
2592         }else{
2593             this.touchViewEl.addClass(['in','show']);
2594         }
2595         
2596         if(this._touchViewMask){
2597             Roo.get(document.body).addClass("x-body-masked");
2598             this._touchViewMask.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
2599             this._touchViewMask.setStyle('z-index', 10000);
2600             this._touchViewMask.addClass('show');
2601         }
2602         
2603         this.doTouchViewQuery();
2604         
2605     },
2606     
2607     hideTouchView : function()
2608     {
2609         this.touchViewEl.removeClass(['in','show']);
2610
2611         if(this.animate){
2612             var _this = this;
2613             (function(){ _this.touchViewEl.setStyle('display', 'none'); }).defer(150);
2614         }else{
2615             this.touchViewEl.setStyle('display', 'none');
2616         }
2617         
2618         if(this._touchViewMask){
2619             this._touchViewMask.removeClass('show');
2620             Roo.get(document.body).removeClass("x-body-masked");
2621         }
2622     },
2623     
2624     setTouchViewValue : function()
2625     {
2626         if(this.multiple){
2627             this.clearItem();
2628         
2629             var _this = this;
2630
2631             Roo.each(this.tickItems, function(o){
2632                 this.addItem(o);
2633             }, this);
2634         }
2635         
2636         this.hideTouchView();
2637     },
2638     
2639     doTouchViewQuery : function()
2640     {
2641         var qe = {
2642             query: '',
2643             forceAll: true,
2644             combo: this,
2645             cancel:false
2646         };
2647         
2648         if(this.fireEvent('beforequery', qe) ===false || qe.cancel){
2649             return false;
2650         }
2651         
2652         if(!this.alwaysQuery || this.mode == 'local'){
2653             this.onTouchViewLoad();
2654             return;
2655         }
2656         
2657         this.store.load();
2658     },
2659     
2660     onTouchViewBeforeLoad : function(combo,opts)
2661     {
2662         return;
2663     },
2664
2665     // private
2666     onTouchViewLoad : function()
2667     {
2668         if(this.store.getCount() < 1){
2669             this.onTouchViewEmptyResults();
2670             return;
2671         }
2672         
2673         this.clearTouchView();
2674         
2675         var rawValue = this.getRawValue();
2676         
2677         var template = (this.multiple) ? Roo.bootstrap.form.ComboBox.listItemCheckbox : Roo.bootstrap.form.ComboBox.listItemRadio;
2678         
2679         this.tickItems = [];
2680         
2681         this.store.data.each(function(d, rowIndex){
2682             var row = this.touchViewListGroup.createChild(template);
2683             
2684             if(typeof(d.data.cls) != 'undefined' && d.data.cls.length){
2685                 row.addClass(d.data.cls);
2686             }
2687             
2688             if(this.displayField && typeof(d.data[this.displayField]) != 'undefined'){
2689                 var cfg = {
2690                     data : d.data,
2691                     html : d.data[this.displayField]
2692                 };
2693                 
2694                 if(this.fireEvent('touchviewdisplay', this, cfg) !== false){
2695                     row.select('.roo-combobox-list-group-item-value', true).first().dom.innerHTML = cfg.html;
2696                 }
2697             }
2698             row.removeClass('selected');
2699             if(!this.multiple && this.valueField &&
2700                     typeof(d.data[this.valueField]) != 'undefined' && d.data[this.valueField] == this.getValue())
2701             {
2702                 // radio buttons..
2703                 row.select('.roo-combobox-list-group-item-box > input', true).first().attr('checked', true);
2704                 row.addClass('selected');
2705             }
2706             
2707             if(this.multiple && this.valueField &&
2708                     typeof(d.data[this.valueField]) != 'undefined' && this.getValue().indexOf(d.data[this.valueField]) != -1)
2709             {
2710                 
2711                 // checkboxes...
2712                 row.select('.roo-combobox-list-group-item-box > input', true).first().attr('checked', true);
2713                 this.tickItems.push(d.data);
2714             }
2715             
2716             row.on('click', this.onTouchViewClick, this, {row : row, rowIndex : rowIndex});
2717             
2718         }, this);
2719         
2720         var firstChecked = this.touchViewListGroup.select('.list-group-item > .roo-combobox-list-group-item-box > input:checked', true).first();
2721         
2722         var bodyHeight = Roo.lib.Dom.getViewHeight() - this.touchViewFooterEl.getHeight() + this.touchViewBodyEl.getPadding('tb');
2723
2724         if(this.modalTitle.length){
2725             bodyHeight = bodyHeight - this.touchViewHeaderEl.getHeight();
2726         }
2727
2728         var listHeight = this.touchViewListGroup.getHeight() + this.touchViewBodyEl.getPadding('tb') * 2;
2729         
2730         if(this.mobile_restrict_height && listHeight < bodyHeight){
2731             this.touchViewBodyEl.setHeight(listHeight);
2732         }
2733         
2734         var _this = this;
2735         
2736         if(firstChecked && listHeight > bodyHeight){
2737             (function() { firstChecked.findParent('li').scrollIntoView(_this.touchViewListGroup.dom); }).defer(500);
2738         }
2739         
2740     },
2741     
2742     onTouchViewLoadException : function()
2743     {
2744         this.hideTouchView();
2745     },
2746     
2747     onTouchViewEmptyResults : function()
2748     {
2749         this.clearTouchView();
2750         
2751         this.touchViewListGroup.createChild(Roo.bootstrap.form.ComboBox.emptyResult);
2752         
2753         this.touchViewListGroup.select('.roo-combobox-touch-view-empty-result', true).first().dom.innerHTML = this.emptyResultText;
2754         
2755     },
2756     
2757     clearTouchView : function()
2758     {
2759         this.touchViewListGroup.dom.innerHTML = '';
2760     },
2761     
2762     onTouchViewClick : function(e, el, o)
2763     {
2764         e.preventDefault();
2765         
2766         var row = o.row;
2767         var rowIndex = o.rowIndex;
2768         
2769         var r = this.store.getAt(rowIndex);
2770         
2771         if(this.fireEvent('beforeselect', this, r, rowIndex) !== false){
2772             
2773             if(!this.multiple){
2774                 Roo.each(this.touchViewListGroup.select('.list-group-item > .roo-combobox-list-group-item-box > input:checked', true).elements, function(c){
2775                     c.dom.removeAttribute('checked');
2776                 }, this);
2777
2778                 row.select('.roo-combobox-list-group-item-box > input', true).first().attr('checked', true);
2779
2780                 this.setFromData(r.data);
2781
2782                 var close = this.closeTriggerEl();
2783
2784                 if(close){
2785                     close.show();
2786                 }
2787
2788                 this.hideTouchView();
2789
2790                 this.fireEvent('select', this, r, rowIndex);
2791
2792                 return;
2793             }
2794
2795             if(this.valueField && typeof(r.data[this.valueField]) != 'undefined' && this.getValue().indexOf(r.data[this.valueField]) != -1){
2796                 row.select('.roo-combobox-list-group-item-box > input', true).first().dom.removeAttribute('checked');
2797                 this.tickItems.splice(this.tickItems.indexOf(r.data), 1);
2798                 return;
2799             }
2800
2801             row.select('.roo-combobox-list-group-item-box > input', true).first().attr('checked', true);
2802             this.addItem(r.data);
2803             this.tickItems.push(r.data);
2804         }
2805     },
2806     
2807     getAutoCreateNativeIOS : function()
2808     {
2809         var cfg = {
2810             cls: 'form-group' //input-group,
2811         };
2812         
2813         var combobox =  {
2814             tag: 'select',
2815             cls : 'roo-ios-select'
2816         };
2817         
2818         if (this.name) {
2819             combobox.name = this.name;
2820         }
2821         
2822         if (this.disabled) {
2823             combobox.disabled = true;
2824         }
2825         
2826         var settings = this;
2827         
2828         ['xs','sm','md','lg'].map(function(size){
2829             if (settings[size]) {
2830                 cfg.cls += ' col-' + size + '-' + settings[size];
2831             }
2832         });
2833         
2834         cfg.cn = combobox;
2835         
2836         return cfg;
2837         
2838     },
2839     
2840     initIOSView : function()
2841     {
2842         this.store.on('load', this.onIOSViewLoad, this);
2843         
2844         return;
2845     },
2846     
2847     onIOSViewLoad : function()
2848     {
2849         if(this.store.getCount() < 1){
2850             return;
2851         }
2852         
2853         this.clearIOSView();
2854         
2855         if(this.allowBlank) {
2856             
2857             var default_text = '-- SELECT --';
2858             
2859             if(this.placeholder.length){
2860                 default_text = this.placeholder;
2861             }
2862             
2863             if(this.emptyTitle.length){
2864                 default_text += ' - ' + this.emptyTitle + ' -';
2865             }
2866             
2867             var opt = this.inputEl().createChild({
2868                 tag: 'option',
2869                 value : 0,
2870                 html : default_text
2871             });
2872             
2873             var o = {};
2874             o[this.valueField] = 0;
2875             o[this.displayField] = default_text;
2876             
2877             this.ios_options.push({
2878                 data : o,
2879                 el : opt
2880             });
2881             
2882         }
2883         
2884         this.store.data.each(function(d, rowIndex){
2885             
2886             var html = '';
2887             
2888             if(this.displayField && typeof(d.data[this.displayField]) != 'undefined'){
2889                 html = d.data[this.displayField];
2890             }
2891             
2892             var value = '';
2893             
2894             if(this.valueField && typeof(d.data[this.valueField]) != 'undefined'){
2895                 value = d.data[this.valueField];
2896             }
2897             
2898             var option = {
2899                 tag: 'option',
2900                 value : value,
2901                 html : html
2902             };
2903             
2904             if(this.value == d.data[this.valueField]){
2905                 option['selected'] = true;
2906             }
2907             
2908             var opt = this.inputEl().createChild(option);
2909             
2910             this.ios_options.push({
2911                 data : d.data,
2912                 el : opt
2913             });
2914             
2915         }, this);
2916         
2917         this.inputEl().on('change', function(){
2918            this.fireEvent('select', this);
2919         }, this);
2920         
2921     },
2922     
2923     clearIOSView: function()
2924     {
2925         this.inputEl().dom.innerHTML = '';
2926         
2927         this.ios_options = [];
2928     },
2929     
2930     setIOSValue: function(v)
2931     {
2932         this.value = v;
2933         
2934         if(!this.ios_options){
2935             return;
2936         }
2937         
2938         Roo.each(this.ios_options, function(opts){
2939            
2940            opts.el.dom.removeAttribute('selected');
2941            
2942            if(opts.data[this.valueField] != v){
2943                return;
2944            }
2945            
2946            opts.el.dom.setAttribute('selected', true);
2947            
2948         }, this);
2949     }
2950
2951     /** 
2952     * @cfg {Boolean} grow 
2953     * @hide 
2954     */
2955     /** 
2956     * @cfg {Number} growMin 
2957     * @hide 
2958     */
2959     /** 
2960     * @cfg {Number} growMax 
2961     * @hide 
2962     */
2963     /**
2964      * @hide
2965      * @method autoSize
2966      */
2967 });
2968
2969 Roo.apply(Roo.bootstrap.form.ComboBox,  {
2970     
2971     header : {
2972         tag: 'div',
2973         cls: 'modal-header',
2974         cn: [
2975             {
2976                 tag: 'h4',
2977                 cls: 'modal-title'
2978             }
2979         ]
2980     },
2981     
2982     body : {
2983         tag: 'div',
2984         cls: 'modal-body',
2985         cn: [
2986             {
2987                 tag: 'ul',
2988                 cls: 'list-group'
2989             }
2990         ]
2991     },
2992     
2993     listItemRadio : {
2994         tag: 'li',
2995         cls: 'list-group-item',
2996         cn: [
2997             {
2998                 tag: 'span',
2999                 cls: 'roo-combobox-list-group-item-value'
3000             },
3001             {
3002                 tag: 'div',
3003                 cls: 'roo-combobox-list-group-item-box pull-xs-right radio-inline radio radio-info',
3004                 cn: [
3005                     {
3006                         tag: 'input',
3007                         type: 'radio'
3008                     },
3009                     {
3010                         tag: 'label'
3011                     }
3012                 ]
3013             }
3014         ]
3015     },
3016     
3017     listItemCheckbox : {
3018         tag: 'li',
3019         cls: 'list-group-item',
3020         cn: [
3021             {
3022                 tag: 'span',
3023                 cls: 'roo-combobox-list-group-item-value'
3024             },
3025             {
3026                 tag: 'div',
3027                 cls: 'roo-combobox-list-group-item-box pull-xs-right checkbox-inline checkbox checkbox-info',
3028                 cn: [
3029                     {
3030                         tag: 'input',
3031                         type: 'checkbox'
3032                     },
3033                     {
3034                         tag: 'label'
3035                     }
3036                 ]
3037             }
3038         ]
3039     },
3040     
3041     emptyResult : {
3042         tag: 'div',
3043         cls: 'alert alert-danger roo-combobox-touch-view-empty-result'
3044     },
3045     
3046     footer : {
3047         tag: 'div',
3048         cls: 'modal-footer',
3049         cn: [
3050             {
3051                 tag: 'div',
3052                 cls: 'row',
3053                 cn: [
3054                     {
3055                         tag: 'div',
3056                         cls: 'col-xs-6 text-left',
3057                         cn: {
3058                             tag: 'button',
3059                             cls: 'btn btn-danger roo-touch-view-cancel',
3060                             html: 'Cancel'
3061                         }
3062                     },
3063                     {
3064                         tag: 'div',
3065                         cls: 'col-xs-6 text-right',
3066                         cn: {
3067                             tag: 'button',
3068                             cls: 'btn btn-success roo-touch-view-ok',
3069                             html: 'OK'
3070                         }
3071                     }
3072                 ]
3073             }
3074         ]
3075         
3076     }
3077 });
3078
3079 Roo.apply(Roo.bootstrap.form.ComboBox,  {
3080     
3081     touchViewTemplate : {
3082         tag: 'div',
3083         cls: 'modal fade roo-combobox-touch-view',
3084         cn: [
3085             {
3086                 tag: 'div',
3087                 cls: 'modal-dialog',
3088                 style : 'position:fixed', // we have to fix position....
3089                 cn: [
3090                     {
3091                         tag: 'div',
3092                         cls: 'modal-content',
3093                         cn: [
3094                             Roo.bootstrap.form.ComboBox.header,
3095                             Roo.bootstrap.form.ComboBox.body,
3096                             Roo.bootstrap.form.ComboBox.footer
3097                         ]
3098                     }
3099                 ]
3100             }
3101         ]
3102     }
3103 });