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