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