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         // 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 idField = this.combo.valueField;
215         var displayField = this.combo.displayField;
216         if (this.items.indexOfKey(rec[idField]) > -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             nameField : displayField ,
225             tipField : displayField ,
226             cb : this
227         });
228         // use the 
229         this.items.add(rec[idField],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         if (this.hiddenEl) {
260             this.hiddenEl.dom.value = '';
261         }
262         
263     },
264     getValue: function()
265     {
266         return this.hiddenEl ? this.hiddenEl.dom.value : '';
267     },
268     setValue: function(v) // not a valid action - must use addItems..
269     {
270          
271         
272         if (this.store.isLocal) {
273             // then we can use the store to find the values..
274             // comma seperated at present.. this needs to allow JSON based encoding..
275             this.hiddenEl.value  = v;
276             var v_ar = [];
277             Roo.each(v.split(','), function(k) {
278                 Roo.log("CHECK " + this.store.valueField + ',' + k));
279                 var li = this.store.query(this.store.valueField, k);
280                 if (!li.length) {
281                     return;
282                 }
283                 add = {};
284                 add[this.store.valueField] = k;
285                 add[this.store.displayField] = li.item(0).data[this.store.displayField];
286                 
287                 this.addItem(add);
288             }, this) 
289             
290                 
291             
292         }
293         
294         
295     },
296     setFromData: function(v)
297     {
298         Roo.log(v);
299       
300         
301     },
302     
303     
304     validateValue : function(value){
305         return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
306         
307     }
308     
309 });
310
311
312
313 /**
314  * @class Roo.form.ComboBoxArray.Item
315  * @extends Roo.BoxComponent
316  * A selected item in the list
317  *  Fred [x]  Brian [x]  [Pick another |v]
318  * 
319  * @constructor
320  * Create a new item.
321  * @param {Object} config Configuration options
322  */
323  
324 Roo.form.ComboBoxArray.Item = function(config) {
325     config.id = Roo.id();
326     Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
327 }
328
329 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
330     data : {},
331     cb: false,
332     nameField : false,
333     tipField : false,
334     
335     
336     defaultAutoCreate : {
337         tag: 'div',
338         cls: 'x-cbarray-item',
339         cn : [ 
340             { tag: 'div' },
341             {
342                 tag: 'img',
343                 width:16,
344                 height : 16,
345                 src : Roo.BLANK_IMAGE_URL ,
346                 align: 'center'
347             }
348         ]
349         
350     },
351     
352  
353     onRender : function(ct, position)
354     {
355         Roo.form.Field.superclass.onRender.call(this, ct, position);
356         
357         if(!this.el){
358             var cfg = this.getAutoCreate();
359             this.el = ct.createChild(cfg, position);
360         }
361         
362         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
363         
364         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
365             this.cb.renderer(this.data) :
366             String.format('{0}',this.data[this.nameField]);
367         
368             
369         this.el.child('div').dom.setAttribute('qtip',
370                         String.format('{0}',this.data[this.tipField])
371         );
372         
373         this.el.child('img').on('click', this.remove, this);
374         
375     },
376    
377     remove : function()
378     {
379         
380         this.cb.items.remove(this);
381         this.el.child('img').un('click', this.remove, this);
382         this.el.remove();
383         this.cb.updateHiddenEl();
384     }
385     
386     
387 });