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