foundation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 { MDCFoundation } from '@material/base/foundation';
  25. import { attributes, booleans, cssClasses } from './constants';
  26. var emptyClientRect = {
  27. bottom: 0,
  28. height: 0,
  29. left: 0,
  30. right: 0,
  31. top: 0,
  32. width: 0,
  33. };
  34. /** MDC Segmented Button Segment Foundation */
  35. var MDCSegmentedButtonSegmentFoundation = /** @class */ (function (_super) {
  36. __extends(MDCSegmentedButtonSegmentFoundation, _super);
  37. function MDCSegmentedButtonSegmentFoundation(adapter) {
  38. return _super.call(this, __assign(__assign({}, MDCSegmentedButtonSegmentFoundation.defaultAdapter), adapter)) || this;
  39. }
  40. Object.defineProperty(MDCSegmentedButtonSegmentFoundation, "defaultAdapter", {
  41. get: function () {
  42. return {
  43. isSingleSelect: function () { return false; }, getAttr: function () { return ''; }, setAttr: function () { return undefined; },
  44. addClass: function () { return undefined; }, removeClass: function () { return undefined; },
  45. hasClass: function () { return false; },
  46. notifySelectedChange: function () { return undefined; },
  47. getRootBoundingClientRect: function () { return emptyClientRect; },
  48. };
  49. },
  50. enumerable: false,
  51. configurable: true
  52. });
  53. /**
  54. * @return Returns true if segment is currently selected, otherwise returns
  55. * false
  56. */
  57. MDCSegmentedButtonSegmentFoundation.prototype.isSelected = function () {
  58. return this.adapter.hasClass(cssClasses.SELECTED);
  59. };
  60. /**
  61. * Sets segment to be selected
  62. */
  63. MDCSegmentedButtonSegmentFoundation.prototype.setSelected = function () {
  64. this.adapter.addClass(cssClasses.SELECTED);
  65. this.setAriaAttr(booleans.TRUE);
  66. };
  67. /**
  68. * Sets segment to be not selected
  69. */
  70. MDCSegmentedButtonSegmentFoundation.prototype.setUnselected = function () {
  71. this.adapter.removeClass(cssClasses.SELECTED);
  72. this.setAriaAttr(booleans.FALSE);
  73. };
  74. /**
  75. * @return Returns segment's segmentId if it was set by client
  76. */
  77. MDCSegmentedButtonSegmentFoundation.prototype.getSegmentId = function () {
  78. var _a;
  79. return (_a = this.adapter.getAttr(attributes.DATA_SEGMENT_ID)) !== null && _a !== void 0 ? _a : undefined;
  80. };
  81. /**
  82. * Called when segment is clicked. If the wrapping segmented button is single
  83. * select, doesn't allow segment to be set to not selected. Otherwise, toggles
  84. * segment's selected status. Finally, emits event to wrapping segmented
  85. * button.
  86. *
  87. * @event selected With detail - SegmentDetail
  88. */
  89. MDCSegmentedButtonSegmentFoundation.prototype.handleClick = function () {
  90. if (this.adapter.isSingleSelect()) {
  91. this.setSelected();
  92. }
  93. else {
  94. this.toggleSelection();
  95. }
  96. this.adapter.notifySelectedChange(this.isSelected());
  97. };
  98. /**
  99. * @return Returns bounding rectangle for ripple effect
  100. */
  101. MDCSegmentedButtonSegmentFoundation.prototype.getDimensions = function () {
  102. return this.adapter.getRootBoundingClientRect();
  103. };
  104. /**
  105. * Sets segment from not selected to selected, or selected to not selected
  106. */
  107. MDCSegmentedButtonSegmentFoundation.prototype.toggleSelection = function () {
  108. if (this.isSelected()) {
  109. this.setUnselected();
  110. }
  111. else {
  112. this.setSelected();
  113. }
  114. };
  115. /**
  116. * Sets appropriate aria attribute, based on wrapping segmented button's
  117. * single selected value, to new value
  118. *
  119. * @param value Value that represents selected status
  120. */
  121. MDCSegmentedButtonSegmentFoundation.prototype.setAriaAttr = function (value) {
  122. if (this.adapter.isSingleSelect()) {
  123. this.adapter.setAttr(attributes.ARIA_CHECKED, value);
  124. }
  125. else {
  126. this.adapter.setAttr(attributes.ARIA_PRESSED, value);
  127. }
  128. };
  129. return MDCSegmentedButtonSegmentFoundation;
  130. }(MDCFoundation));
  131. export { MDCSegmentedButtonSegmentFoundation };
  132. //# sourceMappingURL=foundation.js.map