component.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 } from "tslib";
  24. import { MDCComponent } from '@material/base/component';
  25. import { MDCRipple } from '@material/ripple/component';
  26. import { MDCRippleFoundation } from '@material/ripple/foundation';
  27. import { MDCTabIndicator } from '@material/tab-indicator/component';
  28. import { MDCTabFoundation } from './foundation';
  29. /** MDC Tab */
  30. var MDCTab = /** @class */ (function (_super) {
  31. __extends(MDCTab, _super);
  32. function MDCTab() {
  33. return _super !== null && _super.apply(this, arguments) || this;
  34. }
  35. MDCTab.attachTo = function (root) {
  36. return new MDCTab(root);
  37. };
  38. MDCTab.prototype.initialize = function (rippleFactory, tabIndicatorFactory) {
  39. if (rippleFactory === void 0) { rippleFactory = function (el, foundation) { return new MDCRipple(el, foundation); }; }
  40. if (tabIndicatorFactory === void 0) { tabIndicatorFactory = function (el) { return new MDCTabIndicator(el); }; }
  41. this.id = this.root.id;
  42. var rippleFoundation = new MDCRippleFoundation(MDCRipple.createAdapter(this));
  43. this.ripple = rippleFactory(this.root, rippleFoundation);
  44. var tabIndicatorElement = this.root.querySelector(MDCTabFoundation.strings.TAB_INDICATOR_SELECTOR);
  45. this.tabIndicator = tabIndicatorFactory(tabIndicatorElement);
  46. this.content = this.root.querySelector(MDCTabFoundation.strings.CONTENT_SELECTOR);
  47. };
  48. MDCTab.prototype.initialSyncWithDOM = function () {
  49. var _this = this;
  50. this.handleClick = function () {
  51. _this.foundation.handleClick();
  52. };
  53. this.listen('click', this.handleClick);
  54. };
  55. MDCTab.prototype.destroy = function () {
  56. this.unlisten('click', this.handleClick);
  57. this.ripple.destroy();
  58. _super.prototype.destroy.call(this);
  59. };
  60. MDCTab.prototype.getDefaultFoundation = function () {
  61. var _this = this;
  62. // DO NOT INLINE this variable. For backward compatibility, foundations take
  63. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  64. // methods, we need a separate, strongly typed adapter variable.
  65. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  66. var adapter = {
  67. setAttr: function (attr, value) {
  68. _this.safeSetAttribute(_this.root, attr, value);
  69. },
  70. addClass: function (className) {
  71. _this.root.classList.add(className);
  72. },
  73. removeClass: function (className) {
  74. _this.root.classList.remove(className);
  75. },
  76. hasClass: function (className) { return _this.root.classList.contains(className); },
  77. activateIndicator: function (previousIndicatorClientRect) {
  78. _this.tabIndicator.activate(previousIndicatorClientRect);
  79. },
  80. deactivateIndicator: function () {
  81. _this.tabIndicator.deactivate();
  82. },
  83. notifyInteracted: function () {
  84. _this.emit(MDCTabFoundation.strings.INTERACTED_EVENT, { tabId: _this.id }, true /* bubble */);
  85. },
  86. getOffsetLeft: function () { return _this.root.offsetLeft; },
  87. getOffsetWidth: function () { return _this.root.offsetWidth; },
  88. getContentOffsetLeft: function () { return _this.content.offsetLeft; },
  89. getContentOffsetWidth: function () { return _this.content.offsetWidth; },
  90. focus: function () {
  91. _this.root.focus();
  92. },
  93. isFocused: function () { return _this.root === document.activeElement; },
  94. };
  95. // tslint:enable:object-literal-sort-keys
  96. return new MDCTabFoundation(adapter);
  97. };
  98. Object.defineProperty(MDCTab.prototype, "active", {
  99. /**
  100. * Getter for the active state of the tab
  101. */
  102. get: function () {
  103. return this.foundation.isActive();
  104. },
  105. enumerable: false,
  106. configurable: true
  107. });
  108. Object.defineProperty(MDCTab.prototype, "focusOnActivate", {
  109. set: function (focusOnActivate) {
  110. this.foundation.setFocusOnActivate(focusOnActivate);
  111. },
  112. enumerable: false,
  113. configurable: true
  114. });
  115. /**
  116. * Activates the tab
  117. */
  118. MDCTab.prototype.activate = function (computeIndicatorClientRect) {
  119. this.foundation.activate(computeIndicatorClientRect);
  120. };
  121. /**
  122. * Deactivates the tab
  123. */
  124. MDCTab.prototype.deactivate = function () {
  125. this.foundation.deactivate();
  126. };
  127. /**
  128. * Returns the indicator's client rect
  129. */
  130. MDCTab.prototype.computeIndicatorClientRect = function () {
  131. return this.tabIndicator.computeContentClientRect();
  132. };
  133. MDCTab.prototype.computeDimensions = function () {
  134. return this.foundation.computeDimensions();
  135. };
  136. /**
  137. * Focuses the tab
  138. */
  139. MDCTab.prototype.focus = function () {
  140. this.root.focus();
  141. };
  142. return MDCTab;
  143. }(MDCComponent));
  144. export { MDCTab };
  145. //# sourceMappingURL=component.js.map