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