-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomo.js
305 lines (260 loc) · 9.06 KB
/
domo.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
const html = (strings, ...expressions) => {
let result = '';
strings.forEach((string, i) => {
result += string;
if (i < expressions.length) {
if (expressions[i] instanceof Function) {
const fnName = expressions[i].name || `callback_${Math.random().toString(36).substr(2, 9)}`;
result += fnName;
} else {
result += expressions[i];
}
}
});
result = result.replace(
/<([a-zA-Z][a-zA-Z0-9-]*)((?:\s+[^>]*)?)\/>/g,
'<$1$2></$1>'
);
const fragment = document.createRange().createContextualFragment(result);
// Preserve component instance reference on elements
Array.from(fragment.children).forEach(child => {
if (child.tagName?.includes('-')) {
child._parentComponentInstance = this;
}
});
return fragment;
}
const setupListeners = (component, element) => {
// Set up listeners for the current element
element.getAttributeNames()
.filter(key => key.match(/^on\-/))
.forEach(key => {
if (component[element.getAttribute(key)] instanceof Function) {
element.addEventListener(
key.replace('on-', ''),
component[element.getAttribute(key)].bind(component),
false
);
}
});
// Handle callbacks
const cbAttributes = element.getAttributeNames()
.filter(key => key.match(/^(cb\-)/));
cbAttributes.forEach(attr => {
const callbackValue = element.getAttribute(attr);
const methodName = attributeToCamelCase(attr.replace(/^(cb\-)/, ''));
const componentMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(component));
const matchingMethod = componentMethods.find(method =>
method === callbackValue ||
component[method].name === callbackValue
);
if (matchingMethod) {
element[methodName] = function(...args) {
return component[matchingMethod].apply(component, args);
};
}
});
// Recursively set up listeners for all children
Array.from(element.children).forEach(child => {
if (child.tagName && !child.tagName.includes('-')) {
setupListeners(component, child);
}
});
}
const diff = (currentDom, newDom, changes = {added: [], removed: []}) => {
const currentLength = currentDom.children.length;
const newLength = newDom.children.length;
if (newLength === 0) {
changes.removed = changes.removed.concat(Array.from(currentDom.children));
currentDom.replaceChildren();
return [currentDom, changes];
}
if (currentLength === 0 && newLength > 0) {
const newChildren = Array.from(newDom.children);
changes.removed = changes.removed.concat(Array.from(currentDom.children));
changes.added = changes.added.concat(newChildren);
currentDom.replaceChildren(...newChildren);
return [currentDom, changes];
}
if (currentLength > newLength) {
for (let i = currentLength - 1; i >= newLength; i--) {
const node = currentDom.children[i];
changes.removed.push(node.cloneNode(true));
node.parentNode.removeChild(node);
}
} else if (currentLength < newLength) {
for (let i = currentLength; i < newLength; i++) {
const node = newDom.children[i].cloneNode(true);
changes.added.push(node);
currentDom.appendChild(node);
}
}
for (let i = 0; i < newLength; i++) {
const currentNode = currentDom.children[i];
const newNode = newDom.children[i];
if (currentNode.children.length && newNode.children.length > 0) {
diff(currentNode, newNode, changes);
}
if (currentNode.outerHTML !== newNode.outerHTML) {
const newNodeClone = newNode.cloneNode(true);
changes.removed.push(currentNode.cloneNode(true));
changes.added.push(newNodeClone);
currentNode.replaceWith(newNodeClone);
}
}
return [currentDom, changes];
}
const classNameFromTag = tag =>
tag
.split('-')
.map(part =>
part
.charAt(0)
.toUpperCase() +
part
.slice(1)
.toLowerCase())
.join('');
const attributeToCamelCase = (attr) =>
attr.split('-').map((part, index) =>
index === 0 ? part : part.charAt(0).toUpperCase() + part.slice(1)
).join('');
const init = async (el) => {
if (!el.tagName?.includes('-')) {
if (el.children) {
for (const child of el.children) {
await init(child);
}
}
return;
}
const tag = el.tagName.toLowerCase();
const href = document.querySelector('link[rel="components"]')?.href;
const path = el.getAttribute('module');
const module = await import(href || path);
if (!customElements.get(tag)) {
try {
customElements.define(tag, href ? module[classNameFromTag(tag)] : module.default);
await customElements.whenDefined(tag);
} catch (e) { console.error(`Could not initialize <${tag}>. Check that the component exist and that is has been imported. (${e.message})`); }
}
};
const render = element => {
if (element.componentWillRender.call(element)) {
const template = element.render.call(element);
// Create a temporary container for the new content
const tempContainer = document.createElement('div');
// Handle array of templates
if (Array.isArray(template)) {
const combinedFragment = document.createDocumentFragment();
template.forEach(fragment => {
if (fragment instanceof DocumentFragment) {
combinedFragment.append(...Array.from(fragment.children));
}
});
tempContainer.append(...Array.from(combinedFragment.children));
}
// Handle single template
else if (template instanceof DocumentFragment) {
tempContainer.append(...Array.from(template.children));
}
// Perform the diff and get the updated DOM
const [updatedDom, changes] = diff(element, tempContainer);
// Set up listeners for all new elements
changes.added.forEach(node => {
setupListeners(element, node);
});
if (template) {
element.componentDidRender.call(element);
}
}
}
export default class Domo extends HTMLElement {
constructor() {
super();
this.state = this.getInitialState();
this._renderPending = true; // Add this flag
new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.addedNodes) {
Array
.from(mutation.addedNodes)
.map(el => {
return !!init(el) && !!el.getAttributeNames && setupListeners(this, el)
})
}
if (mutation.type === 'attributes' && mutation.target.tagName.includes('-') && mutation.attributeName.match(/data-/)) {
const datasetKey = mutation.attributeName.replace('data-', '');
mutation.newValue = mutation.target.getAttribute(mutation.attributeName);
mutation.datasetKey = classNameFromTag(datasetKey);
mutation.datasetKey = mutation.datasetKey.charAt(0).toLowerCase() + mutation.datasetKey.slice(1);
mutation.target.didUpdateDataset(mutation);
}
});
}).observe(this, {attributes: true, childList: true, subtree: true, attributeOldValue: true});
init(this);
}
// Add this new lifecycle method
attributeChangedCallback(name, oldValue, newValue) {
if (this._renderPending) {
this._setupCallbacks();
}
}
connectedCallback() {
this._setupCallbacks();
if (this._renderPending) {
this._renderPending = false;
render(this);
}
}
_setupCallbacks() {
// Find the parent component instance
let parent = this.parentElement;
while (parent && !parent.tagName?.includes('-')) {
parent = parent.parentElement;
}
if (parent) {
// Get all cb- attributes
const cbAttributes = this.getAttributeNames()
.filter(key => key.match(/^(cb\-)/));
cbAttributes.forEach(attr => {
const callbackValue = this.getAttribute(attr);
const methodName = attributeToCamelCase(attr.replace(/^(cb\-)/, ''));
// Look for the method on the parent
if (parent[callbackValue] instanceof Function) {
this[methodName] = (...args) => parent[callbackValue].apply(parent, args);
} else {
// Look through parent's prototype methods
const parentMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(parent));
const matchingMethod = parentMethods.find(method =>
method === callbackValue ||
parent[method]?.name === callbackValue
);
if (matchingMethod) {
this[methodName] = (...args) => parent[matchingMethod].apply(parent, args);
}
}
});
}
}
setState(value) {
const newstate = JSON.stringify(value);
if (newstate === null) {
return true;
}
const oldstate = JSON.stringify(this.state);
if (oldstate !== newstate) {
this.state = Object.assign(this.state, JSON.parse(newstate));
this.stateDidChange();
this._renderPending = true;
render(this);
}
}
stateDidChange() { }
getInitialState() { return { } }
componentWillRender() { return true }
didUpdateDataset(mutation) { }
componentDidRender() { }
render() { }
}
export { init, html, diff };