@muscara/htmx-jsonata 0.0.3 → 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 +14 -16
- package/htmx-jsonata.js +6 -6
- package/htmx-jsonata.min.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,32 +2,30 @@
|
|
|
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.
|
|
5
|
+
add the following to your project to begin. **make sure you import after htmx and jsonata**
|
|
6
6
|
```html
|
|
7
|
-
<script src="https://unpkg.com/@muscara/htmx-jsonata/htmx-jsonata.min.js"></script>
|
|
8
|
-
<!-- ... include htmx -->
|
|
9
7
|
<!-- ... include jsonata -->
|
|
8
|
+
<!-- ... include htmx -->
|
|
9
|
+
<script src="https://unpkg.com/@muscara/htmx-jsonata/htmx-jsonata.min.js"></script>
|
|
10
10
|
```
|
|
11
11
|
|
|
12
12
|
> only GET request method is supported
|
|
13
13
|
|
|
14
14
|
## usage
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
<script src="https://unpkg.com/@muscara/htmx-jsonata@0.0.1/htmx-jsonata.min.js"></script>
|
|
18
|
-
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
|
|
19
|
-
<script src="https://cdn.jsdelivr.net/npm/jsonata@2.0.3/jsonata.min.js"></script>
|
|
16
|
+
attributes:
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
<h1 jsonata-property="name"></h1>
|
|
23
|
-
<p jsonata-property="phone"></p>
|
|
24
|
-
</div>
|
|
25
|
-
```
|
|
18
|
+
- `jn-url` (required): the JSON API URL to fetch data from.
|
|
26
19
|
|
|
27
|
-
|
|
20
|
+
- `jn-expression`: jsonata expression to filter, transform, or query the JSON response.
|
|
28
21
|
|
|
29
|
-
`
|
|
22
|
+
- `jn-path`: specifies a path to nested data (supports dot notation).
|
|
30
23
|
|
|
31
|
-
|
|
24
|
+
example use:
|
|
32
25
|
|
|
33
|
-
|
|
26
|
+
```html
|
|
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
|
+
```
|
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.")}();
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@muscara/htmx-jsonata",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "htmx extension that provides jsonata query functionality for htmx elements",
|
|
5
|
-
"main": "htmx-jsonata.js",
|
|
5
|
+
"main": "htmx-jsonata.min.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|