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