component.js 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @license
  3. * Copyright 2021 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 { MDCComponent } from '@material/base/component';
  25. import { MDCRipple } from '@material/ripple/component';
  26. import { MDCRippleFoundation } from '@material/ripple/foundation';
  27. import { Selectors } from './constants';
  28. import { MDCSwitchRenderFoundation } from './foundation';
  29. /**
  30. * `MDCSwitch` provides a component implementation of a Material Design switch.
  31. */
  32. var MDCSwitch = /** @class */ (function (_super) {
  33. __extends(MDCSwitch, _super);
  34. function MDCSwitch(root, foundation) {
  35. var _this = _super.call(this, root, foundation) || this;
  36. _this.root = root;
  37. return _this;
  38. }
  39. /**
  40. * Creates a new `MDCSwitch` and attaches it to the given root element.
  41. * @param root The root to attach to.
  42. * @return the new component instance.
  43. */
  44. MDCSwitch.attachTo = function (root) {
  45. return new MDCSwitch(root);
  46. };
  47. MDCSwitch.prototype.initialize = function () {
  48. this.ripple = new MDCRipple(this.root, this.createRippleFoundation());
  49. };
  50. MDCSwitch.prototype.initialSyncWithDOM = function () {
  51. var rippleElement = this.root.querySelector(Selectors.RIPPLE);
  52. if (!rippleElement) {
  53. throw new Error("Switch " + Selectors.RIPPLE + " element is required.");
  54. }
  55. this.rippleElement = rippleElement;
  56. this.root.addEventListener('click', this.foundation.handleClick);
  57. this.foundation.initFromDOM();
  58. };
  59. MDCSwitch.prototype.destroy = function () {
  60. _super.prototype.destroy.call(this);
  61. this.ripple.destroy();
  62. this.root.removeEventListener('click', this.foundation.handleClick);
  63. };
  64. MDCSwitch.prototype.getDefaultFoundation = function () {
  65. return new MDCSwitchRenderFoundation(this.createAdapter());
  66. };
  67. MDCSwitch.prototype.createAdapter = function () {
  68. var _this = this;
  69. return {
  70. addClass: function (className) {
  71. _this.root.classList.add(className);
  72. },
  73. hasClass: function (className) { return _this.root.classList.contains(className); },
  74. isDisabled: function () { return _this.root.disabled; },
  75. removeClass: function (className) {
  76. _this.root.classList.remove(className);
  77. },
  78. setAriaChecked: function (ariaChecked) {
  79. _this.root.setAttribute('aria-checked', ariaChecked);
  80. },
  81. setDisabled: function (disabled) {
  82. _this.root.disabled = disabled;
  83. },
  84. state: this,
  85. };
  86. };
  87. MDCSwitch.prototype.createRippleFoundation = function () {
  88. return new MDCRippleFoundation(this.createRippleAdapter());
  89. };
  90. MDCSwitch.prototype.createRippleAdapter = function () {
  91. var _this = this;
  92. return __assign(__assign({}, MDCRipple.createAdapter(this)), { computeBoundingRect: function () { return _this.rippleElement.getBoundingClientRect(); }, isUnbounded: function () { return true; } });
  93. };
  94. return MDCSwitch;
  95. }(MDCComponent));
  96. export { MDCSwitch };
  97. //# sourceMappingURL=component.js.map