Roo/form/ComboBoxArray.js
[roojs1] / Roo / form / ComboBoxArray.js
1 /*
2  * Copyright(c) 2010-2012, Roo J Solutions Limited
3  *
4  * Licence LGPL
5  *
6  */
7
8 /**
9  * @class Roo.form.ComboBoxArray
10  * @extends Roo.form.TextField
11  * A facebook style adder... for lists of email / people / countries  etc...
12  * pick multiple items from a combo box, and shows each one.
13  *
14  *  Fred [x]  Brian [x]  [Pick another |v]
15  *
16  *
17  *  For this to work: it needs various extra information
18  *    - normal combo problay has
19  *      name, hiddenName
20  *    + displayField, valueField
21  *
22  *    For our purpose...
23  *
24  *
25  *   If we change from 'extends' to wrapping...
26  *   
27  *  
28  *
29  
30  
31  * @constructor
32  * Create a new ComboBoxArray.
33  * @param {Object} config Configuration options
34  */
35  
36
37 Roo.form.ComboBoxArray = function(config)
38 {
39     
40     Roo.form.ComboBoxArray.superclass.constructor.call(this, config);
41     
42     this.items = new Roo.util.MixedCollection(false);
43     
44     // construct the child combo...
45     
46     
47     
48     
49    
50     
51 }
52
53  
54 Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField,
55
56     /**
57      * @cfg {Roo.form.Combo} combo The combo box that is wrapped
58      */
59     
60     lastData : false,
61     
62     // behavies liek a hiddne field
63     inputType:      'hidden',
64     /**
65      * @cfg {Number} width The width of the box that displays the selected element
66      */ 
67     width:          300,
68
69     
70     
71     /**
72      * @cfg {String} name    The name of the visable items on this form (eg. titles not ids)
73      */
74     name : false,
75     /**
76      * @cfg {String} hiddenName    The hidden name of the field, often contains an comma seperated list of names
77      */
78     hiddenName : false,
79     
80     
81     // private the array of items that are displayed..
82     items  : false,
83     // private - the hidden field el.
84     hiddenEl : false,
85     // private - the filed el..
86     el : false,
87     
88     //validateValue : function() { return true; }, // all values are ok!
89     //onAddClick: function() { },
90     
91     onRender : function(ct, position) 
92     {
93         
94         // create the standard hidden element
95         //Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
96         
97         
98         // give fake names to child combo;
99         this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
100         this.combo.name = this.name? (this.name+'-subcombo') : this.name;
101         
102         this.combo = Roo.factory(this.combo, Roo.form);
103         this.combo.onRender(ct, position);
104         this.combo.initEvents();
105         
106         // assigned so form know we need to do this..
107         this.store          = this.combo.store;
108         this.valueField     = this.combo.valueField;
109         this.displayField   = this.combo.displayField ;
110         
111         
112         this.combo.wrap.addClass('x-cbarray-grp');
113         
114         var cbwrap = this.combo.wrap.createChild(
115             {tag: 'div', cls: 'x-cbarray-cb'},
116             this.combo.el.dom
117         );
118         
119              
120         this.hiddenEl = this.combo.wrap.createChild({
121             tag: 'input',  type:'hidden' , name: this.hiddenName, value : ''
122         });
123         this.el = this.combo.wrap.createChild({
124             tag: 'input',  type:'hidden' , name: this.name, value : ''
125         });
126          //   this.el.dom.removeAttribute("name");
127         
128         
129         this.outerWrap = this.combo.wrap;
130         this.wrap = cbwrap;
131         
132         this.outerWrap.setWidth(this.width);
133         this.outerWrap.dom.removeChild(this.el.dom);
134         
135         this.wrap.dom.appendChild(this.el.dom);
136         this.outerWrap.dom.removeChild(this.combo.trigger.dom);
137         this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
138         
139         this.combo.trigger.setStyle('position','relative');
140         this.combo.trigger.setStyle('left', '0px');
141         this.combo.trigger.setStyle('top', '2px');
142         
143         this.combo.el.setStyle('vertical-align', 'text-bottom');
144         
145         //this.trigger.setStyle('vertical-align', 'top');
146         
147         // this should use the code from combo really... on('add' ....)
148         if (this.adder) {
149             
150         
151             this.adder = this.outerWrap.createChild(
152                 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});  
153             var _t = this;
154             this.adder.on('click', function(e) {
155                 _t.fireEvent('adderclick', this, e);
156             }, _t);
157         }
158         //var _t = this;
159         //this.adder.on('click', this.onAddClick, _t);
160         
161         
162         this.combo.on('select', function(cb, rec, ix) {
163             this.addItem(rec.data);
164             
165             cb.setValue('');
166             cb.el.dom.value = '';
167             //cb.lastData = rec.data;
168             // add to list
169             
170         }, this);
171         
172         
173     },
174     
175     
176     getName: function()
177     {
178         // returns hidden if it's set..
179         if (!this.rendered) {return ''};
180         return  this.hiddenName ? this.hiddenName : this.name;
181         
182     },
183     
184     
185     onResize: function(w, h){
186         
187         return;
188         // not sure if this is needed..
189         //this.combo.onResize(w,h);
190         
191         if(typeof w != 'number'){
192             // we do not handle it!?!?
193             return;
194         }
195         var tw = this.combo.trigger.getWidth();
196         tw += this.addicon ? this.addicon.getWidth() : 0;
197         tw += this.editicon ? this.editicon.getWidth() : 0;
198         var x = w - tw;
199         this.combo.el.setWidth( this.combo.adjustWidth('input', x));
200             
201         this.combo.trigger.setStyle('left', '0px');
202         
203         if(this.list && this.listWidth === undefined){
204             var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
205             this.list.setWidth(lw);
206             this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
207         }
208         
209     
210         
211     },
212     
213     addItem: function(rec)
214     {
215         var valueField = this.combo.valueField;
216         var displayField = this.combo.displayField;
217         if (this.items.indexOfKey(rec[valueField]) > -1) {
218             //console.log("GOT " + rec.data.id);
219             return;
220         }
221         
222         var x = new Roo.form.ComboBoxArray.Item({
223             //id : rec[this.idField],
224             data : rec,
225             displayField : displayField ,
226             tipField : displayField ,
227             cb : this
228         });
229         // use the 
230         this.items.add(rec[valueField],x);
231         // add it before the element..
232         this.updateHiddenEl();
233         x.render(this.outerWrap, this.wrap.dom);
234         // add the image handler..
235     },
236     
237     updateHiddenEl : function()
238     {
239         this.validate();
240         if (!this.hiddenEl) {
241             return;
242         }
243         var ar = [];
244         var idField = this.combo.valueField;
245         
246         this.items.each(function(f) {
247             ar.push(f.data[idField]);
248            
249         });
250         this.hiddenEl.dom.value = ar.join(',');
251         this.validate();
252     },
253     
254     reset : function()
255     {
256         //Roo.form.ComboBoxArray.superclass.reset.call(this); 
257         this.items.each(function(f) {
258            f.remove(); 
259         });
260         this.el.dom.value = '';
261         if (this.hiddenEl) {
262             this.hiddenEl.dom.value = '';
263         }
264         
265     },
266     getValue: function()
267     {
268         return this.hiddenEl ? this.hiddenEl.dom.value : '';
269     },
270     setValue: function(v) // not a valid action - must use addItems..
271     {
272          
273         this.reset();
274         
275         
276         
277         if (this.store.isLocal && (typeof(v) == 'string')) {
278             // then we can use the store to find the values..
279             // comma seperated at present.. this needs to allow JSON based encoding..
280             this.hiddenEl.value  = v;
281             var v_ar = [];
282             Roo.each(v.split(','), function(k) {
283                 Roo.log("CHECK " + this.valueField + ',' + k);
284                 var li = this.store.query(this.valueField, k);
285                 if (!li.length) {
286                     return;
287                 }
288                 add = {};
289                 add[this.valueField] = k;
290                 add[this.displayField] = li.item(0).data[this.displayField];
291                 
292                 this.addItem(add);
293             }, this) 
294              
295         }
296         if (typeof(v) == 'object') {
297             // then let's assume it's an array of objects..
298             Roo.each(v, function(l) {
299                 this.addItem(l);
300             }, this);
301              
302         }
303         
304         
305     },
306     setFromData: function(v)
307     {
308         // this recieves an object, if setValues is called.
309         this.reset();
310         this.el.dom.value = v[this.displayField];
311         this.hiddenEl.dom.value = v[this.valueField];
312         if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
313             return;
314         }
315         var kv = v[this.valueField];
316         var dv = v[this.displayField];
317         kv = typeof(kv) != 'string' ? '' : kv;
318         dv = typeof(dv) != 'string' ? '' : dv;
319         
320         
321         var keys = kv.split(',');
322         var display = dv.split(',');
323         for (var i = 0 ; i < keys.length; i++) {
324             
325             add = {};
326             add[this.valueField] = keys[i];
327             add[this.displayField] = display[i];
328             this.addItem(add);
329         }
330       
331         
332     },
333     
334     
335     validateValue : function(value){
336         return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
337         
338     }
339     
340 });
341
342
343
344 /**
345  * @class Roo.form.ComboBoxArray.Item
346  * @extends Roo.BoxComponent
347  * A selected item in the list
348  *  Fred [x]  Brian [x]  [Pick another |v]
349  * 
350  * @constructor
351  * Create a new item.
352  * @param {Object} config Configuration options
353  */
354  
355 Roo.form.ComboBoxArray.Item = function(config) {
356     config.id = Roo.id();
357     Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
358 }
359
360 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
361     data : {},
362     cb: false,
363     displayField : false,
364     tipField : false,
365     
366     
367     defaultAutoCreate : {
368         tag: 'div',
369         cls: 'x-cbarray-item',
370         cn : [ 
371             { tag: 'div' },
372             {
373                 tag: 'img',
374                 width:16,
375                 height : 16,
376                 src : Roo.BLANK_IMAGE_URL ,
377                 align: 'center'
378             }
379         ]
380         
381     },
382     
383  
384     onRender : function(ct, position)
385     {
386         Roo.form.Field.superclass.onRender.call(this, ct, position);
387         
388         if(!this.el){
389             var cfg = this.getAutoCreate();
390             this.el = ct.createChild(cfg, position);
391         }
392         
393         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
394         
395         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
396             this.cb.renderer(this.data) :
397             String.format('{0}',this.data[this.displayField]);
398         
399             
400         this.el.child('div').dom.setAttribute('qtip',
401                         String.format('{0}',this.data[this.tipField])
402         );
403         
404         this.el.child('img').on('click', this.remove, this);
405         
406     },
407    
408     remove : function()
409     {
410         
411         this.cb.items.remove(this);
412         this.el.child('img').un('click', this.remove, this);
413         this.el.remove();
414         this.cb.updateHiddenEl();
415     }
416     
417     
418 });