roojs-ui.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  *
14  * FIXME - could do with a reset button..
15  * 
16  * @constructor
17  * Create a new ComboCheck
18  * @param {Object} config Configuration options
19  */
20 Roo.form.ComboCheck = function(config){
21     Roo.form.ComboCheck.superclass.constructor.call(this, config);
22     // should verify some data...
23     // like
24     // hiddenName = required..
25     // displayField = required
26     // valudField == required
27     var req= [ 'hiddenName', 'displayField', 'valueField' ];
28     var _t = this;
29     Roo.each(req, function(e) {
30         if ((typeof(_t[e]) == 'undefined' ) || !_t[e].length) {
31             throw "Roo.form.ComboCheck : missing value for: " + e;
32         }
33     });
34     
35     
36 };
37
38 Roo.extend(Roo.form.ComboCheck, Roo.form.ComboBox, {
39      
40      
41     editable : false,
42      
43     selectedClass: 'x-menu-item-checked', 
44     
45     // private
46     onRender : function(ct, position){
47         var _t = this;
48         
49         
50         
51         if(!this.tpl){
52             var cls = 'x-combo-list';
53
54             
55             this.tpl =  new Roo.Template({
56                 html :  '<div class="'+cls+'-item x-menu-check-item">' +
57                    '<img class="x-menu-item-icon" style="margin: 0px;" src="' + Roo.BLANK_IMAGE_URL + '">' + 
58                    '<span>{' + this.displayField + '}</span>' +
59                     '</div>' 
60                 
61             });
62         }
63  
64         
65         Roo.form.ComboCheck.superclass.onRender.call(this, ct, position);
66         this.view.singleSelect = false;
67         this.view.multiSelect = true;
68         this.view.toggleSelect = true;
69         this.pageTb.add(new Roo.Toolbar.Fill(), {
70             
71             text: 'Done',
72             handler: function()
73             {
74                 _t.collapse();
75             }
76         });
77     },
78     
79     onViewOver : function(e, t){
80         // do nothing...
81         return;
82         
83     },
84     
85     onViewClick : function(doFocus,index){
86         return;
87         
88     },
89     select: function () {
90         //Roo.log("SELECT CALLED");
91     },
92      
93     selectByValue : function(xv, scrollIntoView){
94         var ar = this.getValueArray();
95         var sels = [];
96         
97         Roo.each(ar, function(v) {
98             if(v === undefined || v === null){
99                 return;
100             }
101             var r = this.findRecord(this.valueField, v);
102             if(r){
103                 sels.push(this.store.indexOf(r))
104                 
105             }
106         },this);
107         this.view.select(sels);
108         return false;
109     },
110     
111     
112     
113     onSelect : function(record, index){
114        // Roo.log("onselect Called");
115        // this is only called by the clear button now..
116         this.view.clearSelections();
117         this.setValue('[]');
118         if (this.value != this.valueBefore) {
119             this.fireEvent('change', this, this.value, this.valueBefore);
120         }
121     },
122     getValueArray : function()
123     {
124         var ar = [] ;
125         
126         try {
127             //Roo.log(this.value);
128             if (typeof(this.value) == 'undefined') {
129                 return [];
130             }
131             var ar = Roo.decode(this.value);
132             return  ar instanceof Array ? ar : []; //?? valid?
133             
134         } catch(e) {
135             Roo.log(e + "\nRoo.form.ComboCheck:getValueArray  invalid data:" + this.getValue());
136             return [];
137         }
138          
139     },
140     expand : function ()
141     {
142         Roo.form.ComboCheck.superclass.expand.call(this);
143         this.valueBefore = this.value;
144         
145
146     },
147     
148     collapse : function(){
149         Roo.form.ComboCheck.superclass.collapse.call(this);
150         var sl = this.view.getSelectedIndexes();
151         var st = this.store;
152         var nv = [];
153         var tv = [];
154         var r;
155         Roo.each(sl, function(i) {
156             r = st.getAt(i);
157             nv.push(r.get(this.valueField));
158         },this);
159         this.setValue(Roo.encode(nv));
160         if (this.value != this.valueBefore) {
161
162             this.fireEvent('change', this, this.value, this.valueBefore);
163         }
164         
165     },
166     
167     setValue : function(v){
168         // Roo.log(v);
169         this.value = v;
170         
171         var vals = this.getValueArray();
172         var tv = [];
173         Roo.each(vals, function(k) {
174             var r = this.findRecord(this.valueField, k);
175             if(r){
176                 tv.push(r.data[this.displayField]);
177             }else if(this.valueNotFoundText !== undefined){
178                 tv.push( this.valueNotFoundText );
179             }
180         },this);
181        // Roo.log(tv);
182         
183         Roo.form.ComboBox.superclass.setValue.call(this, tv.join(', '));
184         this.hiddenField.value = v;
185         this.value = v;
186     }
187     
188 });