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