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