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