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     el : false,
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.width);
127         this.outerWrap.dom.removeChild(this.el.dom);
128         
129         this.wrap.dom.appendChild(this.el.dom);
130         this.outerWrap.dom.removeChild(this.combo.trigger.dom);
131         this.combo.wrap.dom.appendChild(this.combo.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.hiddenName ? this.hiddenName : this.name;
175         
176     },
177     
178     
179     onResize: function(w, h){
180         
181         return;
182         // not sure if this is needed..
183         this.combo.onResize(w,h);
184         
185         if(typeof w != 'number'){
186             // we do not handle it!?!?
187             return;
188         }
189         var tw = this.combo.trigger.getWidth();
190         tw += this.addicon ? this.addicon.getWidth() : 0;
191         tw += this.editicon ? this.editicon.getWidth() : 0;
192         var x = w - tw;
193         this.combo.el.setWidth( this.combo.adjustWidth('input', x));
194             
195         this.combo.trigger.setStyle('left', '0px');
196         
197         if(this.list && this.listWidth === undefined){
198             var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
199             this.list.setWidth(lw);
200             this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
201         }
202         
203     
204         
205     },
206     
207     addItem: function(rec)
208     {
209         var idField = this.combo.valueField;
210         var displayField = this.combo.displayField;
211         if (this.items.indexOfKey(rec[idField]) > -1) {
212             //console.log("GOT " + rec.data.id);
213             return;
214         }
215         
216         var x = new Roo.form.ComboBoxArray.Item({
217             //id : rec[this.idField],
218             data : rec,
219             nameField : displayField ,
220             tipField : displayField ,
221             cb : this
222         });
223         // use the 
224         this.items.add(rec[idField],x);
225         // add it before the element..
226         this.updateHiddenEl();
227         x.render(this.outerWrap, this.wrap.dom);
228         // add the image handler..
229     },
230     
231     updateHiddenEl : function()
232     {
233         this.validate();
234         if (!this.hiddenEl) {
235             return;
236         }
237         var ar = [];
238         var idField = this.combo.valueField;
239         
240         this.items.each(function(f) {
241             ar.push(f.data[idField]);
242            
243         });
244         this.hiddenEl.dom.value = ar.join(',');
245         this.validate();
246     },
247     
248     reset : function()
249     {
250         Roo.form.ComboBoxArray.superclass.reset.call(this); 
251         this.items.each(function(f) {
252            f.remove(); 
253         });
254         if (this.hiddenEl) {
255             this.hiddenEl.dom.value = '';
256         }
257         
258     },
259     getValue: function()
260     {
261         return this.hiddenEl ? this.hiddenEl.dom.value : '';
262     },
263     setValue: function(v) // not a valid action - must use addItems..
264     {
265         if (typeof(v) != 'object') {
266             return;
267         }
268         Roo.log(v);
269         Roo.log(this.hiddenEl.dom.value);
270         Roo.log(this.el.dom.value);
271         
272         var _this = this;
273         Roo.each(v, function(l) {
274             _this.addItem(l);
275         });
276         
277     },
278     validateValue : function(value){
279         return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
280         
281     }
282     
283 });
284
285
286
287 /**
288  * @class Roo.form.ComboBoxArray.Item
289  * @extends Roo.BoxComponent
290  * A selected item in the list
291  *  Fred [x]  Brian [x]  [Pick another |v]
292  * 
293  * @constructor
294  * Create a new item.
295  * @param {Object} config Configuration options
296  */
297  
298 Roo.form.ComboBoxArray.Item = function(config) {
299     config.id = Roo.id();
300     Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
301 }
302
303 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
304     data : {},
305     cb: false,
306     nameField : false,
307     tipField : false,
308     
309     
310     defaultAutoCreate : {
311         tag: 'div',
312         cls: 'x-cbarray-item',
313         cn : [ 
314             { tag: 'div' },
315             {
316                 tag: 'img',
317                 width:16,
318                 height : 16,
319                 src : Roo.BLANK_IMAGE_URL ,
320                 align: 'center'
321             }
322         ]
323         
324     },
325     
326  
327     onRender : function(ct, position)
328     {
329         Roo.form.Field.superclass.onRender.call(this, ct, position);
330         
331         if(!this.el){
332             var cfg = this.getAutoCreate();
333             this.el = ct.createChild(cfg, position);
334         }
335         
336         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
337         
338         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
339             this.cb.renderer(this.data) :
340             String.format('{0}',this.data[this.nameField]);
341         
342             
343         this.el.child('div').dom.setAttribute('qtip',
344                         String.format('{0}',this.data[this.tipField])
345         );
346         
347         this.el.child('img').on('click', this.remove, this);
348         
349     },
350    
351     remove : function()
352     {
353         
354         this.cb.items.remove(this);
355         this.el.child('img').un('click', this.remove, this);
356         this.el.remove();
357         this.cb.updateHiddenEl();
358     }
359     
360     
361 });