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