@muscara/htmx-jsonata 0.0.4 → 0.0.6
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 +29 -14
- package/db.json +38 -0
- package/htmx-jsonata.js +38 -10
- package/htmx-jsonata.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,30 +2,45 @@
|
|
|
2
2
|
|
|
3
3
|
[htmx extension](https://htmx.org/extensions) that provides [jsonata](https://jsonata.org) query functionality for htmx elements
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
> [!TIP]
|
|
6
|
+
> make sure htmx-jsonata is imported after htmx and jsonata.
|
|
7
|
+
|
|
6
8
|
```html
|
|
7
9
|
<!-- ... include jsonata -->
|
|
8
10
|
<!-- ... include htmx -->
|
|
9
11
|
<script src="https://unpkg.com/@muscara/htmx-jsonata/htmx-jsonata.min.js"></script>
|
|
10
12
|
```
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
## usage
|
|
15
|
-
|
|
16
|
-
attributes:
|
|
14
|
+
## attributes
|
|
17
15
|
|
|
18
|
-
- `
|
|
16
|
+
- `jn-url` (required): the url to fetch data from.
|
|
17
|
+
- `jn-query`: jsonata expression to filter and transform the data
|
|
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.
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
## example
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
example use:
|
|
23
|
+
[sample API response](https://my-json-server.typicode.com/kevinmuscara/htmx-jsonata/events)
|
|
25
24
|
|
|
26
25
|
```html
|
|
27
|
-
<div
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
<div
|
|
27
|
+
jn-url="https://my-json-server.typicode.com/kevinmuscara/htmx-jsonata/events"
|
|
28
|
+
jn-query="
|
|
29
|
+
$[type='login'].{
|
|
30
|
+
'user': user,
|
|
31
|
+
'totalDuration': $sum(duration)
|
|
32
|
+
}
|
|
33
|
+
~> $distinct()"
|
|
34
|
+
>
|
|
35
|
+
<ul>
|
|
36
|
+
<li jn-each>
|
|
37
|
+
<span jn-path="user"></span>: <span jn-path="totalDuration"></span> min
|
|
38
|
+
</li>
|
|
39
|
+
</ul>
|
|
30
40
|
</div>
|
|
31
41
|
```
|
|
42
|
+
|
|
43
|
+
Output:
|
|
44
|
+
* alice: 15 min
|
|
45
|
+
* charlie: 8 min
|
|
46
|
+
* bob: 20 min
|
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
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
onEvent: function(name, evt) {
|
|
12
12
|
if (name === 'htmx:afterProcessNode') {
|
|
13
13
|
const elt = evt.detail.elt;
|
|
14
|
-
const jsonataUrl = elt.getAttribute('
|
|
14
|
+
const jsonataUrl = elt.getAttribute('jn-url');
|
|
15
15
|
|
|
16
16
|
if (jsonataUrl) processJsonataElement(elt, jsonataUrl)
|
|
17
17
|
}
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function processJsonataElement(element, url) {
|
|
24
|
-
const jsonataExpression = element.getAttribute('
|
|
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,10 +57,42 @@
|
|
|
61
57
|
}
|
|
62
58
|
|
|
63
59
|
function populateElements(element, result) {
|
|
64
|
-
const
|
|
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
|
+
|
|
92
|
+
const propertyElements = element.querySelectorAll('[jn-path]');
|
|
65
93
|
|
|
66
94
|
propertyElements.forEach(propElt => {
|
|
67
|
-
const propertyName = propElt.getAttribute('
|
|
95
|
+
const propertyName = propElt.getAttribute('jn-path');
|
|
68
96
|
|
|
69
97
|
if (result && propertyName) {
|
|
70
98
|
const value = getNestedProperty(result, propertyName);
|
|
@@ -115,9 +143,9 @@
|
|
|
115
143
|
}
|
|
116
144
|
|
|
117
145
|
function initializeJsonataElements() {
|
|
118
|
-
const elements = document.querySelectorAll('[
|
|
146
|
+
const elements = document.querySelectorAll('[jn-url]');
|
|
119
147
|
elements.forEach(elt => {
|
|
120
|
-
const url = elt.getAttribute('
|
|
148
|
+
const url = elt.getAttribute('jn-url');
|
|
121
149
|
if (url) processJsonataElement(elt, url);
|
|
122
150
|
});
|
|
123
151
|
}
|
package/htmx-jsonata.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){const t=window.jsonata||("undefined"!=typeof jsonata?jsonata:null);function e(e,o){const r=e.getAttribute("
|
|
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.")}();
|