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