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