Merge branch 'master' of http://git.roojs.com/roojs1
[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: 'Select All',
72             handler: function() {
73                 _t.selectAll();
74             }
75         },
76         {
77             text: 'Done',
78             handler: function() {
79                 _t.collapse();
80             }
81         });
82     },
83     
84     cleanLeadingSpace : function(e)
85     {
86         // this is disabled, as it retriggers setvalue on blur
87         return;
88     },
89     doForce : function() {
90         // no idea what this did, but it blanks out our values.
91         return;
92     },
93     onViewOver : function(e, t){
94         // do nothing...
95         return;
96         
97     },
98     
99     onViewClick : function(doFocus,index){
100         return;
101         
102     },
103     select: function () {
104         //Roo.log("SELECT CALLED");
105     },
106      
107     selectByValue : function(xv, scrollIntoView){
108         var ar = this.getValueArray();
109         var sels = [];
110         
111         Roo.each(ar, function(v) {
112             if(v === undefined || v === null){
113                 return;
114             }
115             var r = this.findRecord(this.valueField, v);
116             if(r){
117                 sels.push(this.store.indexOf(r))
118                 
119             }
120         },this);
121         this.view.select(sels);
122         return false;
123     },
124     
125     selectAll : function()
126     {
127         var sels = [];
128         this.store.each(function(r,i) {
129             sels.push(i);
130         });
131         this.view.select(sels);
132         this.collapse();
133         return false;
134
135     },
136     
137     onSelect : function(record, index){
138        // Roo.log("onselect Called");
139        // this is only called by the clear button now..
140         this.view.clearSelections();
141         this.setValue('[]');
142         if (this.value != this.valueBefore) {
143             this.fireEvent('change', this, this.value, this.valueBefore);
144             this.valueBefore = this.value;
145         }
146     },
147     getValueArray : function()
148     {
149         var ar = [] ;
150         
151         try {
152             //Roo.log(this.value);
153             if (typeof(this.value) == 'undefined') {
154                 return [];
155             }
156             var ar = Roo.decode(this.value);
157             return  ar instanceof Array ? ar : []; //?? valid?
158             
159         } catch(e) {
160             Roo.log(e + "\nRoo.form.ComboCheck:getValueArray  invalid data:" + this.getValue());
161             return [];
162         }
163          
164     },
165     expand : function ()
166     {
167         
168         Roo.form.ComboCheck.superclass.expand.call(this);
169         this.valueBefore = typeof(this.value) == 'undefined' ? '' : this.value;
170         //this.valueBefore = typeof(this.valueBefore) == 'undefined' ? '' : this.valueBefore;
171         
172
173     },
174     
175     collapse : function(){
176         Roo.form.ComboCheck.superclass.collapse.call(this);
177         var sl = this.view.getSelectedIndexes();
178         var st = this.store;
179         var nv = [];
180         var tv = [];
181         var r;
182         Roo.each(sl, function(i) {
183             r = st.getAt(i);
184             nv.push(r.get(this.valueField));
185         },this);
186         this.setValue(Roo.encode(nv));
187         if (this.value != this.valueBefore) {
188
189             this.fireEvent('change', this, this.value, this.valueBefore);
190             this.valueBefore = this.value;
191         }
192         
193     },
194     
195     setValue : function(v){
196         // Roo.log(v);
197         this.value = v;
198         
199         var vals = this.getValueArray();
200         var tv = [];
201         Roo.each(vals, function(k) {
202             var r = this.findRecord(this.valueField, k);
203             if(r){
204                 tv.push(r.data[this.displayField]);
205             }else if(this.valueNotFoundText !== undefined){
206                 tv.push( this.valueNotFoundText );
207             }
208         },this);
209        // Roo.log(tv);
210         
211         Roo.form.ComboBox.superclass.setValue.call(this, tv.join(', '));
212         this.hiddenField.value = v;
213         this.value = v;
214     }
215     
216 });