foundation.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 { isNavigationEvent, KEY, normalizeKey } from '@material/dom/keyboard';
  26. import { MDCChipActionAttributes, MDCChipActionEvents, MDCChipActionFocusBehavior, MDCChipActionInteractionTrigger } from './constants';
  27. var triggerMap = new Map();
  28. triggerMap.set(KEY.SPACEBAR, MDCChipActionInteractionTrigger.SPACEBAR_KEY);
  29. triggerMap.set(KEY.ENTER, MDCChipActionInteractionTrigger.ENTER_KEY);
  30. triggerMap.set(KEY.DELETE, MDCChipActionInteractionTrigger.DELETE_KEY);
  31. triggerMap.set(KEY.BACKSPACE, MDCChipActionInteractionTrigger.BACKSPACE_KEY);
  32. /**
  33. * MDCChipActionFoundation provides a base abstract foundation for all chip
  34. * actions.
  35. */
  36. var MDCChipActionFoundation = /** @class */ (function (_super) {
  37. __extends(MDCChipActionFoundation, _super);
  38. function MDCChipActionFoundation(adapter) {
  39. return _super.call(this, __assign(__assign({}, MDCChipActionFoundation.defaultAdapter), adapter)) || this;
  40. }
  41. Object.defineProperty(MDCChipActionFoundation, "defaultAdapter", {
  42. get: function () {
  43. return {
  44. emitEvent: function () { return undefined; },
  45. focus: function () { return undefined; },
  46. getAttribute: function () { return null; },
  47. getElementID: function () { return ''; },
  48. removeAttribute: function () { return undefined; },
  49. setAttribute: function () { return undefined; },
  50. };
  51. },
  52. enumerable: false,
  53. configurable: true
  54. });
  55. MDCChipActionFoundation.prototype.handleClick = function () {
  56. // Early exit for cases where the click comes from a source other than the
  57. // user's pointer (i.e. programmatic click from AT).
  58. if (this.isDisabled())
  59. return;
  60. this.emitInteraction(MDCChipActionInteractionTrigger.CLICK);
  61. };
  62. MDCChipActionFoundation.prototype.handleKeydown = function (event) {
  63. var key = normalizeKey(event);
  64. if (this.shouldNotifyInteractionFromKey(key)) {
  65. event.preventDefault();
  66. this.emitInteraction(this.getTriggerFromKey(key));
  67. return;
  68. }
  69. if (isNavigationEvent(event)) {
  70. event.preventDefault();
  71. this.emitNavigation(key);
  72. return;
  73. }
  74. };
  75. MDCChipActionFoundation.prototype.setDisabled = function (isDisabled) {
  76. // Use `aria-disabled` for the selectable (listbox) disabled state
  77. if (this.isSelectable()) {
  78. this.adapter.setAttribute(MDCChipActionAttributes.ARIA_DISABLED, "" + isDisabled);
  79. return;
  80. }
  81. if (isDisabled) {
  82. this.adapter.setAttribute(MDCChipActionAttributes.DISABLED, 'true');
  83. }
  84. else {
  85. this.adapter.removeAttribute(MDCChipActionAttributes.DISABLED);
  86. }
  87. };
  88. MDCChipActionFoundation.prototype.isDisabled = function () {
  89. if (this.adapter.getAttribute(MDCChipActionAttributes.ARIA_DISABLED) ===
  90. 'true') {
  91. return true;
  92. }
  93. if (this.adapter.getAttribute(MDCChipActionAttributes.DISABLED) !== null) {
  94. return true;
  95. }
  96. return false;
  97. };
  98. MDCChipActionFoundation.prototype.setFocus = function (behavior) {
  99. // Early exit if not focusable
  100. if (!this.isFocusable()) {
  101. return;
  102. }
  103. // Add it to the tab order and give focus
  104. if (behavior === MDCChipActionFocusBehavior.FOCUSABLE_AND_FOCUSED) {
  105. this.adapter.setAttribute(MDCChipActionAttributes.TAB_INDEX, '0');
  106. this.adapter.focus();
  107. return;
  108. }
  109. // Add to the tab order
  110. if (behavior === MDCChipActionFocusBehavior.FOCUSABLE) {
  111. this.adapter.setAttribute(MDCChipActionAttributes.TAB_INDEX, '0');
  112. return;
  113. }
  114. // Remove it from the tab order
  115. if (behavior === MDCChipActionFocusBehavior.NOT_FOCUSABLE) {
  116. this.adapter.setAttribute(MDCChipActionAttributes.TAB_INDEX, '-1');
  117. return;
  118. }
  119. };
  120. MDCChipActionFoundation.prototype.isFocusable = function () {
  121. if (this.isDisabled()) {
  122. return false;
  123. }
  124. if (this.adapter.getAttribute(MDCChipActionAttributes.ARIA_HIDDEN) ===
  125. 'true') {
  126. return false;
  127. }
  128. return true;
  129. };
  130. MDCChipActionFoundation.prototype.setSelected = function (isSelected) {
  131. // Early exit if not selectable
  132. if (!this.isSelectable()) {
  133. return;
  134. }
  135. this.adapter.setAttribute(MDCChipActionAttributes.ARIA_SELECTED, "" + isSelected);
  136. };
  137. MDCChipActionFoundation.prototype.isSelected = function () {
  138. return this.adapter.getAttribute(MDCChipActionAttributes.ARIA_SELECTED) ===
  139. 'true';
  140. };
  141. MDCChipActionFoundation.prototype.emitInteraction = function (trigger) {
  142. this.adapter.emitEvent(MDCChipActionEvents.INTERACTION, {
  143. actionID: this.adapter.getElementID(),
  144. source: this.actionType(),
  145. trigger: trigger,
  146. });
  147. };
  148. MDCChipActionFoundation.prototype.emitNavigation = function (key) {
  149. this.adapter.emitEvent(MDCChipActionEvents.NAVIGATION, {
  150. source: this.actionType(),
  151. key: key,
  152. });
  153. };
  154. MDCChipActionFoundation.prototype.shouldNotifyInteractionFromKey = function (key) {
  155. var isFromActionKey = key === KEY.ENTER || key === KEY.SPACEBAR;
  156. var isFromRemoveKey = key === KEY.BACKSPACE || key === KEY.DELETE;
  157. if (isFromActionKey) {
  158. return true;
  159. }
  160. if (isFromRemoveKey && this.shouldEmitInteractionOnRemoveKey()) {
  161. return true;
  162. }
  163. return false;
  164. };
  165. MDCChipActionFoundation.prototype.getTriggerFromKey = function (key) {
  166. var trigger = triggerMap.get(key);
  167. if (trigger) {
  168. return trigger;
  169. }
  170. // Default case, should ideally never be returned
  171. return MDCChipActionInteractionTrigger.UNSPECIFIED;
  172. };
  173. return MDCChipActionFoundation;
  174. }(MDCFoundation));
  175. export { MDCChipActionFoundation };
  176. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  177. export default MDCChipActionFoundation;
  178. //# sourceMappingURL=foundation.js.map