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