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