-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.mjs
199 lines (179 loc) · 5.54 KB
/
element.mjs
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
import { reactive, ReactiveVar } from "atomi";
import { createElement, createTextNode, Node, Text } from "./createElement.mjs";
import { normalize } from "./utils.mjs";
import { Binder } from "./binder.mjs";
const PropertyNotAttributeList = ["checked", "disabled"];
const specialAttributes = {
class: (classes, node) => {
if (classes.constructor === Object) {
// NOTE: detected a class map;
return Object.entries(classes)
.filter(([classname, condition]) => {
if (condition instanceof Function) {
return condition();
}
return condition;
}).map((entry) => entry[0])
.join(" ");
}
return classes;
}
}
export function reuse(node, attributes, ...children) {
return element("", [attributes, children], node);
}
export function element(tagName, [attributes = {}, children = []], existingNode) {
const node = existingNode || createElement(tagName);
applyEvents(node, attributes.on);
applyAttributes(node, attributes);
const { space } = reactive((scope) => {
scope.space.content = replaceChildrenOf(node, normalize(children).filter(child => child !== undefined));
});
if (space.content instanceof Promise) {
return new Promise(async resolve => {
await space.content;
resolve(node);
});
}
return node;
}
function applyEvents(node, eventMap = {}) {
for (let [event, listener] of Object.entries(eventMap)) {
// TODO: think if should allow listener be an array.
// is it a valid use case have multiple click events on the same element ?
// TODO: think if reactive event listeners should be a thing
node.addEventListener(event, listener);
}
}
/**
* @param {(string|function|Promise)} value
*/
function unwrapAttribute(value) {
if (value instanceof Function) {
return unwrapAttribute(value());
}
if (value instanceof Promise) {
return value.then(unwrapAttribute);
}
if (value instanceof Binder) {
return value.atom.get();
}
return value;
}
/**
* @param {node} node
* @param {string} attr
* @param {string} value
*/
function applyUnwrapedAttribute(node, attr, value) {
if (PropertyNotAttributeList.includes(attr)) {
node[attr] = value;
}
else {
node.setAttribute(attr, value);
}
return node;
}
/**
* @param {node} node
* @param {string} attr
* @param {(string|function)} value
*/
function applyAttribute(node, attr, value) {
// TODO: accept promises as attribute values
// const newValue = value instanceof Function ? value() : value;
const newValue = unwrapAttribute(value);
// NOTE: async atrribnutes cannot be reactive. If you refer to any atoms incide,
// they would not bind and your attribute would execute exactly once
if (newValue instanceof Promise) {
return newValue.then(unwraped => applyUnwrapedAttribute(node, attr, unwraped));
}
return applyUnwrapedAttribute(node, attr, newValue);
}
/**
* @param {Node} node
* @param {string} attr
* @param {(string|function|ReactiveVar)} value
*/
function bindAttribute(node, attr, value) {
reactive(async scope => {
const attrParser = specialAttributes[attr];
if (attrParser) {
const parsedValue = attrParser(value);
await applyAttribute(node, attr, parsedValue);
}
else {
await applyAttribute(node, attr, value);
}
// NOTE: if after function execution scope did not register any dependencies,
// then there's none. We want just to forget about it.
if (scope.deps.size === 0) {
scope.die();
}
});
if (value instanceof Binder) {
value.bind(attr, node);
}
}
function applyAttributes(node, { on, ...attributes }) {
// NOTE: ignoring 'on' attribute since it's used not as an attrinute, but a way to provide event listeners;
Object.entries(attributes)
.reduce(parseAttribute, [])
.forEach(([attribute, values]) => bindAttribute(node, attribute, values));
}
function parseAttribute(attributes = [], [name, value]) {
// NOTE: aria and data a special cases and can be nested
if (name === "aria" || name === "data") {
return attributes.concat(Object.entries(value).map(([key, val]) => [`${name}-${key}`, val]));
}
return attributes.concat([[name, value]]);
}
function replaceChildrenOf(node, children) {
let allKids = [];
for (let child of children) {
const kids = normalize(renderChild(child));
allKids = allKids.concat(kids.flat());
}
if (allKids.some(kid => kid instanceof Promise)) {
return new Promise(resolve => {
Promise.all(allKids).then(kids => {
while (node.firstChild) { node.removeChild(node.lastChild) }
node.append(...kids.filter(child => child !== undefined));
resolve(node);
});
});
}
while (node.firstChild) { node.removeChild(node.lastChild) }
node.append(...allKids.filter(child => child !== undefined));
return node;
}
// children are:
//
// 1. Rendered HTML element
// 2. Array of rendered HTML elements
// 3. Component (promise)
// 4. Array of Components (promises)
// 5. Function ?
// 6. child: String - createTextNode(child)
//
function renderChild(child) {
if (child instanceof Node) {
return child;
}
if (child instanceof Object && child.dom) {
return renderChild(child.dom);
}
if (child instanceof Function) {
return renderChild(child());
}
if (child instanceof Promise) {
return new Promise(async resolve => { resolve(renderChild(await child)) });
}
if (child instanceof Array) {
return child.map(kid => renderChild(kid))
}
if (child !== undefined) {
return createTextNode(child);
}
return child;
}