-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrecha.js
More file actions
161 lines (153 loc) · 4.36 KB
/
grecha.js
File metadata and controls
161 lines (153 loc) · 4.36 KB
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
const LOREM =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
const s = (prop) => prop.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
function Cleanup() {
const cleanups = new Map();
return {
add: (key, callback) => {
if (!key) {
throw new Error("Component ID is required to register cleanup.");
}
cleanups.set(key, callback);
},
remove: (key) => {
cleanups.delete(key);
},
has: (key) => cleanups.has(key),
clean: () => {
for (const [key, callback] of cleanups) {
callback();
cleanups.delete(key);
}
},
log: () => console.log(Array.from(cleanups.keys())),
};
}
const cleanup = Cleanup();
function State() {
if (!window.state) {
window.state = new Map();
}
return {
set: (key, value, callback) => {
window.state.set(key, value);
if (callback) {
callback(key, value);
}
},
get: (key, callback) => {
if (window.state.has(key)) {
if (callback) {
callback(key, window.state.get(key));
}
return window.state.get(key);
}
},
has: (key) => window.state.has(key),
delete: (key, callback) => {
if (window.state.has(key)) {
window.state.delete(key);
if (callback) {
callback(key);
}
}
},
log: () => window.state,
};
}
function Style(styles) {
let css = document.getElementById("grecha-styles");
if (!css) {
css = document.createElement("style");
css.id = "grecha-styles";
document.head.appendChild(css);
}
css.innerText = Object.entries(styles)
.map(
([selector, style]) =>
`${selector} { ${Object.entries(style)
.map(([prop, val]) => `${s(prop)}: ${val};`)
.join(" ")} }`
)
.join(" ");
}
function Tag(name, ...children) {
const result = document.createElement(name);
for (const child of children) {
if (typeof child === "string") {
result.appendChild(document.createTextNode(child));
} else {
result.appendChild(child);
}
}
result.att$ = function (name, value) {
this.setAttribute(name, value);
return this;
};
result.style$ = function (styles) {
for (const property in styles) {
this.style[s(property)] = styles[property];
}
return this;
};
result.listen$ = function (listener, callback, global = false) {
if (global) {
window[listener] = callback;
return this;
} else {
this[listener] = callback;
return this;
}
};
result.unmount$ = function (callback) {
if (!cleanup.has(this.id)) {
cleanup.add(this.id, () => callback(this));
} else {
console.warn(
"An ID is required for unmount cleanup registration or the ID is already in use."
);
}
return this;
};
result.callback$ = function (callback) {
callback(this);
return this;
};
return result;
}
// prettier-ignore
const MUNDANE_TAGS = ["canvas","h1","h2","h3","p","a","div","span","select",];
for (let tagName of MUNDANE_TAGS) {
window[tagName] = (...children) => Tag(tagName, ...children);
}
const img = (src) => Tag("img").att$("src", src);
const input = (type) => Tag("input").att$("type", type);
const canvas = (id, width, height) =>
Tag("canvas")
.att$("id", id)
.att$("width", width ?? 300)
.att$("height", height ?? 150);
function Router(routes) {
let result = div();
function syncHash() {
let hashLocation = document.location.hash.split("#")[1] || "/";
// console.log(hashLocation);
let [path, queryString] = hashLocation.split("?");
if (!(path in routes)) {
path = "/404";
}
Style(styles());
const params = new URLSearchParams(queryString);
// console.log(params);
const routeComponent = routes[path];
result.replaceChildren(routeComponent(params));
return result;
}
syncHash();
window.addEventListener("hashchange", syncHash);
window.onpopstate = () => {
cleanup.clean();
};
result.refresh = syncHash;
return result;
}