-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refable.ts
133 lines (108 loc) · 3.09 KB
/
refable.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
/*
* Copyright (c) 1986-2024 Ecmel Ercan (https://ecmel.dev/)
* Licensed under the MIT License
*/
export type Class<T> = new (...args: any[]) => T;
export class Application {
private ctors = new Map<string, Class<Controller>>();
private controllers = new WeakMap<Element, Controller>();
private observer: MutationObserver;
constructor() {
this.observer = new MutationObserver((mutations) =>
this.mutated(mutations)
);
}
private mutated(mutations: MutationRecord[]) {
mutations.forEach((mutation) => {
mutation.removedNodes.forEach((node) => this.removeNode(node));
mutation.addedNodes.forEach((node) => this.addNode(node));
});
}
private addNode(node: Node) {
if (node.nodeType === Node.ELEMENT_NODE) {
const el = node as Element;
if (el.hasAttribute("data-controller")) {
this.addController(el);
}
el.querySelectorAll("[data-controller]").forEach((el) =>
this.addController(el)
);
}
}
private removeNode(node: Node) {
if (node.nodeType === Node.ELEMENT_NODE) {
const el = node as Element;
el.querySelectorAll("[data-controller]").forEach((el) =>
this.removeController(el)
);
if (el.hasAttribute("data-controller")) {
this.removeController(el);
}
}
}
private addController(el: Element) {
let controller = this.controllers.get(el);
if (!controller) {
const id = el.getAttribute("data-controller");
if (id) {
const ctor = this.ctors.get(id);
if (ctor) {
controller = new ctor(el, this);
this.controllers.set(el, controller);
queueMicrotask(() => controller?.created());
}
}
}
queueMicrotask(() => controller?.connected());
}
private removeController(el: Element) {
const controller = this.controllers.get(el);
if (controller) {
queueMicrotask(() => controller.disconnected());
}
}
register(id: string, ctor: Class<Controller>) {
this.ctors.set(id, ctor);
}
getController<T extends Controller>(el: Element): T {
return this.controllers.get(el) as T;
}
ready(resolve: Function) {
if (document.readyState == "loading") {
document.addEventListener("DOMContentLoaded", () => resolve());
} else {
resolve();
}
}
run() {
this.ready(() => {
document
.querySelectorAll("[data-controller]")
.forEach((el) => this.addController(el));
this.observer.observe(document, {
childList: true,
subtree: true,
});
});
}
}
export class Controller<T extends Element = Element> {
private _element: T;
private _application: Application;
constructor(element: T, application: Application) {
this._element = element;
this._application = application;
}
get element() {
return this._element;
}
protected getApplication<T extends Application>(): T {
return this._application as T;
}
protected getController<T extends Controller>(el: Element): T {
return this._application.getController<T>(el);
}
created() {}
connected() {}
disconnected() {}
}