_ToggleButtonMixin.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. define("dijit/form/_ToggleButtonMixin", [
  2. "dojo/_base/declare", // declare
  3. "dojo/dom-attr" // domAttr.set
  4. ], function(declare, domAttr){
  5. // module:
  6. // dijit/form/_ToggleButtonMixin
  7. // summary:
  8. // A mixin to provide functionality to allow a button that can be in two states (checked or not).
  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. // checked: Boolean
  13. // Corresponds to the native HTML <input> element's attribute.
  14. // In markup, specified as "checked='checked'" or just "checked".
  15. // True if the button is depressed, or the checkbox is checked,
  16. // or the radio button is selected, etc.
  17. checked: false,
  18. // aria-pressed for toggle buttons, and aria-checked for checkboxes
  19. _aria_attr: "aria-pressed",
  20. _onClick: function(/*Event*/ evt){
  21. var original = this.checked;
  22. this._set('checked', !original); // partially set the toggled value, assuming the toggle will work, so it can be overridden in the onclick handler
  23. var ret = this.inherited(arguments); // the user could reset the value here
  24. this.set('checked', ret ? this.checked : original); // officially set the toggled or user value, or reset it back
  25. return ret;
  26. },
  27. _setCheckedAttr: function(/*Boolean*/ value, /*Boolean?*/ priorityChange){
  28. this._set("checked", value);
  29. domAttr.set(this.focusNode || this.domNode, "checked", value);
  30. (this.focusNode || this.domNode).setAttribute(this._aria_attr, value ? "true" : "false"); // aria values should be strings
  31. this._handleOnChange(value, priorityChange);
  32. },
  33. reset: function(){
  34. // summary:
  35. // Reset the widget's value to what it was at initialization time
  36. this._hasBeenBlurred = false;
  37. // set checked state to original setting
  38. this.set('checked', this.params.checked || false);
  39. }
  40. });
  41. });