@muscara/htmx-jsonata 0.0.4 → 0.0.5
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 +6 -6
- package/htmx-jsonata.js +6 -6
- package/htmx-jsonata.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,17 +15,17 @@ add the following to your project to begin. **make sure you import after htmx an
|
|
|
15
15
|
|
|
16
16
|
attributes:
|
|
17
17
|
|
|
18
|
-
- `
|
|
18
|
+
- `jn-url` (required): the JSON API URL to fetch data from.
|
|
19
19
|
|
|
20
|
-
- `
|
|
20
|
+
- `jn-expression`: jsonata expression to filter, transform, or query the JSON response.
|
|
21
21
|
|
|
22
|
-
- `
|
|
22
|
+
- `jn-path`: specifies a path to nested data (supports dot notation).
|
|
23
23
|
|
|
24
24
|
example use:
|
|
25
25
|
|
|
26
26
|
```html
|
|
27
|
-
<div
|
|
28
|
-
<h1
|
|
29
|
-
<p
|
|
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
30
|
</div>
|
|
31
31
|
```
|
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-expression');
|
|
25
25
|
|
|
26
26
|
fetch(url)
|
|
27
27
|
.then(response => {
|
|
@@ -61,10 +61,10 @@
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
function populateElements(element, result) {
|
|
64
|
-
const propertyElements = element.querySelectorAll('[
|
|
64
|
+
const propertyElements = element.querySelectorAll('[jn-path]');
|
|
65
65
|
|
|
66
66
|
propertyElements.forEach(propElt => {
|
|
67
|
-
const propertyName = propElt.getAttribute('
|
|
67
|
+
const propertyName = propElt.getAttribute('jn-path');
|
|
68
68
|
|
|
69
69
|
if (result && propertyName) {
|
|
70
70
|
const value = getNestedProperty(result, propertyName);
|
|
@@ -115,9 +115,9 @@
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
function initializeJsonataElements() {
|
|
118
|
-
const elements = document.querySelectorAll('[
|
|
118
|
+
const elements = document.querySelectorAll('[jn-url]');
|
|
119
119
|
elements.forEach(elt => {
|
|
120
|
-
const url = elt.getAttribute('
|
|
120
|
+
const url = elt.getAttribute('jn-url');
|
|
121
121
|
if (url) processJsonataElement(elt, url);
|
|
122
122
|
});
|
|
123
123
|
}
|
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,r){const o=e.getAttribute("jn-expression");fetch(r).then((t=>{if(!t.ok)throw new Error(`error: ${t.status}`);return t.json()})).then((r=>{let a=r;if(o)try{const s=t(o).evaluate(r);Promise.resolve(s).then((t=>{a=t,Array.isArray(a)&&1===a.length&&(a=a[0]),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){t.querySelectorAll("[jn-path]").forEach((t=>{const n=t.getAttribute("jn-path");if(e&&n){const r=function(t,e){if(!t||!e)return;let n=t;const r=e.split(".");for(const t of r){const e=t.match(/^(.+?)\[(\d+)\]$/);if(e){const t=e[1],r=parseInt(e[2],10);if(!n||"object"!=typeof n||!(t in n))return;if(n=n[t],!(Array.isArray(n)&&r>=0&&r<n.length))return;n=n[r]}else{if(!n||"object"!=typeof n||!(t in n))return;n=n[t]}}return n}(e,n);null!=r&&("INPUT"===t.tagName||"TEXTAREA"===t.tagName?t.value=r:t.textContent=r)}}))}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,r=t.getAttribute("jn-url");r&&e(t,r)}return!0}}),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",(()=>r())):r()):console.error("htmx-jsonata extension requires the jsonata library. Include it before this extension.")}();
|