@daz4126/helium 0.6.0 → 0.7.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 +25 -8
- package/package.json +1 -1
package/helium.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
let ue = true;
|
|
1
2
|
export default function helium(data = {}) {
|
|
2
3
|
const root =
|
|
3
4
|
document.querySelector("[\\@helium]") ||
|
|
4
5
|
document.querySelector("[data-helium]") ||
|
|
5
6
|
document.body;
|
|
7
|
+
if (Object.keys(data).length > 0)
|
|
8
|
+
document
|
|
9
|
+
.querySelectorAll("[data-helium-processed]")
|
|
10
|
+
.forEach((el) => el.removeAttribute("data-helium-processed"));
|
|
6
11
|
const [bindings, refs] = [new Map(), new Map()];
|
|
7
12
|
const $ = (selector) => document.querySelector(selector);
|
|
8
13
|
const html = (string) => {
|
|
@@ -12,6 +17,7 @@ export default function helium(data = {}) {
|
|
|
12
17
|
};
|
|
13
18
|
|
|
14
19
|
const ajax = (url, method, target, options) => {
|
|
20
|
+
console.log(url, method, target, options);
|
|
15
21
|
fetch(url, {
|
|
16
22
|
method,
|
|
17
23
|
headers: { "Content-Type": "application/json" },
|
|
@@ -22,10 +28,13 @@ export default function helium(data = {}) {
|
|
|
22
28
|
? res.json()
|
|
23
29
|
: res.text(),
|
|
24
30
|
)
|
|
25
|
-
.then((data) =>
|
|
31
|
+
.then((data) => {
|
|
32
|
+
state[target] = data;
|
|
33
|
+
});
|
|
26
34
|
};
|
|
27
35
|
const get = (url, prop) => ajax(url, "GET", prop);
|
|
28
36
|
const post = (url, params, prop) => ajax(url, "POST", prop, params);
|
|
37
|
+
const put = (url, params, prop) => ajax(url, "PUT", prop, params);
|
|
29
38
|
const patch = (url, params, prop) => ajax(url, "PATCH", prop, params);
|
|
30
39
|
const del = (url, params, prop) => ajax(url, "DELETE", prop, params);
|
|
31
40
|
|
|
@@ -57,6 +66,7 @@ export default function helium(data = {}) {
|
|
|
57
66
|
html,
|
|
58
67
|
get,
|
|
59
68
|
post,
|
|
69
|
+
put,
|
|
60
70
|
patch,
|
|
61
71
|
del,
|
|
62
72
|
...Object.values(data),
|
|
@@ -84,6 +94,7 @@ export default function helium(data = {}) {
|
|
|
84
94
|
"$html",
|
|
85
95
|
"$get",
|
|
86
96
|
"$post",
|
|
97
|
+
"$put",
|
|
87
98
|
"$patch",
|
|
88
99
|
"$delete",
|
|
89
100
|
...Object.keys(data),
|
|
@@ -96,7 +107,7 @@ export default function helium(data = {}) {
|
|
|
96
107
|
}
|
|
97
108
|
|
|
98
109
|
// processElements now returns an array of newly-created binding objects
|
|
99
|
-
function processElements(element) {
|
|
110
|
+
function processElements(element, addEvents = ue) {
|
|
100
111
|
const newlyAddedBindings = [];
|
|
101
112
|
|
|
102
113
|
// quick guard: avoid re-processing same node
|
|
@@ -104,12 +115,10 @@ export default function helium(data = {}) {
|
|
|
104
115
|
element.nodeType === 1 &&
|
|
105
116
|
element.hasAttribute &&
|
|
106
117
|
element.hasAttribute("data-helium-processed")
|
|
107
|
-
)
|
|
118
|
+
)
|
|
108
119
|
return newlyAddedBindings;
|
|
109
|
-
|
|
110
|
-
if (element.nodeType === 1 && element.setAttribute) {
|
|
120
|
+
if (element.nodeType === 1 && element.setAttribute)
|
|
111
121
|
element.setAttribute("data-helium-processed", "");
|
|
112
|
-
}
|
|
113
122
|
|
|
114
123
|
const heliumElements = [
|
|
115
124
|
element,
|
|
@@ -164,6 +173,7 @@ export default function helium(data = {}) {
|
|
|
164
173
|
html,
|
|
165
174
|
get,
|
|
166
175
|
post,
|
|
176
|
+
put,
|
|
167
177
|
patch,
|
|
168
178
|
del,
|
|
169
179
|
...Object.values(data),
|
|
@@ -256,12 +266,16 @@ export default function helium(data = {}) {
|
|
|
256
266
|
html,
|
|
257
267
|
get,
|
|
258
268
|
post,
|
|
269
|
+
put,
|
|
259
270
|
patch,
|
|
260
271
|
del,
|
|
261
272
|
...Object.values(data),
|
|
262
273
|
...[...refs.values()],
|
|
263
274
|
);
|
|
264
|
-
} else if (
|
|
275
|
+
} else if (
|
|
276
|
+
(name.startsWith("@") || name.startsWith("data-he-on")) &&
|
|
277
|
+
addEvents
|
|
278
|
+
) {
|
|
265
279
|
// event handling — keep this as before (register listeners)
|
|
266
280
|
const [eventName, ...modifiers] = name
|
|
267
281
|
.slice(name.startsWith("@") ? 1 : 10)
|
|
@@ -318,6 +332,7 @@ export default function helium(data = {}) {
|
|
|
318
332
|
html,
|
|
319
333
|
get,
|
|
320
334
|
post,
|
|
335
|
+
put,
|
|
321
336
|
patch,
|
|
322
337
|
del,
|
|
323
338
|
...Object.values(data),
|
|
@@ -347,7 +362,7 @@ export default function helium(data = {}) {
|
|
|
347
362
|
node.nodeType === 1 &&
|
|
348
363
|
!node.hasAttribute("data-helium-processed")
|
|
349
364
|
) {
|
|
350
|
-
const newly = processElements(node);
|
|
365
|
+
const newly = processElements(node, false);
|
|
351
366
|
// apply only the bindings we just created for this node
|
|
352
367
|
newly.forEach(applyBinding);
|
|
353
368
|
}
|
|
@@ -358,5 +373,7 @@ export default function helium(data = {}) {
|
|
|
358
373
|
|
|
359
374
|
const initialBindings = processElements(root);
|
|
360
375
|
for (const [key, items] of bindings.entries()) items.forEach(applyBinding);
|
|
376
|
+
ue = false;
|
|
361
377
|
}
|
|
378
|
+
|
|
362
379
|
helium();
|