Roo.form.ComboBoxArray = function(config)
{
this.addEvents({
'beforeremove' : true,
'remove' : true
});
Roo.form.ComboBoxArray.superclass.constructor.call(this, config);
this.items = new Roo.util.MixedCollection(false);
}
Roo.extend(Roo.form.ComboBoxArray, Roo.form.TextField,
{
lastData : false,
inputType: 'hidden',
width: 300,
name : false,
hiddenName : false,
seperator : ',',
items : false,
hiddenEl : false,
el : false,
onRender : function(ct, position)
{
this.combo.hiddenName = this.hiddenName ? (this.hiddenName+'-subcombo') : this.hiddenName;
this.combo.name = this.name ? (this.name+'-subcombo') : this.name;
this.combo = Roo.factory(this.combo, Roo.form);
this.combo.onRender(ct, position);
if (typeof(this.combo.width) != 'undefined') {
this.combo.onResize(this.combo.width,0);
}
this.combo.initEvents();
this.store = this.combo.store;
this.valueField = this.combo.valueField;
this.displayField = this.combo.displayField ;
this.combo.wrap.addClass('x-cbarray-grp');
var cbwrap = this.combo.wrap.createChild(
{tag: 'div', cls: 'x-cbarray-cb'},
this.combo.el.dom
);
this.hiddenEl = this.combo.wrap.createChild({
tag: 'input', type:'hidden' , name: this.hiddenName, value : ''
});
this.el = this.combo.wrap.createChild({
tag: 'input', type:'hidden' , name: this.name, value : ''
});
this.outerWrap = this.combo.wrap;
this.wrap = cbwrap;
this.outerWrap.setWidth(this.width);
this.outerWrap.dom.removeChild(this.el.dom);
this.wrap.dom.appendChild(this.el.dom);
this.outerWrap.dom.removeChild(this.combo.trigger.dom);
this.combo.wrap.dom.appendChild(this.combo.trigger.dom);
this.combo.trigger.setStyle('position','relative');
this.combo.trigger.setStyle('left', '0px');
this.combo.trigger.setStyle('top', '2px');
this.combo.el.setStyle('vertical-align', 'text-bottom');
if (this.adder) {
this.adder = this.outerWrap.createChild(
{tag: 'img', src: Roo.BLANK_IMAGE_URL, cls: 'x-form-adder', style: 'margin-left:2px'});
var _t = this;
this.adder.on('click', function(e) {
_t.fireEvent('adderclick', this, e);
}, _t);
}
this.combo.on('select', function(cb, rec, ix) {
this.addItem(rec.data);
cb.setValue('');
cb.el.dom.value = '';
}, this);
},
getName: function()
{
if (!this.rendered) {return ''};
return this.hiddenName ? this.hiddenName : this.name;
},
onResize: function(w, h){
return;
if(typeof w != 'number'){
return;
}
var tw = this.combo.trigger.getWidth();
tw += this.addicon ? this.addicon.getWidth() : 0;
tw += this.editicon ? this.editicon.getWidth() : 0;
var x = w - tw;
this.combo.el.setWidth( this.combo.adjustWidth('input', x));
this.combo.trigger.setStyle('left', '0px');
if(this.list && this.listWidth === undefined){
var lw = Math.max(x + this.combo.trigger.getWidth(), this.combo.minListWidth);
this.list.setWidth(lw);
this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
}
},
addItem: function(rec)
{
var valueField = this.combo.valueField;
var displayField = this.combo.displayField;
if (this.items.indexOfKey(rec[valueField]) > -1) {
return;
}
var x = new Roo.form.ComboBoxArray.Item({
data : rec,
displayField : displayField ,
tipField : displayField ,
cb : this
});
this.items.add(rec[valueField],x);
this.updateHiddenEl();
x.render(this.outerWrap, this.wrap.dom);
},
updateHiddenEl : function()
{
this.validate();
if (!this.hiddenEl) {
return;
}
var ar = [];
var idField = this.combo.valueField;
this.items.each(function(f) {
ar.push(f.data[idField]);
});
this.hiddenEl.dom.value = ar.join(this.seperator);
this.validate();
},
reset : function()
{
this.items.clear();
Roo.each(this.outerWrap.select('.x-cbarray-item', true).elements, function(el){
el.remove();
});
this.el.dom.value = '';
if (this.hiddenEl) {
this.hiddenEl.dom.value = '';
}
},
getValue: function()
{
return this.hiddenEl ? this.hiddenEl.dom.value : '';
},
setValue: function(v) {
this.reset();
if (this.store.isLocal && (typeof(v) == 'string')) {
this.hiddenEl.value = v;
var v_ar = [];
Roo.each(v.split(this.seperator), function(k) {
Roo.log("CHECK " + this.valueField + ',' + k);
var li = this.store.query(this.valueField, k);
if (!li.length) {
return;
}
var add = {};
add[this.valueField] = k;
add[this.displayField] = li.item(0).data[this.displayField];
this.addItem(add);
}, this)
}
if (typeof(v) == 'object' ) {
Roo.each(v, function(l) {
var add = l;
if (typeof(l) == 'string') {
add = {};
add[this.valueField] = l;
add[this.displayField] = l
}
this.addItem(add);
}, this);
}
},
setFromData: function(v)
{
this.reset();
this.el.dom.value = v[this.displayField];
this.hiddenEl.dom.value = v[this.valueField];
if (typeof(v[this.valueField]) != 'string' || !v[this.valueField].length) {
return;
}
var kv = v[this.valueField];
var dv = v[this.displayField];
kv = typeof(kv) != 'string' ? '' : kv;
dv = typeof(dv) != 'string' ? '' : dv;
var keys = kv.split(this.seperator);
var display = dv.split(this.seperator);
for (var i = 0 ; i < keys.length; i++) {
add = {};
add[this.valueField] = keys[i];
add[this.displayField] = display[i];
this.addItem(add);
}
},
validate : function(){
if(this.disabled || this.validateValue(this.processValue(this.getValue()))){
this.clearInvalid();
return true;
}
return false;
},
validateValue : function(value){
return Roo.form.ComboBoxArray.superclass.validateValue.call(this, this.getValue());
},
isDirty : function() {
if(this.disabled) {
return false;
}
try {
var d = Roo.decode(String(this.originalValue));
} catch (e) {
return String(this.getValue()) !== String(this.originalValue);
}
var originalValue = [];
for (var i = 0; i < d.length; i++){
originalValue.push(d[i][this.valueField]);
}
return String(this.getValue()) !== String(originalValue.join(this.seperator));
}
});
Roo.form.ComboBoxArray.Item = function(config) {
config.id = Roo.id();
Roo.form.ComboBoxArray.Item.superclass.constructor.call(this, config);
}
Roo.extend(Roo.form.ComboBoxArray.Item, Roo.BoxComponent, {
data : {},
cb: false,
displayField : false,
tipField : false,
defaultAutoCreate : {
tag: 'div',
cls: 'x-cbarray-item',
cn : [
{ tag: 'div' },
{
tag: 'img',
width:16,
height : 16,
src : Roo.BLANK_IMAGE_URL ,
align: 'center'
}
]
},
onRender : function(ct, position)
{
Roo.form.Field.superclass.onRender.call(this, ct, position);
if(!this.el){
var cfg = this.getAutoCreate();
this.el = ct.createChild(cfg, position);
}
this.el.child('img').dom.setAttribute('src', Roo.BLANK_IMAGE_URL);
this.el.child('div').dom.innerHTML = this.cb.renderer ?
this.cb.renderer(this.data) :
String.format('{0}',this.data[this.displayField]);
this.el.child('div').dom.setAttribute('qtip',
String.format('{0}',this.data[this.tipField])
);
this.el.child('img').on('click', this.remove, this);
},
remove : function()
{
if(this.cb.disabled){
return;
}
if(false !== this.cb.fireEvent('beforeremove', this.cb, this)){
this.cb.items.remove(this);
this.el.child('img').un('click', this.remove, this);
this.el.remove();
this.cb.updateHiddenEl();
this.cb.fireEvent('remove', this.cb, this);
}
}
});