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