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 Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField
54
55     /**
56      * @cfg {Roo.form.Combo} combo The combo box that is wrapped
57      */
58     
59     lastData : false,
60     
61     // behavies liek a hiddne field
62     inputType:      'hidden',
63     /**
64      * @cfg {Number} width The width of the box that displays the selected element
65      */ 
66     width:          300,
67
68     
69     
70     /**
71      * @cfg {String} name    The name of the visable items on this form (eg. titles not ids)
72      */
73     name : false,
74     /**
75      * @cfg {String} name    The hidden name of the field, often contains an comma seperated list of names
76      */
77     hiddenName : false,
78     
79     
80     // private the array of items that are displayed..
81     items  : false,
82     // private - the hidden field el.
83     hiddenEl : false,
84     
85     boxWidth : 200, // use to set the box around the entry..
86     
87     //validateValue : function() { return true; }, // all values are ok!
88     //onAddClick: function() { },
89     
90     onRender : function(ct, position) 
91     {
92         
93         // create the standard hidden element
94         Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
95         
96         
97         // give fake names to child combo;
98         this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
99         this.combo.name = this.name? (this.name+'-subcombo') : this.name;
100         
101         this.combo = Roo.factory(this.combo, Roo.form);
102         this.combo.onRender(ct, position);
103         
104         
105         this.combo.wrap.addClass('x-cbarray-grp');
106         
107         var cbwrap = this.combo.wrap.createChild(
108             {tag: 'div', cls: 'x-cbarray-cb'},
109             this.combo.el.dom
110         );
111         
112              
113         this.hiddenEl = this.combo.wrap.createChild({
114             tag: 'input',  type:'hidden' , name: this.hiddenName, value : ''
115         });
116          //   this.el.dom.removeAttribute("name");
117         
118         
119         this.outerWrap = this.combo.wrap;
120         this.wrap = cbwrap;
121         
122         this.outerWrap.setWidth(this.boxWidth);
123         this.outerWrap.dom.removeChild(this.el.dom);
124         
125         this.wrap.dom.appendChild(this.el.dom);
126         this.outerWrap.dom.removeChild(this.trigger.dom);
127         this.combo.wrap.dom.appendChild(this.trigger.dom);
128         
129         this.combo.trigger.setStyle('position','relative');
130         this.combo.trigger.setStyle('left', '0px');
131         this.combo.trigger.setStyle('top', '2px');
132         
133         this.combo.el.setStyle('vertical-align', 'text-bottom');
134         
135         //this.trigger.setStyle('vertical-align', 'top');
136         
137         // this should use the code from combo really... on('add' ....)
138         if (this.adder) {
139             
140         
141             this.adder = this.outerWrap.createChild(
142                 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});  
143             var _t = this;
144             this.adder.on('click', function(e) {
145                 _t.fireEvent('adderclick', this, e);
146             }, _t);
147         }
148         //var _t = this;
149         //this.adder.on('click', this.onAddClick, _t);
150         
151         
152         this.combo.on('select', function(cb, rec, ix) {
153             this.addItem(rec.data);
154             
155             cb.setValue('');
156             cb.el.dom.value = '';
157             //cb.lastData = rec.data;
158             // add to list
159             
160         }, this);
161         
162         
163     },
164     
165     
166     getName: function()
167     {
168         // returns hidden if it's set..
169         if (!this.rendered) {return ''};
170         return  this.hiddenListName;
171         
172     },
173     
174     
175     onResize: function(w, h){
176         
177         this.combo.onResize(w,h);
178         
179         if(typeof w != 'number'){
180             // we do not handle it!?!?
181             return;
182         }
183         var tw = this.combo.trigger.getWidth();
184         tw += this.addicon ? this.addicon.getWidth() : 0;
185         tw += this.editicon ? this.editicon.getWidth() : 0;
186         var x = w - tw;
187         this.combo.el.setWidth( this.combo.adjustWidth('input', x));
188             
189         this.combo.trigger.setStyle('left', '0px');
190         
191         if(this.list && this.listWidth === undefined){
192             var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
193             this.list.setWidth(lw);
194             this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
195         }
196         
197     
198         
199     },
200     
201     addItem: function(rec)
202     {
203         var idField = this.combo.valueField;
204         var displayField = this.combo.displayField;
205         if (this.items.indexOfKey(rec[idField]) > -1) {
206             //console.log("GOT " + rec.data.id);
207             return;
208         }
209         
210         var x = new Roo.form.ComboBoxArray.Item({
211             //id : rec[this.idField],
212             data : rec,
213             nameField : displayField ,
214             tipField : displayField ,
215             cb : this
216         });
217         // use the 
218         this.items.add(rec[idField],x);
219         // add it before the element..
220         this.updateHiddenEl();
221         x.render(this.outerWrap, this.wrap.dom);
222         // add the image handler..
223     },
224     
225     updateHiddenEl : function()
226     {
227         this.validate();
228         if (!this.hiddenEl) {
229             return;
230         }
231         var ar = [];
232         var idField = this.combo.valueField;
233         
234         this.items.each(function(f) {
235             ar.push(f.data[idField]);
236            
237         });
238         this.hiddenEl.dom.value = ar.join(',');
239         this.validate();
240     },
241     
242     reset : function()
243     {
244         Roo.form.ComboBoxArray.superclass.reset.call(this); 
245         this.items.each(function(f) {
246            f.remove(); 
247         });
248         if (this.hiddenEl) {
249             this.hiddenEl.dom.value = '';
250         }
251         
252     },
253     getValue: function()
254     {
255         return this.hiddenEl ? this.hiddenEl.dom.value : '';
256     },
257     setValue: function(v) // not a valid action - must use addItems..
258     {
259         if (typeof(v) != 'object') {
260             return;
261         }
262         var _this = this;
263         Roo.each(v, function(l) {
264             _this.addItem(l);
265         });
266         
267     },
268     validateValue : function(value){
269         return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
270         
271     }
272     
273 });
274
275
276
277 /**
278  * @class Roo.form.ComboBoxArray.Item
279  * @extends Roo.BoxComponent
280  * A selected item in the list
281  *  Fred [x]  Brian [x]  [Pick another |v]
282  * 
283  * @constructor
284  * Create a new item.
285  * @param {Object} config Configuration options
286  */
287  
288 Roo.form.ComboBoxArray.Item = function(config) {
289     config.id = Roo.id();
290     Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
291 }
292
293 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
294     data : {},
295     cb: false,
296     nameField : false,
297     tipField : false,
298     
299     
300     defaultAutoCreate : {
301         tag: 'div',
302         cls: 'x-cbarray-item',
303         cn : [ 
304             { tag: 'div' },
305             {
306                 tag: 'img',
307                 width:16,
308                 height : 16,
309                 src : Roo.BLANK_IMAGE_URL ,
310                 align: 'center'
311             }
312         ]
313         
314     },
315     
316  
317     onRender : function(ct, position)
318     {
319         Roo.form.Field.superclass.onRender.call(this, ct, position);
320         
321         if(!this.el){
322             var cfg = this.getAutoCreate();
323             this.el = ct.createChild(cfg, position);
324         }
325         
326         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
327         
328         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
329             this.cb.renderer(this.data) :
330             String.format('{0}',this.data[this.nameField]);
331         
332             
333         this.el.child('div').dom.setAttribute('qtip',
334                         String.format('{0}',this.data[this.tipField])
335         );
336         
337         this.el.child('img').on('click', this.remove, this);
338         
339     },
340    
341     remove : function()
342     {
343         
344         this.cb.items.remove(this);
345         this.el.child('img').un('click', this.remove, this);
346         this.el.remove();
347         this.cb.updateHiddenEl();
348     }
349     
350     
351 });