component.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 { __extends, __values } from "tslib";
  24. import { MDCComponent } from '@material/base/component';
  25. import { MDCRipple } from '@material/ripple/component';
  26. import { cssClasses, strings } from './constants';
  27. import { MDCFixedTopAppBarFoundation } from './fixed/foundation';
  28. import { MDCShortTopAppBarFoundation } from './short/foundation';
  29. import { MDCTopAppBarFoundation } from './standard/foundation';
  30. /** MDC Top App Bar */
  31. var MDCTopAppBar = /** @class */ (function (_super) {
  32. __extends(MDCTopAppBar, _super);
  33. function MDCTopAppBar() {
  34. return _super !== null && _super.apply(this, arguments) || this;
  35. }
  36. MDCTopAppBar.attachTo = function (root) {
  37. return new MDCTopAppBar(root);
  38. };
  39. MDCTopAppBar.prototype.initialize = function (rippleFactory) {
  40. if (rippleFactory === void 0) { rippleFactory = function (el) { return MDCRipple.attachTo(el); }; }
  41. this.navIcon =
  42. this.root.querySelector(strings.NAVIGATION_ICON_SELECTOR);
  43. // Get all icons in the toolbar and instantiate the ripples
  44. var icons = Array.from(this.root.querySelectorAll(strings.ACTION_ITEM_SELECTOR));
  45. if (this.navIcon) {
  46. icons.push(this.navIcon);
  47. }
  48. this.iconRipples = icons.map(function (icon) {
  49. var ripple = rippleFactory(icon);
  50. ripple.unbounded = true;
  51. return ripple;
  52. });
  53. this.scrollTarget = window;
  54. };
  55. MDCTopAppBar.prototype.initialSyncWithDOM = function () {
  56. this.handleNavigationClick =
  57. this.foundation.handleNavigationClick.bind(this.foundation);
  58. this.handleWindowResize =
  59. this.foundation.handleWindowResize.bind(this.foundation);
  60. this.handleTargetScroll =
  61. this.foundation.handleTargetScroll.bind(this.foundation);
  62. this.scrollTarget.addEventListener('scroll', this.handleTargetScroll);
  63. if (this.navIcon) {
  64. this.navIcon.addEventListener('click', this.handleNavigationClick);
  65. }
  66. var isFixed = this.root.classList.contains(cssClasses.FIXED_CLASS);
  67. var isShort = this.root.classList.contains(cssClasses.SHORT_CLASS);
  68. if (!isShort && !isFixed) {
  69. window.addEventListener('resize', this.handleWindowResize);
  70. }
  71. };
  72. MDCTopAppBar.prototype.destroy = function () {
  73. var e_1, _a;
  74. try {
  75. for (var _b = __values(this.iconRipples), _c = _b.next(); !_c.done; _c = _b.next()) {
  76. var iconRipple = _c.value;
  77. iconRipple.destroy();
  78. }
  79. }
  80. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  81. finally {
  82. try {
  83. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  84. }
  85. finally { if (e_1) throw e_1.error; }
  86. }
  87. this.scrollTarget.removeEventListener('scroll', this.handleTargetScroll);
  88. if (this.navIcon) {
  89. this.navIcon.removeEventListener('click', this.handleNavigationClick);
  90. }
  91. var isFixed = this.root.classList.contains(cssClasses.FIXED_CLASS);
  92. var isShort = this.root.classList.contains(cssClasses.SHORT_CLASS);
  93. if (!isShort && !isFixed) {
  94. window.removeEventListener('resize', this.handleWindowResize);
  95. }
  96. _super.prototype.destroy.call(this);
  97. };
  98. MDCTopAppBar.prototype.setScrollTarget = function (target) {
  99. // Remove scroll handler from the previous scroll target
  100. this.scrollTarget.removeEventListener('scroll', this.handleTargetScroll);
  101. this.scrollTarget = target;
  102. // Initialize scroll handler on the new scroll target
  103. this.handleTargetScroll =
  104. this.foundation.handleTargetScroll.bind(this.foundation);
  105. this.scrollTarget.addEventListener('scroll', this.handleTargetScroll);
  106. };
  107. MDCTopAppBar.prototype.getDefaultFoundation = function () {
  108. var _this = this;
  109. // DO NOT INLINE this variable. For backward compatibility, foundations take
  110. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  111. // methods, we need a separate, strongly typed adapter variable.
  112. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  113. var adapter = {
  114. hasClass: function (className) { return _this.root.classList.contains(className); },
  115. addClass: function (className) {
  116. _this.root.classList.add(className);
  117. },
  118. removeClass: function (className) {
  119. _this.root.classList.remove(className);
  120. },
  121. setStyle: function (property, value) {
  122. _this.root.style.setProperty(property, value);
  123. },
  124. getTopAppBarHeight: function () { return _this.root.clientHeight; },
  125. notifyNavigationIconClicked: function () {
  126. _this.emit(strings.NAVIGATION_EVENT, {});
  127. },
  128. getViewportScrollY: function () {
  129. var win = _this.scrollTarget;
  130. var el = _this.scrollTarget;
  131. return win.pageYOffset !== undefined ? win.pageYOffset : el.scrollTop;
  132. },
  133. getTotalActionItems: function () {
  134. return _this.root.querySelectorAll(strings.ACTION_ITEM_SELECTOR)
  135. .length;
  136. },
  137. };
  138. // tslint:enable:object-literal-sort-keys
  139. var foundation;
  140. if (this.root.classList.contains(cssClasses.SHORT_CLASS)) {
  141. foundation = new MDCShortTopAppBarFoundation(adapter);
  142. }
  143. else if (this.root.classList.contains(cssClasses.FIXED_CLASS)) {
  144. foundation = new MDCFixedTopAppBarFoundation(adapter);
  145. }
  146. else {
  147. foundation = new MDCTopAppBarFoundation(adapter);
  148. }
  149. return foundation;
  150. };
  151. return MDCTopAppBar;
  152. }(MDCComponent));
  153. export { MDCTopAppBar };
  154. //# sourceMappingURL=component.js.map