component.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 { MDCComponent } from '@material/base/component';
  25. import { MDCRipple } from '@material/ripple/component';
  26. import { MDCRippleFoundation } from '@material/ripple/foundation';
  27. import { events } from './constants';
  28. import { MDCSegmentedButtonSegmentFoundation } from './foundation';
  29. /**
  30. * Implementation of MDCSegmentedButtonSegmentFoundation
  31. */
  32. var MDCSegmentedButtonSegment = /** @class */ (function (_super) {
  33. __extends(MDCSegmentedButtonSegment, _super);
  34. function MDCSegmentedButtonSegment() {
  35. return _super !== null && _super.apply(this, arguments) || this;
  36. }
  37. Object.defineProperty(MDCSegmentedButtonSegment.prototype, "ripple", {
  38. get: function () {
  39. return this.rippleComponent;
  40. },
  41. enumerable: false,
  42. configurable: true
  43. });
  44. MDCSegmentedButtonSegment.attachTo = function (root) {
  45. return new MDCSegmentedButtonSegment(root);
  46. };
  47. MDCSegmentedButtonSegment.prototype.initialize = function (rippleFactory) {
  48. var _this = this;
  49. if (rippleFactory === void 0) { rippleFactory = function (el, foundation) {
  50. return new MDCRipple(el, foundation);
  51. }; }
  52. var rippleAdapter = __assign(__assign({}, MDCRipple.createAdapter(this)), { computeBoundingRect: function () { return _this.foundation.getDimensions(); } });
  53. this.rippleComponent =
  54. rippleFactory(this.root, new MDCRippleFoundation(rippleAdapter));
  55. };
  56. MDCSegmentedButtonSegment.prototype.initialSyncWithDOM = function () {
  57. var _this = this;
  58. this.handleClick = function () {
  59. _this.foundation.handleClick();
  60. };
  61. this.listen(events.CLICK, this.handleClick);
  62. };
  63. MDCSegmentedButtonSegment.prototype.destroy = function () {
  64. this.ripple.destroy();
  65. this.unlisten(events.CLICK, this.handleClick);
  66. _super.prototype.destroy.call(this);
  67. };
  68. MDCSegmentedButtonSegment.prototype.getDefaultFoundation = function () {
  69. var _this = this;
  70. // DO NOT INLINE this variable. For backward compatibility, foundations take
  71. // a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
  72. // methods, we need a separate, strongly typed adapter variable.
  73. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  74. var adapter = {
  75. isSingleSelect: function () {
  76. return _this.isSingleSelect;
  77. },
  78. getAttr: function (attrName) {
  79. return _this.root.getAttribute(attrName);
  80. },
  81. setAttr: function (attrName, value) {
  82. _this.safeSetAttribute(_this.root, attrName, value);
  83. },
  84. addClass: function (className) {
  85. _this.root.classList.add(className);
  86. },
  87. removeClass: function (className) {
  88. _this.root.classList.remove(className);
  89. },
  90. hasClass: function (className) {
  91. return _this.root.classList.contains(className);
  92. },
  93. notifySelectedChange: function (selected) {
  94. _this.emit(events.SELECTED, { index: _this.index, selected: selected, segmentId: _this.getSegmentId() }, true /* shouldBubble */);
  95. },
  96. getRootBoundingClientRect: function () {
  97. return _this.root.getBoundingClientRect();
  98. }
  99. };
  100. return new MDCSegmentedButtonSegmentFoundation(adapter);
  101. };
  102. /**
  103. * Sets segment's index value
  104. *
  105. * @param index Segment's index within wrapping segmented button
  106. */
  107. MDCSegmentedButtonSegment.prototype.setIndex = function (index) {
  108. this.index = index;
  109. };
  110. /**
  111. * Sets segment's isSingleSelect value
  112. *
  113. * @param isSingleSelect True if wrapping segmented button is single select
  114. */
  115. MDCSegmentedButtonSegment.prototype.setIsSingleSelect = function (isSingleSelect) {
  116. this.isSingleSelect = isSingleSelect;
  117. };
  118. /**
  119. * @return Returns true if segment is currently selected, otherwise returns
  120. * false
  121. */
  122. MDCSegmentedButtonSegment.prototype.isSelected = function () {
  123. return this.foundation.isSelected();
  124. };
  125. /**
  126. * Sets segment to be selected
  127. */
  128. MDCSegmentedButtonSegment.prototype.setSelected = function () {
  129. this.foundation.setSelected();
  130. };
  131. /**
  132. * Sets segment to be not selected
  133. */
  134. MDCSegmentedButtonSegment.prototype.setUnselected = function () {
  135. this.foundation.setUnselected();
  136. };
  137. /**
  138. * @return Returns segment's segmentId if it was set by client
  139. */
  140. MDCSegmentedButtonSegment.prototype.getSegmentId = function () {
  141. return this.foundation.getSegmentId();
  142. };
  143. return MDCSegmentedButtonSegment;
  144. }(MDCComponent));
  145. export { MDCSegmentedButtonSegment };
  146. //# sourceMappingURL=component.js.map