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     onLoad: function() {
91         if(!this.hasFocus){
92             return;
93         }
94         if(!this.store.getCount()){
95             this.onEmptyResults();
96             return;
97         }
98         this.expand();
99         this.restrictHeight();
100         // 
101         
102         if(!this.selectByValue(this.value, true)){
103             this.select(0, true);
104         }
105      
106         
107         
108     },
109     */
110     selectByValue : function(xv, scrollIntoView){
111         var ar = this.getValueArray();
112         var sels = []
113         
114         Roo.each(ar, funciton(v) {
115             if(v !== undefined && v !== null){
116                 var r = this.findRecord(this.valueField || this.displayField, v);
117                 if(r){
118                     sels.push(this.store.indexOf(r))
119                     
120                 }
121         })
122         return false;
123     },
124     
125     
126     
127     onSelect : function(record, index){
128         Roo.log("onselect Called");
129        // this is only called by the clear button now..
130        this.view.clearSelections();
131     },
132     getValueArray : function()
133     {
134         var ar = [] ;
135         try {
136             var ar = Roo.decode(this.value);
137             return  ar instanceof Array ? ar : []; //?? valid?
138             
139         } catch(e) {
140             Roo.log(e + "\nRoo.form.ComboCheck:getValueArray  invalid data:" + this.getValue());
141             return [];
142         }
143          
144     },
145     
146     
147     collapse : function(){
148         Roo.form.ComboCheck.superclass.collapse.call(this);
149         var sl = this.view.getSelectedIndexes();
150         var st = this.store;
151         var nv = [];
152         var tv = [];
153         var r;
154         Roo.each(sl, function(i) {
155             r = st.getAt(i);
156             nv.push(r.get(this.valueField));
157         },this);
158         this.setValue(Roo.encode(nv))
159         
160     },
161     
162     setValue : function(v){
163         // Roo.log(v);
164         this.value = v;
165         
166         var vals = this.getValueArray();
167         var tv = [];
168         Roo.each(vals, function(k) {
169             var r = this.findRecord(this.valueField, k);
170             if(r){
171                 tv.push(r.data[this.displayField]);
172             }else if(this.valueNotFoundText !== undefined){
173                 tv.push( this.valueNotFoundText );
174             }
175         },this);
176        // Roo.log(tv);
177         
178         Roo.form.ComboBox.superclass.setValue.call(this, tv.join(', '));
179         this.hiddenField.value = v;
180         this.value = v;
181     }
182     
183 });