2 * Copyright(c) 2010-2012, Roo J Solutions Limited
9 * @class Roo.form.ComboBoxArray
10 * @extends Roo.form.TextField
11 * A facebook style adder... for lists of email / people / countries etc...
12 * pick multiple items from a combo box, and shows each one.
14 * Fred [x] Brian [x] [Pick another |v]
17 * For this to work: it needs various extra information
18 * - normal combo problay has
20 * + displayField, valueField
25 * If we change from 'extends' to wrapping...
32 * Create a new ComboBoxArray.
33 * @param {Object} config Configuration options
37 Roo.form.ComboBoxArray = function(config)
42 * Fires when remove the value from the list
43 * @param {Roo.form.ComboBox} combo This combo box
50 Roo.form.ComboBoxArray.superclass.constructor.call(this, config);
52 this.items = new Roo.util.MixedCollection(false);
54 // construct the child combo...
64 Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField,
67 * @cfg {Roo.form.Combo} combo The combo box that is wrapped
72 // behavies liek a hiddne field
75 * @cfg {Number} width The width of the box that displays the selected element
82 * @cfg {String} name The name of the visable items on this form (eg. titles not ids)
86 * @cfg {String} hiddenName The hidden name of the field, often contains an comma seperated list of names
91 // private the array of items that are displayed..
93 // private - the hidden field el.
95 // private - the filed el..
98 //validateValue : function() { return true; }, // all values are ok!
99 //onAddClick: function() { },
101 onRender : function(ct, position)
104 // create the standard hidden element
105 //Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
108 // give fake names to child combo;
109 this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
110 this.combo.name = this.name? (this.name+'-subcombo') : this.name;
112 this.combo = Roo.factory(this.combo, Roo.form);
113 this.combo.onRender(ct, position);
114 if (typeof(this.combo.width) != 'undefined') {
115 this.combo.onResize(this.combo.width,0);
118 this.combo.initEvents();
120 // assigned so form know we need to do this..
121 this.store = this.combo.store;
122 this.valueField = this.combo.valueField;
123 this.displayField = this.combo.displayField ;
126 this.combo.wrap.addClass('x-cbarray-grp');
128 var cbwrap = this.combo.wrap.createChild(
129 {tag: 'div', cls: 'x-cbarray-cb'},
134 this.hiddenEl = this.combo.wrap.createChild({
135 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
137 this.el = this.combo.wrap.createChild({
138 tag: 'input', type:'hidden' , name: this.name, value : ''
140 // this.el.dom.removeAttribute("name");
143 this.outerWrap = this.combo.wrap;
146 this.outerWrap.setWidth(this.width);
147 this.outerWrap.dom.removeChild(this.el.dom);
149 this.wrap.dom.appendChild(this.el.dom);
150 this.outerWrap.dom.removeChild(this.combo.trigger.dom);
151 this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
153 this.combo.trigger.setStyle('position','relative');
154 this.combo.trigger.setStyle('left', '0px');
155 this.combo.trigger.setStyle('top', '2px');
157 this.combo.el.setStyle('vertical-align', 'text-bottom');
159 //this.trigger.setStyle('vertical-align', 'top');
161 // this should use the code from combo really... on('add' ....)
165 this.adder = this.outerWrap.createChild(
166 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
168 this.adder.on('click', function(e) {
169 _t.fireEvent('adderclick', this, e);
173 //this.adder.on('click', this.onAddClick, _t);
176 this.combo.on('select', function(cb, rec, ix) {
177 this.addItem(rec.data);
180 cb.el.dom.value = '';
181 //cb.lastData = rec.data;
192 // returns hidden if it's set..
193 if (!this.rendered) {return ''};
194 return this.hiddenName ? this.hiddenName : this.name;
199 onResize: function(w, h){
202 // not sure if this is needed..
203 //this.combo.onResize(w,h);
205 if(typeof w != 'number'){
206 // we do not handle it!?!?
209 var tw = this.combo.trigger.getWidth();
210 tw += this.addicon ? this.addicon.getWidth() : 0;
211 tw += this.editicon ? this.editicon.getWidth() : 0;
213 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
215 this.combo.trigger.setStyle('left', '0px');
217 if(this.list && this.listWidth === undefined){
218 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
219 this.list.setWidth(lw);
220 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
227 addItem: function(rec)
229 var valueField = this.combo.valueField;
230 var displayField = this.combo.displayField;
231 if (this.items.indexOfKey(rec[valueField]) > -1) {
232 //console.log("GOT " + rec.data.id);
236 var x = new Roo.form.ComboBoxArray.Item({
237 //id : rec[this.idField],
239 displayField : displayField ,
240 tipField : displayField ,
244 this.items.add(rec[valueField],x);
245 // add it before the element..
246 this.updateHiddenEl();
247 x.render(this.outerWrap, this.wrap.dom);
248 // add the image handler..
251 updateHiddenEl : function()
254 if (!this.hiddenEl) {
258 var idField = this.combo.valueField;
260 this.items.each(function(f) {
261 ar.push(f.data[idField]);
264 this.hiddenEl.dom.value = ar.join(',');
270 //Roo.form.ComboBoxArray.superclass.reset.call(this);
271 this.items.each(function(f) {
274 this.el.dom.value = '';
276 this.hiddenEl.dom.value = '';
282 return this.hiddenEl ? this.hiddenEl.dom.value : '';
284 setValue: function(v) // not a valid action - must use addItems..
291 if (this.store.isLocal && (typeof(v) == 'string')) {
292 // then we can use the store to find the values..
293 // comma seperated at present.. this needs to allow JSON based encoding..
294 this.hiddenEl.value = v;
296 Roo.each(v.split(','), function(k) {
297 Roo.log("CHECK " + this.valueField + ',' + k);
298 var li = this.store.query(this.valueField, k);
303 add[this.valueField] = k;
304 add[this.displayField] = li.item(0).data[this.displayField];
310 if (typeof(v) == 'object') {
311 // then let's assume it's an array of objects..
312 Roo.each(v, function(l) {
320 setFromData: function(v)
322 // this recieves an object, if setValues is called.
324 this.el.dom.value = v[this.displayField];
325 this.hiddenEl.dom.value = v[this.valueField];
326 if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
329 var kv = v[this.valueField];
330 var dv = v[this.displayField];
331 kv = typeof(kv) != 'string' ? '' : kv;
332 dv = typeof(dv) != 'string' ? '' : dv;
335 var keys = kv.split(',');
336 var display = dv.split(',');
337 for (var i = 0 ; i < keys.length; i++) {
340 add[this.valueField] = keys[i];
341 add[this.displayField] = display[i];
349 * Validates the combox array value
350 * @return {Boolean} True if the value is valid, else false
352 validate : function(){
353 if(this.disabled || this.validateValue(this.processValue(this.getValue()))){
360 validateValue : function(value){
361 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
369 isDirty : function() {
375 var d = Roo.decode(String(this.originalValue));
377 return String(this.getValue()) !== String(this.originalValue);
380 var originalValue = [];
382 for (var i = 0; i < d.length; i++){
383 originalValue.push(d[i][this.valueField]);
386 return String(this.getValue()) !== String(originalValue.join(','));
395 * @class Roo.form.ComboBoxArray.Item
396 * @extends Roo.BoxComponent
397 * A selected item in the list
398 * Fred [x] Brian [x] [Pick another |v]
402 * @param {Object} config Configuration options
405 Roo.form.ComboBoxArray.Item = function(config) {
406 config.id = Roo.id();
407 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
410 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
413 displayField : false,
417 defaultAutoCreate : {
419 cls: 'x-cbarray-item',
426 src : Roo.BLANK_IMAGE_URL ,
434 onRender : function(ct, position)
436 Roo.form.Field.superclass.onRender.call(this, ct, position);
439 var cfg = this.getAutoCreate();
440 this.el = ct.createChild(cfg, position);
443 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
445 this.el.child('div').dom.innerHTML = this.cb.renderer ?
446 this.cb.renderer(this.data) :
447 String.format('{0}',this.data[this.displayField]);
450 this.el.child('div').dom.setAttribute('qtip',
451 String.format('{0}',this.data[this.tipField])
454 this.el.child('img').on('click', this.remove, this);
461 this.cb.items.remove(this);
462 this.el.child('img').un('click', this.remove, this);
464 this.cb.updateHiddenEl();
466 Roo.log('remove?????');
467 this.fireEvent('remove', this);