UNPKG

4.97 kBJavaScriptView Raw
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 */
23import { __values } from "tslib";
24import { strings } from './constants';
25var AUTO_INIT_ATTR = strings.AUTO_INIT_ATTR, AUTO_INIT_STATE_ATTR = strings.AUTO_INIT_STATE_ATTR, INITIALIZED_STATE = strings.INITIALIZED_STATE;
26var registry = {};
27var CONSOLE_WARN = console.warn.bind(console); // tslint:disable-line:no-console
28function 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/* istanbul ignore next: optional argument is not a branch statement */
44/**
45 * Auto-initializes all MDC components on a page.
46 */
47function 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]; // tslint:disable-line:variable-name
61 if (typeof Constructor !== 'function') {
62 throw new Error("(mdc-auto-init) Could not find constructor in registry for " + ctorName);
63 }
64 // TODO: Should we make an eslint rule for an attachTo() static method?
65 // See https://github.com/Microsoft/TypeScript/issues/14600 for discussion of static interface support in TS
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// Constructor is PascalCased because it is a direct reference to a class, rather than an instance of a class.
88// tslint:disable-next-line:variable-name
89mdcAutoInit.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};
100mdcAutoInit.deregister = function (componentName) {
101 delete registry[componentName];
102};
103/** @nocollapse */
104mdcAutoInit.deregisterAll = function () {
105 var keys = Object.keys(registry);
106 keys.forEach(this.deregister, this);
107};
108// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
109export default mdcAutoInit;
110export { mdcAutoInit };
111//# sourceMappingURL=index.js.map
\No newline at end of file