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