1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | import { __values } from "tslib";
|
24 | import { strings } from './constants';
|
25 | var AUTO_INIT_ATTR = strings.AUTO_INIT_ATTR, AUTO_INIT_STATE_ATTR = strings.AUTO_INIT_STATE_ATTR, INITIALIZED_STATE = strings.INITIALIZED_STATE;
|
26 | var registry = {};
|
27 | var CONSOLE_WARN = console.warn.bind(console);
|
28 | function emit(evtType, evtData, shouldBubble) {
|
29 | if (shouldBubble === void 0) { shouldBubble = false; }
|
30 | var evt;
|
31 | if (typeof CustomEvent === 'function') {
|
32 | evt = new CustomEvent(evtType, {
|
33 | bubbles: shouldBubble,
|
34 | detail: evtData,
|
35 | });
|
36 | }
|
37 | else {
|
38 | evt = document.createEvent('CustomEvent');
|
39 | evt.initCustomEvent(evtType, shouldBubble, false, evtData);
|
40 | }
|
41 | document.dispatchEvent(evt);
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function mdcAutoInit(root) {
|
48 | var e_1, _a;
|
49 | if (root === void 0) { root = document; }
|
50 | var components = [];
|
51 | var nodes = [].slice.call(root.querySelectorAll("[" + AUTO_INIT_ATTR + "]"));
|
52 | nodes = nodes.filter(function (node) { return node.getAttribute(AUTO_INIT_STATE_ATTR) !== INITIALIZED_STATE; });
|
53 | try {
|
54 | for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
|
55 | var node = nodes_1_1.value;
|
56 | var ctorName = node.getAttribute(AUTO_INIT_ATTR);
|
57 | if (!ctorName) {
|
58 | throw new Error('(mdc-auto-init) Constructor name must be given.');
|
59 | }
|
60 | var Constructor = registry[ctorName];
|
61 | if (typeof Constructor !== 'function') {
|
62 | throw new Error("(mdc-auto-init) Could not find constructor in registry for " + ctorName);
|
63 | }
|
64 |
|
65 |
|
66 | var component = Constructor.attachTo(node);
|
67 | Object.defineProperty(node, ctorName, {
|
68 | configurable: true,
|
69 | enumerable: false,
|
70 | value: component,
|
71 | writable: false,
|
72 | });
|
73 | components.push(component);
|
74 | node.setAttribute(AUTO_INIT_STATE_ATTR, INITIALIZED_STATE);
|
75 | }
|
76 | }
|
77 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
78 | finally {
|
79 | try {
|
80 | if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
|
81 | }
|
82 | finally { if (e_1) throw e_1.error; }
|
83 | }
|
84 | emit('MDCAutoInit:End', {});
|
85 | return components;
|
86 | }
|
87 |
|
88 |
|
89 | mdcAutoInit.register = function (componentName, Constructor, warn) {
|
90 | if (warn === void 0) { warn = CONSOLE_WARN; }
|
91 | if (typeof Constructor !== 'function') {
|
92 | throw new Error("(mdc-auto-init) Invalid Constructor value: " + Constructor + ". Expected function.");
|
93 | }
|
94 | var registryValue = registry[componentName];
|
95 | if (registryValue) {
|
96 | warn("(mdc-auto-init) Overriding registration for " + componentName + " with " + Constructor + ". Was: " + registryValue);
|
97 | }
|
98 | registry[componentName] = Constructor;
|
99 | };
|
100 | mdcAutoInit.deregister = function (componentName) {
|
101 | delete registry[componentName];
|
102 | };
|
103 |
|
104 | mdcAutoInit.deregisterAll = function () {
|
105 | var keys = Object.keys(registry);
|
106 | keys.forEach(this.deregister, this);
|
107 | };
|
108 |
|
109 | export default mdcAutoInit;
|
110 | export { mdcAutoInit };
|
111 |
|
\ | No newline at end of file |