foundation.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @license
  3. * Copyright 2020 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 { Action, CloseReason, cssClasses, numbers } from './constants';
  26. var OPENING = cssClasses.OPENING, OPEN = cssClasses.OPEN, CLOSING = cssClasses.CLOSING;
  27. /**
  28. * Foundation class for banner. Responsibilities include opening and closing the
  29. * banner.
  30. */
  31. var MDCBannerFoundation = /** @class */ (function (_super) {
  32. __extends(MDCBannerFoundation, _super);
  33. function MDCBannerFoundation(adapter) {
  34. var _this = _super.call(this, __assign(__assign({}, MDCBannerFoundation.defaultAdapter), adapter)) || this;
  35. _this.isOpened = false;
  36. // Request id for open animation, used to cancel the refresh callback
  37. // request on close() and destroy().
  38. _this.animationFrame = 0;
  39. // Timer id for close and open animation, used to cancel the timer on
  40. // close() and destroy().
  41. _this.animationTimer = 0;
  42. return _this;
  43. }
  44. Object.defineProperty(MDCBannerFoundation, "defaultAdapter", {
  45. get: function () {
  46. return {
  47. addClass: function () { return undefined; },
  48. getContentHeight: function () { return 0; },
  49. notifyClosed: function () { return undefined; },
  50. notifyClosing: function () { return undefined; },
  51. notifyOpened: function () { return undefined; },
  52. notifyOpening: function () { return undefined; },
  53. notifyActionClicked: function () { return undefined; },
  54. releaseFocus: function () { return undefined; },
  55. removeClass: function () { return undefined; },
  56. setStyleProperty: function () { return undefined; },
  57. trapFocus: function () { return undefined; },
  58. };
  59. },
  60. enumerable: false,
  61. configurable: true
  62. });
  63. MDCBannerFoundation.prototype.destroy = function () {
  64. cancelAnimationFrame(this.animationFrame);
  65. this.animationFrame = 0;
  66. clearTimeout(this.animationTimer);
  67. this.animationTimer = 0;
  68. };
  69. MDCBannerFoundation.prototype.open = function () {
  70. var _this = this;
  71. this.isOpened = true;
  72. this.adapter.removeClass(CLOSING);
  73. this.adapter.addClass(OPENING);
  74. this.adapter.notifyOpening();
  75. var contentHeight = this.adapter.getContentHeight();
  76. this.animationFrame = requestAnimationFrame(function () {
  77. _this.adapter.addClass(OPEN);
  78. _this.adapter.setStyleProperty('height', contentHeight + "px");
  79. _this.animationTimer = setTimeout(function () {
  80. _this.handleAnimationTimerEnd();
  81. _this.adapter.trapFocus();
  82. _this.adapter.notifyOpened();
  83. }, numbers.BANNER_ANIMATION_OPEN_TIME_MS);
  84. });
  85. };
  86. /**
  87. * @param reason Why the banner was closed. Value will be passed to
  88. * events.CLOSING and events.CLOSED via the `event.detail.reason`
  89. * property. Standard values are CloseReason.PRIMARY and
  90. * CloseReason.SECONDARY, but CloseReason.UNSPECIFIED is provided for
  91. * custom handling of programmatic closing of the banner.
  92. */
  93. MDCBannerFoundation.prototype.close = function (reason) {
  94. var _this = this;
  95. if (!this.isOpened) {
  96. // Avoid redundant close calls (and events), e.g. repeated interactions as
  97. // the banner is animating closed
  98. return;
  99. }
  100. cancelAnimationFrame(this.animationFrame);
  101. this.animationFrame = 0;
  102. this.isOpened = false;
  103. this.adapter.notifyClosing(reason);
  104. this.adapter.addClass(CLOSING);
  105. this.adapter.setStyleProperty('height', '0');
  106. this.adapter.removeClass(OPEN);
  107. this.adapter.removeClass(OPENING);
  108. clearTimeout(this.animationTimer);
  109. this.animationTimer = setTimeout(function () {
  110. _this.adapter.releaseFocus();
  111. _this.handleAnimationTimerEnd();
  112. _this.adapter.notifyClosed(reason);
  113. }, numbers.BANNER_ANIMATION_CLOSE_TIME_MS);
  114. };
  115. MDCBannerFoundation.prototype.isOpen = function () {
  116. return this.isOpened;
  117. };
  118. MDCBannerFoundation.prototype.handlePrimaryActionClick = function (disableAutoClose) {
  119. if (disableAutoClose === void 0) { disableAutoClose = false; }
  120. if (disableAutoClose) {
  121. this.adapter.notifyActionClicked(Action.PRIMARY);
  122. }
  123. else {
  124. this.close(CloseReason.PRIMARY);
  125. }
  126. };
  127. MDCBannerFoundation.prototype.handleSecondaryActionClick = function (disableAutoClose) {
  128. if (disableAutoClose === void 0) { disableAutoClose = false; }
  129. if (disableAutoClose) {
  130. this.adapter.notifyActionClicked(Action.SECONDARY);
  131. }
  132. else {
  133. this.close(CloseReason.SECONDARY);
  134. }
  135. };
  136. MDCBannerFoundation.prototype.layout = function () {
  137. var contentHeight = this.adapter.getContentHeight();
  138. this.adapter.setStyleProperty('height', contentHeight + "px");
  139. };
  140. MDCBannerFoundation.prototype.handleAnimationTimerEnd = function () {
  141. this.animationTimer = 0;
  142. this.adapter.removeClass(OPENING);
  143. this.adapter.removeClass(CLOSING);
  144. };
  145. return MDCBannerFoundation;
  146. }(MDCFoundation));
  147. export { MDCBannerFoundation };
  148. //# sourceMappingURL=foundation.js.map