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         if (typeof(this.combo.width) != 'undefined') {
105             this.combo.onResize(this.combo.width,0);
106         }
107         
108         this.combo.initEvents();
109         
110         // assigned so form know we need to do this..
111         this.store          = this.combo.store;
112         this.valueField     = this.combo.valueField;
113         this.displayField   = this.combo.displayField ;
114         
115         
116         this.combo.wrap.addClass('x-cbarray-grp');
117         
118         var cbwrap = this.combo.wrap.createChild(
119             {tag: 'div', cls: 'x-cbarray-cb'},
120             this.combo.el.dom
121         );
122         
123              
124         this.hiddenEl = this.combo.wrap.createChild({
125             tag: 'input',  type:'hidden' , name: this.hiddenName, value : ''
126         });
127         this.el = this.combo.wrap.createChild({
128             tag: 'input',  type:'hidden' , name: this.name, value : ''
129         });
130          //   this.el.dom.removeAttribute("name");
131         
132         
133         this.outerWrap = this.combo.wrap;
134         this.wrap = cbwrap;
135         
136         this.outerWrap.setWidth(this.width);
137         this.outerWrap.dom.removeChild(this.el.dom);
138         
139         this.wrap.dom.appendChild(this.el.dom);
140         this.outerWrap.dom.removeChild(this.combo.trigger.dom);
141         this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
142         
143         this.combo.trigger.setStyle('position','relative');
144         this.combo.trigger.setStyle('left', '0px');
145         this.combo.trigger.setStyle('top', '2px');
146         
147         this.combo.el.setStyle('vertical-align', 'text-bottom');
148         
149         //this.trigger.setStyle('vertical-align', 'top');
150         
151         // this should use the code from combo really... on('add' ....)
152         if (this.adder) {
153             
154         
155             this.adder = this.outerWrap.createChild(
156                 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});  
157             var _t = this;
158             this.adder.on('click', function(e) {
159                 _t.fireEvent('adderclick', this, e);
160             }, _t);
161         }
162         //var _t = this;
163         //this.adder.on('click', this.onAddClick, _t);
164         
165         
166         this.combo.on('select', function(cb, rec, ix) {
167             this.addItem(rec.data);
168             
169             cb.setValue('');
170             cb.el.dom.value = '';
171             //cb.lastData = rec.data;
172             // add to list
173             
174         }, this);
175         
176         
177     },
178     
179     
180     getName: function()
181     {
182         // returns hidden if it's set..
183         if (!this.rendered) {return ''};
184         return  this.hiddenName ? this.hiddenName : this.name;
185         
186     },
187     
188     
189     onResize: function(w, h){
190         
191         return;
192         // not sure if this is needed..
193         //this.combo.onResize(w,h);
194         
195         if(typeof w != 'number'){
196             // we do not handle it!?!?
197             return;
198         }
199         var tw = this.combo.trigger.getWidth();
200         tw += this.addicon ? this.addicon.getWidth() : 0;
201         tw += this.editicon ? this.editicon.getWidth() : 0;
202         var x = w - tw;
203         this.combo.el.setWidth( this.combo.adjustWidth('input', x));
204             
205         this.combo.trigger.setStyle('left', '0px');
206         
207         if(this.list && this.listWidth === undefined){
208             var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
209             this.list.setWidth(lw);
210             this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
211         }
212         
213     
214         
215     },
216     
217     addItem: function(rec)
218     {
219         var valueField = this.combo.valueField;
220         var displayField = this.combo.displayField;
221         if (this.items.indexOfKey(rec[valueField]) > -1) {
222             //console.log("GOT " + rec.data.id);
223             return;
224         }
225         
226         var x = new Roo.form.ComboBoxArray.Item({
227             //id : rec[this.idField],
228             data : rec,
229             displayField : displayField ,
230             tipField : displayField ,
231             cb : this
232         });
233         // use the 
234         this.items.add(rec[valueField],x);
235         // add it before the element..
236         this.updateHiddenEl();
237         x.render(this.outerWrap, this.wrap.dom);
238         // add the image handler..
239     },
240     
241     updateHiddenEl : function()
242     {
243         this.validate();
244         if (!this.hiddenEl) {
245             return;
246         }
247         var ar = [];
248         var idField = this.combo.valueField;
249         
250         this.items.each(function(f) {
251             ar.push(f.data[idField]);
252            
253         });
254         this.hiddenEl.dom.value = ar.join(',');
255         this.validate();
256     },
257     
258     reset : function()
259     {
260         //Roo.form.ComboBoxArray.superclass.reset.call(this); 
261         this.items.each(function(f) {
262            f.remove(); 
263         });
264         this.el.dom.value = '';
265         if (this.hiddenEl) {
266             this.hiddenEl.dom.value = '';
267         }
268         
269     },
270     getValue: function()
271     {
272         return this.hiddenEl ? this.hiddenEl.dom.value : '';
273     },
274     setValue: function(v) // not a valid action - must use addItems..
275     {
276          
277         this.reset();
278         
279         
280         
281         if (this.store.isLocal && (typeof(v) == 'string')) {
282             // then we can use the store to find the values..
283             // comma seperated at present.. this needs to allow JSON based encoding..
284             this.hiddenEl.value  = v;
285             var v_ar = [];
286             Roo.each(v.split(','), function(k) {
287                 Roo.log("CHECK " + this.valueField + ',' + k);
288                 var li = this.store.query(this.valueField, k);
289                 if (!li.length) {
290                     return;
291                 }
292                 var add = {};
293                 add[this.valueField] = k;
294                 add[this.displayField] = li.item(0).data[this.displayField];
295                 
296                 this.addItem(add);
297             }, this) 
298              
299         }
300         if (typeof(v) == 'object') {
301             // then let's assume it's an array of objects..
302             Roo.each(v, function(l) {
303                 this.addItem(l);
304             }, this);
305              
306         }
307         
308         
309     },
310     setFromData: function(v)
311     {
312         // this recieves an object, if setValues is called.
313         this.reset();
314         this.el.dom.value = v[this.displayField];
315         this.hiddenEl.dom.value = v[this.valueField];
316         if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
317             return;
318         }
319         var kv = v[this.valueField];
320         var dv = v[this.displayField];
321         kv = typeof(kv) != 'string' ? '' : kv;
322         dv = typeof(dv) != 'string' ? '' : dv;
323         
324         
325         var keys = kv.split(',');
326         var display = dv.split(',');
327         for (var i = 0 ; i < keys.length; i++) {
328             
329             add = {};
330             add[this.valueField] = keys[i];
331             add[this.displayField] = display[i];
332             this.addItem(add);
333         }
334       
335         
336     },
337     
338     
339     validateValue : function(value){
340         return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
341         
342     }
343     
344 });
345
346
347
348 /**
349  * @class Roo.form.ComboBoxArray.Item
350  * @extends Roo.BoxComponent
351  * A selected item in the list
352  *  Fred [x]  Brian [x]  [Pick another |v]
353  * 
354  * @constructor
355  * Create a new item.
356  * @param {Object} config Configuration options
357  */
358  
359 Roo.form.ComboBoxArray.Item = function(config) {
360     config.id = Roo.id();
361     Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
362 }
363
364 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
365     data : {},
366     cb: false,
367     displayField : false,
368     tipField : false,
369     
370     
371     defaultAutoCreate : {
372         tag: 'div',
373         cls: 'x-cbarray-item',
374         cn : [ 
375             { tag: 'div' },
376             {
377                 tag: 'img',
378                 width:16,
379                 height : 16,
380                 src : Roo.BLANK_IMAGE_URL ,
381                 align: 'center'
382             }
383         ]
384         
385     },
386     
387  
388     onRender : function(ct, position)
389     {
390         Roo.form.Field.superclass.onRender.call(this, ct, position);
391         
392         if(!this.el){
393             var cfg = this.getAutoCreate();
394             this.el = ct.createChild(cfg, position);
395         }
396         
397         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
398         
399         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
400             this.cb.renderer(this.data) :
401             String.format('{0}',this.data[this.displayField]);
402         
403             
404         this.el.child('div').dom.setAttribute('qtip',
405                         String.format('{0}',this.data[this.tipField])
406         );
407         
408         this.el.child('img').on('click', this.remove, this);
409         
410     },
411    
412     remove : function()
413     {
414         
415         this.cb.items.remove(this);
416         this.el.child('img').un('click', this.remove, this);
417         this.el.remove();
418         this.cb.updateHiddenEl();
419     }
420     
421     
422 });