Ext.define('Ext.form.field.Checkbox', {
extend: 'Ext.form.field.Base',
alias: ['widget.checkboxfield', 'widget.checkbox'],
alternateClassName: 'Ext.form.Checkbox',
requires: ['Ext.XTemplate', 'Ext.form.CheckboxManager' ],
componentLayout: 'field',
childEls: [
'boxLabelEl'
],
fieldSubTpl: [
'<tpl if="boxLabel && boxLabelAlign == \'before\'">',
'{beforeBoxLabelTpl}',
'<label id="{cmpId}-boxLabelEl" {boxLabelAttrTpl} class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">',
'{beforeBoxLabelTextTpl}',
'{boxLabel}',
'{afterBoxLabelTextTpl}',
'</label>',
'{afterBoxLabelTpl}',
'</tpl>',
'<input type="button" id="{id}" {inputAttrTpl}',
'<tpl if="tabIdx"> tabIndex="{tabIdx}"</tpl>',
'<tpl if="disabled"> disabled="disabled"</tpl>',
'<tpl if="fieldStyle"> style="{fieldStyle}"</tpl>',
' class="{fieldCls} {typeCls}" autocomplete="off" hidefocus="true" />',
'<tpl if="boxLabel && boxLabelAlign == \'after\'">',
'{beforeBoxLabelTpl}',
'<label id="{cmpId}-boxLabelEl" {boxLabelAttrTpl} class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">',
'{beforeBoxLabelTextTpl}',
'{boxLabel}',
'{afterBoxLabelTextTpl}',
'</label>',
'{afterBoxLabelTpl}',
'</tpl>',
{
disableFormats: true,
compiled: true
}
],
subTplInsertions: [
'beforeBoxLabelTpl',
'afterBoxLabelTpl',
'beforeBoxLabelTextTpl',
'afterBoxLabelTextTpl',
'boxLabelAttrTpl',
'inputAttrTpl'
],
isCheckbox: true,
focusCls: 'form-cb-focus',
fieldBodyCls: Ext.baseCSSPrefix + 'form-cb-wrap',
checked: false,
checkedCls: Ext.baseCSSPrefix + 'form-cb-checked',
boxLabelCls: Ext.baseCSSPrefix + 'form-cb-label',
boxLabelAlign: 'after',
inputValue: 'on',
checkChangeEvents: [],
inputType: 'checkbox',
onRe: /^on$/i,
initComponent: function() {
this.callParent(arguments);
this.getManager().add(this);
},
initValue: function() {
var me = this,
checked = !!me.checked;
me.originalValue = me.lastValue = checked;
me.setValue(checked);
},
getElConfig: function() {
var me = this;
if (me.isChecked(me.rawValue, me.inputValue)) {
me.addCls(me.checkedCls);
}
return me.callParent();
},
getFieldStyle: function() {
return Ext.isObject(this.fieldStyle) ? Ext.DomHelper.generateStyles(this.fieldStyle) : this.fieldStyle ||'';
},
getSubTplData: function() {
var me = this;
return Ext.apply(me.callParent(), {
disabled : me.readOnly || me.disabled,
boxLabel : me.boxLabel,
boxLabelCls : me.boxLabelCls,
boxLabelAlign : me.boxLabelAlign
});
},
initEvents: function() {
var me = this;
me.callParent();
me.mon(me.inputEl, 'click', me.onBoxClick, me);
},
onBoxClick: function(e) {
var me = this;
if (!me.disabled && !me.readOnly) {
this.setValue(!this.checked);
}
},
getRawValue: function() {
return this.checked;
},
getValue: function() {
return this.checked;
},
getSubmitValue: function() {
var unchecked = this.uncheckedValue,
uncheckedVal = Ext.isDefined(unchecked) ? unchecked : null;
return this.checked ? this.inputValue : uncheckedVal;
},
isChecked: function(rawValue, inputValue) {
return (rawValue === true || rawValue === 'true' || rawValue === '1' || rawValue === 1 ||
(((Ext.isString(rawValue) || Ext.isNumber(rawValue)) && inputValue) ? rawValue == inputValue : this.onRe.test(rawValue)));
},
setRawValue: function(value) {
var me = this,
inputEl = me.inputEl,
checked = me.isChecked(value, me.inputValue);
if (inputEl) {
me[checked ? 'addCls' : 'removeCls'](me.checkedCls);
}
me.checked = me.rawValue = checked;
return checked;
},
setValue: function(checked) {
var me = this,
boxes, i, len, box;
if (Ext.isArray(checked)) {
boxes = me.getManager().getByName(me.name, me.getFormId()).items;
len = boxes.length;
for (i = 0; i < len; ++i) {
box = boxes[i];
box.setValue(Ext.Array.contains(checked, box.inputValue));
}
} else {
me.callParent(arguments);
}
return me;
},
valueToRaw: function(value) {
return value;
},
onChange: function(newVal, oldVal) {
var me = this,
handler = me.handler;
if (handler) {
handler.call(me.scope || me, me, newVal);
}
me.callParent(arguments);
},
resetOriginalValue: function( fromBoxInGroup){
var me = this,
boxes,
box,
len,
i;
if (!fromBoxInGroup) {
boxes = me.getManager().getByName(me.name, me.getFormId()).items;
len = boxes.length;
for (i = 0; i < len; ++i) {
box = boxes[i];
if (box !== me) {
boxes[i].resetOriginalValue(true);
}
}
}
me.callParent();
},
beforeDestroy: function(){
this.callParent();
this.getManager().removeAtKey(this.id);
},
getManager: function() {
return Ext.form.CheckboxManager;
},
onEnable: function() {
var me = this,
inputEl = me.inputEl;
me.callParent();
if (inputEl) {
inputEl.dom.disabled = me.readOnly;
}
},
setReadOnly: function(readOnly) {
var me = this,
inputEl = me.inputEl;
if (inputEl) {
inputEl.dom.disabled = !!readOnly || me.disabled;
}
me.callParent(arguments);
},
getFormId: function(){
var me = this,
form;
if (!me.formId) {
form = me.up('form');
if (form) {
me.formId = form.id;
}
}
return me.formId;
}
});