| 1 | define([ |
| 2 | "dojo/_base/declare", // declare |
| 3 | "dojo/dom-attr" // domAttr.set |
| 4 | ], function(declare, domAttr){ |
| 5 | |
| 6 | // module: |
| 7 | // dijit/form/_ToggleButtonMixin |
| 8 | |
| 9 | return declare("dijit.form._ToggleButtonMixin", null, { |
| 10 | // summary: |
| 11 | // A mixin to provide functionality to allow a button that can be in two states (checked or not). |
| 12 | |
| 13 | // checked: Boolean |
| 14 | // Corresponds to the native HTML `<input>` element's attribute. |
| 15 | // In markup, specified as "checked='checked'" or just "checked". |
| 16 | // True if the button is depressed, or the checkbox is checked, |
| 17 | // or the radio button is selected, etc. |
| 18 | checked: false, |
| 19 | |
| 20 | // aria-pressed for toggle buttons, and aria-checked for checkboxes |
| 21 | _aria_attr: "aria-pressed", |
| 22 | |
| 23 | _onClick: function(/*Event*/ evt){ |
| 24 | var original = this.checked; |
| 25 | this._set('checked', !original); // partially set the toggled value, assuming the toggle will work, so it can be overridden in the onclick handler |
| 26 | var ret = this.inherited(arguments); // the user could reset the value here |
| 27 | this.set('checked', ret ? this.checked : original); // officially set the toggled or user value, or reset it back |
| 28 | return ret; |
| 29 | }, |
| 30 | |
| 31 | _setCheckedAttr: function(/*Boolean*/ value, /*Boolean?*/ priorityChange){ |
| 32 | this._set("checked", value); |
| 33 | var node = this.focusNode || this.domNode; |
| 34 | if(this._created){ // IE is not ready to handle checked attribute (affects tab order) |
| 35 | // needlessly setting "checked" upsets IE's tab order |
| 36 | if(domAttr.get(node, "checked") != !!value){ |
| 37 | domAttr.set(node, "checked", !!value); // "mixed" -> true |
| 38 | } |
| 39 | } |
| 40 | node.setAttribute(this._aria_attr, String(value)); // aria values should be strings |
| 41 | this._handleOnChange(value, priorityChange); |
| 42 | }, |
| 43 | |
| 44 | postCreate: function(){ // use postCreate instead of startup so users forgetting to call startup are OK |
| 45 | this.inherited(arguments); |
| 46 | var node = this.focusNode || this.domNode; |
| 47 | if(this.checked){ |
| 48 | // need this here instead of on the template so IE8 tab order works |
| 49 | node.setAttribute('checked', 'checked'); |
| 50 | } |
| 51 | |
| 52 | // Update our reset value if it hasn't yet been set (because this.set() |
| 53 | // is only called when there *is* a value) |
| 54 | if(this._resetValue === undefined){ |
| 55 | this._lastValueReported = this._resetValue = this.checked; |
| 56 | } |
| 57 | }, |
| 58 | |
| 59 | reset: function(){ |
| 60 | // summary: |
| 61 | // Reset the widget's value to what it was at initialization time |
| 62 | |
| 63 | this._hasBeenBlurred = false; |
| 64 | |
| 65 | // set checked state to original setting |
| 66 | this.set('checked', this.params.checked || false); |
| 67 | } |
| 68 | }); |
| 69 | }); |