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 before remove the value from the list
43 * @param {Roo.form.ComboBoxArray} _self This combo box array
44 * @param {Roo.form.ComboBoxArray.Item} item removed item
46 'beforeremove' : true,
49 * Fires when remove the value from the list
50 * @param {Roo.form.ComboBoxArray} _self This combo box array
51 * @param {Roo.form.ComboBoxArray.Item} item removed item
58 Roo.form.ComboBoxArray.superclass.constructor.call(this, config);
60 this.items = new Roo.util.MixedCollection(false);
62 // construct the child combo...
72 Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField,
75 * @cfg {Roo.form.Combo} combo The combo box that is wrapped
80 // behavies liek a hiddne field
83 * @cfg {Number} width The width of the box that displays the selected element
90 * @cfg {String} name The name of the visable items on this form (eg. titles not ids)
94 * @cfg {String} hiddenName The hidden name of the field, often contains an comma seperated list of names
98 * @cfg {String} seperator The value seperator normally ','
102 // private the array of items that are displayed..
104 // private - the hidden field el.
106 // private - the filed el..
109 //validateValue : function() { return true; }, // all values are ok!
110 //onAddClick: function() { },
112 onRender : function(ct, position)
115 // create the standard hidden element
116 //Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
119 // give fake names to child combo;
120 this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
121 this.combo.name = this.name ? (this.name+'-subcombo') : this.name;
123 this.combo = Roo.factory(this.combo, Roo.form);
124 this.combo.onRender(ct, position);
125 if (typeof(this.combo.width) != 'undefined') {
126 this.combo.onResize(this.combo.width,0);
129 this.combo.initEvents();
131 // assigned so form know we need to do this..
132 this.store = this.combo.store;
133 this.valueField = this.combo.valueField;
134 this.displayField = this.combo.displayField ;
137 this.combo.wrap.addClass('x-cbarray-grp');
139 var cbwrap = this.combo.wrap.createChild(
140 {tag: 'div', cls: 'x-cbarray-cb'},
145 this.hiddenEl = this.combo.wrap.createChild({
146 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
148 this.el = this.combo.wrap.createChild({
149 tag: 'input', type:'hidden' , name: this.name, value : ''
151 // this.el.dom.removeAttribute("name");
154 this.outerWrap = this.combo.wrap;
157 this.outerWrap.setWidth(this.width);
158 this.outerWrap.dom.removeChild(this.el.dom);
160 this.wrap.dom.appendChild(this.el.dom);
161 this.outerWrap.dom.removeChild(this.combo.trigger.dom);
162 this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
164 this.combo.trigger.setStyle('position','relative');
165 this.combo.trigger.setStyle('left', '0px');
166 this.combo.trigger.setStyle('top', '2px');
168 this.combo.el.setStyle('vertical-align', 'text-bottom');
170 //this.trigger.setStyle('vertical-align', 'top');
172 // this should use the code from combo really... on('add' ....)
176 this.adder = this.outerWrap.createChild(
177 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
179 this.adder.on('click', function(e) {
180 _t.fireEvent('adderclick', this, e);
184 //this.adder.on('click', this.onAddClick, _t);
187 this.combo.on('select', function(cb, rec, ix) {
188 this.addItem(rec.data);
191 cb.el.dom.value = '';
192 //cb.lastData = rec.data;
203 // returns hidden if it's set..
204 if (!this.rendered) {return ''};
205 return this.hiddenName ? this.hiddenName : this.name;
210 onResize: function(w, h){
213 // not sure if this is needed..
214 //this.combo.onResize(w,h);
216 if(typeof w != 'number'){
217 // we do not handle it!?!?
220 var tw = this.combo.trigger.getWidth();
221 tw += this.addicon ? this.addicon.getWidth() : 0;
222 tw += this.editicon ? this.editicon.getWidth() : 0;
224 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
226 this.combo.trigger.setStyle('left', '0px');
228 if(this.list && this.listWidth === undefined){
229 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
230 this.list.setWidth(lw);
231 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
238 addItem: function(rec)
240 var valueField = this.combo.valueField;
241 var displayField = this.combo.displayField;
243 if (this.items.indexOfKey(rec[valueField]) > -1) {
244 //console.log("GOT " + rec.data.id);
248 var x = new Roo.form.ComboBoxArray.Item({
249 //id : rec[this.idField],
251 displayField : displayField ,
252 tipField : displayField ,
256 this.items.add(rec[valueField],x);
257 // add it before the element..
258 this.updateHiddenEl();
259 x.render(this.outerWrap, this.wrap.dom);
260 // add the image handler..
263 updateHiddenEl : function()
266 if (!this.hiddenEl) {
270 var idField = this.combo.valueField;
272 this.items.each(function(f) {
273 ar.push(f.data[idField]);
275 this.hiddenEl.dom.value = ar.join(this.seperator);
283 Roo.each(this.outerWrap.select('.x-cbarray-item', true).elements, function(el){
287 this.el.dom.value = '';
289 this.hiddenEl.dom.value = '';
295 return this.hiddenEl ? this.hiddenEl.dom.value : '';
297 setValue: function(v) // not a valid action - must use addItems..
302 if (this.store.isLocal && (typeof(v) == 'string')) {
303 // then we can use the store to find the values..
304 // comma seperated at present.. this needs to allow JSON based encoding..
305 this.hiddenEl.value = v;
307 Roo.each(v.split(this.seperator), function(k) {
308 Roo.log("CHECK " + this.valueField + ',' + k);
309 var li = this.store.query(this.valueField, k);
314 add[this.valueField] = k;
315 add[this.displayField] = li.item(0).data[this.displayField];
321 if (typeof(v) == 'object' ) {
322 // then let's assume it's an array of objects..
323 Roo.each(v, function(l) {
325 if (typeof(l) == 'string') {
327 add[this.valueField] = l;
328 add[this.displayField] = l
337 setFromData: function(v)
339 // this recieves an object, if setValues is called.
341 this.el.dom.value = v[this.displayField];
342 this.hiddenEl.dom.value = v[this.valueField];
343 if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
346 var kv = v[this.valueField];
347 var dv = v[this.displayField];
348 kv = typeof(kv) != 'string' ? '' : kv;
349 dv = typeof(dv) != 'string' ? '' : dv;
352 var keys = kv.split(this.seperator);
353 var display = dv.split(this.seperator);
354 for (var i = 0 ; i < keys.length; i++) {
356 add[this.valueField] = keys[i];
357 add[this.displayField] = display[i];
365 * Validates the combox array value
366 * @return {Boolean} True if the value is valid, else false
368 validate : function(){
369 if(this.disabled || this.validateValue(this.processValue(this.getValue()))){
376 validateValue : function(value){
377 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
385 isDirty : function() {
391 var d = Roo.decode(String(this.originalValue));
393 return String(this.getValue()) !== String(this.originalValue);
396 var originalValue = [];
398 for (var i = 0; i < d.length; i++){
399 originalValue.push(d[i][this.valueField]);
402 return String(this.getValue()) !== String(originalValue.join(this.seperator));
411 * @class Roo.form.ComboBoxArray.Item
412 * @extends Roo.BoxComponent
413 * A selected item in the list
414 * Fred [x] Brian [x] [Pick another |v]
418 * @param {Object} config Configuration options
421 Roo.form.ComboBoxArray.Item = function(config) {
422 config.id = Roo.id();
423 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
426 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
429 displayField : false,
433 defaultAutoCreate : {
435 cls: 'x-cbarray-item',
442 src : Roo.BLANK_IMAGE_URL ,
450 onRender : function(ct, position)
452 Roo.form.Field.superclass.onRender.call(this, ct, position);
455 var cfg = this.getAutoCreate();
456 this.el = ct.createChild(cfg, position);
459 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
461 this.el.child('div').dom.innerHTML = this.cb.renderer ?
462 this.cb.renderer(this.data) :
463 String.format('{0}',this.data[this.displayField]);
466 this.el.child('div').dom.setAttribute('qtip',
467 String.format('{0}',this.data[this.tipField])
470 this.el.child('img').on('click', this.remove, this);
476 if(this.cb.disabled){
480 if(false !== this.cb.fireEvent('beforeremove', this.cb, this)){
481 this.cb.items.remove(this);
482 this.el.child('img').un('click', this.remove, this);
484 this.cb.updateHiddenEl();
486 this.cb.fireEvent('remove', this.cb, this);