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