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