-
Notifications
You must be signed in to change notification settings - Fork 9
/
template.js
149 lines (149 loc) · 4.26 KB
/
template.js
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
138
139
140
141
142
143
144
145
146
147
148
149
class Template {
constructor(element, style) {
this.fragment = element;
this.style = style;
this.host = null;
this.eventHandlers = {};
this.elements = {};
this.children = {};
this.state = {};
this.destroyed = false;
}
getElement(selector) {
const element = this.elements[selector];
if (element != undefined) {
return element;
}
if (this.host == undefined || this.host.shadowRoot == undefined) {
throw new Error("Template has not been mounted");
}
const result = this.host.shadowRoot.querySelector(selector);
if (result == undefined) {
throw new Error(`Element ${selector} not found`);
}
if (!(result instanceof HTMLElement)) {
throw new Error(`${selector} did not return an HTMLElement`);
}
this.elements[selector] = result;
return result;
}
setState(obj) {
let changed = false;
for (let key in obj) {
if (this.state[key] !== obj[key]) {
const value = obj[key];
changed = true;
if (value == undefined) {
delete this.state[key];
}
else {
this.state[key] = value;
}
}
}
if (changed) {
this.emit("change", this.state);
}
return this;
}
removeChild(name) {
const child = this.children[name];
if (child == undefined) {
return this;
}
child.unmount();
delete this.children[name];
return this;
}
addChild(selector, child) {
if (this.children[selector] != undefined) {
throw new Error("Child already mounted");
}
const element = this.getElement(selector);
child.mount(element);
this.children[selector] = child;
return this;
}
addChildren(obj) {
for (let query in obj) {
const child = obj[query];
if (child == undefined) {
continue;
}
this.addChild(query, child);
}
return this;
}
getChild(query) {
const child = this.children[query];
if (child == undefined) {
throw new Error(`Unknown child ${query}`);
}
return child;
}
mount(host) {
if (this.host) {
throw new Error("Already mounted");
}
this.host = host;
this.host.innerText = "";
if (!this.host.shadowRoot) {
this.host.attachShadow({ mode: "open" });
}
if (!this.host.shadowRoot) {
throw new Error("Failed to create shadow root");
}
this.host.shadowRoot.appendChild(this.style);
this.host.shadowRoot.appendChild(this.fragment.content.cloneNode(true));
return this;
}
unmount() {
for (const query in this.children) {
const child = this.children[query];
if (child == undefined) {
continue;
}
child.unmount();
}
if (this.host) {
this.host.innerText = "";
if (this.host.shadowRoot) {
this.host.shadowRoot.innerHTML = "";
}
}
this.host = null;
this.eventHandlers = {};
this.elements = {};
this.children = {};
this.state = {};
this.destroyed = true;
return;
}
on(event, handler) {
const handlers = this.eventHandlers[event] || [];
this.eventHandlers[event] = handlers;
handlers.push(handler);
return this;
}
emit(event, ...args) {
const handlers = this.eventHandlers[event];
if (handlers == undefined) {
return this;
}
handlers.forEach((handler) => {
handler(...args);
});
return this;
}
static createElement(html) {
const template = document.createElement("template");
template.innerHTML = html;
return template;
}
static createStyle(css) {
const style = document.createElement("style");
style.textContent = css;
return style;
}
}
export default Template;