@muscara/htmx-jsonata 0.0.5 → 0.0.7
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/README.md +9 -17
- package/db.json +38 -0
- package/htmx-jsonata.js +33 -5
- package/htmx-jsonata.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,30 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
[htmx extension](https://htmx.org/extensions) that provides [jsonata](https://jsonata.org) query functionality for htmx elements
|
|
4
4
|
|
|
5
|
-
add the following to your project to begin. **make sure you import after htmx and jsonata**
|
|
6
5
|
```html
|
|
7
6
|
<!-- ... include jsonata -->
|
|
8
7
|
<!-- ... include htmx -->
|
|
9
|
-
<script src="https://unpkg.com/@muscara/htmx-jsonata/htmx-jsonata.min.js"></script>
|
|
8
|
+
<script src="https://unpkg.com/@muscara/htmx-jsonata@0.0.6/htmx-jsonata.min.js"></script>
|
|
10
9
|
```
|
|
11
10
|
|
|
12
|
-
>
|
|
11
|
+
> [!TIP]
|
|
12
|
+
> make sure htmx-jsonata is imported after htmx and jsonata.
|
|
13
13
|
|
|
14
|
-
##
|
|
15
|
-
|
|
16
|
-
attributes:
|
|
17
|
-
|
|
18
|
-
- `jn-url` (required): the JSON API URL to fetch data from.
|
|
19
|
-
|
|
20
|
-
- `jn-expression`: jsonata expression to filter, transform, or query the JSON response.
|
|
14
|
+
## attributes
|
|
21
15
|
|
|
16
|
+
- `jn-url` (required): the url to fetch data from.
|
|
17
|
+
- `jn-query`: jsonata expression to filter and transform the data
|
|
22
18
|
- `jn-path`: specifies a path to nested data (supports dot notation).
|
|
19
|
+
- `jn-each`: marks an element as a template to be cloned for each item in an array response.
|
|
23
20
|
|
|
24
|
-
|
|
21
|
+
## try it
|
|
25
22
|
|
|
26
|
-
|
|
27
|
-
<div jn-url="https://jsonplaceholder.typicode.com/users" jn-expression="$[name='Leanne Graham']">
|
|
28
|
-
<h1 jn-path="name"></h1>
|
|
29
|
-
<p jn-path="phone"></p>
|
|
30
|
-
</div>
|
|
31
|
-
```
|
|
23
|
+
for live examples, use the [playground](https://htmx-jsonata.kevinmuscara.deno.net/)
|
package/db.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"users": [
|
|
3
|
+
{
|
|
4
|
+
"id": 1,
|
|
5
|
+
"name": "Kevin Muscara",
|
|
6
|
+
"username": "muscara",
|
|
7
|
+
"email": "kevin@muscara.com",
|
|
8
|
+
"address": {
|
|
9
|
+
"street": "Light road",
|
|
10
|
+
"suite": "Apt. 556",
|
|
11
|
+
"city": "Cincinnati",
|
|
12
|
+
"zipcode": "45245"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"events": [
|
|
17
|
+
{
|
|
18
|
+
"type": "login",
|
|
19
|
+
"user": "alice",
|
|
20
|
+
"duration": 15
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"type": "purchase",
|
|
24
|
+
"user": "bob",
|
|
25
|
+
"amount": 120
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"type": "login",
|
|
29
|
+
"user": "charlie",
|
|
30
|
+
"duration": 8
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"type": "login",
|
|
34
|
+
"user": "bob",
|
|
35
|
+
"duration": 20
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
}
|
package/htmx-jsonata.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function processJsonataElement(element, url) {
|
|
24
|
-
const jsonataExpression = element.getAttribute('jn-
|
|
24
|
+
const jsonataExpression = element.getAttribute('jn-query');
|
|
25
25
|
|
|
26
26
|
fetch(url)
|
|
27
27
|
.then(response => {
|
|
@@ -41,10 +41,6 @@
|
|
|
41
41
|
Promise.resolve(evaluateResult).then(evalResult => {
|
|
42
42
|
result = evalResult;
|
|
43
43
|
|
|
44
|
-
if (Array.isArray(result) && result.length === 1) {
|
|
45
|
-
result = result[0];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
44
|
populateElements(element, result);
|
|
49
45
|
});
|
|
50
46
|
} catch (error) {
|
|
@@ -61,6 +57,38 @@
|
|
|
61
57
|
}
|
|
62
58
|
|
|
63
59
|
function populateElements(element, result) {
|
|
60
|
+
const eachElement = element.querySelector('[jn-each]');
|
|
61
|
+
|
|
62
|
+
if (eachElement && Array.isArray(result)) {
|
|
63
|
+
const parent = eachElement.parentElement;
|
|
64
|
+
const template = eachElement.cloneNode(true);
|
|
65
|
+
parent.innerHTML = '';
|
|
66
|
+
|
|
67
|
+
result.forEach(item => {
|
|
68
|
+
const clone = template.cloneNode(true);
|
|
69
|
+
clone.removeAttribute('jn-each');
|
|
70
|
+
|
|
71
|
+
const pathElements = clone.querySelectorAll('[jn-path]');
|
|
72
|
+
pathElements.forEach(pathElt => {
|
|
73
|
+
const propertyName = pathElt.getAttribute('jn-path');
|
|
74
|
+
if (item && propertyName) {
|
|
75
|
+
const value = getNestedProperty(item, propertyName);
|
|
76
|
+
if (value !== undefined && value !== null) {
|
|
77
|
+
if (pathElt.tagName === 'INPUT' || pathElt.tagName === 'TEXTAREA') {
|
|
78
|
+
pathElt.value = value;
|
|
79
|
+
} else {
|
|
80
|
+
pathElt.textContent = value;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
parent.appendChild(clone);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
64
92
|
const propertyElements = element.querySelectorAll('[jn-path]');
|
|
65
93
|
|
|
66
94
|
propertyElements.forEach(propElt => {
|
package/htmx-jsonata.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){const t=window.jsonata||("undefined"!=typeof jsonata?jsonata:null);function e(e,
|
|
1
|
+
!function(){const t=window.jsonata||("undefined"!=typeof jsonata?jsonata:null);function e(e,o){const r=e.getAttribute("jn-query");fetch(o).then((t=>{if(!t.ok)throw new Error(`error: ${t.status}`);return t.json()})).then((o=>{let a=o;if(r)try{const c=t(r).evaluate(o);Promise.resolve(c).then((t=>{a=t,n(e,a)}))}catch(t){return void console.error("Error evaluating jsonata expression:",t)}else n(e,a)})).catch((t=>{console.error("Error fetching or processing jsonata data:",t)}))}function n(t,e){const n=t.querySelector("[jn-each]");if(n&&Array.isArray(e)){const t=n.parentElement,r=n.cloneNode(!0);return t.innerHTML="",void e.forEach((e=>{const n=r.cloneNode(!0);n.removeAttribute("jn-each");n.querySelectorAll("[jn-path]").forEach((t=>{const n=t.getAttribute("jn-path");if(e&&n){const r=o(e,n);null!=r&&("INPUT"===t.tagName||"TEXTAREA"===t.tagName?t.value=r:t.textContent=r)}})),t.appendChild(n)}))}t.querySelectorAll("[jn-path]").forEach((t=>{const n=t.getAttribute("jn-path");if(e&&n){const r=o(e,n);null!=r&&("INPUT"===t.tagName||"TEXTAREA"===t.tagName?t.value=r:t.textContent=r)}}))}function o(t,e){if(!t||!e)return;let n=t;const o=e.split(".");for(const t of o){const e=t.match(/^(.+?)\[(\d+)\]$/);if(e){const t=e[1],o=parseInt(e[2],10);if(!n||"object"!=typeof n||!(t in n))return;if(n=n[t],!(Array.isArray(n)&&o>=0&&o<n.length))return;n=n[o]}else{if(!n||"object"!=typeof n||!(t in n))return;n=n[t]}}return n}function r(){document.querySelectorAll("[jn-url]").forEach((t=>{const n=t.getAttribute("jn-url");n&&e(t,n)}))}t?("undefined"!=typeof htmx&&htmx.defineExtension("jsonata",{onEvent:function(t,n){if("htmx:afterProcessNode"===t){const t=n.detail.elt,o=t.getAttribute("jn-url");o&&e(t,o)}return!0}}),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",(()=>r())):r()):console.error("htmx-jsonata extension requires the jsonata library. Include it before this extension.")}();
|