3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.form.Checkbox
9 * @extends Ext.form.Field
10 * Single checkbox field. Can be used as a direct replacement for traditional checkbox fields.
12 * Creates a new Checkbox
13 * @param {Object} config Configuration options
16 Ext.form.Checkbox = Ext.extend(Ext.form.Field, {
18 * @cfg {String} focusClass The CSS class to use when the checkbox receives focus (defaults to undefined)
20 focusClass : undefined,
22 * @cfg {String} fieldClass The default CSS class for the checkbox (defaults to 'x-form-field')
24 fieldClass : 'x-form-field',
26 * @cfg {Boolean} checked <tt>true</tt> if the checkbox should render initially checked (defaults to <tt>false</tt>)
30 * @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to
31 * {tag: 'input', type: 'checkbox', autocomplete: 'off'})
33 defaultAutoCreate : { tag: 'input', type: 'checkbox', autocomplete: 'off'},
35 * @cfg {String} boxLabel The text that appears beside the checkbox
38 * @cfg {String} inputValue The value that should go into the generated input element's value attribute
41 * @cfg {Function} handler A function called when the {@link #checked} value changes (can be used instead of
42 * handling the check event). The handler is passed the following parameters:
43 * <div class="mdetail-params"><ul>
44 * <li><b>checkbox</b> : Ext.form.Checkbox<div class="sub-desc">The Checkbox being toggled.</div></li>
45 * <li><b>checked</b> : Boolean<div class="sub-desc">The new checked state of the checkbox.</div></li>
49 * @cfg {Object} scope An object to use as the scope ('this' reference) of the {@link #handler} function
50 * (defaults to this Checkbox).
57 initComponent : function(){
58 Ext.form.Checkbox.superclass.initComponent.call(this);
62 * Fires when the checkbox is checked or unchecked.
63 * @param {Ext.form.Checkbox} this This checkbox
64 * @param {Boolean} checked The new checked value
71 onResize : function(){
72 Ext.form.Checkbox.superclass.onResize.apply(this, arguments);
73 if(!this.boxLabel && !this.fieldLabel){
74 this.el.alignTo(this.wrap, 'c-c');
79 initEvents : function(){
80 Ext.form.Checkbox.superclass.initEvents.call(this);
90 * Overridden and disabled. The editor element does not support standard valid/invalid marking.
93 markInvalid : Ext.emptyFn,
96 * Overridden and disabled. The editor element does not support standard valid/invalid marking.
99 clearInvalid : Ext.emptyFn,
102 onRender : function(ct, position){
103 Ext.form.Checkbox.superclass.onRender.call(this, ct, position);
104 if(this.inputValue !== undefined){
105 this.el.dom.value = this.inputValue;
107 this.wrap = this.el.wrap({cls: 'x-form-check-wrap'});
109 this.wrap.createChild({tag: 'label', htmlFor: this.el.id, cls: 'x-form-cb-label', html: this.boxLabel});
114 this.checked = this.el.dom.checked;
116 // Need to repaint for IE, otherwise positioning is broken
120 this.resizeEl = this.positionEl = this.wrap;
124 onDestroy : function(){
125 Ext.destroy(this.wrap);
126 Ext.form.Checkbox.superclass.onDestroy.call(this);
130 initValue : function() {
131 this.originalValue = this.getValue();
135 * Returns the checked state of the checkbox.
136 * @return {Boolean} True if checked, else false
138 getValue : function(){
140 return this.el.dom.checked;
146 onClick : function(){
147 if(this.el.dom.checked != this.checked){
148 this.setValue(this.el.dom.checked);
153 * Sets the checked state of the checkbox, fires the 'check' event, and calls a
154 * <code>{@link #handler}</code> (if configured).
155 * @param {Boolean/String} checked The following values will check the checkbox:
156 * <code>true, 'true', '1', or 'on'</code>. Any other value will uncheck the checkbox.
157 * @return {Ext.form.Field} this
159 setValue : function(v){
160 var checked = this.checked ;
161 this.checked = (v === true || v === 'true' || v == '1' || String(v).toLowerCase() == 'on');
163 this.el.dom.checked = this.checked;
164 this.el.dom.defaultChecked = this.checked;
166 if(checked != this.checked){
167 this.fireEvent('check', this, this.checked);
169 this.handler.call(this.scope || this, this, this.checked);
175 Ext.reg('checkbox', Ext.form.Checkbox);