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