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