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     onResize: function(w, h){
129         Roo.form.ComboBox.superclass.onResize.apply(this, arguments);
130         
131         if(typeof w != 'number'){
132             // we do not handle it!?!?
133             return;
134         }
135         var tw = this.trigger.getWidth();
136         tw += this.addicon ? this.addicon.getWidth() : 0;
137         tw += this.editicon ? this.editicon.getWidth() : 0;
138         var x = w - tw;
139         this.el.setWidth( this.adjustWidth('input', x));
140             
141         this.trigger.setStyle('left', '0px');
142         
143         if(this.list && this.listWidth === undefined){
144             var lw = Math.max(x + this.trigger.getWidth(), this.minListWidth);
145             this.list.setWidth(lw);
146             this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
147         }
148         
149     
150         
151     },
152     
153     addItem: function(rec)
154     {
155         if (this.items.indexOfKey(rec[this.idField]) > -1) {
156             //console.log("GOT " + rec.data.id);
157             return;
158         }
159         
160         var x = new Roo.form.ComboBoxArray.Item({
161             //id : rec[this.idField],
162             data : rec,
163             nameField : this.nameField,
164             tipField : this.tipField,
165             cb : this
166         });
167         // use the 
168         this.items.add(rec[this.idField],x);
169         // add it before the element..
170         this.updateHiddenEl();
171         x.render(this.outerWrap, this.wrap.dom);
172         // add the image handler..
173     },
174     
175     updateHiddenEl : function()
176     {
177         this.validate();
178         if (!this.hiddenEl) {
179             return;
180         }
181         var ar = [];
182         var idField = this.idField;
183         this.items.each(function(f) {
184             ar.push(f.data[idField]);
185            
186         });
187         this.hiddenEl.dom.value = ar.join(',');
188         this.validate();
189     },
190     
191     reset : function()
192     {
193         Roo.form.ComboBoxArray.superclass.reset.call(this); 
194         this.items.each(function(f) {
195            f.remove(); 
196         });
197         if (this.hiddenEl) {
198             this.hiddenEl.dom.value = '';
199         }
200         
201     },
202     getValue: function()
203     {
204         return this.hiddenEl ? this.hiddenEl.dom.value : '';
205     },
206     setValue: function(v) // not a valid action - must use addItems..
207     {
208         if (typeof(v) != 'object') {
209             return;
210         }
211         var _this = this;
212         Roo.each(v, function(l) {
213             _this.addItem(l);
214         });
215         
216     },
217     validateValue : function(value){
218         return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
219         
220     }
221     
222 });
223
224
225
226 /**
227  * @class Roo.form.ComboBoxArray.Item
228  * @extends Roo.BoxComponent
229  * A selected item in the list
230  *  Fred [x]  Brian [x]  [Pick another |v]
231  * 
232  * @constructor
233  * Create a new item.
234  * @param {Object} config Configuration options
235  */
236  
237 Roo.form.ComboBoxArray.Item = function(config) {
238     config.id = Roo.id();
239     Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
240 }
241
242 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
243     data : {},
244     cb: false,
245     defaultAutoCreate : {tag: 'div', cls: 'x-cbarray-item', cn : [ 
246             { tag: 'div' },
247             { tag: 'img', width:16, height : 16, src : Roo.BLANK_IMAGE_URL , align: 'center' }
248         ]
249         
250     },
251     
252  
253     onRender : function(ct, position){
254         Roo.form.Field.superclass.onRender.call(this, ct, position);
255         if(!this.el){
256             var cfg = this.getAutoCreate();
257             this.el = ct.createChild(cfg, position);
258         }
259         this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
260         
261         this.el.child('div').dom.innerHTML = this.cb.renderer ? 
262             this.cb.renderer(this.data) : String.format('{0}',this.data[this.nameField]);
263         this.el.child('div').dom.setAttribute('qtip', String.format('{0}',this.data[this.tipField]));
264         
265         this.el.child('img').on('click', this.remove, this);
266         
267     },
268    
269     remove : function()
270     {
271         
272         this.cb.items.remove(this);
273         this.el.child('img').un('click', this.remove, this);
274         this.el.remove();
275         this.cb.updateHiddenEl();
276     }
277     
278     
279 });