Ext.define('Ext.form.CheckboxGroup', {
extend:'Ext.form.FieldContainer',
mixins: {
field: 'Ext.form.field.Field'
},
alias: 'widget.checkboxgroup',
requires: ['Ext.layout.container.CheckboxGroup', 'Ext.form.field.Base'],
columns : 'auto',
vertical : false,
allowBlank : true,
blankText : "You must select at least one item in this group",
defaultType : 'checkboxfield',
groupCls : Ext.baseCSSPrefix + 'form-check-group',
fieldBodyCls: Ext.baseCSSPrefix + 'form-checkboxgroup-body',
layout: 'checkboxgroup',
initComponent: function() {
var me = this;
me.callParent();
me.initField();
},
initValue: function() {
var me = this,
valueCfg = me.value;
me.originalValue = me.lastValue = valueCfg || me.getValue();
if (valueCfg) {
me.setValue(valueCfg);
}
},
onFieldAdded: function(field) {
var me = this;
if (field.isCheckbox) {
me.mon(field, 'change', me.checkChange, me);
}
me.callParent(arguments);
},
onFieldRemoved: function(field) {
var me = this;
if (field.isCheckbox) {
me.mun(field, 'change', me.checkChange, me);
}
me.callParent(arguments);
},
isEqual: function(value1, value2) {
var toQueryString = Ext.Object.toQueryString;
return toQueryString(value1) === toQueryString(value2);
},
getErrors: function() {
var errors = [];
if (!this.allowBlank && Ext.isEmpty(this.getChecked())) {
errors.push(this.blankText);
}
return errors;
},
getBoxes: function(query) {
return this.query('[isCheckbox]' + (query||''));
},
eachBox: function(fn, scope) {
Ext.Array.forEach(this.getBoxes(), fn, scope || this);
},
getChecked: function() {
return this.getBoxes('[checked]');
},
isDirty: function(){
var boxes = this.getBoxes(),
b ,
bLen = boxes.length;
for (b = 0; b < bLen; b++) {
if (boxes[b].isDirty()) {
return true;
}
}
},
setReadOnly: function(readOnly) {
var boxes = this.getBoxes(),
b,
bLen = boxes.length;
for (b = 0; b < bLen; b++) {
boxes[b].setReadOnly(readOnly);
}
this.readOnly = readOnly;
},
reset: function() {
var me = this,
hadError = me.hasActiveError(),
preventMark = me.preventMark;
me.preventMark = true;
me.batchChanges(function() {
var boxes = me.getBoxes(),
b,
bLen = boxes.length;
for (b = 0; b < bLen; b++) {
boxes[b].reset();
}
});
me.preventMark = preventMark;
me.unsetActiveError();
if (hadError) {
me.updateLayout();
}
},
resetOriginalValue: function(){
var me = this,
boxes = me.getBoxes(),
b,
bLen = boxes.length;
for (b = 0; b < bLen; b++) {
boxes[b].resetOriginalValue();
}
me.originalValue = me.getValue();
me.checkDirty();
},
setValue: function(value) {
var me = this,
boxes = me.getBoxes(),
b,
bLen = boxes.length,
box, name,
cbValue;
me.batchChanges(function() {
for (b = 0; b < bLen; b++) {
box = boxes[b];
name = box.getName();
cbValue = false;
if (value && value.hasOwnProperty(name)) {
if (Ext.isArray(value[name])) {
cbValue = Ext.Array.contains(value[name], box.inputValue);
} else {
cbValue = value[name];
}
}
box.setValue(cbValue);
}
});
return me;
},
getValue: function() {
var values = {},
boxes = this.getBoxes(),
b,
bLen = boxes.length,
box, name, inputValue, bucket;
for (b = 0; b < bLen; b++) {
box = boxes[b];
name = box.getName();
inputValue = box.inputValue;
if (box.getValue()) {
if (values.hasOwnProperty(name)) {
bucket = values[name];
if (!Ext.isArray(bucket)) {
bucket = values[name] = [bucket];
}
bucket.push(inputValue);
} else {
values[name] = inputValue;
}
}
}
return values;
},
getSubmitData: function() {
return null;
},
getModelData: function() {
return null;
},
validate: function() {
var me = this,
errors,
isValid,
wasValid;
if (me.disabled) {
isValid = true;
} else {
errors = me.getErrors();
isValid = Ext.isEmpty(errors);
wasValid = !me.hasActiveError();
if (isValid) {
me.unsetActiveError();
} else {
me.setActiveError(errors);
}
}
if (isValid !== wasValid) {
me.fireEvent('validitychange', me, isValid);
me.updateLayout();
}
return isValid;
}
}, function() {
this.borrow(Ext.form.field.Base, ['markInvalid', 'clearInvalid']);
});