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