index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license
  3. * Copyright 2016 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 { __values } from "tslib";
  24. import { strings } from './constants';
  25. var AUTO_INIT_ATTR = strings.AUTO_INIT_ATTR, DATASET_AUTO_INIT_STATE = strings.DATASET_AUTO_INIT_STATE, INITIALIZED_STATE = strings.INITIALIZED_STATE;
  26. var registry = {};
  27. // tslint:disable-next-line:no-console
  28. var CONSOLE_WARN = console.warn.bind(console);
  29. function emit(evtType, evtData, shouldBubble) {
  30. if (shouldBubble === void 0) { shouldBubble = false; }
  31. var evt;
  32. if (typeof CustomEvent === 'function') {
  33. evt = new CustomEvent(evtType, {
  34. bubbles: shouldBubble,
  35. detail: evtData,
  36. });
  37. }
  38. else {
  39. evt = document.createEvent('CustomEvent');
  40. evt.initCustomEvent(evtType, shouldBubble, false, evtData);
  41. }
  42. document.dispatchEvent(evt);
  43. }
  44. /* istanbul ignore next: optional argument is not a branch statement */
  45. /**
  46. * Auto-initializes all MDC components on a page.
  47. */
  48. function mdcAutoInit(root) {
  49. var e_1, _a;
  50. if (root === void 0) { root = document; }
  51. var components = [];
  52. var nodes = Array.from(root.querySelectorAll("[" + AUTO_INIT_ATTR + "]"));
  53. nodes = nodes.filter(function (node) { return node.dataset[DATASET_AUTO_INIT_STATE] !== INITIALIZED_STATE; });
  54. try {
  55. for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
  56. var node = nodes_1_1.value;
  57. var ctorName = node.getAttribute(AUTO_INIT_ATTR);
  58. if (!ctorName) {
  59. throw new Error('(mdc-auto-init) Constructor name must be given.');
  60. }
  61. // tslint:disable-next-line:enforce-name-casing
  62. var Constructor = registry[ctorName];
  63. if (typeof Constructor !== 'function') {
  64. throw new Error("(mdc-auto-init) Could not find constructor in registry for " + ctorName);
  65. }
  66. // TODO: Should we make an eslint rule for an attachTo() static method?
  67. // See https://github.com/Microsoft/TypeScript/issues/14600 for discussion
  68. // of static interface support in TS
  69. var component = Constructor.attachTo(node);
  70. Object.defineProperty(node, ctorName, {
  71. configurable: true,
  72. enumerable: false,
  73. value: component,
  74. writable: false,
  75. });
  76. components.push(component);
  77. node.dataset[DATASET_AUTO_INIT_STATE] = INITIALIZED_STATE;
  78. }
  79. }
  80. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  81. finally {
  82. try {
  83. if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
  84. }
  85. finally { if (e_1) throw e_1.error; }
  86. }
  87. emit('MDCAutoInit:End', {});
  88. return components;
  89. }
  90. // Constructor is PascalCased because it is a direct reference to a class,
  91. // rather than an instance of a class.
  92. mdcAutoInit.register = function (componentName,
  93. // tslint:disable-next-line:enforce-name-casing
  94. Constructor, warn) {
  95. if (warn === void 0) { warn = CONSOLE_WARN; }
  96. if (typeof Constructor !== 'function') {
  97. throw new Error("(mdc-auto-init) Invalid Constructor value: " + Constructor + ". Expected function.");
  98. }
  99. var registryValue = registry[componentName];
  100. if (registryValue) {
  101. warn("(mdc-auto-init) Overriding registration for " + componentName + " with " + Constructor + ". Was: " + registryValue);
  102. }
  103. registry[componentName] = Constructor;
  104. };
  105. mdcAutoInit.deregister = function (componentName) {
  106. delete registry[componentName];
  107. };
  108. /** @nocollapse */
  109. mdcAutoInit.deregisterAll = function () {
  110. var e_2, _a;
  111. try {
  112. for (var _b = __values(Object.keys(registry)), _c = _b.next(); !_c.done; _c = _b.next()) {
  113. var componentName = _c.value;
  114. mdcAutoInit.deregister(componentName);
  115. }
  116. }
  117. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  118. finally {
  119. try {
  120. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  121. }
  122. finally { if (e_2) throw e_2.error; }
  123. }
  124. };
  125. // tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
  126. export default mdcAutoInit;
  127. export { mdcAutoInit };
  128. //# sourceMappingURL=index.js.map