foundation.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @license
  3. * Copyright 2018 Google Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. import { __assign, __extends } from "tslib";
  24. import { MDCFoundation } from '@material/base/foundation';
  25. import { cssClasses, strings } from './constants';
  26. /** MDC Icon Button Toggle Foundation */
  27. var MDCIconButtonToggleFoundation = /** @class */ (function (_super) {
  28. __extends(MDCIconButtonToggleFoundation, _super);
  29. function MDCIconButtonToggleFoundation(adapter) {
  30. var _this = _super.call(this, __assign(__assign({}, MDCIconButtonToggleFoundation.defaultAdapter), adapter)) || this;
  31. /**
  32. * Whether the icon button has an aria label that changes depending on
  33. * toggled state.
  34. */
  35. _this.hasToggledAriaLabel = false;
  36. return _this;
  37. }
  38. Object.defineProperty(MDCIconButtonToggleFoundation, "cssClasses", {
  39. get: function () {
  40. return cssClasses;
  41. },
  42. enumerable: false,
  43. configurable: true
  44. });
  45. Object.defineProperty(MDCIconButtonToggleFoundation, "strings", {
  46. get: function () {
  47. return strings;
  48. },
  49. enumerable: false,
  50. configurable: true
  51. });
  52. Object.defineProperty(MDCIconButtonToggleFoundation, "defaultAdapter", {
  53. get: function () {
  54. return {
  55. addClass: function () { return undefined; },
  56. hasClass: function () { return false; },
  57. notifyChange: function () { return undefined; },
  58. removeClass: function () { return undefined; },
  59. getAttr: function () { return null; },
  60. setAttr: function () { return undefined; },
  61. };
  62. },
  63. enumerable: false,
  64. configurable: true
  65. });
  66. MDCIconButtonToggleFoundation.prototype.init = function () {
  67. var ariaLabelOn = this.adapter.getAttr(strings.DATA_ARIA_LABEL_ON);
  68. var ariaLabelOff = this.adapter.getAttr(strings.DATA_ARIA_LABEL_OFF);
  69. if (ariaLabelOn && ariaLabelOff) {
  70. if (this.adapter.getAttr(strings.ARIA_PRESSED) !== null) {
  71. throw new Error('MDCIconButtonToggleFoundation: Button should not set ' +
  72. '`aria-pressed` if it has a toggled aria label.');
  73. }
  74. this.hasToggledAriaLabel = true;
  75. }
  76. else {
  77. this.adapter.setAttr(strings.ARIA_PRESSED, String(this.isOn()));
  78. }
  79. };
  80. MDCIconButtonToggleFoundation.prototype.handleClick = function () {
  81. this.toggle();
  82. this.adapter.notifyChange({ isOn: this.isOn() });
  83. };
  84. MDCIconButtonToggleFoundation.prototype.isOn = function () {
  85. return this.adapter.hasClass(cssClasses.ICON_BUTTON_ON);
  86. };
  87. MDCIconButtonToggleFoundation.prototype.toggle = function (isOn) {
  88. if (isOn === void 0) { isOn = !this.isOn(); }
  89. // Toggle UI based on state.
  90. if (isOn) {
  91. this.adapter.addClass(cssClasses.ICON_BUTTON_ON);
  92. }
  93. else {
  94. this.adapter.removeClass(cssClasses.ICON_BUTTON_ON);
  95. }
  96. // Toggle aria attributes based on state.
  97. if (this.hasToggledAriaLabel) {
  98. var ariaLabel = isOn ?
  99. this.adapter.getAttr(strings.DATA_ARIA_LABEL_ON) :
  100. this.adapter.getAttr(strings.DATA_ARIA_LABEL_OFF);
  101. this.adapter.setAttr(strings.ARIA_LABEL, ariaLabel || '');
  102. }
  103. else {
  104. this.adapter.setAttr(strings.ARIA_PRESSED, "" + isOn);
  105. }
  106. };
  107. return MDCIconButtonToggleFoundation;
  108. }(MDCFoundation));
  109. export { MDCIconButtonToggleFoundation };
  110. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  111. export default MDCIconButtonToggleFoundation;
  112. //# sourceMappingURL=foundation.js.map