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         this.hiddenName += '-lastselected';
83         
84         Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position); 
85         this.wrap.addClass('x-cbarray-grp');
86         var cbwrap = this.wrap.createChild(
87             {tag: 'div', cls: 'x-cbarray-cb'},
88             this.el.dom
89         );
90         
91              
92         this.hiddenEl = this.wrap.createChild({
93             tag: 'input',  type:'hidden' , name: this.hiddenListName, value : ''
94         });
95          //   this.el.dom.removeAttribute("name");
96         
97         
98         this.outerWrap = this.wrap;
99         this.wrap = cbwrap;
100         this.outerWrap.setWidth(this.boxWidth);
101         this.outerWrap.dom.removeChild(this.el.dom);
102         this.wrap.dom.appendChild(this.el.dom);
103         this.outerWrap.dom.removeChild(this.trigger.dom);
104         this.wrap.dom.appendChild(this.trigger.dom);
105          
106         this.trigger.setStyle('position','relative');
107         this.trigger.setStyle('left', '0px');
108         this.trigger.setStyle('top', '2px');
109         this.el.setStyle('vertical-align', 'text-bottom');
110         
111         //this.trigger.setStyle('vertical-align', 'top');
112         if (this.adder) {
113             
114         
115             this.adder = this.outerWrap.createChild(
116                 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});  
117             var _t = this;
118             this.adder.on('click', function(e) {
119                 _t.fireEvent('adderclick', this, e);
120             }, _t);
121         }
122         //var _t = this;
123         //this.adder.on('click', this.onAddClick, _t);
124         
125     },
126     
127     
128     getName: function()
129     {
130         // returns hidden if it's set..
131         if (!this.rendered) {return ''};
132         return  this.hiddenListName;
133         
134     },
135     
136     
137     onResize: function(w, h){
138         Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
139         
140         if(typeof w != 'number'){
141             // we do not handle it!?!?
142             return;
143         }
144         var tw = this.trigger.getWidth();
145         tw += this.addicon ? this.addicon.getWidth() : 0;
146         tw += this.editicon ? this.editicon.getWidth() : 0;
147         var x = w - tw;
148         this.el.setWidth( this.adjustWidth('input', x));
149             
150         this.trigger.setStyle('left', '0px');
151         
152         if(this.list && this.listWidth === undefined){
153             var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
154             this.list.setWidth(lw);
155             this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
156         }
157         
158     
159         
160     },
161     
162     addItem: function(rec)
163     {
164         if (this.items.indexOfKey(rec[this.idField]) > -1) {
165             //console.log("GOT " + rec.data.id);
166             return;
167         }
168         
169         var x = new Roo.form.ComboBoxArray.Item({
170             //id : rec[this.idField],
171             data : rec,
172             nameField : this.nameField,
173             tipField : this.tipField,
174             cb : this
175         });
176         // use the 
177         this.items.add(rec[this.idField],x);
178         // add it before the element..
179         this.updateHiddenEl();
180         x.render(this.outerWrap, this.wrap.dom);
181         // add the image handler..
182     },
183     
184     updateHiddenEl : function()
185     {
186         this.validate();
187         if (!this.hiddenEl) {
188             return;
189         }
190         var ar = [];
191         var idField = this.idField;
192         this.items.each(function(f) {
193             ar.push(f.data[idField]);
194            
195         });
196         this.hiddenEl.dom.value = ar.join(',');
197         this.validate();
198     },
199     
200     reset : function()
201     {
202         Roo.form.ComboBoxArray.superclass.reset.call(this); 
203         this.items.each(function(f) {
204            f.remove(); 
205         });
206         if (this.hiddenEl) {
207             this.hiddenEl.dom.value = '';
208         }
209         
210     },
211     getValue: function()
212     {
213         return this.hiddenEl ? this.hiddenEl.dom.value : '';
214     },
215     setValue: function(v) // not a valid action - must use addItems..
216     {
217         if (typeof(v) != 'object') {
218             return;
219         }
220         var _this = this;
221         Roo.each(v, function(l) {
222             _this.addItem(l);
223         });
224         
225     },
226     validateValue : function(value){
227         return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
228         
229     }
230     
231 });
232
233
234
235 /**
236  * @class Roo.form.ComboBoxArray.Item
237  * @extends Roo.BoxComponent
238  * A selected item in the list
239  *  Fred [x]  Brian [x]  [Pick another |v]
240  * 
241  * @constructor
242  * Create a new item.
243  * @param {Object} config Configuration options
244  */
245  
246 Roo.form.ComboBoxArray.Item = function(config) {
247     config.id = Roo.id();
248     Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
249 }
250
251 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
252     data : {},
253     cb: false,
254     defaultAutoCreate : {tag: 'div', cls: 'x-cbarray-item', cn : [ 
255             { tag: 'div' },
256             { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
257         ]
258         
259     },
260     
261  
262     onRender : function(ct, position){
263         Roo.form.Field.superclass.onRender.call(this, ct, position);
264         if(!this.el){
265             var cfg = this.getAutoCreate();
266             this.el = ct.createChild(cfg, position);
267         }
268         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
269         
270         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
271             this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
272         this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
273         
274         this.el.child('img').on('click', this.remove, this);
275         
276     },
277    
278     remove : function()
279     {
280         
281         this.cb.items.remove(this);
282         this.el.child('img').un('click', this.remove, this);
283         this.el.remove();
284         this.cb.updateHiddenEl();
285     }
286     
287     
288 });