Roo/form/ComboCheck.js
[roojs1] / Roo / form / ComboCheck.js
1 /*
2  * RooJS Library 1.1.1
3  * Copyright(c) 2008-2011  Alan Knowles
4  *
5  * License - LGPL
6  */
7  
8
9 /**
10  * @class Roo.form.ComboCheck
11  * @extends Roo.form.ComboBox
12  * A combobox for multiple select items.
13  * @constructor
14  * Create a new ComboCheck
15  * @param {Object} config Configuration options
16  */
17 Roo.form.ComboCheck = function(config){
18     Roo.form.ComboCheck.superclass.constructor.call(this, config);
19     // should verify some data...
20     // like
21     // hiddenName = required..
22     // displayField = required
23     // valudField == required
24     var req= [ 'hiddenName', 'displayField', 'valueField' ];
25     var _t = this;
26     Roo.each(req, function(e) {
27         if ((typeof(_t[e]) == 'undefined' ) || !_t[e].length) {
28             throw "Roo.form.ComboCheck : missing value for: " + e;
29         }
30     })
31     
32    
33 };
34
35 Roo.extend(Roo.form.ComboCheck, Roo.form.ComboBox, {
36      
37      
38     editable : false,
39      
40     //selectedClass: 'x-menu-item-checked', - this is the mouse over class...
41     
42     // private
43     onRender : function(ct, position){
44         var _t = this;
45         
46         
47         
48         if(!this.tpl){
49             var cls = 'x-combo-list';
50
51             
52             this.tpl =  new Roo.Template({
53                 html :  '<div class="'+cls+'-item x-menu-check-item{' + this.valueField + ':this.checked}">' +
54                    '<img class="x-menu-item-icon" style="margin: 0px;" src="' + Roo.BLANK_IMAGE_URL + '">' + 
55                    '<span>{' + this.displayField + '}</span>' +
56                     '</div>',
57                 checked: function (value, allValues) {
58                     var ar = _t.getValueArray();
59                     return ar.indexOf(value) > -1 ? ' x-menu-item-checked' : ''
60                 }
61             });
62         }
63  
64         
65         Roo.form.ComboCheck.superclass.onRender.call(this, ct, position);
66         this.view.singleSelect = false;
67         
68     },
69     
70     onViewOver : function(e, t){
71         // do nothing...
72         return;
73         if(this.inKeyMode){ // prevent key nav and mouse over conflicts
74             return;
75         }
76         var item = this.view.findItemFromChild(t);
77         if(item){
78             var index = this.view.indexOf(item);
79             this.select(index, false);
80         }
81     },
82     
83     onViewClick : function(doFocus,t,x){
84         Roo.log(doFocus);
85         Roo.log(t);
86         Roo.log(x);
87         if(this.inKeyMode){ // prevent key nav and mouse over conflicts
88             return;
89         }
90         var item = this.view.findItemFromChild(t);
91         if(item){
92             var index = this.view.indexOf(item);
93             this.select(index, false);
94         }
95         
96         return;
97         var index = this.view.getSelectedIndexes()[0];
98         var r = this.store.getAt(index);
99         if(r){
100             this.onSelect(r, index);
101         }
102         if(doFocus !== false && !this.blockFocus){
103             this.el.focus();
104         }
105     },
106  
107     
108     onSelect : function(record, index){
109         if(this.fireEvent('beforeselect', this, record, index) !== false){
110             
111             
112             this.setFromData(index > -1 ? record.data : false);
113             
114             
115             
116             //this.collapse();
117             this.fireEvent('select', this, record, index);
118         }
119     },
120     getValueArray : function()
121     {
122         var ar = [] ;
123         try {
124             var ret = Roo.decode(thist.getValue());
125             ret = typeof(ar) == 'Array' ? ar : []; //?? valid?
126             return ret;
127         } catch(e) {
128             Roo.log("Roo.form.ComboCheck:getValueArray  invalid data:" + this.getValue());
129             return [];
130         }
131         
132         
133     } 
134 });