foundation.js 6.1 KB

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