@daz4126/helium 0.20.0 → 0.21.0
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/helium.js +22 -21
- package/package.json +1 -1
package/helium.js
CHANGED
|
@@ -32,13 +32,17 @@ window.helium = function() {
|
|
|
32
32
|
const $ = s => document.querySelector(s);
|
|
33
33
|
const html = s => Object.assign(document.createElement("template"),{innerHTML:s.trim()}).content.firstChild
|
|
34
34
|
|
|
35
|
-
const update = (data,
|
|
36
|
-
const
|
|
35
|
+
const update = (data,targets,actions,template) => {
|
|
36
|
+
const newTargets = [];
|
|
37
|
+
targets.forEach((target,i) => {
|
|
38
|
+
const element = target instanceof Node ? target : (HELIUM.refs.get(target.trim()) || $(target.trim()));
|
|
37
39
|
if(element){
|
|
38
|
-
const content =
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
const content = template ? template(data) : data;
|
|
41
|
+
actions[i] ? element[actions[i]=="replace"?"replaceWith":actions[i]](html(content)) : element.innerHTML = content;
|
|
42
|
+
newTargets.push(actions[i] ? content : element)
|
|
41
43
|
} else state[target] = data
|
|
44
|
+
})
|
|
45
|
+
return newTargets
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
const ajax = (u,m,o={},p={}) => {
|
|
@@ -64,7 +68,7 @@ fetch(u, {
|
|
|
64
68
|
}).then(d =>
|
|
65
69
|
d.t && "Turbo"
|
|
66
70
|
? Turbo.renderStreamMessage(d.d)
|
|
67
|
-
: update(d, o.target, o.loading ? "replace" : o.action, o.template)
|
|
71
|
+
: update(d, o.target, o.loading ? o.action.map(a => a && "replace") : o.action, o.template)
|
|
68
72
|
).catch(e => console.error("AJAX:", e.message));
|
|
69
73
|
}
|
|
70
74
|
|
|
@@ -224,15 +228,13 @@ function processElements(element) {
|
|
|
224
228
|
Object.assign(state, parseEx(value));
|
|
225
229
|
}
|
|
226
230
|
else if (name.startsWith(":") || name.startsWith("data-he-attr:")) {
|
|
227
|
-
|
|
228
|
-
deferredBindings.push({el, prop: name.slice(name.startsWith(":") ? 1 : 13), fn});
|
|
231
|
+
deferredBindings.push({el, prop: name.slice(name.startsWith(":") ? 1 : 13), fn: compile(value, true)});
|
|
229
232
|
}
|
|
230
233
|
else if (he(name, "ref")) {
|
|
231
234
|
HELIUM.refs.set("$" + value, el);
|
|
232
235
|
}
|
|
233
236
|
else if (he(name, "text", "html")) {
|
|
234
|
-
|
|
235
|
-
deferredBindings.push({el, prop: he(name, "text") ? "textContent" : "innerHTML", fn});
|
|
237
|
+
deferredBindings.push({el, prop: he(name, "text") ? "textContent" : "innerHTML", fn: compile(value, true)});
|
|
236
238
|
}
|
|
237
239
|
else if (he(name, "bind")) {
|
|
238
240
|
const event = (isCheckbox || isRadio || isSelect) ? "change" : "input";
|
|
@@ -241,7 +243,7 @@ function processElements(element) {
|
|
|
241
243
|
el.addEventListener(event, inputHandler);
|
|
242
244
|
if (!HELIUM.listeners.has(el)) HELIUM.listeners.set(el, []);
|
|
243
245
|
HELIUM.listeners.get(el).push({receiver: el, event, handler: inputHandler});
|
|
244
|
-
|
|
246
|
+
deferredBindings.push({el, prop, fn: compile(value, true)});
|
|
245
247
|
if (isCheckbox) el.checked = !!state[value];
|
|
246
248
|
else if (isRadio) el.checked = el.value == state[value];
|
|
247
249
|
else el.value = state[value] ?? "";
|
|
@@ -251,14 +253,13 @@ function processElements(element) {
|
|
|
251
253
|
deferredBindings.push({el, prop: "hidden", fn});
|
|
252
254
|
}
|
|
253
255
|
else if (he(name, "calculate")) {
|
|
254
|
-
|
|
255
|
-
const fn = compile(value, true);
|
|
256
|
-
deferredBindings.push({el, calc, prop: null, fn});
|
|
256
|
+
deferredBindings.push({el, calc: name.split(":")[1], prop: null, fn: compile(value, true)});
|
|
257
257
|
}
|
|
258
258
|
else if (he(name, "effect")) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
259
|
+
deferredBindings.push({el, prop: null, fn: compile(value, true), keys: name.split(":").slice(1)});
|
|
260
|
+
}
|
|
261
|
+
else if (he(name, "import")) {
|
|
262
|
+
value.split(",").map(s => s.trim()).forEach(v => state[v] = window[v]);
|
|
262
263
|
}
|
|
263
264
|
else if (he(name, "init")) {
|
|
264
265
|
initFn = compile(value, true);
|
|
@@ -286,7 +287,9 @@ function processElements(element) {
|
|
|
286
287
|
if (!mods.includes("outside") || !el.contains(e.target)) {
|
|
287
288
|
if (isHttpMethod) {
|
|
288
289
|
const getAttr = name => el.getAttribute(`data-he-${name}`) || el.getAttribute(`@${name}`);
|
|
289
|
-
const
|
|
290
|
+
const pairs = (getAttr('target') || "").split(",").map(p => p.split(":").map(s => s.trim()));
|
|
291
|
+
const target = pairs.map(([target]) => target);
|
|
292
|
+
const action = pairs.map(([, action]) => action);
|
|
290
293
|
const options = {
|
|
291
294
|
...(getAttr("options") && parseEx(getAttr("options") || "{}")),
|
|
292
295
|
...(target && { target }),
|
|
@@ -305,9 +308,7 @@ function processElements(element) {
|
|
|
305
308
|
}
|
|
306
309
|
const params = exFn(paramsAttr);
|
|
307
310
|
ajax(value, eventName.toUpperCase(), options, params);
|
|
308
|
-
} else
|
|
309
|
-
exFn(value);
|
|
310
|
-
}
|
|
311
|
+
} else exFn(value);
|
|
311
312
|
}
|
|
312
313
|
if (mods.includes("once")) receiver.removeEventListener(event, handler);
|
|
313
314
|
};
|