-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist.js
241 lines (240 loc) · 6.19 KB
/
dist.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
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
// util.ts
var READY_STATE_CHANGE = "readystatechange";
var p;
function documentReady() {
return p = p || new Promise((resolve) => {
const doc = document;
const checkReady = () => {
if (doc.readyState === "complete") {
resolve();
doc.removeEventListener(READY_STATE_CHANGE, checkReady);
}
};
doc.addEventListener(READY_STATE_CHANGE, checkReady);
checkReady();
});
}
var boldColor = (color) => `color: ${color}; font-weight: bold;`;
var defaultEventColor = "#f012be";
function logEvent({
component: component2,
e,
module,
color
}) {
if (typeof __DEV__ === "boolean" && !__DEV__)
return;
const event = e.type;
console.groupCollapsed(
`${module}> %c${event}%c on %c${component2}`,
boldColor(color || defaultEventColor),
"",
boldColor("#1a80cc")
);
console.log(e);
if (e.target) {
console.log(e.target);
}
console.groupEnd();
}
// mod.ts
var registry = {};
function assert(assertion, message) {
if (!assertion) {
throw new Error(message);
}
}
function assertComponentNameIsValid(name) {
assert(typeof name === "string", "The name should be a string");
assert(
!!registry[name],
`The component of the given name is not registered: ${name}`
);
}
function component(name) {
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}-\u{1F48A}`;
const hooks = [({ el }) => {
el.classList.add(name);
el.classList.add(initClass);
el.addEventListener(`__ummount__:${name}`, () => {
el.classList.remove(initClass);
}, { once: true });
}];
const mountHooks = [];
const initializer = (el) => {
if (!el.classList.contains(initClass)) {
const e = new CustomEvent("__mount__", { bubbles: false });
const ctx = createEventContext(e, el);
hooks.map((cb) => {
cb(ctx);
});
mountHooks.map((cb) => {
cb(ctx);
});
}
};
initializer.sel = `.${name}:not(.${initClass})`;
registry[name] = initializer;
documentReady().then(() => {
mount(name);
});
const on = new Proxy(() => {
}, {
set(_, type, value) {
return addEventBindHook(name, hooks, mountHooks, type, value);
},
get(_, outside) {
if (outside === "outside") {
return new Proxy({}, {
set(_2, type, value) {
assert(
typeof value === "function",
`Event handler must be a function, ${typeof value} (${value}) is given`
);
hooks.push(({ el }) => {
const listener = (e) => {
if (el !== e.target && !el.contains(e.target)) {
logEvent({
module: "outside",
color: "#39cccc",
e,
component: name
});
value(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(_, type, value) {
return addEventBindHook(
name,
hooks,
mountHooks,
type,
// deno-lint-ignore no-explicit-any
value,
selector
);
}
});
}
});
const is = (name2) => {
hooks.push(({ el }) => {
el.classList.add(name2);
});
};
const sub = (type) => is(`sub:${type}`);
const innerHTML = (html) => {
hooks.push(({ el }) => {
el.innerHTML = html;
});
};
return { on, is, sub, innerHTML };
}
function createEventContext(e, el) {
return {
e,
el,
query: (s) => el.querySelector(s),
queryAll: (s) => el.querySelectorAll(s),
pub: (type, data) => {
document.querySelectorAll(`.sub\\:${type}`).forEach((el2) => {
el2.dispatchEvent(
new CustomEvent(type, { bubbles: false, detail: data })
);
});
}
};
}
function addEventBindHook(name, hooks, mountHooks, type, handler, selector) {
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) => {
if (!selector || [].some.call(
el.querySelectorAll(selector),
(node) => node === e.target || node.contains(e.target)
)) {
logEvent({
module: "\u{1F48A}",
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;
}
function mount(name, el) {
let classNames;
if (!name) {
classNames = Object.keys(registry);
} else {
assertComponentNameIsValid(name);
classNames = [name];
}
classNames.map((className) => {
[].map.call(
(el || document).querySelectorAll(registry[className].sel),
registry[className]
);
});
}
function unmount(name, el) {
assert(
!!registry[name],
`The component of the given name is not registered: ${name}`
);
el.dispatchEvent(new CustomEvent(`__unmount__:${name}`));
}
export {
component,
mount,
unmount
};
/*! Capsule v0.6.0 | Copyright 2022 Yoshiya Hinosawa and Capsule contributors | MIT license */