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
99 // private the array of items that are displayed..
101 // private - the hidden field el.
103 // private - the filed el..
106 //validateValue : function() { return true; }, // all values are ok!
107 //onAddClick: function() { },
109 onRender : function(ct, position)
112 // create the standard hidden element
113 //Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
116 // give fake names to child combo;
117 this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
118 this.combo.name = this.name? (this.name+'-subcombo') : this.name;
120 this.combo = Roo.factory(this.combo, Roo.form);
121 this.combo.onRender(ct, position);
122 if (typeof(this.combo.width) != 'undefined') {
123 this.combo.onResize(this.combo.width,0);
126 this.combo.initEvents();
128 // assigned so form know we need to do this..
129 this.store = this.combo.store;
130 this.valueField = this.combo.valueField;
131 this.displayField = this.combo.displayField ;
134 this.combo.wrap.addClass('x-cbarray-grp');
136 var cbwrap = this.combo.wrap.createChild(
137 {tag: 'div', cls: 'x-cbarray-cb'},
142 this.hiddenEl = this.combo.wrap.createChild({
143 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
145 this.el = this.combo.wrap.createChild({
146 tag: 'input', type:'hidden' , name: this.name, value : ''
148 // this.el.dom.removeAttribute("name");
151 this.outerWrap = this.combo.wrap;
154 this.outerWrap.setWidth(this.width);
155 this.outerWrap.dom.removeChild(this.el.dom);
157 this.wrap.dom.appendChild(this.el.dom);
158 this.outerWrap.dom.removeChild(this.combo.trigger.dom);
159 this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
161 this.combo.trigger.setStyle('position','relative');
162 this.combo.trigger.setStyle('left', '0px');
163 this.combo.trigger.setStyle('top', '2px');
165 this.combo.el.setStyle('vertical-align', 'text-bottom');
167 //this.trigger.setStyle('vertical-align', 'top');
169 // this should use the code from combo really... on('add' ....)
173 this.adder = this.outerWrap.createChild(
174 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
176 this.adder.on('click', function(e) {
177 _t.fireEvent('adderclick', this, e);
181 //this.adder.on('click', this.onAddClick, _t);
184 this.combo.on('select', function(cb, rec, ix) {
185 this.addItem(rec.data);
188 cb.el.dom.value = '';
189 //cb.lastData = rec.data;
200 // returns hidden if it's set..
201 if (!this.rendered) {return ''};
202 return this.hiddenName ? this.hiddenName : this.name;
207 onResize: function(w, h){
210 // not sure if this is needed..
211 //this.combo.onResize(w,h);
213 if(typeof w != 'number'){
214 // we do not handle it!?!?
217 var tw = this.combo.trigger.getWidth();
218 tw += this.addicon ? this.addicon.getWidth() : 0;
219 tw += this.editicon ? this.editicon.getWidth() : 0;
221 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
223 this.combo.trigger.setStyle('left', '0px');
225 if(this.list && this.listWidth === undefined){
226 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
227 this.list.setWidth(lw);
228 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
235 addItem: function(rec)
237 var valueField = this.combo.valueField;
238 var displayField = this.combo.displayField;
239 if (this.items.indexOfKey(rec[valueField]) > -1) {
240 //console.log("GOT " + rec.data.id);
244 var x = new Roo.form.ComboBoxArray.Item({
245 //id : rec[this.idField],
247 displayField : displayField ,
248 tipField : displayField ,
252 this.items.add(rec[valueField],x);
253 // add it before the element..
254 this.updateHiddenEl();
255 x.render(this.outerWrap, this.wrap.dom);
256 // add the image handler..
259 updateHiddenEl : function()
262 if (!this.hiddenEl) {
266 var idField = this.combo.valueField;
268 this.items.each(function(f) {
269 ar.push(f.data[idField]);
272 this.hiddenEl.dom.value = ar.join(',');
278 //Roo.form.ComboBoxArray.superclass.reset.call(this);
279 this.items.each(function(f) {
282 this.el.dom.value = '';
284 this.hiddenEl.dom.value = '';
290 return this.hiddenEl ? this.hiddenEl.dom.value : '';
292 setValue: function(v) // not a valid action - must use addItems..
299 if (this.store.isLocal && (typeof(v) == 'string')) {
300 // then we can use the store to find the values..
301 // comma seperated at present.. this needs to allow JSON based encoding..
302 this.hiddenEl.value = v;
304 Roo.each(v.split(','), function(k) {
305 Roo.log("CHECK " + this.valueField + ',' + k);
306 var li = this.store.query(this.valueField, k);
311 add[this.valueField] = k;
312 add[this.displayField] = li.item(0).data[this.displayField];
318 if (typeof(v) == 'object' ) {
319 // then let's assume it's an array of objects..
320 Roo.each(v, function(l) {
328 setFromData: function(v)
330 // this recieves an object, if setValues is called.
332 this.el.dom.value = v[this.displayField];
333 this.hiddenEl.dom.value = v[this.valueField];
334 if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
337 var kv = v[this.valueField];
338 var dv = v[this.displayField];
339 kv = typeof(kv) != 'string' ? '' : kv;
340 dv = typeof(dv) != 'string' ? '' : dv;
343 var keys = kv.split(',');
344 var display = dv.split(',');
345 for (var i = 0 ; i < keys.length; i++) {
348 add[this.valueField] = keys[i];
349 add[this.displayField] = display[i];
357 * Validates the combox array value
358 * @return {Boolean} True if the value is valid, else false
360 validate : function(){
361 if(this.disabled || this.validateValue(this.processValue(this.getValue()))){
368 validateValue : function(value){
369 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
377 isDirty : function() {
383 var d = Roo.decode(String(this.originalValue));
385 return String(this.getValue()) !== String(this.originalValue);
388 var originalValue = [];
390 for (var i = 0; i < d.length; i++){
391 originalValue.push(d[i][this.valueField]);
394 return String(this.getValue()) !== String(originalValue.join(','));
403 * @class Roo.form.ComboBoxArray.Item
404 * @extends Roo.BoxComponent
405 * A selected item in the list
406 * Fred [x] Brian [x] [Pick another |v]
410 * @param {Object} config Configuration options
413 Roo.form.ComboBoxArray.Item = function(config) {
414 config.id = Roo.id();
415 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
418 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
421 displayField : false,
425 defaultAutoCreate : {
427 cls: 'x-cbarray-item',
434 src : Roo.BLANK_IMAGE_URL ,
442 onRender : function(ct, position)
444 Roo.form.Field.superclass.onRender.call(this, ct, position);
447 var cfg = this.getAutoCreate();
448 this.el = ct.createChild(cfg, position);
451 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
453 this.el.child('div').dom.innerHTML = this.cb.renderer ?
454 this.cb.renderer(this.data) :
455 String.format('{0}',this.data[this.displayField]);
458 this.el.child('div').dom.setAttribute('qtip',
459 String.format('{0}',this.data[this.tipField])
462 this.el.child('img').on('click', this.remove, this);
468 if(this.cb.disabled){
472 if(false !== this.cb.fireEvent('remove', this.cb, this)){
473 this.cb.items.remove(this);
474 this.el.child('img').un('click', this.remove, this);
476 this.cb.updateHiddenEl();
478 this.cb.fireEvent('remove', this.cb, this);