component.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, __makeTemplateObject } from "tslib";
  24. import { MDCComponent } from '@material/base/component';
  25. import { closest } from '@material/dom/ponyfill';
  26. import { MDCRipple } from '@material/ripple/component';
  27. import { MDCRippleFoundation } from '@material/ripple/foundation';
  28. import { safeAttrPrefix } from 'safevalues';
  29. import { safeElement } from 'safevalues/dom';
  30. import { computePrimaryActionRippleClientRect, GRAPHIC_SELECTED_WIDTH_STYLE_PROP } from './component-ripple';
  31. import { MDCChipActionCssClasses } from './constants';
  32. import { MDCChipPrimaryActionFoundation } from './primary-foundation';
  33. import { MDCChipTrailingActionFoundation } from './trailing-foundation';
  34. var ALLOWED_ATTR_PREFIXES = [
  35. safeAttrPrefix(templateObject_1 || (templateObject_1 = __makeTemplateObject(["aria-"], ["aria-"]))),
  36. safeAttrPrefix(templateObject_2 || (templateObject_2 = __makeTemplateObject(["data-"], ["data-"]))),
  37. safeAttrPrefix(templateObject_3 || (templateObject_3 = __makeTemplateObject(["disabled"], ["disabled"]))),
  38. safeAttrPrefix(templateObject_4 || (templateObject_4 = __makeTemplateObject(["role"], ["role"]))),
  39. safeAttrPrefix(templateObject_5 || (templateObject_5 = __makeTemplateObject(["tabindex"], ["tabindex"]))),
  40. ];
  41. /**
  42. * MDCChipAction provides component encapsulation of the different foundation
  43. * implementations.
  44. */
  45. var MDCChipAction = /** @class */ (function (_super) {
  46. __extends(MDCChipAction, _super);
  47. function MDCChipAction() {
  48. var _this = _super !== null && _super.apply(this, arguments) || this;
  49. _this.rootHTML = _this.root;
  50. return _this;
  51. }
  52. MDCChipAction.attachTo = function (root) {
  53. return new MDCChipAction(root);
  54. };
  55. Object.defineProperty(MDCChipAction.prototype, "ripple", {
  56. get: function () {
  57. return this.rippleInstance;
  58. },
  59. enumerable: false,
  60. configurable: true
  61. });
  62. MDCChipAction.prototype.initialize = function (rippleFactory) {
  63. var _this = this;
  64. if (rippleFactory === void 0) { rippleFactory = function (el, foundation) {
  65. return new MDCRipple(el, foundation);
  66. }; }
  67. var rippleAdapter = __assign(__assign({}, MDCRipple.createAdapter(this)), { computeBoundingRect: function () { return _this.computeRippleClientRect(); } });
  68. this.rippleInstance =
  69. rippleFactory(this.root, new MDCRippleFoundation(rippleAdapter));
  70. };
  71. MDCChipAction.prototype.initialSyncWithDOM = function () {
  72. var _this = this;
  73. this.handleClick = function () {
  74. _this.foundation.handleClick();
  75. };
  76. this.handleKeydown = function (event) {
  77. _this.foundation.handleKeydown(event);
  78. };
  79. this.listen('click', this.handleClick);
  80. this.listen('keydown', this.handleKeydown);
  81. };
  82. MDCChipAction.prototype.destroy = function () {
  83. this.ripple.destroy();
  84. this.unlisten('click', this.handleClick);
  85. this.unlisten('keydown', this.handleKeydown);
  86. _super.prototype.destroy.call(this);
  87. };
  88. MDCChipAction.prototype.getDefaultFoundation = function () {
  89. var _this = this;
  90. // DO NOT INLINE this variable. For backward compatibility, foundations take
  91. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  92. // methods, we need a separate, strongly typed adapter variable.
  93. var adapter = {
  94. emitEvent: function (eventName, eventDetail) {
  95. _this.emit(eventName, eventDetail, true /* shouldBubble */);
  96. },
  97. focus: function () {
  98. _this.rootHTML.focus();
  99. },
  100. getAttribute: function (attrName) { return _this.root.getAttribute(attrName); },
  101. getElementID: function () { return _this.root.id; },
  102. removeAttribute: function (name) {
  103. _this.root.removeAttribute(name);
  104. },
  105. setAttribute: function (name, value) {
  106. safeElement.setPrefixedAttribute(ALLOWED_ATTR_PREFIXES, _this.root, name, value);
  107. },
  108. };
  109. if (this.root.classList.contains(MDCChipActionCssClasses.TRAILING_ACTION)) {
  110. return new MDCChipTrailingActionFoundation(adapter);
  111. }
  112. // Default to the primary foundation
  113. return new MDCChipPrimaryActionFoundation(adapter);
  114. };
  115. MDCChipAction.prototype.setDisabled = function (isDisabled) {
  116. this.foundation.setDisabled(isDisabled);
  117. };
  118. MDCChipAction.prototype.isDisabled = function () {
  119. return this.foundation.isDisabled();
  120. };
  121. MDCChipAction.prototype.setFocus = function (behavior) {
  122. this.foundation.setFocus(behavior);
  123. };
  124. MDCChipAction.prototype.isFocusable = function () {
  125. return this.foundation.isFocusable();
  126. };
  127. MDCChipAction.prototype.setSelected = function (isSelected) {
  128. this.foundation.setSelected(isSelected);
  129. };
  130. MDCChipAction.prototype.isSelected = function () {
  131. return this.foundation.isSelected();
  132. };
  133. MDCChipAction.prototype.isSelectable = function () {
  134. return this.foundation.isSelectable();
  135. };
  136. MDCChipAction.prototype.actionType = function () {
  137. return this.foundation.actionType();
  138. };
  139. MDCChipAction.prototype.computeRippleClientRect = function () {
  140. if (this.root.classList.contains(MDCChipActionCssClasses.PRIMARY_ACTION)) {
  141. var chipRoot = closest(this.root, "." + MDCChipActionCssClasses.CHIP_ROOT);
  142. // Return the root client rect since it's better than nothing
  143. if (!chipRoot)
  144. return this.root.getBoundingClientRect();
  145. var graphicWidth = window.getComputedStyle(chipRoot).getPropertyValue(GRAPHIC_SELECTED_WIDTH_STYLE_PROP);
  146. return computePrimaryActionRippleClientRect(chipRoot.getBoundingClientRect(), graphicWidth);
  147. }
  148. return this.root.getBoundingClientRect();
  149. };
  150. return MDCChipAction;
  151. }(MDCComponent));
  152. export { MDCChipAction };
  153. var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5;
  154. //# sourceMappingURL=component.js.map