foundation.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 { __assign, __extends, __values } from "tslib";
  24. import { MDCFoundation } from '@material/base/foundation';
  25. import { strings } from './constants';
  26. var INTERACTION_EVENTS = ['click', 'keydown'];
  27. /** MDC Select Icon Foundation */
  28. var MDCSelectIconFoundation = /** @class */ (function (_super) {
  29. __extends(MDCSelectIconFoundation, _super);
  30. function MDCSelectIconFoundation(adapter) {
  31. var _this = _super.call(this, __assign(__assign({}, MDCSelectIconFoundation.defaultAdapter), adapter)) || this;
  32. _this.savedTabIndex = null;
  33. _this.interactionHandler = function (evt) {
  34. _this.handleInteraction(evt);
  35. };
  36. return _this;
  37. }
  38. Object.defineProperty(MDCSelectIconFoundation, "strings", {
  39. get: function () {
  40. return strings;
  41. },
  42. enumerable: false,
  43. configurable: true
  44. });
  45. Object.defineProperty(MDCSelectIconFoundation, "defaultAdapter", {
  46. /**
  47. * See {@link MDCSelectIconAdapter} for typing information on parameters and
  48. * return types.
  49. */
  50. get: function () {
  51. // tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
  52. return {
  53. getAttr: function () { return null; },
  54. setAttr: function () { return undefined; },
  55. removeAttr: function () { return undefined; },
  56. setContent: function () { return undefined; },
  57. registerInteractionHandler: function () { return undefined; },
  58. deregisterInteractionHandler: function () { return undefined; },
  59. notifyIconAction: function () { return undefined; },
  60. };
  61. // tslint:enable:object-literal-sort-keys
  62. },
  63. enumerable: false,
  64. configurable: true
  65. });
  66. MDCSelectIconFoundation.prototype.init = function () {
  67. var e_1, _a;
  68. this.savedTabIndex = this.adapter.getAttr('tabindex');
  69. try {
  70. for (var INTERACTION_EVENTS_1 = __values(INTERACTION_EVENTS), INTERACTION_EVENTS_1_1 = INTERACTION_EVENTS_1.next(); !INTERACTION_EVENTS_1_1.done; INTERACTION_EVENTS_1_1 = INTERACTION_EVENTS_1.next()) {
  71. var evtType = INTERACTION_EVENTS_1_1.value;
  72. this.adapter.registerInteractionHandler(evtType, this.interactionHandler);
  73. }
  74. }
  75. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  76. finally {
  77. try {
  78. if (INTERACTION_EVENTS_1_1 && !INTERACTION_EVENTS_1_1.done && (_a = INTERACTION_EVENTS_1.return)) _a.call(INTERACTION_EVENTS_1);
  79. }
  80. finally { if (e_1) throw e_1.error; }
  81. }
  82. };
  83. MDCSelectIconFoundation.prototype.destroy = function () {
  84. var e_2, _a;
  85. try {
  86. for (var INTERACTION_EVENTS_2 = __values(INTERACTION_EVENTS), INTERACTION_EVENTS_2_1 = INTERACTION_EVENTS_2.next(); !INTERACTION_EVENTS_2_1.done; INTERACTION_EVENTS_2_1 = INTERACTION_EVENTS_2.next()) {
  87. var evtType = INTERACTION_EVENTS_2_1.value;
  88. this.adapter.deregisterInteractionHandler(evtType, this.interactionHandler);
  89. }
  90. }
  91. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  92. finally {
  93. try {
  94. if (INTERACTION_EVENTS_2_1 && !INTERACTION_EVENTS_2_1.done && (_a = INTERACTION_EVENTS_2.return)) _a.call(INTERACTION_EVENTS_2);
  95. }
  96. finally { if (e_2) throw e_2.error; }
  97. }
  98. };
  99. MDCSelectIconFoundation.prototype.setDisabled = function (disabled) {
  100. if (!this.savedTabIndex) {
  101. return;
  102. }
  103. if (disabled) {
  104. this.adapter.setAttr('tabindex', '-1');
  105. this.adapter.removeAttr('role');
  106. }
  107. else {
  108. this.adapter.setAttr('tabindex', this.savedTabIndex);
  109. this.adapter.setAttr('role', strings.ICON_ROLE);
  110. }
  111. };
  112. MDCSelectIconFoundation.prototype.setAriaLabel = function (label) {
  113. this.adapter.setAttr('aria-label', label);
  114. };
  115. MDCSelectIconFoundation.prototype.setContent = function (content) {
  116. this.adapter.setContent(content);
  117. };
  118. MDCSelectIconFoundation.prototype.handleInteraction = function (evt) {
  119. var isEnterKey = evt.key === 'Enter' ||
  120. evt.keyCode === 13;
  121. if (evt.type === 'click' || isEnterKey) {
  122. this.adapter.notifyIconAction();
  123. }
  124. };
  125. return MDCSelectIconFoundation;
  126. }(MDCFoundation));
  127. export { MDCSelectIconFoundation };
  128. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  129. export default MDCSelectIconFoundation;
  130. //# sourceMappingURL=foundation.js.map