-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
284 lines (260 loc) · 8.15 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
/*! Cell v0.1.5 | Copyright 2024 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;
};
}
/**
* The context of the component. This context is passed as the first argument to the component function for each mount.
*
* @typeParam EL The element type of the component
*/
export interface Context<EL = HTMLElement> {
/** The element */
el: EL;
/** The event registry. You can register event listener on `el` easily with this helper. */
on: EventRegistry;
/** Publishes the event. Events are delivered to elements which have `sub:event` class.
* The dispatched events don't bubbles up */
pub<T = unknown>(event: string, data?: T): void;
/** Add sub:event class to the component element */
sub(event: string): void;
/** 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>;
}
/** The component type */
export type Component = <T extends HTMLElement>(
ctx: Context<T>,
) => string | undefined | void;
/** The event handler type */
export type EventHandler = (e: Event) => 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}`,
);
}
type MountHook = (el: HTMLElement) => void;
/**
* Register the component with the given name
*
* @param component The component function
* @param name The component name
*/
export function register(component: Component, name: string) {
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}-💊`;
/** Initializes the html element by the given configuration. */
const initializer = (el: HTMLElement) => {
if (!el.classList.contains(initClass)) {
// 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(`__unmount__:${name}`, () => {
el.classList.remove(initClass);
}, { once: true });
// deno-lint-ignore no-explicit-any
const on: any = new Proxy(() => {}, {
// simple event handler (like on.click = (e) => {})
set(_: unknown, type: string, value: unknown): boolean {
// deno-lint-ignore no-explicit-any
addEventListener(name, el, type, value as any);
return true;
},
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`,
);
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)(e);
}
};
document.addEventListener(type, listener);
el.addEventListener(`__unmount__:${name}`, () => {
document.removeEventListener(type, listener);
}, { once: true });
return true;
},
});
}
return null;
},
// event delegation handler (like on(".button").click = (e) => {}))
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 {
addEventListener(
name,
el,
type,
// deno-lint-ignore no-explicit-any
value as any,
selector,
);
return true;
},
});
},
});
const pub = (type: string, data?: unknown) => {
document.querySelectorAll(`.sub\\:${type}`).forEach((el) => {
el.dispatchEvent(
new CustomEvent(type, { bubbles: false, detail: data }),
);
});
};
const sub = (type: string) => el.classList.add(`sub:${type}`);
const context = {
el,
on,
pub,
sub,
query: <T extends HTMLElement = HTMLElement>(s: string) =>
el.querySelector(s) as T | null,
queryAll: <T extends HTMLElement = HTMLElement>(s: string) =>
el.querySelectorAll(s) as NodeListOf<T>,
};
const html = component(context);
if (typeof html === "string") {
el.innerHTML = html;
}
}
};
// The selector
initializer.sel = `.${name}:not(.${initClass})`;
registry[name] = initializer;
if (document.readyState === "complete") {
mount();
} else {
documentReady().then(() => {
mount(name);
});
}
}
function addEventListener(
name: string,
el: HTMLElement,
type: string,
handler: (e: Event) => void,
selector?: string,
) {
assert(
typeof handler === "function",
`Event handler must be a function, ${typeof handler} (${handler}) is given`,
);
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(e);
}
};
el.addEventListener(`__unmount__:${name}`, () => {
el.removeEventListener(type, listener);
}, { once: true });
el.addEventListener(type, listener);
}
/**
* Mount the components to the doms.
*
* @param name The component name to mount. If not given, all components are mounted.
* @param el The elements of the children of this element will be initialied. If not given, the whole document is used.
*/
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],
);
});
}
/**
* Unmount the component from the dom.
*
* @param name The component name to unmount.
* @param el The element of the component to unmount.
*/
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}`));
}