foundation.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, numbers, strings } from './constants';
  26. var OPENING = cssClasses.OPENING, OPEN = cssClasses.OPEN, CLOSING = cssClasses.CLOSING;
  27. var REASON_ACTION = strings.REASON_ACTION, REASON_DISMISS = strings.REASON_DISMISS;
  28. /** MDC Snackbar Foundation */
  29. var MDCSnackbarFoundation = /** @class */ (function (_super) {
  30. __extends(MDCSnackbarFoundation, _super);
  31. function MDCSnackbarFoundation(adapter) {
  32. var _this = _super.call(this, __assign(__assign({}, MDCSnackbarFoundation.defaultAdapter), adapter)) || this;
  33. _this.opened = false;
  34. _this.animationFrame = 0;
  35. _this.animationTimer = 0;
  36. _this.autoDismissTimer = 0;
  37. _this.autoDismissTimeoutMs = numbers.DEFAULT_AUTO_DISMISS_TIMEOUT_MS;
  38. _this.closeOnEscape = true;
  39. return _this;
  40. }
  41. Object.defineProperty(MDCSnackbarFoundation, "cssClasses", {
  42. get: function () {
  43. return cssClasses;
  44. },
  45. enumerable: false,
  46. configurable: true
  47. });
  48. Object.defineProperty(MDCSnackbarFoundation, "strings", {
  49. get: function () {
  50. return strings;
  51. },
  52. enumerable: false,
  53. configurable: true
  54. });
  55. Object.defineProperty(MDCSnackbarFoundation, "numbers", {
  56. get: function () {
  57. return numbers;
  58. },
  59. enumerable: false,
  60. configurable: true
  61. });
  62. Object.defineProperty(MDCSnackbarFoundation, "defaultAdapter", {
  63. get: function () {
  64. return {
  65. addClass: function () { return undefined; },
  66. announce: function () { return undefined; },
  67. notifyClosed: function () { return undefined; },
  68. notifyClosing: function () { return undefined; },
  69. notifyOpened: function () { return undefined; },
  70. notifyOpening: function () { return undefined; },
  71. removeClass: function () { return undefined; },
  72. };
  73. },
  74. enumerable: false,
  75. configurable: true
  76. });
  77. MDCSnackbarFoundation.prototype.destroy = function () {
  78. this.clearAutoDismissTimer();
  79. cancelAnimationFrame(this.animationFrame);
  80. this.animationFrame = 0;
  81. clearTimeout(this.animationTimer);
  82. this.animationTimer = 0;
  83. this.adapter.removeClass(OPENING);
  84. this.adapter.removeClass(OPEN);
  85. this.adapter.removeClass(CLOSING);
  86. };
  87. MDCSnackbarFoundation.prototype.open = function () {
  88. var _this = this;
  89. this.clearAutoDismissTimer();
  90. this.opened = true;
  91. this.adapter.notifyOpening();
  92. this.adapter.removeClass(CLOSING);
  93. this.adapter.addClass(OPENING);
  94. this.adapter.announce();
  95. // Wait a frame once display is no longer "none", to establish basis for
  96. // animation
  97. this.runNextAnimationFrame(function () {
  98. _this.adapter.addClass(OPEN);
  99. _this.animationTimer = setTimeout(function () {
  100. var timeoutMs = _this.getTimeoutMs();
  101. _this.handleAnimationTimerEnd();
  102. _this.adapter.notifyOpened();
  103. if (timeoutMs !== numbers.INDETERMINATE) {
  104. _this.autoDismissTimer = setTimeout(function () {
  105. _this.close(REASON_DISMISS);
  106. }, timeoutMs);
  107. }
  108. }, numbers.SNACKBAR_ANIMATION_OPEN_TIME_MS);
  109. });
  110. };
  111. /**
  112. * @param reason Why the snackbar was closed. Value will be passed to
  113. * CLOSING_EVENT and CLOSED_EVENT via the `event.detail.reason` property.
  114. * Standard values are REASON_ACTION and REASON_DISMISS, but custom
  115. * client-specific values may also be used if desired.
  116. */
  117. MDCSnackbarFoundation.prototype.close = function (reason) {
  118. var _this = this;
  119. if (reason === void 0) { reason = ''; }
  120. if (!this.opened) {
  121. // Avoid redundant close calls (and events), e.g. repeated interactions as
  122. // the snackbar is animating closed
  123. return;
  124. }
  125. cancelAnimationFrame(this.animationFrame);
  126. this.animationFrame = 0;
  127. this.clearAutoDismissTimer();
  128. this.opened = false;
  129. this.adapter.notifyClosing(reason);
  130. this.adapter.addClass(cssClasses.CLOSING);
  131. this.adapter.removeClass(cssClasses.OPEN);
  132. this.adapter.removeClass(cssClasses.OPENING);
  133. clearTimeout(this.animationTimer);
  134. this.animationTimer = setTimeout(function () {
  135. _this.handleAnimationTimerEnd();
  136. _this.adapter.notifyClosed(reason);
  137. }, numbers.SNACKBAR_ANIMATION_CLOSE_TIME_MS);
  138. };
  139. MDCSnackbarFoundation.prototype.isOpen = function () {
  140. return this.opened;
  141. };
  142. MDCSnackbarFoundation.prototype.getTimeoutMs = function () {
  143. return this.autoDismissTimeoutMs;
  144. };
  145. MDCSnackbarFoundation.prototype.setTimeoutMs = function (timeoutMs) {
  146. // Use shorter variable names to make the code more readable
  147. var minValue = numbers.MIN_AUTO_DISMISS_TIMEOUT_MS;
  148. var maxValue = numbers.MAX_AUTO_DISMISS_TIMEOUT_MS;
  149. var indeterminateValue = numbers.INDETERMINATE;
  150. if (timeoutMs === numbers.INDETERMINATE ||
  151. (timeoutMs <= maxValue && timeoutMs >= minValue)) {
  152. this.autoDismissTimeoutMs = timeoutMs;
  153. }
  154. else {
  155. throw new Error("\n timeoutMs must be an integer in the range " + minValue + "\u2013" + maxValue + "\n (or " + indeterminateValue + " to disable), but got '" + timeoutMs + "'");
  156. }
  157. };
  158. MDCSnackbarFoundation.prototype.getCloseOnEscape = function () {
  159. return this.closeOnEscape;
  160. };
  161. MDCSnackbarFoundation.prototype.setCloseOnEscape = function (closeOnEscape) {
  162. this.closeOnEscape = closeOnEscape;
  163. };
  164. MDCSnackbarFoundation.prototype.handleKeyDown = function (evt) {
  165. var isEscapeKey = evt.key === 'Escape' || evt.keyCode === 27;
  166. if (isEscapeKey && this.getCloseOnEscape()) {
  167. this.close(REASON_DISMISS);
  168. }
  169. };
  170. MDCSnackbarFoundation.prototype.handleActionButtonClick = function (_evt) {
  171. this.close(REASON_ACTION);
  172. };
  173. MDCSnackbarFoundation.prototype.handleActionIconClick = function (_evt) {
  174. this.close(REASON_DISMISS);
  175. };
  176. MDCSnackbarFoundation.prototype.clearAutoDismissTimer = function () {
  177. clearTimeout(this.autoDismissTimer);
  178. this.autoDismissTimer = 0;
  179. };
  180. MDCSnackbarFoundation.prototype.handleAnimationTimerEnd = function () {
  181. this.animationTimer = 0;
  182. this.adapter.removeClass(cssClasses.OPENING);
  183. this.adapter.removeClass(cssClasses.CLOSING);
  184. };
  185. /**
  186. * Runs the given logic on the next animation frame, using setTimeout to
  187. * factor in Firefox reflow behavior.
  188. */
  189. MDCSnackbarFoundation.prototype.runNextAnimationFrame = function (callback) {
  190. var _this = this;
  191. cancelAnimationFrame(this.animationFrame);
  192. this.animationFrame = requestAnimationFrame(function () {
  193. _this.animationFrame = 0;
  194. clearTimeout(_this.animationTimer);
  195. _this.animationTimer = setTimeout(callback, 0);
  196. });
  197. };
  198. return MDCSnackbarFoundation;
  199. }(MDCFoundation));
  200. export { MDCSnackbarFoundation };
  201. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  202. export default MDCSnackbarFoundation;
  203. //# sourceMappingURL=foundation.js.map