]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/field/Checkbox.js
2 * The checkbox field is an enhanced version of the native browser checkbox and is great for enabling your user to
3 * choose one or more items from a set (for example choosing toppings for a pizza order). It works like any other
4 * {@link Ext.field.Field field} and is usually found in the context of a form:
8 * @example miniphone preview
9 * var form = Ext.create('Ext.form.Panel', {
13 * xtype: 'checkboxfield',
20 * xtype: 'checkboxfield',
28 * { xtype: 'spacer' },
31 * handler: function() {
32 * var form = Ext.ComponentQuery.query('formpanel')[0],
33 * values = form.getValues();
36 * "Tomato: " + ((values.tomato) ? "yes" : "no") +
37 * "<br />Salami: " + ((values.salami) ? "yes" : "no")
48 * The form above contains two check boxes - one for Tomato, one for Salami. We configured the Tomato checkbox to be
49 * checked immediately on load, and the Salami checkbox to be unchecked. We also specified an optional text
50 * {@link #value} that will be sent when we submit the form. We can get this value using the Form's
51 * {@link Ext.form.Panel#getValues getValues} function, or have it sent as part of the data that is sent when the
54 * form.getValues(); //contains a key called 'tomato' if the Tomato field is still checked
55 * form.submit(); //will send 'tomato' in the form submission data
57 * For more information regarding forms and fields, please review [Using Forms in Sencha Touch Guide](../../../components/forms.html)
59 Ext
.define('Ext.field.Checkbox', {
60 extend
: 'Ext.field.Field',
61 alternateClassName
: 'Ext.form.Checkbox',
63 xtype
: 'checkboxfield',
71 * Fires just before the field blurs if the field value has changed.
72 * @param {Ext.field.Checkbox} this This field.
73 * @param {Boolean} newValue The new value.
74 * @param {Boolean} oldValue The original value.
79 * Fires when the checkbox is checked.
80 * @param {Ext.field.Checkbox} this This checkbox.
81 * @param {Ext.EventObject} e This event object.
86 * Fires when the checkbox is unchecked.
87 * @param {Ext.field.Checkbox} this This checkbox.
88 * @param {Ext.EventObject} e This event object.
99 * @cfg {String} value The string value to submit if the item is in a checked state.
105 * @cfg {Boolean} checked `true` if the checkbox should render initially checked.
111 * @cfg {Number} tabIndex
124 cls
: Ext
.baseCSSPrefix
+ 'input-checkbox'
128 * @cfg {Boolean} labelMaskTap
134 theme
: ['Windows', 'Blackberry', 'Blackberry103', 'Tizen'],
139 initialize: function() {
141 component
= me
.getComponent();
151 component
.doMaskTap
= Ext
.emptyFn
;
160 doInitValue: function() {
162 initialConfig
= me
.getInitialConfig();
164 // you can have a value or checked config, but checked get priority
165 if (initialConfig
.hasOwnProperty('value')) {
166 me
.originalState
= initialConfig
.value
;
169 if (initialConfig
.hasOwnProperty('checked')) {
170 me
.originalState
= initialConfig
.checked
;
173 me
.callParent(arguments
);
177 updateInputType: function(newInputType
) {
178 var component
= this.getComponent();
180 component
.setType(newInputType
);
185 updateName: function(newName
) {
186 var component
= this.getComponent();
188 component
.setName(newName
);
193 * Returns the field checked value.
194 * @return {Mixed} The field value.
196 getChecked: function() {
197 // we need to get the latest value from the {@link #input} and then update the value
198 this._checked
= this.getComponent().getChecked();
199 return this._checked
;
203 * Returns the submit value for the checkbox which can be used when submitting forms.
204 * @return {Boolean/String} value The value of {@link #value} or `true`, if {@link #checked}.
206 getSubmitValue: function() {
207 return (this.getChecked()) ? Ext
.isEmpty(this._value
) ? true : this._value
: null;
210 setChecked: function(newChecked
) {
211 this.updateChecked(newChecked
);
212 this._checked
= newChecked
;
215 updateChecked: function(newChecked
) {
216 this.getComponent().setChecked(newChecked
);
218 // only call onChange (which fires events) if the component has been initialized
219 if (this.initialized
) {
225 onMaskTap: function(component
, e
) {
227 dom
= me
.getComponent().input
.dom
;
229 if (me
.getDisabled()) {
233 //we must manually update the input dom with the new checked value
234 dom
.checked
= !dom
.checked
;
238 //return false so the mask does not disappear
243 * Fires the `check` or `uncheck` event when the checked value of this component changes.
246 onChange: function(e
) {
248 oldChecked
= me
._checked
,
249 newChecked
= me
.getChecked();
251 // only fire the event when the value changes
252 if (oldChecked
!= newChecked
) {
254 me
.fireEvent('check', me
, e
);
256 me
.fireEvent('uncheck', me
, e
);
259 me
.fireEvent('change', me
, newChecked
, oldChecked
);
265 * Method called when this {@link Ext.field.Checkbox} has been checked.
267 doChecked
: Ext
.emptyFn
,
271 * Method called when this {@link Ext.field.Checkbox} has been unchecked.
273 doUnChecked
: Ext
.emptyFn
,
276 * Returns the checked state of the checkbox.
277 * @return {Boolean} `true` if checked, `false` otherwise.
279 isChecked: function() {
280 return this.getChecked();
284 * Set the checked state of the checkbox to `true`.
285 * @return {Ext.field.Checkbox} This checkbox.
288 return this.setChecked(true);
292 * Set the checked state of the checkbox to `false`.
293 * @return {Ext.field.Checkbox} This checkbox.
295 uncheck: function() {
296 return this.setChecked(false);
299 getSameGroupFields: function() {
301 component
= me
.up('formpanel') || me
.up('fieldset'),
303 replaceLeft
= me
.qsaLeftRe
,
304 replaceRight
= me
.qsaRightRe
,
305 //handle baseCls with multiple class values
306 baseCls
= me
.getBaseCls().split(' ').join('.'),
308 elements
, element
, i
, ln
;
312 Ext
.Logger
.warn('Ext.field.Radio components must always be descendants of an Ext.form.Panel or Ext.form.FieldSet.');
314 component
= Ext
.Viewport
;
317 // This is to handle ComponentQuery's lack of handling [name=foo[bar]] properly
318 name
= name
.replace(replaceLeft
, '\\[');
319 name
= name
.replace(replaceRight
, '\\]');
321 elements
= Ext
.query('[name=' + name
+ ']', component
.element
.dom
);
322 ln
= elements
.length
;
323 for (i
= 0; i
< ln
; i
++) {
324 element
= elements
[i
];
325 element
= Ext
.fly(element
).up('.' + baseCls
);
326 if (element
&& element
.id
) {
327 components
.push(Ext
.getCmp(element
.id
));
334 * Returns an array of values from the checkboxes in the group that are checked.
337 getGroupValues: function() {
340 this.getSameGroupFields().forEach(function(field
) {
341 if (field
.getChecked()) {
342 values
.push(field
.getValue());
350 * Set the status of all matched checkboxes in the same group to checked.
351 * @param {Array} values An array of values.
352 * @return {Ext.field.Checkbox} This checkbox.
354 setGroupValues: function(values
) {
355 this.getSameGroupFields().forEach(function(field
) {
356 field
.setChecked((values
.indexOf(field
.getValue()) !== -1));
363 * Resets the status of all matched checkboxes in the same group to checked.
364 * @return {Ext.field.Checkbox} This checkbox.
366 resetGroupValues: function() {
367 this.getSameGroupFields().forEach(function(field
) {
368 field
.setChecked(field
.originalState
);
375 this.setChecked(this.originalState
);