4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-form-field-Checkbox'>/**
19 </span> * @docauthor Robert Dougan <rob@sencha.com>
21 * Single checkbox field. Can be used as a direct replacement for traditional checkbox fields. Also serves as a
22 * parent class for {@link Ext.form.field.Radio radio buttons}.
26 * In addition to the {@link Ext.form.Labelable standard field labeling options}, checkboxes
27 * may be given an optional {@link #boxLabel} which will be displayed immediately after checkbox. Also see
28 * {@link Ext.form.CheckboxGroup} for a convenient method of grouping related checkboxes.
32 * The main value of a checkbox is a boolean, indicating whether or not the checkbox is checked.
33 * The following values will check the checkbox:
40 * Any other value will uncheck the checkbox.
42 * In addition to the main boolean value, you may also specify a separate {@link #inputValue}. This will be
43 * sent as the parameter value when the form is {@link Ext.form.Basic#submit submitted}. You will want to set
44 * this value if you have multiple checkboxes with the same {@link #name}. If not specified, the value `on`
50 * Ext.create('Ext.form.Panel', {
53 * title: 'Pizza Order',
56 * xtype: 'fieldcontainer',
57 * fieldLabel: 'Toppings',
58 * defaultType: 'checkboxfield',
61 * boxLabel : 'Anchovies',
66 * boxLabel : 'Artichoke Hearts',
82 * text: 'Select Bacon',
83 * handler: function() {
84 * Ext.getCmp('checkbox3').setValue(true);
90 * handler: function() {
91 * Ext.getCmp('checkbox1').setValue(true);
92 * Ext.getCmp('checkbox2').setValue(true);
93 * Ext.getCmp('checkbox3').setValue(true);
97 * text: 'Deselect All',
98 * handler: function() {
99 * Ext.getCmp('checkbox1').setValue(false);
100 * Ext.getCmp('checkbox2').setValue(false);
101 * Ext.getCmp('checkbox3').setValue(false);
105 * renderTo: Ext.getBody()
108 Ext.define('Ext.form.field.Checkbox', {
109 extend: 'Ext.form.field.Base',
110 alias: ['widget.checkboxfield', 'widget.checkbox'],
111 alternateClassName: 'Ext.form.Checkbox',
112 requires: ['Ext.XTemplate', 'Ext.form.CheckboxManager'],
114 // note: {id} here is really {inputId}, but {cmpId} is available
116 '<tpl if="boxLabel && boxLabelAlign == \'before\'">',
117 '<label id="{cmpId}-boxLabelEl" class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">{boxLabel}</label>',
119 // Creates not an actual checkbox, but a button which is given aria role="checkbox" and
120 // styled with a custom checkbox image. This allows greater control and consistency in
121 // styling, and using a button allows it to gain focus and handle keyboard nav properly.
122 '<input type="button" id="{id}" ',
123 '<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
124 'class="{fieldCls} {typeCls}" autocomplete="off" hidefocus="true" />',
125 '<tpl if="boxLabel && boxLabelAlign == \'after\'">',
126 '<label id="{cmpId}-boxLabelEl" class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">{boxLabel}</label>',
129 disableFormats: true,
136 <span id='Ext-form-field-Checkbox-cfg-focusCls'> /**
137 </span> * @cfg {String} [focusCls='x-form-cb-focus']
138 * The CSS class to use when the checkbox receives focus
140 focusCls: Ext.baseCSSPrefix + 'form-cb-focus',
142 <span id='Ext-form-field-Checkbox-cfg-fieldCls'> /**
143 </span> * @cfg {String} [fieldCls='x-form-field']
144 * The default CSS class for the checkbox
147 <span id='Ext-form-field-Checkbox-cfg-fieldBodyCls'> /**
148 </span> * @cfg {String} [fieldBodyCls='x-form-cb-wrap']
149 * An extra CSS class to be applied to the body content element in addition to {@link #fieldBodyCls}.
152 fieldBodyCls: Ext.baseCSSPrefix + 'form-cb-wrap',
154 <span id='Ext-form-field-Checkbox-cfg-checked'> /**
155 </span> * @cfg {Boolean} checked
156 * true if the checkbox should render initially checked
160 <span id='Ext-form-field-Checkbox-cfg-checkedCls'> /**
161 </span> * @cfg {String} [checkedCls='x-form-cb-checked']
162 * The CSS class added to the component's main element when it is in the checked state.
164 checkedCls: Ext.baseCSSPrefix + 'form-cb-checked',
166 <span id='Ext-form-field-Checkbox-cfg-boxLabel'> /**
167 </span> * @cfg {String} boxLabel
168 * An optional text label that will appear next to the checkbox. Whether it appears before or after the checkbox is
169 * determined by the {@link #boxLabelAlign} config.
172 <span id='Ext-form-field-Checkbox-cfg-boxLabelCls'> /**
173 </span> * @cfg {String} [boxLabelCls='x-form-cb-label']
174 * The CSS class to be applied to the {@link #boxLabel} element
176 boxLabelCls: Ext.baseCSSPrefix + 'form-cb-label',
178 <span id='Ext-form-field-Checkbox-cfg-boxLabelAlign'> /**
179 </span> * @cfg {String} boxLabelAlign
180 * The position relative to the checkbox where the {@link #boxLabel} should appear. Recognized values are 'before'
183 boxLabelAlign: 'after',
185 <span id='Ext-form-field-Checkbox-cfg-inputValue'> /**
186 </span> * @cfg {String} inputValue
187 * The value that should go into the generated input element's value attribute and should be used as the parameter
188 * value when submitting as part of a form.
192 <span id='Ext-form-field-Checkbox-cfg-uncheckedValue'> /**
193 </span> * @cfg {String} uncheckedValue
194 * If configured, this will be submitted as the checkbox's value during form submit if the checkbox is unchecked. By
195 * default this is undefined, which results in nothing being submitted for the checkbox field when the form is
196 * submitted (the default behavior of HTML checkboxes).
199 <span id='Ext-form-field-Checkbox-cfg-handler'> /**
200 </span> * @cfg {Function} handler
201 * A function called when the {@link #checked} value changes (can be used instead of handling the {@link #change
203 * @cfg {Ext.form.field.Checkbox} handler.checkbox The Checkbox being toggled.
204 * @cfg {Boolean} handler.checked The new checked state of the checkbox.
207 <span id='Ext-form-field-Checkbox-cfg-scope'> /**
208 </span> * @cfg {Object} scope
209 * An object to use as the scope ('this' reference) of the {@link #handler} function (defaults to this Checkbox).
213 checkChangeEvents: [],
214 inputType: 'checkbox',
215 ariaRole: 'checkbox',
220 initComponent: function(){
221 this.callParent(arguments);
222 this.getManager().add(this);
225 initValue: function() {
227 checked = !!me.checked;
229 <span id='Ext-form-field-Checkbox-property-originalValue'> /**
230 </span> * @property {Object} originalValue
231 * The original value of the field as configured in the {@link #checked} configuration, or as loaded by the last
232 * form load operation if the form's {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad} setting is `true`.
234 me.originalValue = me.lastValue = checked;
236 // Set the initial checked state
237 me.setValue(checked);
241 onRender : function(ct, position) {
244 <span id='Ext-form-field-Checkbox-property-boxLabelEl'> /**
245 </span> * @property {Ext.Element} boxLabelEl
246 * A reference to the label element created for the {@link #boxLabel}. Only present if the component has been
247 * rendered and has a boxLabel configured.
249 me.addChildEls('boxLabelEl');
251 Ext.applyIf(me.subTplData, {
252 boxLabel: me.boxLabel,
253 boxLabelCls: me.boxLabelCls,
254 boxLabelAlign: me.boxLabelAlign
257 me.callParent(arguments);
260 initEvents: function() {
263 me.mon(me.inputEl, 'click', me.onBoxClick, me);
266 <span id='Ext-form-field-Checkbox-method-onBoxClick'> /**
267 </span> * @private Handle click on the checkbox button
269 onBoxClick: function(e) {
271 if (!me.disabled && !me.readOnly) {
272 this.setValue(!this.checked);
276 <span id='Ext-form-field-Checkbox-method-getRawValue'> /**
277 </span> * Returns the checked state of the checkbox.
278 * @return {Boolean} True if checked, else false
280 getRawValue: function() {
284 <span id='Ext-form-field-Checkbox-method-getValue'> /**
285 </span> * Returns the checked state of the checkbox.
286 * @return {Boolean} True if checked, else false
288 getValue: function() {
292 <span id='Ext-form-field-Checkbox-method-getSubmitValue'> /**
293 </span> * Returns the submit value for the checkbox which can be used when submitting forms.
294 * @return {Boolean/Object} True if checked; otherwise either the {@link #uncheckedValue} or null.
296 getSubmitValue: function() {
297 var unchecked = this.uncheckedValue,
298 uncheckedVal = Ext.isDefined(unchecked) ? unchecked : null;
299 return this.checked ? this.inputValue : uncheckedVal;
302 <span id='Ext-form-field-Checkbox-method-setRawValue'> /**
303 </span> * Sets the checked state of the checkbox.
305 * @param {Boolean/String/Number} value The following values will check the checkbox:
306 * `true, 'true', '1', 1, or 'on'`, as well as a String that matches the {@link #inputValue}.
307 * Any other value will uncheck the checkbox.
308 * @return {Boolean} the new checked state of the checkbox
310 setRawValue: function(value) {
312 inputEl = me.inputEl,
313 inputValue = me.inputValue,
314 checked = (value === true || value === 'true' || value === '1' || value === 1 ||
315 (((Ext.isString(value) || Ext.isNumber(value)) && inputValue) ? value == inputValue : me.onRe.test(value)));
318 inputEl.dom.setAttribute('aria-checked', checked);
319 me[checked ? 'addCls' : 'removeCls'](me.checkedCls);
322 me.checked = me.rawValue = checked;
326 <span id='Ext-form-field-Checkbox-method-setValue'> /**
327 </span> * Sets the checked state of the checkbox, and invokes change detection.
328 * @param {Boolean/String} checked The following values will check the checkbox: `true, 'true', '1', or 'on'`, as
329 * well as a String that matches the {@link #inputValue}. Any other value will uncheck the checkbox.
330 * @return {Ext.form.field.Checkbox} this
332 setValue: function(checked) {
335 // If an array of strings is passed, find all checkboxes in the group with the same name as this
336 // one and check all those whose inputValue is in the array, unchecking all the others. This is to
337 // facilitate setting values from Ext.form.Basic#setValues, but is not publicly documented as we
338 // don't want users depending on this behavior.
339 if (Ext.isArray(checked)) {
340 me.getManager().getByName(me.name).each(function(cb) {
341 cb.setValue(Ext.Array.contains(checked, cb.inputValue));
344 me.callParent(arguments);
351 valueToRaw: function(value) {
352 // No extra conversion for checkboxes
356 <span id='Ext-form-field-Checkbox-method-onChange'> /**
358 * Called when the checkbox's checked state changes. Invokes the {@link #handler} callback
359 * function if specified.
361 onChange: function(newVal, oldVal) {
363 handler = me.handler;
365 handler.call(me.scope || me, me, newVal);
367 me.callParent(arguments);
371 beforeDestroy: function(){
373 this.getManager().removeAtKey(this.id);
377 getManager: function() {
378 return Ext.form.CheckboxManager;
381 onEnable: function() {
383 inputEl = me.inputEl;
386 // Can still be disabled if the field is readOnly
387 inputEl.dom.disabled = me.readOnly;
391 setReadOnly: function(readOnly) {
393 inputEl = me.inputEl;
395 // Set the button to disabled when readonly
396 inputEl.dom.disabled = readOnly || me.disabled;
398 me.readOnly = readOnly;
401 // Calculates and returns the natural width of the bodyEl. It's possible that the initial rendering will
402 // cause the boxLabel to wrap and give us a bad width, so we must prevent wrapping while measuring.
403 getBodyNaturalWidth: function() {
408 bodyEl.setStyle(ws, 'nowrap');
409 width = bodyEl.getWidth();
410 bodyEl.setStyle(ws, '');