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