olum-compiler 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/core/app.js +0 -530
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "olum-compiler",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "A module for compiling/bundling olumjs projects",
|
|
5
5
|
"main": "bin/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -32,4 +32,4 @@
|
|
|
32
32
|
"magic-string": "^0.26.7",
|
|
33
33
|
"ws": "^8.5.0"
|
|
34
34
|
}
|
|
35
|
-
}
|
|
35
|
+
}
|
package/core/app.js
DELETED
|
@@ -1,530 +0,0 @@
|
|
|
1
|
-
window.olum = {
|
|
2
|
-
app: {}, // this is a must to access live changes
|
|
3
|
-
$emit(event, data) {
|
|
4
|
-
this.dispatchEvent(event, data);
|
|
5
|
-
},
|
|
6
|
-
dispatchEvent(event, data) {
|
|
7
|
-
window.dispatchEvent(new CustomEvent(event, { detail: data }));
|
|
8
|
-
},
|
|
9
|
-
mkElm(type, compName, compId) {
|
|
10
|
-
const el = document.createElement(type);
|
|
11
|
-
if (compName && compId) el.setAttribute("data-olum", JSON.stringify({ compName, compId }));
|
|
12
|
-
return el;
|
|
13
|
-
},
|
|
14
|
-
injectStyle(compName, cssContent) {
|
|
15
|
-
if (!cssContent || !cssContent.trim()) return;
|
|
16
|
-
const id = "olum-style-" + compName;
|
|
17
|
-
if (document.getElementById(id)) return;
|
|
18
|
-
const tag = document.createElement("style");
|
|
19
|
-
tag.id = id;
|
|
20
|
-
tag.textContent = cssContent;
|
|
21
|
-
document.head.appendChild(tag);
|
|
22
|
-
},
|
|
23
|
-
proxyHandler(obj, watcher, el) {
|
|
24
|
-
function mkHash(str) {
|
|
25
|
-
var hash = 0;
|
|
26
|
-
var i;
|
|
27
|
-
var char;
|
|
28
|
-
if (str.length === 0) return hash;
|
|
29
|
-
for (i = 0; i < str.length; i++) {
|
|
30
|
-
char = str.charCodeAt(i);
|
|
31
|
-
hash = (hash << 5) - hash + char;
|
|
32
|
-
hash |= 0; // Convert to 32bit integer
|
|
33
|
-
}
|
|
34
|
-
return hash;
|
|
35
|
-
}
|
|
36
|
-
var handler = {
|
|
37
|
-
get: function (obj, key) {
|
|
38
|
-
return obj[key];
|
|
39
|
-
},
|
|
40
|
-
set: function (obj, key, newVal) {
|
|
41
|
-
if (key === "__olum__") return false;
|
|
42
|
-
const oldVal = obj[key];
|
|
43
|
-
if (oldVal === newVal) return true;
|
|
44
|
-
obj[key] = newVal;
|
|
45
|
-
if (watcher && watcher[key] && typeof watcher[key] === "function") watcher[key](oldVal, newVal);
|
|
46
|
-
// todo prefix compName with instance e.g. you may have one component used twice so compName-1 & compName-2
|
|
47
|
-
const dataObj = { compName: obj.__olum__.compName, compId: obj.__olum__.compId };
|
|
48
|
-
const hash = mkHash(dataObj.compName + dataObj.compId);
|
|
49
|
-
dataObj.hash = hash;
|
|
50
|
-
|
|
51
|
-
window.olum.$emit("updateOlumComp", dataObj);
|
|
52
|
-
return true;
|
|
53
|
-
},
|
|
54
|
-
deleteProperty: function (obj, key) {
|
|
55
|
-
if (key === "__olum__") return false;
|
|
56
|
-
delete obj[key];
|
|
57
|
-
// todo prefix compName with instance e.g. you may have one component used twice so compName-1 & compName-2
|
|
58
|
-
const dataObj = { compName: obj.__olum__.compName, compId: obj.__olum__.compId };
|
|
59
|
-
const hash = mkHash(dataObj.compName + dataObj.compId);
|
|
60
|
-
dataObj.hash = hash;
|
|
61
|
-
|
|
62
|
-
window.olum.$emit("updateOlumComp", dataObj);
|
|
63
|
-
return true;
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
return new Proxy(obj, handler);
|
|
67
|
-
},
|
|
68
|
-
proxyHandlerForStore(obj, originalProxy) {
|
|
69
|
-
const handler = {
|
|
70
|
-
get: function (obj, key) {
|
|
71
|
-
return originalProxy[key];
|
|
72
|
-
},
|
|
73
|
-
set: function (obj, key, val) {
|
|
74
|
-
obj[key] = val;
|
|
75
|
-
originalProxy[key] = val;
|
|
76
|
-
return true;
|
|
77
|
-
},
|
|
78
|
-
deleteProperty: function (obj, key) {
|
|
79
|
-
delete obj[key];
|
|
80
|
-
delete originalProxy[key];
|
|
81
|
-
return true;
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
return new Proxy(obj, handler);
|
|
85
|
-
},
|
|
86
|
-
clean(fragment) {
|
|
87
|
-
const str = String(fragment).trim();
|
|
88
|
-
if (str === "null") return null;
|
|
89
|
-
return str;
|
|
90
|
-
},
|
|
91
|
-
// #2 (escape-by-default): HTML-escape a value before it is interpolated into a template.
|
|
92
|
-
// The compiler wraps every text/attribute interpolation `{expr}` in `olum.esc(expr)` so a
|
|
93
|
-
// user-supplied string (a todo title, a comment, anything) can't inject markup/scripts (XSS)
|
|
94
|
-
// or visually break rendering when it contains <, >, &, or quotes.
|
|
95
|
-
// - null/undefined render as "" (instead of the literal text "null"/"undefined").
|
|
96
|
-
// - To render trusted HTML on purpose, opt out explicitly with `olum.raw(html)` (below);
|
|
97
|
-
// esc() detects the marker it returns and passes the HTML through unescaped.
|
|
98
|
-
esc(value) {
|
|
99
|
-
if (value === null || value === undefined) return "";
|
|
100
|
-
if (value && value.__olumRaw === true) return value.html; // explicit raw-HTML opt-in
|
|
101
|
-
return String(value)
|
|
102
|
-
.replace(/&/g, "&") // must run first so the entities below aren't double-escaped
|
|
103
|
-
.replace(/</g, "<")
|
|
104
|
-
.replace(/>/g, ">")
|
|
105
|
-
.replace(/"/g, """)
|
|
106
|
-
.replace(/'/g, "'");
|
|
107
|
-
},
|
|
108
|
-
// #2 opt-in escape hatch: mark a string as trusted raw HTML so `esc()` leaves it untouched.
|
|
109
|
-
// Usage in a template: {olum.raw(props.richText)}. Use it ONLY on HTML you control or have
|
|
110
|
-
// already sanitized — this is the deliberate, greppable way to bypass auto-escaping.
|
|
111
|
-
raw(html) {
|
|
112
|
-
return { __olumRaw: true, html: html == null ? "" : String(html) };
|
|
113
|
-
},
|
|
114
|
-
eventsHandler(el, nodes, compName, methodsRefObj) {
|
|
115
|
-
function event(item, str, modifiers) {
|
|
116
|
-
const eventName = str.split("|")[0];
|
|
117
|
-
str = str.split("|").slice(1).join();
|
|
118
|
-
|
|
119
|
-
let methods = str.split("&");
|
|
120
|
-
const data = methods.map((chunk) => {
|
|
121
|
-
const chunks = chunk.split("=");
|
|
122
|
-
const name = chunks[0];
|
|
123
|
-
const args = JSON.parse(chunks.slice(1).join(""));
|
|
124
|
-
return { args, methodName: name };
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
const opts = {
|
|
128
|
-
once: false,
|
|
129
|
-
passive: false,
|
|
130
|
-
capture: false,
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
if (modifiers && modifiers.length) {
|
|
134
|
-
if (modifiers.includes("once")) opts.once = true;
|
|
135
|
-
if (modifiers.includes("passive")) opts.passive = true;
|
|
136
|
-
if (modifiers.includes("capture")) opts.capture = true;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
item.addEventListener(
|
|
140
|
-
eventName.slice(2),
|
|
141
|
-
(e) => {
|
|
142
|
-
if (modifiers && modifiers.length) {
|
|
143
|
-
if (modifiers.includes("prevent")) e.preventDefault();
|
|
144
|
-
if (modifiers.includes("stop")) e.stopPropagation();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function init() {
|
|
148
|
-
data.forEach((obj) => {
|
|
149
|
-
if (methodsRefObj[obj.methodName]) {
|
|
150
|
-
if (obj.args.length) {
|
|
151
|
-
const eventIndex = obj.args.indexOf("$event");
|
|
152
|
-
if (eventIndex !== -1) obj.args.splice(eventIndex, 1, e);
|
|
153
|
-
methodsRefObj[obj.methodName](...obj.args);
|
|
154
|
-
} else {
|
|
155
|
-
methodsRefObj[obj.methodName](e);
|
|
156
|
-
}
|
|
157
|
-
} else {
|
|
158
|
-
console.warn("olum: can't access the method");
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (modifiers && modifiers.length && modifiers.includes("self")) {
|
|
164
|
-
if (e.target === item) init();
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
init();
|
|
168
|
-
},
|
|
169
|
-
opts
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
item.removeAttribute("data-o-event");
|
|
173
|
-
item.removeAttribute("data-o-event-mode");
|
|
174
|
-
// item.removeAttribute("data-child-of");
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
nodes.forEach((node) => {
|
|
178
|
-
const hasEvent = olum.clean(node.getAttribute("data-o-event"));
|
|
179
|
-
const hasMode = olum.clean(node.getAttribute("data-o-event-mode"));
|
|
180
|
-
const mode = hasMode && hasMode.trim() !== "" ? hasMode.split(".") : [];
|
|
181
|
-
if (hasEvent) event(node, hasEvent, mode);
|
|
182
|
-
});
|
|
183
|
-
},
|
|
184
|
-
stylesHandler(el, nodes, compName) {
|
|
185
|
-
function setStyles(item, obj, keys) {
|
|
186
|
-
let str = "";
|
|
187
|
-
keys.forEach((key) => {
|
|
188
|
-
const val = obj[key];
|
|
189
|
-
if (val) {
|
|
190
|
-
// console.log({ key, val });
|
|
191
|
-
str += key + ": " + val + "; ";
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
str = str.trim();
|
|
195
|
-
// console.log(str);
|
|
196
|
-
if (str !== "") item.setAttribute("style", str);
|
|
197
|
-
item.removeAttribute("data-o-style");
|
|
198
|
-
// item.removeAttribute("data-child-of");
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function style(item, str) {
|
|
202
|
-
const obj = JSON.parse(str);
|
|
203
|
-
const keys = Object.keys(obj);
|
|
204
|
-
if (keys.length) {
|
|
205
|
-
// console.log("directObj: ", obj);
|
|
206
|
-
setStyles(item, obj, keys);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
nodes.forEach((node) => {
|
|
211
|
-
const hasStyle = olum.clean(node.getAttribute("data-o-style"));
|
|
212
|
-
if (hasStyle) style(node, hasStyle);
|
|
213
|
-
});
|
|
214
|
-
},
|
|
215
|
-
handleMarkup(compName, id, el, methods) {
|
|
216
|
-
el.setAttribute("data-child-of", compName);
|
|
217
|
-
el.setAttribute("data-o-" + id, "");
|
|
218
|
-
var nodes = el.querySelectorAll("*");
|
|
219
|
-
nodes.forEach((node) => {
|
|
220
|
-
node.setAttribute("data-child-of", compName);
|
|
221
|
-
node.setAttribute("data-o-" + id, "");
|
|
222
|
-
});
|
|
223
|
-
this.eventsHandler(el, nodes, compName, methods);
|
|
224
|
-
this.stylesHandler(el, nodes, compName);
|
|
225
|
-
return el;
|
|
226
|
-
},
|
|
227
|
-
isObj(obj) {
|
|
228
|
-
return obj !== null && typeof obj === "object";
|
|
229
|
-
},
|
|
230
|
-
isFullArr(arr) {
|
|
231
|
-
return !!(this.isObj(arr) && Array.isArray(arr) && arr.length);
|
|
232
|
-
},
|
|
233
|
-
isFullObj(obj) {
|
|
234
|
-
return !!(this.isObj(obj) && Array.isArray(Object.keys(obj)) && Object.keys(obj).length);
|
|
235
|
-
},
|
|
236
|
-
createStore(entry) {
|
|
237
|
-
const store = {};
|
|
238
|
-
const map = [];
|
|
239
|
-
store[entry.__OLUM__.compName] = entry; // bind root component
|
|
240
|
-
|
|
241
|
-
// bind children components
|
|
242
|
-
const recursive = (comp) => {
|
|
243
|
-
if (this.isFullArr(comp.__OLUM__.components)) {
|
|
244
|
-
comp.__OLUM__.components.forEach((item) => {
|
|
245
|
-
var key = Object.keys(item)[0];
|
|
246
|
-
var newComp = item[key]();
|
|
247
|
-
if (!store[key]) store[key] = newComp;
|
|
248
|
-
|
|
249
|
-
const obj = { name: key, children: newComp.__OLUM__.components.map((obj) => Object.keys(obj)[0]) };
|
|
250
|
-
map.push(obj);
|
|
251
|
-
|
|
252
|
-
if (this.isFullArr(obj.children)) recursive(newComp);
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
recursive(entry);
|
|
257
|
-
|
|
258
|
-
return { store, map };
|
|
259
|
-
},
|
|
260
|
-
|
|
261
|
-
// direct <olum> placeholders of a container: those with no <olum> ancestor inside the container.
|
|
262
|
-
// (deeper placeholders belong to a nested component and are handled when that component renders)
|
|
263
|
-
directOlums(container) {
|
|
264
|
-
return Array.prototype.slice.call(container.querySelectorAll("olum")).filter((p) => {
|
|
265
|
-
const anc = p.parentElement && p.parentElement.closest && p.parentElement.closest("olum");
|
|
266
|
-
return !anc || !container.contains(anc);
|
|
267
|
-
});
|
|
268
|
-
},
|
|
269
|
-
buildTree(comp, store, compKey) {
|
|
270
|
-
const rootElm = comp.__OLUM__.getElm;
|
|
271
|
-
if (!rootElm) return null;
|
|
272
|
-
const self = this;
|
|
273
|
-
// lazy global factory registry: name -> factory. Accumulated from every instance's components map
|
|
274
|
-
// as we descend, so slot-passed and cross-file components resolve regardless of where they were declared.
|
|
275
|
-
const registry = window.olum.app.registry || (window.olum.app.registry = {});
|
|
276
|
-
|
|
277
|
-
function renderChildren(containerComp, containerKey, containerElm) {
|
|
278
|
-
if (containerComp.__OLUM__.components) Object.assign(registry, containerComp.__OLUM__.components);
|
|
279
|
-
const placeholders = self.directOlums(containerElm);
|
|
280
|
-
const occ = {}; // per-name occurrence counter -> positional instance keys, stable across re-renders
|
|
281
|
-
placeholders.forEach((placeholder) => {
|
|
282
|
-
const name = placeholder.getAttribute("name");
|
|
283
|
-
const factory = registry[name] || (containerComp.__OLUM__.components && containerComp.__OLUM__.components[name]);
|
|
284
|
-
if (!factory) {
|
|
285
|
-
console.warn("olum: couldn't find " + name + " Component while building the tree!");
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
// #3 (keyed reconciliation): a placeholder rendered inside a keyed <for> carries data-o-key
|
|
289
|
-
// (the evaluated `key={...}` value for that item). Keyed instances are stored under
|
|
290
|
-
// "...Name@<key>", so the SAME instance — and therefore its state and DOM — is reused for a
|
|
291
|
-
// given item no matter where it moves, is inserted, or is removed in the list. Without a key
|
|
292
|
-
// we fall back to positional "...Name#<i>" keys, where state follows position, not identity
|
|
293
|
-
// (delete the middle item of a list and the survivors inherit the wrong neighbour's state).
|
|
294
|
-
const keyVal = placeholder.getAttribute("data-o-key");
|
|
295
|
-
let instanceKey;
|
|
296
|
-
if (keyVal !== null && keyVal !== "") {
|
|
297
|
-
instanceKey = containerKey + ">" + name + "@" + keyVal;
|
|
298
|
-
} else {
|
|
299
|
-
occ[name] = occ[name] === undefined ? 0 : occ[name] + 1;
|
|
300
|
-
instanceKey = containerKey + ">" + name + "#" + occ[name];
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// reuse the instance from a previous render (preserves its state) or mint a fresh one
|
|
304
|
-
let child = store[instanceKey];
|
|
305
|
-
if (!child) {
|
|
306
|
-
child = factory(instanceKey);
|
|
307
|
-
store[instanceKey] = child;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// handle props
|
|
311
|
-
child.parentCompName = containerKey;
|
|
312
|
-
const propsJson = placeholder.getAttribute("data-o-props");
|
|
313
|
-
child.incomingProps = propsJson ? JSON.parse(decodeURIComponent(propsJson)) : {};
|
|
314
|
-
const srcStr = placeholder.getAttribute("data-o-props-src") || "";
|
|
315
|
-
child.incomingPropSources = {};
|
|
316
|
-
if (srcStr) srcStr.split("|").forEach((pair) => {
|
|
317
|
-
const parts = pair.split(":"); // propKey:kind:srcKey
|
|
318
|
-
const propKey = parts[0], kind = parts[1], srcKey = parts[2];
|
|
319
|
-
if (propKey && kind && srcKey) child.incomingPropSources[propKey] = { kind, key: srcKey };
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
// handle slot/children — must be set before getElm so ${children} resolves in the child's template
|
|
323
|
-
child.children = placeholder.innerHTML.trim();
|
|
324
|
-
|
|
325
|
-
const elm = child.__OLUM__.getElm;
|
|
326
|
-
if (elm) {
|
|
327
|
-
elm.setAttribute("data-o-if", placeholder.getAttribute("if") ? placeholder.getAttribute("if") : "olum-no-condition"); // display if condition value (truthy, falsy)
|
|
328
|
-
placeholder.replaceWith(elm);
|
|
329
|
-
renderChildren(child, instanceKey, elm); // recurse into this child's own subtree
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
renderChildren(comp, compKey, rootElm);
|
|
335
|
-
return rootElm;
|
|
336
|
-
},
|
|
337
|
-
getInnerNames(entry) {
|
|
338
|
-
const comps = [];
|
|
339
|
-
const map = window.olum.app.map;
|
|
340
|
-
if (map) {
|
|
341
|
-
map.find((obj) => {
|
|
342
|
-
if (obj.name == entry) obj.children.forEach((child) => comps.push(child));
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
function recursive(num) {
|
|
347
|
-
const child = comps[num];
|
|
348
|
-
map.forEach((obj) => {
|
|
349
|
-
if (obj.name == child) obj.children.forEach((item) => comps.push(item));
|
|
350
|
-
});
|
|
351
|
-
if (num + 1 <= comps.length) recursive(num + 1);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (comps.length) recursive(0);
|
|
355
|
-
|
|
356
|
-
// console.warn("innerComps: ", comps);
|
|
357
|
-
return comps;
|
|
358
|
-
},
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
class Olum {
|
|
362
|
-
root = null;
|
|
363
|
-
$(s) {
|
|
364
|
-
this.root = document.querySelector(s);
|
|
365
|
-
return this;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
use(comp) {
|
|
369
|
-
const entry = comp();
|
|
370
|
-
const { store, rootKey } = this.share(entry);
|
|
371
|
-
const tree = window.olum.buildTree(entry, store, rootKey);
|
|
372
|
-
if (!tree) return console.warn("olum: couldn't build tree!");
|
|
373
|
-
|
|
374
|
-
this.setupListeners(store); // unmounted hook & state system
|
|
375
|
-
|
|
376
|
-
this.root.append(tree); // bind full comps tree
|
|
377
|
-
// handle mounted hook
|
|
378
|
-
if (entry.hooks.mounted) entry.hooks.mounted(); // force mounting parent component (entry point comp) regardless of the data-o-if value
|
|
379
|
-
entry.hooks.isMounted = true;
|
|
380
|
-
// mount every instance created during buildTree (keyed by runtime instance key)
|
|
381
|
-
Object.keys(store).forEach((key) => {
|
|
382
|
-
if (key === rootKey) return;
|
|
383
|
-
const c = store[key];
|
|
384
|
-
if (!c || !c.el) return;
|
|
385
|
-
const ifConValue = c.el.getAttribute("data-o-if");
|
|
386
|
-
if (!ifConValue) {
|
|
387
|
-
// don't render comp because it has falsy value
|
|
388
|
-
} else {
|
|
389
|
-
if (["olum-no-condition", "true"].includes(ifConValue)) {
|
|
390
|
-
if (c.hooks.mounted) c.hooks.mounted();
|
|
391
|
-
c.hooks.isMounted = true;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
getPath(el, root) {
|
|
398
|
-
const path = [];
|
|
399
|
-
let cur = el;
|
|
400
|
-
while (cur && cur !== root) {
|
|
401
|
-
const parent = cur.parentElement;
|
|
402
|
-
if (!parent) break;
|
|
403
|
-
const siblings = Array.from(parent.children).filter(c => c.tagName === cur.tagName);
|
|
404
|
-
path.unshift({ tag: cur.tagName, index: siblings.indexOf(cur) });
|
|
405
|
-
cur = parent;
|
|
406
|
-
}
|
|
407
|
-
return path;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
findByPath(root, path) {
|
|
411
|
-
let cur = root;
|
|
412
|
-
for (const step of path) {
|
|
413
|
-
const siblings = Array.from(cur.children).filter(c => c.tagName === step.tag);
|
|
414
|
-
cur = siblings[step.index];
|
|
415
|
-
if (!cur) return null;
|
|
416
|
-
}
|
|
417
|
-
return cur;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
setupListeners(store) {
|
|
421
|
-
function mkHash(str) {
|
|
422
|
-
var hash = 0;
|
|
423
|
-
var i;
|
|
424
|
-
var char;
|
|
425
|
-
if (str.length === 0) return hash;
|
|
426
|
-
for (i = 0; i < str.length; i++) {
|
|
427
|
-
char = str.charCodeAt(i);
|
|
428
|
-
hash = (hash << 5) - hash + char;
|
|
429
|
-
hash |= 0;
|
|
430
|
-
}
|
|
431
|
-
return hash;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
// const snapshots = {}; // persists across re-renders: { [compName]: Map<pathKey, entry> }
|
|
435
|
-
|
|
436
|
-
window.addEventListener("updateOlumComp", (e) => {
|
|
437
|
-
if (e && e.detail && e.detail.compName && e.detail.compId && e.detail.hash) {
|
|
438
|
-
if (e.detail.hash !== mkHash(e.detail.compName + e.detail.compId)) return;
|
|
439
|
-
const comp = store[e.detail.compName];
|
|
440
|
-
if (!comp || !comp.el) return;
|
|
441
|
-
if (!document.body.contains(comp.el)) return; // comp was unmounted; ignore stale state updates
|
|
442
|
-
|
|
443
|
-
const compName = e.detail.compName;
|
|
444
|
-
|
|
445
|
-
// snapshot input values of child components currently in DOM
|
|
446
|
-
// only child components (data-child-of !== compName) so we don't overwrite state inputs with stale values
|
|
447
|
-
// entries not updated here (hidden components) keep their previous snapshot value intact
|
|
448
|
-
// if (!snapshots[compName]) snapshots[compName] = new Map();
|
|
449
|
-
// comp.el.querySelectorAll("input, textarea, select").forEach(field => {
|
|
450
|
-
// const childOf = field.getAttribute("data-child-of");
|
|
451
|
-
// if (childOf && childOf !== compName) {
|
|
452
|
-
// const path = this.getPath(field, comp.el);
|
|
453
|
-
// const entry = { path };
|
|
454
|
-
// if (field.type === "checkbox" || field.type === "radio") entry.checked = field.checked;
|
|
455
|
-
// else entry.value = field.value;
|
|
456
|
-
// snapshots[compName].set(JSON.stringify(path), entry);
|
|
457
|
-
// }
|
|
458
|
-
// });
|
|
459
|
-
|
|
460
|
-
const innerNames = Object.keys(store).filter(name => name !== compName);
|
|
461
|
-
const prevMounted = {};
|
|
462
|
-
innerNames.forEach((name) => {
|
|
463
|
-
const c = store[name];
|
|
464
|
-
if (c) prevMounted[name] = c.hooks.isMounted;
|
|
465
|
-
});
|
|
466
|
-
|
|
467
|
-
// get current active element location/path before re-render (buildTree)
|
|
468
|
-
const activeEl = document.activeElement;
|
|
469
|
-
const selStart = activeEl && activeEl.selectionStart != null ? activeEl.selectionStart : null;
|
|
470
|
-
const selEnd = activeEl && activeEl.selectionEnd != null ? activeEl.selectionEnd : null;
|
|
471
|
-
const activePath = (activeEl && comp.el.contains(activeEl)) ? this.getPath(activeEl, comp.el) : null;
|
|
472
|
-
|
|
473
|
-
const treeElm = window.olum.buildTree(comp, store, compName);
|
|
474
|
-
if (!treeElm) return console.warn("olum: couldn't build tree!");
|
|
475
|
-
comp.el.replaceWith(treeElm);
|
|
476
|
-
|
|
477
|
-
// restore child component input values after rebuild
|
|
478
|
-
// snapshots[compName].forEach(({ path, value, checked }) => {
|
|
479
|
-
// const field = this.findByPath(treeElm, path);
|
|
480
|
-
// if (!field) return; // component is hidden in this render, skip
|
|
481
|
-
// if (checked !== undefined) field.checked = checked;
|
|
482
|
-
// else if (value !== undefined) field.value = value;
|
|
483
|
-
// });
|
|
484
|
-
|
|
485
|
-
// load prev active element after re-render (buildTree) to focus (e.g. input) or restore its data (e.g. forms)
|
|
486
|
-
if (activePath && activePath.length) {
|
|
487
|
-
const toRestore = this.findByPath(treeElm, activePath);
|
|
488
|
-
if (toRestore) {
|
|
489
|
-
if (toRestore.focus && typeof toRestore.focus == "function") toRestore.focus();
|
|
490
|
-
if (selStart !== null && toRestore.setSelectionRange && typeof toRestore.setSelectionRange == "function") toRestore.setSelectionRange(selStart, selEnd);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
// after rebuild, call lifecycle hooks for any inner component that changed visibility.
|
|
495
|
-
// recompute keys here so instances created during this rebuild (e.g. a grown loop) are included.
|
|
496
|
-
const afterNames = Object.keys(store).filter(name => name !== compName);
|
|
497
|
-
afterNames.forEach((name) => {
|
|
498
|
-
const c = store[name];
|
|
499
|
-
if (!c) return;
|
|
500
|
-
const isInDOM = document.body.contains(c.el);
|
|
501
|
-
if (prevMounted[name] && !isInDOM) {
|
|
502
|
-
// was mounted, now removed from DOM → unMounted
|
|
503
|
-
if (c.hooks.unMounted && !c.hooks.isUnMounted) {
|
|
504
|
-
c.hooks.unMounted();
|
|
505
|
-
c.hooks.isUnMounted = true;
|
|
506
|
-
c.hooks.isMounted = false;
|
|
507
|
-
}
|
|
508
|
-
} else if (!prevMounted[name] && isInDOM) {
|
|
509
|
-
// was not mounted, now in DOM → mounted
|
|
510
|
-
if (c.hooks.mounted && !c.hooks.isMounted) {
|
|
511
|
-
c.hooks.mounted();
|
|
512
|
-
c.hooks.isMounted = true;
|
|
513
|
-
c.hooks.isUnMounted = false;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
});
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
share(entry) {
|
|
522
|
-
// store now holds live instances keyed by runtime instance key; buildTree populates it lazily.
|
|
523
|
-
// registry holds name->factory and is accumulated as the tree is walked.
|
|
524
|
-
const store = {};
|
|
525
|
-
const rootKey = entry.__OLUM__.compName;
|
|
526
|
-
store[rootKey] = entry;
|
|
527
|
-
Object.assign(window.olum.app, { store, registry: {} }); // this is a must to access live changes
|
|
528
|
-
return { store, rootKey };
|
|
529
|
-
}
|
|
530
|
-
}
|