This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathindex.ts
137 lines (118 loc) · 4.58 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @license
* Copyright 2016 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// tslint:disable:only-arrow-functions
import {MDCComponent} from '@material/base/component';
import {MDCFoundation} from '@material/base/foundation';
import {strings} from './constants';
const {AUTO_INIT_ATTR, DATASET_AUTO_INIT_STATE, INITIALIZED_STATE} = strings;
/** MDC Attachable */
export interface MDCAttachable extends Function {
attachTo(root: HTMLElement): MDCComponent<MDCFoundation>;
}
interface InternalComponentRegistry {
[key: string]: MDCAttachable;
}
const registry: InternalComponentRegistry = {};
// tslint:disable-next-line:no-console
const CONSOLE_WARN = console.warn.bind(console);
function emit<T extends object>(
eventType: string, eventData: T, shouldBubble = false) {
let event;
if (typeof CustomEvent === 'function') {
event = new CustomEvent<T>(eventType, {
bubbles: shouldBubble,
detail: eventData,
});
} else {
event = document.createEvent('CustomEvent');
event.initCustomEvent(eventType, shouldBubble, false, eventData);
}
document.dispatchEvent(event);
}
/* istanbul ignore next: optional argument is not a branch statement */
/**
* Auto-initializes all MDC components on a page.
*/
function mdcAutoInit(root: ParentNode = document) {
const components = [];
let nodes =
Array.from(root.querySelectorAll<HTMLElement>(`[${AUTO_INIT_ATTR}]`));
nodes = nodes.filter(
(node) => node.dataset[DATASET_AUTO_INIT_STATE] !== INITIALIZED_STATE);
for (const node of nodes) {
const ctorName = node.getAttribute(AUTO_INIT_ATTR);
if (!ctorName) {
throw new Error('(mdc-auto-init) Constructor name must be given.');
}
// tslint:disable-next-line:enforce-name-casing
const Constructor = registry[ctorName];
if (typeof Constructor !== 'function') {
throw new Error(
`(mdc-auto-init) Could not find constructor in registry for ${
ctorName}`);
}
// TODO: Should we make an eslint rule for an attachTo() static method?
// See https://github.com/Microsoft/TypeScript/issues/14600 for discussion
// of static interface support in TS
const component = Constructor.attachTo(node);
Object.defineProperty(node, ctorName, {
configurable: true,
enumerable: false,
value: component,
writable: false,
});
components.push(component);
node.dataset[DATASET_AUTO_INIT_STATE] = INITIALIZED_STATE;
}
emit('MDCAutoInit:End', {});
return components;
}
// Constructor is PascalCased because it is a direct reference to a class,
// rather than an instance of a class.
mdcAutoInit.register = function(
componentName: string,
// tslint:disable-next-line:enforce-name-casing
Constructor: MDCAttachable, warn = CONSOLE_WARN) {
if (typeof Constructor !== 'function') {
throw new Error(`(mdc-auto-init) Invalid Constructor value: ${
Constructor}. Expected function.`);
}
const registryValue = registry[componentName];
if (registryValue) {
warn(`(mdc-auto-init) Overriding registration for ${componentName} with ${
Constructor}. Was: ${registryValue}`);
}
registry[componentName] = Constructor;
};
mdcAutoInit.deregister = function(componentName: string) {
delete registry[componentName];
};
/** @nocollapse */
mdcAutoInit.deregisterAll = function() {
for (const componentName of Object.keys(registry)) {
mdcAutoInit.deregister(componentName);
}
};
// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
export default mdcAutoInit;
export {mdcAutoInit};