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.ComboBoxArray} _self This combo box array
44 * @param {Roo.form.ComboBoxArray.Item} item removed item
51 Roo.form.ComboBoxArray.superclass.constructor.call(this, config);
53 this.items = new Roo.util.MixedCollection(false);
55 // construct the child combo...
65 Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField,
68 * @cfg {Roo.form.Combo} combo The combo box that is wrapped
73 // behavies liek a hiddne field
76 * @cfg {Number} width The width of the box that displays the selected element
83 * @cfg {String} name The name of the visable items on this form (eg. titles not ids)
87 * @cfg {String} hiddenName The hidden name of the field, often contains an comma seperated list of names
92 // private the array of items that are displayed..
94 // private - the hidden field el.
96 // private - the filed el..
99 //validateValue : function() { return true; }, // all values are ok!
100 //onAddClick: function() { },
102 onRender : function(ct, position)
105 // create the standard hidden element
106 //Roo.form.ComboBoxArray.superclass.onRender.call(this, ct, position);
109 // give fake names to child combo;
110 this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
111 this.combo.name = this.name? (this.name+'-subcombo') : this.name;
113 this.combo = Roo.factory(this.combo, Roo.form);
114 this.combo.onRender(ct, position);
115 if (typeof(this.combo.width) != 'undefined') {
116 this.combo.onResize(this.combo.width,0);
119 this.combo.initEvents();
121 // assigned so form know we need to do this..
122 this.store = this.combo.store;
123 this.valueField = this.combo.valueField;
124 this.displayField = this.combo.displayField ;
127 this.combo.wrap.addClass('x-cbarray-grp');
129 var cbwrap = this.combo.wrap.createChild(
130 {tag: 'div', cls: 'x-cbarray-cb'},
135 this.hiddenEl = this.combo.wrap.createChild({
136 tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
138 this.el = this.combo.wrap.createChild({
139 tag: 'input', type:'hidden' , name: this.name, value : ''
141 // this.el.dom.removeAttribute("name");
144 this.outerWrap = this.combo.wrap;
147 this.outerWrap.setWidth(this.width);
148 this.outerWrap.dom.removeChild(this.el.dom);
150 this.wrap.dom.appendChild(this.el.dom);
151 this.outerWrap.dom.removeChild(this.combo.trigger.dom);
152 this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
154 this.combo.trigger.setStyle('position','relative');
155 this.combo.trigger.setStyle('left', '0px');
156 this.combo.trigger.setStyle('top', '2px');
158 this.combo.el.setStyle('vertical-align', 'text-bottom');
160 //this.trigger.setStyle('vertical-align', 'top');
162 // this should use the code from combo really... on('add' ....)
166 this.adder = this.outerWrap.createChild(
167 {tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
169 this.adder.on('click', function(e) {
170 _t.fireEvent('adderclick', this, e);
174 //this.adder.on('click', this.onAddClick, _t);
177 this.combo.on('select', function(cb, rec, ix) {
178 this.addItem(rec.data);
181 cb.el.dom.value = '';
182 //cb.lastData = rec.data;
193 // returns hidden if it's set..
194 if (!this.rendered) {return ''};
195 return this.hiddenName ? this.hiddenName : this.name;
200 onResize: function(w, h){
203 // not sure if this is needed..
204 //this.combo.onResize(w,h);
206 if(typeof w != 'number'){
207 // we do not handle it!?!?
210 var tw = this.combo.trigger.getWidth();
211 tw += this.addicon ? this.addicon.getWidth() : 0;
212 tw += this.editicon ? this.editicon.getWidth() : 0;
214 this.combo.el.setWidth( this.combo.adjustWidth('input', x));
216 this.combo.trigger.setStyle('left', '0px');
218 if(this.list && this.listWidth === undefined){
219 var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
220 this.list.setWidth(lw);
221 this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
228 addItem: function(rec)
230 var valueField = this.combo.valueField;
231 var displayField = this.combo.displayField;
232 if (this.items.indexOfKey(rec[valueField]) > -1) {
233 Roo.log("GOT Record");
238 var x = new Roo.form.ComboBoxArray.Item({
239 //id : rec[this.idField],
241 displayField : displayField ,
242 tipField : displayField ,
246 this.items.add(rec[valueField],x);
247 // add it before the element..
248 this.updateHiddenEl();
249 x.render(this.outerWrap, this.wrap.dom);
250 // add the image handler..
253 updateHiddenEl : function()
256 if (!this.hiddenEl) {
260 var idField = this.combo.valueField;
262 this.items.each(function(f) {
263 ar.push(f.data[idField]);
266 this.hiddenEl.dom.value = ar.join(',');
272 //Roo.form.ComboBoxArray.superclass.reset.call(this);
273 this.items.each(function(f) {
276 this.el.dom.value = '';
278 this.hiddenEl.dom.value = '';
284 return this.hiddenEl ? this.hiddenEl.dom.value : '';
286 setValue: function(v) // not a valid action - must use addItems..
293 if (this.store.isLocal && (typeof(v) == 'string')) {
294 // then we can use the store to find the values..
295 // comma seperated at present.. this needs to allow JSON based encoding..
296 this.hiddenEl.value = v;
298 Roo.each(v.split(','), function(k) {
299 Roo.log("CHECK " + this.valueField + ',' + k);
300 var li = this.store.query(this.valueField, k);
305 add[this.valueField] = k;
306 add[this.displayField] = li.item(0).data[this.displayField];
312 if (typeof(v) == 'object' ) {
313 // then let's assume it's an array of objects..
314 Roo.each(v, function(l) {
322 setFromData: function(v)
324 // this recieves an object, if setValues is called.
326 this.el.dom.value = v[this.displayField];
327 this.hiddenEl.dom.value = v[this.valueField];
328 if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
331 var kv = v[this.valueField];
332 var dv = v[this.displayField];
333 kv = typeof(kv) != 'string' ? '' : kv;
334 dv = typeof(dv) != 'string' ? '' : dv;
337 var keys = kv.split(',');
338 var display = dv.split(',');
339 for (var i = 0 ; i < keys.length; i++) {
342 add[this.valueField] = keys[i];
343 add[this.displayField] = display[i];
351 * Validates the combox array value
352 * @return {Boolean} True if the value is valid, else false
354 validate : function(){
355 if(this.disabled || this.validateValue(this.processValue(this.getValue()))){
362 validateValue : function(value){
363 return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
371 isDirty : function() {
377 var d = Roo.decode(String(this.originalValue));
379 return String(this.getValue()) !== String(this.originalValue);
382 var originalValue = [];
384 for (var i = 0; i < d.length; i++){
385 originalValue.push(d[i][this.valueField]);
388 return String(this.getValue()) !== String(originalValue.join(','));
397 * @class Roo.form.ComboBoxArray.Item
398 * @extends Roo.BoxComponent
399 * A selected item in the list
400 * Fred [x] Brian [x] [Pick another |v]
404 * @param {Object} config Configuration options
407 Roo.form.ComboBoxArray.Item = function(config) {
408 config.id = Roo.id();
409 Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
412 Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
415 displayField : false,
419 defaultAutoCreate : {
421 cls: 'x-cbarray-item',
428 src : Roo.BLANK_IMAGE_URL ,
436 onRender : function(ct, position)
438 Roo.form.Field.superclass.onRender.call(this, ct, position);
441 var cfg = this.getAutoCreate();
442 this.el = ct.createChild(cfg, position);
445 this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
447 this.el.child('div').dom.innerHTML = this.cb.renderer ?
448 this.cb.renderer(this.data) :
449 String.format('{0}',this.data[this.displayField]);
452 this.el.child('div').dom.setAttribute('qtip',
453 String.format('{0}',this.data[this.tipField])
456 this.el.child('img').on('click', this.remove, this);
462 if(this.cb.disabled){
465 this.cb.items.remove(this);
466 this.el.child('img').un('click', this.remove, this);
468 this.cb.updateHiddenEl();
470 this.cb.fireEvent('remove', this.cb, this);