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