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