component.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @license
  3. * Copyright 2016 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 { __extends } from "tslib";
  24. import { MDCComponent } from '@material/base/component';
  25. import { applyPassive } from '@material/dom/events';
  26. import { matches } from '@material/dom/ponyfill';
  27. import { MDCRippleFoundation } from './foundation';
  28. import * as util from './util';
  29. /** MDC Ripple */
  30. var MDCRipple = /** @class */ (function (_super) {
  31. __extends(MDCRipple, _super);
  32. function MDCRipple() {
  33. var _this = _super !== null && _super.apply(this, arguments) || this;
  34. _this.disabled = false;
  35. return _this;
  36. }
  37. MDCRipple.attachTo = function (root, opts) {
  38. if (opts === void 0) { opts = {
  39. isUnbounded: undefined
  40. }; }
  41. var ripple = new MDCRipple(root);
  42. // Only override unbounded behavior if option is explicitly specified
  43. if (opts.isUnbounded !== undefined) {
  44. ripple.unbounded = opts.isUnbounded;
  45. }
  46. return ripple;
  47. };
  48. MDCRipple.createAdapter = function (instance) {
  49. return {
  50. addClass: function (className) {
  51. instance.root.classList.add(className);
  52. },
  53. browserSupportsCssVars: function () { return util.supportsCssVariables(window); },
  54. computeBoundingRect: function () { return instance.root.getBoundingClientRect(); },
  55. containsEventTarget: function (target) { return instance.root.contains(target); },
  56. deregisterDocumentInteractionHandler: function (evtType, handler) {
  57. document.documentElement.removeEventListener(evtType, handler, applyPassive());
  58. },
  59. deregisterInteractionHandler: function (evtType, handler) {
  60. instance.root.removeEventListener(evtType, handler, applyPassive());
  61. },
  62. deregisterResizeHandler: function (handler) {
  63. window.removeEventListener('resize', handler);
  64. },
  65. getWindowPageOffset: function () {
  66. return ({ x: window.pageXOffset, y: window.pageYOffset });
  67. },
  68. isSurfaceActive: function () { return matches(instance.root, ':active'); },
  69. isSurfaceDisabled: function () { return Boolean(instance.disabled); },
  70. isUnbounded: function () { return Boolean(instance.unbounded); },
  71. registerDocumentInteractionHandler: function (evtType, handler) {
  72. document.documentElement.addEventListener(evtType, handler, applyPassive());
  73. },
  74. registerInteractionHandler: function (evtType, handler) {
  75. instance.root.addEventListener(evtType, handler, applyPassive());
  76. },
  77. registerResizeHandler: function (handler) {
  78. window.addEventListener('resize', handler);
  79. },
  80. removeClass: function (className) {
  81. instance.root.classList.remove(className);
  82. },
  83. updateCssVariable: function (varName, value) {
  84. instance.root.style.setProperty(varName, value);
  85. },
  86. };
  87. };
  88. Object.defineProperty(MDCRipple.prototype, "unbounded", {
  89. get: function () {
  90. return Boolean(this.isUnbounded);
  91. },
  92. set: function (unbounded) {
  93. this.isUnbounded = Boolean(unbounded);
  94. this.setUnbounded();
  95. },
  96. enumerable: false,
  97. configurable: true
  98. });
  99. MDCRipple.prototype.activate = function () {
  100. this.foundation.activate();
  101. };
  102. MDCRipple.prototype.deactivate = function () {
  103. this.foundation.deactivate();
  104. };
  105. MDCRipple.prototype.layout = function () {
  106. this.foundation.layout();
  107. };
  108. MDCRipple.prototype.getDefaultFoundation = function () {
  109. return new MDCRippleFoundation(MDCRipple.createAdapter(this));
  110. };
  111. MDCRipple.prototype.initialSyncWithDOM = function () {
  112. var root = this.root;
  113. this.isUnbounded = 'mdcRippleIsUnbounded' in root.dataset;
  114. };
  115. /**
  116. * Closure Compiler throws an access control error when directly accessing a
  117. * protected or private property inside a getter/setter, like unbounded above.
  118. * By accessing the protected property inside a method, we solve that problem.
  119. * That's why this function exists.
  120. */
  121. MDCRipple.prototype.setUnbounded = function () {
  122. this.foundation.setUnbounded(Boolean(this.isUnbounded));
  123. };
  124. return MDCRipple;
  125. }(MDCComponent));
  126. export { MDCRipple };
  127. //# sourceMappingURL=component.js.map