-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
290 lines (267 loc) · 7.95 KB
/
mod.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*! Capsule v0.6.1 | Copyright 2022 Yoshiya Hinosawa and Capsule contributors | MIT license */
import { documentReady, logEvent } from "./util.ts";
interface Initializer {
(el: HTMLElement): void;
/** The elector for the component */
sel: string;
}
interface RegistryType {
[key: string]: Initializer;
}
interface EventRegistry {
outside: {
[key: string]: EventHandler;
};
// deno-lint-ignore ban-types
[key: string]: EventHandler | {};
(selector: string): {
[key: string]: EventHandler;
};
}
interface ComponentResult {
on: EventRegistry;
is(name: string): void;
sub(type: string): void;
innerHTML(html: string): void;
}
interface ComponentEventContext {
/** The event */
e: Event;
/** The element */
el: HTMLElement;
/** Queries elements by the given selector under the component dom */
query<T extends HTMLElement = HTMLElement>(selector: string): T | null;
/** Queries all elements by the given selector under the component dom */
queryAll<T extends HTMLElement = HTMLElement>(
selector: string,
): NodeListOf<T>;
/** Publishes the event. Events are delivered to elements which have `sub:event` class.
* The dispatched events don't bubbles up */
pub<T = unknown>(name: string, data?: T): void;
}
type EventHandler = (el: ComponentEventContext) => void;
/** The registry of component initializers. */
const registry: RegistryType = {};
/**
* Asserts the given condition holds, otherwise throws.
* @param assertion The assertion expression
* @param message The assertion message
*/
function assert(assertion: boolean, message: string): void {
if (!assertion) {
throw new Error(message);
}
}
/** Asserts the given name is a valid component name.
* @param name The component name */
function assertComponentNameIsValid(name: unknown): void {
assert(typeof name === "string", "The name should be a string");
assert(
!!registry[name as string],
`The component of the given name is not registered: ${name}`,
);
}
export function component(name: string): ComponentResult {
assert(
typeof name === "string" && !!name,
"Component name must be a non-empty string",
);
assert(
!registry[name],
`The component of the given name is already registered: ${name}`,
);
const initClass = `${name}-💊`;
// Hooks for mount phase
const hooks: EventHandler[] = [({ el }) => {
// FIXME(kt3k): the below can be written as .add(name, initClass)
// when deno_dom fixes add class.
el.classList.add(name);
el.classList.add(initClass);
el.addEventListener(`__ummount__:${name}`, () => {
el.classList.remove(initClass);
}, { once: true });
}];
const mountHooks: EventHandler[] = [];
/** Initializes the html element by the given configuration. */
const initializer = (el: HTMLElement) => {
if (!el.classList.contains(initClass)) {
const e = new CustomEvent("__mount__", { bubbles: false });
const ctx = createEventContext(e, el);
// Initialize `before mount` hooks
// This includes:
// - initialization of event handlers
// - initialization of innerHTML
// - initialization of class names (is, sub)
hooks.map((cb) => {
cb(ctx);
});
// Execute __mount__ hooks
mountHooks.map((cb) => {
cb(ctx);
});
}
};
// The selector
initializer.sel = `.${name}:not(.${initClass})`;
registry[name] = initializer;
documentReady().then(() => {
mount(name);
});
// deno-lint-ignore no-explicit-any
const on: any = new Proxy(() => {}, {
set(_: unknown, type: string, value: unknown): boolean {
// deno-lint-ignore no-explicit-any
return addEventBindHook(name, hooks, mountHooks, type, value as any);
},
get(_: unknown, outside: string) {
if (outside === "outside") {
return new Proxy({}, {
set(_: unknown, type: string, value: unknown): boolean {
assert(
typeof value === "function",
`Event handler must be a function, ${typeof value} (${value}) is given`,
);
hooks.push(({ el }) => {
const listener = (e: Event) => {
// deno-lint-ignore no-explicit-any
if (el !== e.target && !el.contains(e.target as any)) {
logEvent({
module: "outside",
color: "#39cccc",
e,
component: name,
});
(value as EventHandler)(createEventContext(e, el));
}
};
document.addEventListener(type, listener);
el.addEventListener(`__unmount__:${name}`, () => {
document.removeEventListener(type, listener);
}, { once: true });
});
return true;
},
});
}
return null;
},
apply(_target, _thisArg, args) {
const selector = args[0];
assert(
typeof selector === "string",
"Delegation selector must be a string. ${typeof selector} is given.",
);
return new Proxy({}, {
set(_: unknown, type: string, value: unknown): boolean {
return addEventBindHook(
name,
hooks,
mountHooks,
type,
// deno-lint-ignore no-explicit-any
value as any,
selector,
);
},
});
},
});
const is = (name: string) => {
hooks.push(({ el }) => {
el.classList.add(name);
});
};
const sub = (type: string) => is(`sub:${type}`);
const innerHTML = (html: string) => {
hooks.push(({ el }) => {
el.innerHTML = html;
});
};
return { on, is, sub, innerHTML };
}
function createEventContext(e: Event, el: HTMLElement): ComponentEventContext {
return {
e,
el,
query: (s: string) => el.querySelector(s),
queryAll: (s: string) => el.querySelectorAll(s),
pub: (type: string, data?: unknown) => {
document.querySelectorAll(`.sub\\:${type}`).forEach((el) => {
el.dispatchEvent(
new CustomEvent(type, { bubbles: false, detail: data }),
);
});
},
};
}
function addEventBindHook(
name: string,
hooks: EventHandler[],
mountHooks: EventHandler[],
type: string,
handler: (ctx: ComponentEventContext) => void,
selector?: string,
): boolean {
assert(
typeof handler === "function",
`Event handler must be a function, ${typeof handler} (${handler}) is given`,
);
if (type === "__mount__") {
mountHooks.push(handler);
return true;
}
if (type === "__unmount__") {
hooks.push(({ el }) => {
el.addEventListener(`__unmount__:${name}`, () => {
handler(createEventContext(new CustomEvent("__unmount__"), el));
}, { once: true });
});
return true;
}
hooks.push(({ el }) => {
const listener = (e: Event) => {
if (
!selector ||
[].some.call(
el.querySelectorAll(selector),
(node: Node) => node === e.target || node.contains(e.target as Node),
)
) {
logEvent({
module: "💊",
color: "#e0407b",
e,
component: name,
});
handler(createEventContext(e, el));
}
};
el.addEventListener(`__unmount__:${name}`, () => {
el.removeEventListener(type, listener);
}, { once: true });
el.addEventListener(type, listener);
});
return true;
}
export function mount(name?: string | null, el?: HTMLElement) {
let classNames: string[];
if (!name) {
classNames = Object.keys(registry);
} else {
assertComponentNameIsValid(name);
classNames = [name];
}
classNames.map((className) => {
[].map.call(
(el || document).querySelectorAll(registry[className].sel),
registry[className],
);
});
}
export function unmount(name: string, el: HTMLElement) {
assert(
!!registry[name],
`The component of the given name is not registered: ${name}`,
);
el.dispatchEvent(new CustomEvent(`__unmount__:${name}`));
}