3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.form.Checkbox"></div>/**
15 * @class Ext.form.Checkbox
16 * @extends Ext.form.Field
17 * Single checkbox field. Can be used as a direct replacement for traditional checkbox fields.
19 * Creates a new Checkbox
20 * @param {Object} config Configuration options
23 Ext.form.Checkbox = Ext.extend(Ext.form.Field, {
24 <div id="cfg-Ext.form.Checkbox-focusClass"></div>/**
25 * @cfg {String} focusClass The CSS class to use when the checkbox receives focus (defaults to undefined)
27 focusClass : undefined,
28 <div id="cfg-Ext.form.Checkbox-fieldClass"></div>/**
29 * @cfg {String} fieldClass The default CSS class for the checkbox (defaults to 'x-form-field')
31 fieldClass : 'x-form-field',
32 <div id="cfg-Ext.form.Checkbox-checked"></div>/**
33 * @cfg {Boolean} checked <tt>true</tt> if the checkbox should render initially checked (defaults to <tt>false</tt>)
36 <div id="cfg-Ext.form.Checkbox-autoCreate"></div>/**
37 * @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to
38 * {tag: 'input', type: 'checkbox', autocomplete: 'off'})
40 defaultAutoCreate : { tag: 'input', type: 'checkbox', autocomplete: 'off'},
41 <div id="cfg-Ext.form.Checkbox-boxLabel"></div>/**
42 * @cfg {String} boxLabel The text that appears beside the checkbox
44 <div id="cfg-Ext.form.Checkbox-inputValue"></div>/**
45 * @cfg {String} inputValue The value that should go into the generated input element's value attribute
47 <div id="cfg-Ext.form.Checkbox-handler"></div>/**
48 * @cfg {Function} handler A function called when the {@link #checked} value changes (can be used instead of
49 * handling the check event). The handler is passed the following parameters:
50 * <div class="mdetail-params"><ul>
51 * <li><b>checkbox</b> : Ext.form.Checkbox<div class="sub-desc">The Checkbox being toggled.</div></li>
52 * <li><b>checked</b> : Boolean<div class="sub-desc">The new checked state of the checkbox.</div></li>
55 <div id="cfg-Ext.form.Checkbox-scope"></div>/**
56 * @cfg {Object} scope An object to use as the scope ('this' reference) of the {@link #handler} function
57 * (defaults to this Checkbox).
64 initComponent : function(){
65 Ext.form.Checkbox.superclass.initComponent.call(this);
67 <div id="event-Ext.form.Checkbox-check"></div>/**
69 * Fires when the checkbox is checked or unchecked.
70 * @param {Ext.form.Checkbox} this This checkbox
71 * @param {Boolean} checked The new checked value
78 onResize : function(){
79 Ext.form.Checkbox.superclass.onResize.apply(this, arguments);
80 if(!this.boxLabel && !this.fieldLabel){
81 this.el.alignTo(this.wrap, 'c-c');
86 initEvents : function(){
87 Ext.form.Checkbox.superclass.initEvents.call(this);
95 <div id="method-Ext.form.Checkbox-markInvalid"></div>/**
97 * Overridden and disabled. The editor element does not support standard valid/invalid marking.
100 markInvalid : Ext.emptyFn,
101 <div id="method-Ext.form.Checkbox-clearInvalid"></div>/**
103 * Overridden and disabled. The editor element does not support standard valid/invalid marking.
106 clearInvalid : Ext.emptyFn,
109 onRender : function(ct, position){
110 Ext.form.Checkbox.superclass.onRender.call(this, ct, position);
111 if(this.inputValue !== undefined){
112 this.el.dom.value = this.inputValue;
114 this.wrap = this.el.wrap({cls: 'x-form-check-wrap'});
116 this.wrap.createChild({tag: 'label', htmlFor: this.el.id, cls: 'x-form-cb-label', html: this.boxLabel});
121 this.checked = this.el.dom.checked;
123 // Need to repaint for IE, otherwise positioning is broken
127 this.resizeEl = this.positionEl = this.wrap;
131 onDestroy : function(){
132 Ext.destroy(this.wrap);
133 Ext.form.Checkbox.superclass.onDestroy.call(this);
137 initValue : function() {
138 this.originalValue = this.getValue();
141 <div id="method-Ext.form.Checkbox-getValue"></div>/**
142 * Returns the checked state of the checkbox.
143 * @return {Boolean} True if checked, else false
145 getValue : function(){
147 return this.el.dom.checked;
153 onClick : function(){
154 if(this.el.dom.checked != this.checked){
155 this.setValue(this.el.dom.checked);
159 <div id="method-Ext.form.Checkbox-setValue"></div>/**
160 * Sets the checked state of the checkbox, fires the 'check' event, and calls a
161 * <code>{@link #handler}</code> (if configured).
162 * @param {Boolean/String} checked The following values will check the checkbox:
163 * <code>true, 'true', '1', or 'on'</code>. Any other value will uncheck the checkbox.
164 * @return {Ext.form.Field} this
166 setValue : function(v){
167 var checked = this.checked ;
168 this.checked = (v === true || v === 'true' || v == '1' || String(v).toLowerCase() == 'on');
170 this.el.dom.checked = this.checked;
171 this.el.dom.defaultChecked = this.checked;
173 if(checked != this.checked){
174 this.fireEvent('check', this, this.checked);
176 this.handler.call(this.scope || this, this, this.checked);
182 Ext.reg('checkbox', Ext.form.Checkbox);