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