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', 
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">' +
54                    '<img class="x-menu-item-icon" style="margin: 0px;" src="' + Roo.BLANK_IMAGE_URL + '">' + 
55                    '<span>{' + this.displayField + '}</span>' +
56                     '</div>' 
57                 
58             });
59         }
60  
61         
62         Roo.form.ComboCheck.superclass.onRender.call(this, ct, position);
63         this.view.singleSelect = false;
64         this.view.multiSelect = true;
65         this.view.toggleSelect = true;
66         this.pageTb.add(new Roo.Toolbar.Fill(), {
67             
68             text: 'Done',
69             handler: function()
70             {
71                 _t.collapse();
72             }
73         });
74     },
75     
76     onViewOver : function(e, t){
77         // do nothing...
78         return;
79         
80     },
81     
82     onViewClick : function(doFocus,index){
83         return;
84         
85     },
86     select: function () {
87         Roo.log("SELECT CALLED");
88     },
89      
90     selectByValue : function(xv, scrollIntoView){
91         var ar = this.getValueArray();
92         var sels = [];
93         
94         Roo.each(ar, function(v) {
95             if(v === undefined || v === null){
96                 return;
97             }
98             var r = this.findRecord(this.valueField, v);
99             if(r){
100                 sels.push(this.store.indexOf(r))
101                 
102             }
103         },this);
104         this.view.select(sels);
105         return false;
106     },
107     
108     
109     
110     onSelect : function(record, index){
111         Roo.log("onselect Called");
112        // this is only called by the clear button now..
113        this.view.clearSelections();
114     },
115     getValueArray : function()
116     {
117         var ar = [] ;
118         
119         try {
120             Roo.log(this.value)
121             var ar = Roo.decode(this.value);
122             return  ar instanceof Array ? ar : []; //?? valid?
123             
124         } catch(e) {
125             Roo.log(e + "\nRoo.form.ComboCheck:getValueArray  invalid data:" + this.getValue());
126             return [];
127         }
128          
129     },
130     expand : function ()
131     {
132         Roo.form.ComboCheck.superclass.setValue.call(this);
133         this.valueBefore = this.value;
134         
135
136     },
137     
138     collapse : function(){
139         Roo.form.ComboCheck.superclass.collapse.call(this);
140         var sl = this.view.getSelectedIndexes();
141         var st = this.store;
142         var nv = [];
143         var tv = [];
144         var r;
145         Roo.each(sl, function(i) {
146             r = st.getAt(i);
147             nv.push(r.get(this.valueField));
148         },this);
149         this.setValue(Roo.encode(nv))
150         if (this.value != this.valueBefore) {
151
152             this.fireEvent('change', this, this.value, this.valueBefore);
153         }
154         
155     },
156     
157     setValue : function(v){
158         // Roo.log(v);
159         this.value = v;
160         
161         var vals = this.getValueArray();
162         var tv = [];
163         Roo.each(vals, function(k) {
164             var r = this.findRecord(this.valueField, k);
165             if(r){
166                 tv.push(r.data[this.displayField]);
167             }else if(this.valueNotFoundText !== undefined){
168                 tv.push( this.valueNotFoundText );
169             }
170         },this);
171        // Roo.log(tv);
172         
173         Roo.form.ComboBox.superclass.setValue.call(this, tv.join(', '));
174         this.hiddenField.value = v;
175         this.value = v;
176     }
177     
178 });