Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / widgets / form / Checkbox.js
1 /*!
2  * Ext JS Library 3.0.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
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/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to
31      * {tag: 'input', type: 'checkbox', autocomplete: 'off'})
32      */
33     defaultAutoCreate : { tag: 'input', type: 'checkbox', autocomplete: 'off'},
34     /**
35      * @cfg {String} boxLabel The text that appears beside the checkbox
36      */
37     /**
38      * @cfg {String} inputValue The value that should go into the generated input element's value attribute
39      */
40     /**
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>
46      * </ul></div>
47      */
48     /**
49      * @cfg {Object} scope An object to use as the scope ('this' reference) of the {@link #handler} function
50      * (defaults to this Checkbox).
51      */
52
53     // private
54     actionMode : 'wrap',
55     
56         // private
57     initComponent : function(){
58         Ext.form.Checkbox.superclass.initComponent.call(this);
59         this.addEvents(
60             /**
61              * @event check
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
65              */
66             'check'
67         );
68     },
69
70     // private
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');
75         }
76     },
77
78     // private
79     initEvents : function(){
80         Ext.form.Checkbox.superclass.initEvents.call(this);
81         this.mon(this.el, 'click', this.onClick, this);
82         this.mon(this.el, 'change', this.onClick, this);
83     },
84
85         // private
86     getResizeEl : function(){
87         return this.wrap;
88     },
89
90     // private
91     getPositionEl : function(){
92         return this.wrap;
93     },
94
95     /**
96      * @hide
97      * Overridden and disabled. The editor element does not support standard valid/invalid marking.
98      * @method
99      */
100     markInvalid : Ext.emptyFn,
101     /**
102      * @hide
103      * Overridden and disabled. The editor element does not support standard valid/invalid marking.
104      * @method
105      */
106     clearInvalid : Ext.emptyFn,
107
108     // private
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;
113         }
114         this.wrap = this.el.wrap({cls: 'x-form-check-wrap'});
115         if(this.boxLabel){
116             this.wrap.createChild({tag: 'label', htmlFor: this.el.id, cls: 'x-form-cb-label', html: this.boxLabel});
117         }
118         if(this.checked){
119             this.setValue(true);
120         }else{
121             this.checked = this.el.dom.checked;
122         }
123     },
124
125     // private
126     onDestroy : function(){
127         Ext.destroy(this.wrap);
128         Ext.form.Checkbox.superclass.onDestroy.call(this);
129     },
130
131     // private
132     initValue : function() {
133         this.originalValue = this.getValue();
134     },
135
136     /**
137      * Returns the checked state of the checkbox.
138      * @return {Boolean} True if checked, else false
139      */
140     getValue : function(){
141         if(this.rendered){
142             return this.el.dom.checked;
143         }
144         return false;
145     },
146
147         // private
148     onClick : function(){
149         if(this.el.dom.checked != this.checked){
150             this.setValue(this.el.dom.checked);
151         }
152     },
153
154     /**
155      * Sets the checked state of the checkbox, fires the 'check' event, and calls a
156      * <code>{@link #handler}</code> (if configured).
157      * @param {Boolean/String} checked The following values will check the checkbox:
158      * <code>true, 'true', '1', or 'on'</code>. Any other value will uncheck the checkbox.
159      * @return {Ext.form.Field} this
160      */
161     setValue : function(v){
162         var checked = this.checked ;
163         this.checked = (v === true || v === 'true' || v == '1' || String(v).toLowerCase() == 'on');
164         if(this.rendered){
165             this.el.dom.checked = this.checked;
166             this.el.dom.defaultChecked = this.checked;
167         }
168         if(checked != this.checked){
169             this.fireEvent('check', this, this.checked);
170             if(this.handler){
171                 this.handler.call(this.scope || this, this, this.checked);
172             }
173         }
174         return this;
175     }
176 });
177 Ext.reg('checkbox', Ext.form.Checkbox);