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