component.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 { __extends } from "tslib";
  24. import { MDCComponent } from '@material/base/component';
  25. import { FocusTrap } from '@material/dom/focus-trap';
  26. import { closest } from '@material/dom/ponyfill';
  27. import { events, selectors } from './constants';
  28. import { MDCBannerFoundation } from './foundation';
  29. /** Vanilla implementation of banner component. */
  30. var MDCBanner = /** @class */ (function (_super) {
  31. __extends(MDCBanner, _super);
  32. function MDCBanner() {
  33. return _super !== null && _super.apply(this, arguments) || this;
  34. }
  35. MDCBanner.attachTo = function (root) {
  36. return new MDCBanner(root);
  37. };
  38. MDCBanner.prototype.initialize = function (focusTrapFactory) {
  39. var _this = this;
  40. if (focusTrapFactory === void 0) { focusTrapFactory = function (el, focusOptions) {
  41. return new FocusTrap(el, focusOptions);
  42. }; }
  43. this.contentEl = this.root.querySelector(selectors.CONTENT);
  44. this.textEl = this.root.querySelector(selectors.TEXT);
  45. this.primaryActionEl =
  46. this.root.querySelector(selectors.PRIMARY_ACTION);
  47. this.secondaryActionEl =
  48. this.root.querySelector(selectors.SECONDARY_ACTION);
  49. this.focusTrapFactory = focusTrapFactory;
  50. this.handleContentClick = function (evt) {
  51. var target = evt.target;
  52. if (closest(target, selectors.PRIMARY_ACTION)) {
  53. _this.foundation.handlePrimaryActionClick();
  54. }
  55. else if (closest(target, selectors.SECONDARY_ACTION)) {
  56. _this.foundation.handleSecondaryActionClick();
  57. }
  58. };
  59. };
  60. MDCBanner.prototype.initialSyncWithDOM = function () {
  61. this.registerContentClickHandler(this.handleContentClick);
  62. this.focusTrap = this.focusTrapFactory(this.root, { initialFocusEl: this.primaryActionEl });
  63. };
  64. MDCBanner.prototype.destroy = function () {
  65. _super.prototype.destroy.call(this);
  66. this.deregisterContentClickHandler(this.handleContentClick);
  67. };
  68. MDCBanner.prototype.layout = function () {
  69. this.foundation.layout();
  70. };
  71. /**
  72. * Opens the banner and fires events.OPENING to indicate the beginning of its
  73. * opening animation and then events.OPENED once the animation finishes.
  74. */
  75. MDCBanner.prototype.open = function () {
  76. this.foundation.open();
  77. };
  78. /**
  79. * Closes the banner and fires events.CLOSING to indicate the beginning of its
  80. * closing animation and then events.CLOSED once the animation finishes.
  81. * @param reason Why the banner was closed. Value will be passed to
  82. * events.CLOSING and events.CLOSED via the `event.detail.reason`
  83. * property. Standard values are CloseReason.PRIMARY and
  84. * CloseReason.SECONDARY, but CloseReason.UNSPECIFIED is provided for
  85. * custom handling of programmatic closing of the banner.
  86. */
  87. MDCBanner.prototype.close = function (reason) {
  88. this.foundation.close(reason);
  89. };
  90. MDCBanner.prototype.getDefaultFoundation = function () {
  91. var _this = this;
  92. // DO NOT INLINE this variable. For backward compatibility, foundations take
  93. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  94. // methods, we need a separate, strongly typed adapter variable.
  95. var adapter = {
  96. addClass: function (className) {
  97. _this.root.classList.add(className);
  98. },
  99. getContentHeight: function () {
  100. return _this.contentEl.offsetHeight;
  101. },
  102. notifyClosed: function (reason) {
  103. _this.emit(events.CLOSED, { reason: reason });
  104. },
  105. notifyClosing: function (reason) {
  106. _this.emit(events.CLOSING, { reason: reason });
  107. },
  108. notifyOpened: function () {
  109. _this.emit(events.OPENED, {});
  110. },
  111. notifyOpening: function () {
  112. _this.emit(events.OPENING, {});
  113. },
  114. notifyActionClicked: function (action) {
  115. _this.emit(events.ACTION_CLICKED, { action: action });
  116. },
  117. releaseFocus: function () {
  118. _this.focusTrap.releaseFocus();
  119. },
  120. removeClass: function (className) {
  121. _this.root.classList.remove(className);
  122. },
  123. setStyleProperty: function (propertyName, value) {
  124. _this.root.style.setProperty(propertyName, value);
  125. },
  126. trapFocus: function () {
  127. _this.focusTrap.trapFocus();
  128. },
  129. };
  130. return new MDCBannerFoundation(adapter);
  131. };
  132. Object.defineProperty(MDCBanner.prototype, "isOpen", {
  133. get: function () {
  134. return this.foundation.isOpen();
  135. },
  136. enumerable: false,
  137. configurable: true
  138. });
  139. MDCBanner.prototype.getText = function () {
  140. return this.textEl.textContent || '';
  141. };
  142. MDCBanner.prototype.setText = function (text) {
  143. this.textEl.textContent = text;
  144. };
  145. MDCBanner.prototype.getPrimaryActionText = function () {
  146. return this.primaryActionEl.textContent || '';
  147. };
  148. MDCBanner.prototype.setPrimaryActionText = function (actionButtonText) {
  149. this.primaryActionEl.textContent = actionButtonText;
  150. };
  151. /** Returns null if the banner has no secondary action. */
  152. MDCBanner.prototype.getSecondaryActionText = function () {
  153. return this.secondaryActionEl ? this.secondaryActionEl.textContent || '' :
  154. null;
  155. };
  156. MDCBanner.prototype.setSecondaryActionText = function (actionButtonText) {
  157. if (this.secondaryActionEl) {
  158. this.secondaryActionEl.textContent = actionButtonText;
  159. }
  160. };
  161. MDCBanner.prototype.registerContentClickHandler = function (handler) {
  162. this.contentEl.addEventListener('click', handler);
  163. };
  164. MDCBanner.prototype.deregisterContentClickHandler = function (handler) {
  165. this.contentEl.removeEventListener('click', handler);
  166. };
  167. return MDCBanner;
  168. }(MDCComponent));
  169. export { MDCBanner };
  170. //# sourceMappingURL=component.js.map