Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / docs / source / Checkbox.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.2.1
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.form.Checkbox"></div>/**
16  * @class Ext.form.Checkbox
17  * @extends Ext.form.Field
18  * Single checkbox field.  Can be used as a direct replacement for traditional checkbox fields.
19  * @constructor
20  * Creates a new Checkbox
21  * @param {Object} config Configuration options
22  * @xtype checkbox
23  */
24 Ext.form.Checkbox = Ext.extend(Ext.form.Field,  {
25     <div id="cfg-Ext.form.Checkbox-focusClass"></div>/**
26      * @cfg {String} focusClass The CSS class to use when the checkbox receives focus (defaults to undefined)
27      */
28     focusClass : undefined,
29     <div id="cfg-Ext.form.Checkbox-fieldClass"></div>/**
30      * @cfg {String} fieldClass The default CSS class for the checkbox (defaults to 'x-form-field')
31      */
32     fieldClass : 'x-form-field',
33     <div id="cfg-Ext.form.Checkbox-checked"></div>/**
34      * @cfg {Boolean} checked <tt>true</tt> if the checkbox should render initially checked (defaults to <tt>false</tt>)
35      */
36     checked : false,
37     <div id="cfg-Ext.form.Checkbox-boxLabel"></div>/**
38      * @cfg {String} boxLabel The text that appears beside the checkbox
39      */
40     boxLabel: '&#160;',
41     <div id="cfg-Ext.form.Checkbox-autoCreate"></div>/**
42      * @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to
43      * {tag: 'input', type: 'checkbox', autocomplete: 'off'})
44      */
45     defaultAutoCreate : { tag: 'input', type: 'checkbox', autocomplete: 'off'},
46     <div id="cfg-Ext.form.Checkbox-boxLabel"></div>/**
47      * @cfg {String} boxLabel The text that appears beside the checkbox
48      */
49     <div id="cfg-Ext.form.Checkbox-inputValue"></div>/**
50      * @cfg {String} inputValue The value that should go into the generated input element's value attribute
51      */
52     <div id="cfg-Ext.form.Checkbox-handler"></div>/**
53      * @cfg {Function} handler A function called when the {@link #checked} value changes (can be used instead of
54      * handling the check event). The handler is passed the following parameters:
55      * <div class="mdetail-params"><ul>
56      * <li><b>checkbox</b> : Ext.form.Checkbox<div class="sub-desc">The Checkbox being toggled.</div></li>
57      * <li><b>checked</b> : Boolean<div class="sub-desc">The new checked state of the checkbox.</div></li>
58      * </ul></div>
59      */
60     <div id="cfg-Ext.form.Checkbox-scope"></div>/**
61      * @cfg {Object} scope An object to use as the scope ('this' reference) of the {@link #handler} function
62      * (defaults to this Checkbox).
63      */
64
65     // private
66     actionMode : 'wrap',
67
68         // private
69     initComponent : function(){
70         Ext.form.Checkbox.superclass.initComponent.call(this);
71         this.addEvents(
72             <div id="event-Ext.form.Checkbox-check"></div>/**
73              * @event check
74              * Fires when the checkbox is checked or unchecked.
75              * @param {Ext.form.Checkbox} this This checkbox
76              * @param {Boolean} checked The new checked value
77              */
78             'check'
79         );
80     },
81
82     // private
83     onResize : function(){
84         Ext.form.Checkbox.superclass.onResize.apply(this, arguments);
85         if(!this.boxLabel && !this.fieldLabel){
86             this.el.alignTo(this.wrap, 'c-c');
87         }
88     },
89
90     // private
91     initEvents : function(){
92         Ext.form.Checkbox.superclass.initEvents.call(this);
93         this.mon(this.el, {
94             scope: this,
95             click: this.onClick,
96             change: this.onClick
97         });
98     },
99
100     <div id="method-Ext.form.Checkbox-markInvalid"></div>/**
101      * @hide
102      * Overridden and disabled. The editor element does not support standard valid/invalid marking.
103      * @method
104      */
105     markInvalid : Ext.emptyFn,
106     <div id="method-Ext.form.Checkbox-clearInvalid"></div>/**
107      * @hide
108      * Overridden and disabled. The editor element does not support standard valid/invalid marking.
109      * @method
110      */
111     clearInvalid : Ext.emptyFn,
112
113     // private
114     onRender : function(ct, position){
115         Ext.form.Checkbox.superclass.onRender.call(this, ct, position);
116         if(this.inputValue !== undefined){
117             this.el.dom.value = this.inputValue;
118         }
119         this.wrap = this.el.wrap({cls: 'x-form-check-wrap'});
120         if(this.boxLabel){
121             this.wrap.createChild({tag: 'label', htmlFor: this.el.id, cls: 'x-form-cb-label', html: this.boxLabel});
122         }
123         if(this.checked){
124             this.setValue(true);
125         }else{
126             this.checked = this.el.dom.checked;
127         }
128         // Need to repaint for IE, otherwise positioning is broken
129         if(Ext.isIE){
130             this.wrap.repaint();
131         }
132         this.resizeEl = this.positionEl = this.wrap;
133     },
134
135     // private
136     onDestroy : function(){
137         Ext.destroy(this.wrap);
138         Ext.form.Checkbox.superclass.onDestroy.call(this);
139     },
140
141     // private
142     initValue : function() {
143         this.originalValue = this.getValue();
144     },
145
146     <div id="method-Ext.form.Checkbox-getValue"></div>/**
147      * Returns the checked state of the checkbox.
148      * @return {Boolean} True if checked, else false
149      */
150     getValue : function(){
151         if(this.rendered){
152             return this.el.dom.checked;
153         }
154         return this.checked;
155     },
156
157         // private
158     onClick : function(){
159         if(this.el.dom.checked != this.checked){
160             this.setValue(this.el.dom.checked);
161         }
162     },
163
164     <div id="method-Ext.form.Checkbox-setValue"></div>/**
165      * Sets the checked state of the checkbox, fires the 'check' event, and calls a
166      * <code>{@link #handler}</code> (if configured).
167      * @param {Boolean/String} checked The following values will check the checkbox:
168      * <code>true, 'true', '1', or 'on'</code>. Any other value will uncheck the checkbox.
169      * @return {Ext.form.Field} this
170      */
171     setValue : function(v){
172         var checked = this.checked ;
173         this.checked = (v === true || v === 'true' || v == '1' || String(v).toLowerCase() == 'on');
174         if(this.rendered){
175             this.el.dom.checked = this.checked;
176             this.el.dom.defaultChecked = this.checked;
177         }
178         if(checked != this.checked){
179             this.fireEvent('check', this, this.checked);
180             if(this.handler){
181                 this.handler.call(this.scope || this, this, this.checked);
182             }
183         }
184         return this;
185     }
186 });
187 Ext.reg('checkbox', Ext.form.Checkbox);
188 </pre>    
189 </body>
190 </html>