haori 0.1.4 → 0.2.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/README.ja.md +16 -16
- package/README.md +22 -23
- package/dist/haori.cjs.js +9 -9
- package/dist/haori.cjs.js.map +1 -1
- package/dist/haori.es.js +788 -678
- package/dist/haori.es.js.map +1 -1
- package/dist/haori.iife.js +9 -9
- package/dist/haori.iife.js.map +1 -1
- package/dist/index.d.ts +35 -1
- package/dist/package.json +1 -1
- package/dist/src/core.d.ts +28 -0
- package/dist/src/core.d.ts.map +1 -1
- package/dist/src/core.js +57 -13
- package/dist/src/core.js.map +1 -1
- package/dist/src/fragment.d.ts +6 -0
- package/dist/src/fragment.d.ts.map +1 -1
- package/dist/src/fragment.js +17 -0
- package/dist/src/fragment.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/procedure.d.ts +14 -0
- package/dist/src/procedure.d.ts.map +1 -1
- package/dist/src/procedure.js +82 -0
- package/dist/src/procedure.js.map +1 -1
- package/dist/tests/core.test.js +74 -0
- package/dist/tests/core.test.js.map +1 -1
- package/dist/tests/fragment.test.js +10 -0
- package/dist/tests/fragment.test.js.map +1 -1
- package/dist/tests/procedure-action-operations.test.js +133 -0
- package/dist/tests/procedure-action-operations.test.js.map +1 -1
- package/package.json +1 -1
package/README.ja.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Haori.js は、HTML 属性を中心にして動的な UI を実現する軽量なライブラリです。JavaScript をほとんど書かずに、データバインディング、条件分岐、繰り返し処理、フォームの双方向バインディング、サーバー通信などを HTML 属性で宣言できます。
|
|
4
4
|
|
|
5
|
-
バージョン: 0.
|
|
5
|
+
バージョン: 0.2.0
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -49,7 +49,7 @@ CDN:
|
|
|
49
49
|
ES Module:
|
|
50
50
|
|
|
51
51
|
```js
|
|
52
|
-
import Haori from 'haori'
|
|
52
|
+
import Haori from 'haori';
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
---
|
|
@@ -61,25 +61,25 @@ HTML だけで簡単に使えます。以下は最小の例です。
|
|
|
61
61
|
```html
|
|
62
62
|
<!DOCTYPE html>
|
|
63
63
|
<html lang="ja">
|
|
64
|
-
<head>
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
</head>
|
|
69
|
-
<body>
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
</body>
|
|
64
|
+
<head>
|
|
65
|
+
<meta charset="utf-8" />
|
|
66
|
+
<title>Haori サンプル</title>
|
|
67
|
+
<script src="https://cdn.jsdelivr.net/npm/haori/dist/haori.iife.js"></script>
|
|
68
|
+
</head>
|
|
69
|
+
<body>
|
|
70
|
+
<div data-bind='{"name":"太郎"}'>
|
|
71
|
+
<p>こんにちは、{{name}} さん</p>
|
|
72
|
+
</div>
|
|
73
|
+
</body>
|
|
74
74
|
</html>
|
|
75
75
|
```
|
|
76
76
|
|
|
77
77
|
JavaScript からマウントする例:
|
|
78
78
|
|
|
79
79
|
```js
|
|
80
|
-
import Haori from 'haori'
|
|
80
|
+
import Haori from 'haori';
|
|
81
81
|
|
|
82
|
-
Haori.mount(document.body, {
|
|
82
|
+
Haori.mount(document.body, {items: [{name: 'りんご'}, {name: 'みかん'}]});
|
|
83
83
|
```
|
|
84
84
|
|
|
85
85
|
---
|
|
@@ -112,7 +112,7 @@ Haori.mount(document.body, { items: [ { name: 'りんご' }, { name: 'みかん'
|
|
|
112
112
|
4. 新しい版数タグから GitHub Release を公開する
|
|
113
113
|
5. npm、jsDelivr、GitHub Release の assets が新しい版数を指すことを確認する
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
6. 依存インストール
|
|
116
116
|
|
|
117
117
|
```bash
|
|
118
118
|
npm install
|
|
@@ -180,4 +180,4 @@ git push origin --tags
|
|
|
180
180
|
|
|
181
181
|
---
|
|
182
182
|
|
|
183
|
-
README の作成にあたって追加してほしい項目(API 参照、図、例など)があれば教えてください。
|
|
183
|
+
README の作成にあたって追加してほしい項目(API 参照、図、例など)があれば教えてください。
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Haori.js is a lightweight, HTML-first UI library that enables dynamic user interfaces primarily through HTML attributes. It lets you declare data bindings, conditional rendering, list rendering, form two-way binding, server fetches, and HTML imports without writing much JavaScript.
|
|
4
4
|
|
|
5
|
-
Version: 0.
|
|
5
|
+
Version: 0.2.0
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -22,13 +22,13 @@ Contents
|
|
|
22
22
|
|
|
23
23
|
- Design principle: HTML-first — declare UI behavior with HTML attributes
|
|
24
24
|
- Key features:
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
- Data binding via `data-bind`
|
|
26
|
+
- Conditional rendering via `data-if`
|
|
27
|
+
- List rendering via `data-each`
|
|
28
|
+
- Two-way form binding (automatic binding based on `name` attributes)
|
|
29
|
+
- Server fetches via `data-fetch`
|
|
30
|
+
- HTML imports via `data-import`
|
|
31
|
+
- Zero runtime dependencies (uses browser-native APIs)
|
|
32
32
|
|
|
33
33
|
## Installation
|
|
34
34
|
|
|
@@ -49,7 +49,7 @@ This CDN URL follows the latest published npm release.
|
|
|
49
49
|
ES Module import:
|
|
50
50
|
|
|
51
51
|
```js
|
|
52
|
-
import Haori from 'haori'
|
|
52
|
+
import Haori from 'haori';
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
---
|
|
@@ -61,25 +61,25 @@ You can use Haori with plain HTML. Minimal example:
|
|
|
61
61
|
```html
|
|
62
62
|
<!DOCTYPE html>
|
|
63
63
|
<html lang="en">
|
|
64
|
-
<head>
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
</head>
|
|
69
|
-
<body>
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
</body>
|
|
64
|
+
<head>
|
|
65
|
+
<meta charset="utf-8" />
|
|
66
|
+
<title>Haori Sample</title>
|
|
67
|
+
<script src="https://cdn.jsdelivr.net/npm/haori/dist/haori.iife.js"></script>
|
|
68
|
+
</head>
|
|
69
|
+
<body>
|
|
70
|
+
<div data-bind='{"name":"Taro"}'>
|
|
71
|
+
<p>Hello, {{name}}</p>
|
|
72
|
+
</div>
|
|
73
|
+
</body>
|
|
74
74
|
</html>
|
|
75
75
|
```
|
|
76
76
|
|
|
77
77
|
Mounting from JavaScript:
|
|
78
78
|
|
|
79
79
|
```js
|
|
80
|
-
import Haori from 'haori'
|
|
80
|
+
import Haori from 'haori';
|
|
81
81
|
|
|
82
|
-
Haori.mount(document.body, {
|
|
82
|
+
Haori.mount(document.body, {items: [{name: 'apple'}, {name: 'orange'}]});
|
|
83
83
|
```
|
|
84
84
|
|
|
85
85
|
---
|
|
@@ -112,7 +112,7 @@ Quick release memo:
|
|
|
112
112
|
4. Publish a GitHub Release from the new version tag.
|
|
113
113
|
5. Confirm npm, jsDelivr, and the GitHub Release assets reflect the new version.
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
6. Install dependencies
|
|
116
116
|
|
|
117
117
|
```bash
|
|
118
118
|
npm install
|
|
@@ -181,4 +181,3 @@ For more detailed usage, attribute specs, and internal design, see:
|
|
|
181
181
|
---
|
|
182
182
|
|
|
183
183
|
If you would like additional sections (API reference, diagrams, more examples), tell me what to include and I will expand the README.
|
|
184
|
-
|
package/dist/haori.cjs.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
2
|
-
`)}static evaluate(t,e={}){if(t.trim()==="")return
|
|
3
|
-
${
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const D=class D{static isEnabled(){return D.devMode}static enable(){D.devMode=!0}static disable(){D.devMode=!1}static set(t){D.devMode=t}};D.devMode=!1;let C=D;const M=class M{static detect(){try{const t=document.currentScript||document.querySelector('script[src*="haori"]');if(t instanceof HTMLScriptElement){const r=t.getAttribute("data-prefix")||M._prefix;M._prefix=r.endsWith("-")?r:r+"-"}if(t instanceof HTMLScriptElement&&t.hasAttribute(`${M._prefix}dev`)){C.set(!0);return}const e=window.location.hostname;if(e==="localhost"||e.endsWith(".localhost")||e==="127.0.0.1"||e==="::1"||e.endsWith(".local")){C.set(!0);return}C.set(!1)}catch{}}static get prefix(){return M._prefix}};M._prefix="data-";let f=M;document.readyState==="loading"?document.addEventListener("DOMContentLoaded",f.detect):f.detect();class h{static info(t,...e){C.isEnabled()&&console.log&&console.log(t,...e)}static warn(t,...e){C.isEnabled()&&console.warn&&console.warn(t,...e)}static error(t,...e){console.error(t,...e)}}class z{constructor(){this.MAX_BUDGET=8,this.queue=[],this.processing=!1}enqueue(t,e=!1){let r,n;const i=new Promise((a,o)=>{r=a,n=o}),s={task:t,timestamp:performance.now(),promise:i,resolve:r,reject:n};return e?this.queue.unshift(s):this.queue.push(s),this.scheduleProcessing(),i}async processQueue(){if(!(this.processing||this.queue.length===0)){this.processing=!0;try{const t=performance.now();for(;this.queue.length>0;){const e=this.queue.shift();if(!e)return;try{const r=await e.task();e.resolve(r)}catch(r){e.reject(r),h.error("[Haori]",`Task ${e.timestamp} failed:`,r)}if(performance.now()-t>this.MAX_BUDGET)break}}catch(t){h.error("[Haori]","Error processing queue:",t)}finally{this.processing=!1,this.queue.length>0&&this.scheduleProcessing()}}}scheduleProcessing(){this.processing||(typeof requestAnimationFrame<"u"?requestAnimationFrame(()=>{this.processQueue()}):setTimeout(()=>{this.processQueue()},16))}async wait(){if(this.queue.length===0&&!this.processing)return;const t=this.queue.map(e=>e.promise);t.length>0&&await Promise.allSettled(t)}}const X=class X{static enqueue(t,e=!1){return this.ASYNC_QUEUE.enqueue(t,e)}static wait(){return this.ASYNC_QUEUE.wait()}};X.ASYNC_QUEUE=new z;let v=X;class K{static dialog(t){return v.enqueue(()=>{window.alert(t)},!0)}static async toast(t,e){const r=document.createElement("div");r.className=`haori-toast haori-toast-${e}`,r.textContent=t,r.setAttribute("popover","manual"),r.setAttribute("role","status"),r.setAttribute("aria-live","polite"),document.body.appendChild(r),r.showPopover(),setTimeout(()=>{try{r.hidePopover()}finally{r.remove()}},3e3)}static confirm(t){return v.enqueue(()=>window.confirm(t),!0)}static openDialog(t){return v.enqueue(()=>{t instanceof HTMLDialogElement?t.showModal():h.error("[Haori]","Element is not a dialog: ",t)},!0)}static closeDialog(t){return v.enqueue(()=>{t instanceof HTMLDialogElement?t.close():h.error("[Haori]","Element is not a dialog: ",t)},!0)}static addErrorMessage(t,e){return v.enqueue(()=>{if(t instanceof HTMLFormElement){t.setAttribute("data-message",e);return}if(t.parentElement){t.parentElement.setAttribute("data-message",e);return}t.setAttribute("data-message",e)},!0)}static clearMessages(t){return v.enqueue(()=>{t.removeAttribute("data-message"),t.querySelectorAll("[data-message]").forEach(e=>{e.removeAttribute("data-message")})},!0)}}const Q=["addErrorMessage","clearMessages"];function W(){const t=globalThis.window?.Haori;return Q.every(r=>typeof t?.[r]=="function")?t:K}class b{static getValues(t){const e={};return b.getPartValues(t,e)}static getPartValues(t,e){const r=t.getAttribute("name"),n=t.getAttribute(`${f.prefix}form-object`),i=t.getAttribute(`${f.prefix}form-list`);if(r){i?Array.isArray(e[String(r)])?e[String(r)].push(t.getValue()):e[String(r)]=[t.getValue()]:e[String(r)]=t.getValue(),n&&h.warn("Haori",`Element cannot have both ${f.prefix}form-object and name attributes.`);for(const s of t.getChildElementFragments())b.getPartValues(s,e)}else if(n){const s={};for(const a of t.getChildElementFragments())b.getPartValues(a,s);Object.keys(s).length>0&&(e[String(n)]=s),i&&h.warn("Haori",`Element cannot have both ${f.prefix}form-list and ${f.prefix}form-object attributes.`)}else if(i){const s=[];for(const a of t.getChildElementFragments()){const o={};b.getPartValues(a,o),Object.keys(o).length>0&&s.push(o)}s.length>0&&(e[String(i)]=s)}else for(const s of t.getChildElementFragments())b.getPartValues(s,e);return e}static setValues(t,e,r=!1){return b.setPartValues(t,e,null,r)}static setPartValues(t,e,r=null,n=!1){const i=[],s=t.getAttribute("name"),a=t.getAttribute(`${f.prefix}form-object`),o=t.getAttribute(`${f.prefix}form-list`),p=t.getAttribute(`${f.prefix}form-detach`);if(s){if(!p||n){const c=e[String(s)];o&&Array.isArray(c)&&r!==null?i.push(t.setValue(c[r])):typeof c=="string"||typeof c=="number"||typeof c=="boolean"||c===null?i.push(t.setValue(c)):i.push(t.setValue(String(c)))}}else if(a){const c=e[String(a)];if(c&&typeof c=="object")for(const y of t.getChildElementFragments())i.push(b.setPartValues(y,c,null,n))}else if(o){const c=e[String(o)];if(Array.isArray(c)){const y=t.getChildElementFragments();for(let l=0;l<y.length;l++){const d=y[l];c.length>l?i.push(b.setPartValues(d,c[l],l,n)):i.push(b.setPartValues(d,{},l,n))}}}else for(const c of t.getChildElementFragments())i.push(b.setPartValues(c,e,null,n));return Promise.all(i).then(()=>{})}static async reset(t){b.clearValues(t),await Promise.all([b.clearMessages(t),b.clearEachClones(t)]),await v.enqueue(()=>{const e=t.getTarget();if(e instanceof HTMLFormElement)e.reset();else{const r=e.parentElement;if(r){const n=e.nextElementSibling,i=document.createElement("form");i.appendChild(e),i.reset(),r.insertBefore(e,n)}}}),await T.evaluateAll(t)}static clearEachClones(t){const e=[],r=i=>{if(i.hasAttribute(`${f.prefix}each`))for(const s of i.getChildElementFragments()){const a=s.hasAttribute(`${f.prefix}each-before`),o=s.hasAttribute(`${f.prefix}each-after`);!a&&!o&&e.push(s.remove())}},n=i=>{r(i);for(const s of i.getChildElementFragments())n(s)};r(t);for(const i of t.getChildElementFragments())n(i);return Promise.all(e).then(()=>{})}static clearValues(t){t.clearValue();for(const e of t.getChildElementFragments())b.clearValues(e)}static clearMessages(t){return W().clearMessages(t.getTarget())}static addErrorMessage(t,e,r){const n=[],i=W(),s=b.findFragmentsByKey(t,e);return s.forEach(a=>{n.push(i.addErrorMessage(a.getTarget(),r))}),s.length===0&&n.push(i.addErrorMessage(t.getTarget(),r)),Promise.all(n).then(()=>{})}static findFragmentsByKey(t,e){return b.findFragmentByKeyParts(t,e.split("."))}static findFragmentByKeyParts(t,e){const r=[],n=e[0];if(e.length==1&&t.getAttribute("name")===n&&r.push(t),t.hasAttribute(`${f.prefix}form-object`))e.length>1&&t.getAttribute(`${f.prefix}form-object`)===n&&t.getChildElementFragments().forEach(s=>{r.push(...b.findFragmentByKeyParts(s,e.slice(1)))});else if(t.hasAttribute(`${f.prefix}form-list`)){if(e.length>1){const i=t.getAttribute(`${f.prefix}form-list`),s=n.lastIndexOf("["),a=n.lastIndexOf("]");if(s!==-1&&a!==-1&&s<a){const o=n.substring(0,s);if(i===o){const p=n.substring(s+1,a),c=Number(p);if(isNaN(c))h.error("Haori",`Invalid index: ${n}`);else{const y=t.getChildElementFragments().filter(l=>l.hasAttribute(`${f.prefix}row`));c<y.length&&r.push(...b.findFragmentByKeyParts(y[c],e.slice(1)))}}}}}else t.getChildElementFragments().forEach(i=>{r.push(...b.findFragmentByKeyParts(i,e))});return r}static getFormFragment(t){if(t.getTarget()instanceof HTMLFormElement)return t;const r=t.getParent();return r?this.getFormFragment(r):null}}const S=class S{static getForbiddenBindingValues(){const t=globalThis,e=[t,t.window,t.document,t.navigator,t.history,t.localStorage,t.sessionStorage,t.fetch,t.Function,t.setTimeout,t.setInterval,t.requestAnimationFrame,t.alert,t.confirm,t.prompt];return t.window?.location&&e.push(t.window.location),e.filter(r=>r!=null)}static buildAssignments(t){const e=new Set(t);return this.FORBIDDEN_NAMES.filter(r=>!e.has(r)).map(r=>`const ${r} = undefined`).join(`;
|
|
2
|
+
`)}static evaluate(t,e={}){if(t.trim()==="")return h.warn("[Haori]",t,"Expression is empty"),null;if(this.containsDangerousPatterns(t))return h.warn("[Haori]",t,"Expression contains dangerous patterns"),null;if(this.containsForbiddenKeys(e))return h.warn("[Haori]",e,"Binded values contain forbidden keys"),null;if(this.containsForbiddenBindingValues(e))return h.warn("[Haori]",e,"Binded values contain forbidden values"),null;const r=Object.keys(e).filter(s=>!this.FORBIDDEN_BINDING_NAMES.has(s)).sort(),n=`${t}:${r.join(",")}`;let i=this.EXPRESSION_CACHE.get(n);if(!i){const s=this.buildAssignments(r),a=s?`"use strict";
|
|
3
|
+
${s};
|
|
4
4
|
return (${t});`:`"use strict";
|
|
5
|
-
return (${t});`;try{
|
|
6
|
-
`;case"r":return"\r";case"t":return" ";case"v":return"\v";case"0":return"\0";default:return r}})}static wrapBoundValues(t){const e=new WeakMap,r={};return Object.entries(t).forEach(([n,s])=>{r[n]=this.wrapBoundValue(s,e)}),r}static wrapBoundValue(t,e){if(!this.shouldWrapValue(t))return t;const r=t,n=e.get(r);if(n!==void 0)return n;const s=new Proxy(r,{get:(i,a,o)=>{if(typeof a=="string"&&this.FORBIDDEN_PROPERTY_NAMES.has(a))return;const g=Reflect.get(i,a,o);return typeof a=="symbol"?g:this.wrapBoundValue(g,e)},has:(i,a)=>typeof a=="string"&&this.FORBIDDEN_PROPERTY_NAMES.has(a)?!1:Reflect.has(i,a),getOwnPropertyDescriptor:(i,a)=>{if(!(typeof a=="string"&&this.FORBIDDEN_PROPERTY_NAMES.has(a)))return Reflect.getOwnPropertyDescriptor(i,a)},apply:(i,a,o)=>{const g=Reflect.apply(i,a,o);return this.isIteratorLike(g)?g:this.wrapBoundValue(g,e)},construct:(i,a,o)=>this.wrapBoundValue(Reflect.construct(i,a,o),e)});return e.set(r,s),s}static shouldWrapValue(t){if(typeof t=="function")return!0;if(t===null||typeof t!="object")return!1;if(Array.isArray(t))return!0;const e=Object.getPrototypeOf(t);return e===Object.prototype||e===null}static withBlockedPropertyAccess(t){const r=[{target:Object.prototype,property:"constructor"},{target:Function.prototype,property:"constructor"},{target:Object.prototype,property:"__proto__"}].map(n=>({...n,descriptor:Object.getOwnPropertyDescriptor(n.target,n.property)})).filter(n=>n.descriptor?.configurable===!0);r.forEach(({target:n,property:s})=>{Object.defineProperty(n,s,{configurable:!0,enumerable:!1,get:()=>{},set:()=>{}})});try{return t()}finally{r.forEach(({target:n,property:s,descriptor:i})=>{i!==void 0&&Object.defineProperty(n,s,i)})}}static isIteratorLike(t){return t===null||typeof t!="object"?!1:typeof t.next=="function"}static containsForbiddenKeys(t){if(!t||typeof t!="object")return!1;for(const e of Object.keys(t))if(this.FORBIDDEN_BINDING_NAMES.has(e))return!0;return!1}static containsForbiddenBindingValues(t,e=new WeakSet){if(!t||typeof t!="object"||e.has(t))return!1;if(e.add(t),this.getForbiddenBindingValues().some(r=>r===t))return!0;for(const r of Object.values(t)){if(typeof r=="function"){if(this.getForbiddenBindingValues().some(n=>n===r))return!0;continue}if(this.containsForbiddenBindingValues(r,e))return!0}return!1}};P.FORBIDDEN_NAMES=["window","self","globalThis","frames","parent","top","Function","setTimeout","setInterval","requestAnimationFrame","alert","confirm","prompt","fetch","XMLHttpRequest","Reflect","constructor","__proto__","prototype","Object","document","location","navigator","localStorage","sessionStorage","IndexedDB","history"],P.STRICT_FORBIDDEN_NAMES=["eval","arguments"],P.REBINDABLE_FORBIDDEN_NAMES=new Set(["location"]),P.FORBIDDEN_BINDING_NAMES=new Set([...P.FORBIDDEN_NAMES.filter(t=>!P.REBINDABLE_FORBIDDEN_NAMES.has(t)),"constructor","__proto__","prototype",...P.STRICT_FORBIDDEN_NAMES]),P.FORBIDDEN_PROPERTY_NAMES=new Set(["constructor","__proto__","prototype"]),P.DISALLOWED_KEYWORDS=new Set(["await","break","case","catch","class","const","continue","debugger","default","delete","do","else","export","finally","for","function","if","import","in","instanceof","let","new","return","switch","this","throw","try","typeof","var","void","while","with","yield"]),P.EXPRESSION_CACHE=new Map;let I=P;const k=class k{constructor(t){this.parent=null,this.mounted=!1,this.skipMutationNodes=!1,this.target=t,k.FRAGMENT_CACHE.set(t,this)}static get(t){if(t==null)return null;if(k.FRAGMENT_CACHE.has(t))return k.FRAGMENT_CACHE.get(t);let e;switch(t.nodeType){case Node.ELEMENT_NODE:e=new x(t);break;case Node.TEXT_NODE:e=new H(t);break;case Node.COMMENT_NODE:e=new J(t);break;default:return f.warn("[Haori]","Unsupported node type:",t.nodeType),null}return e}isSkipMutationNodes(){return this.skipMutationNodes}unmount(){if(!this.mounted||this.skipMutationNodes)return Promise.resolve();if(this.parent){const t=this.parent,e=t.skipMutationNodes;return v.enqueue(()=>{t.skipMutationNodes=!0,this.target.parentNode===t.getTarget()&&t.getTarget().removeChild(this.target),this.mounted=!1}).finally(()=>{t.skipMutationNodes=e})}else{const t=this.target.parentNode;if(t)return v.enqueue(()=>{this.target.parentNode===t&&t.removeChild(this.target),this.mounted=!1});this.mounted=!1}return Promise.resolve()}mount(){if(this.mounted||this.skipMutationNodes)return Promise.resolve();if(this.parent){const t=this.parent,e=t.skipMutationNodes;return v.enqueue(()=>{t.skipMutationNodes=!0,this.target.parentNode!==t.getTarget()&&t.getTarget().appendChild(this.target),this.mounted=!0}).finally(()=>{t.skipMutationNodes=e})}return Promise.resolve()}isMounted(){return this.mounted}setMounted(t){this.mounted=t}remove(t=!0){return this.parent&&this.parent.removeChild(this),k.FRAGMENT_CACHE.delete(this.target),t?this.unmount():Promise.resolve()}getTarget(){return this.target}getParent(){return this.parent}setParent(t){this.parent=t}};k.FRAGMENT_CACHE=new WeakMap;let A=k;class x extends A{constructor(t){super(t),this.INPUT_EVENT_TYPES=["text","password","email","url","tel","search","number","range","color","date","datetime-local","month","time","week"],this.children=[],this.attributeMap=new Map,this.bindingData=null,this.bindingDataCache=null,this.visible=!0,this.display=null,this.template=null,this.listKey=null,this.value=null,this.skipMutationAttributes=!1,this.skipChangeValue=!1,this.syncValue(),t.getAttributeNames().forEach(e=>{const r=t.getAttribute(e);if(r!==null&&!this.attributeMap.has(e)){const n=new L(e,r);this.attributeMap.set(e,n)}}),t.childNodes.forEach(e=>{const r=A.get(e);r.setParent(this),this.children.push(r)})}getChildren(){return this.children}getChildElementFragments(){return this.children.filter(t=>t instanceof x)}pushChild(t){this.children.push(t),t.setParent(this)}removeChild(t){const e=this.children.indexOf(t);if(e<0){f.warn("[Haori]","Child fragment not found.",t);return}this.children.splice(e,1),t.setParent(null)}clone(){const t=new x(this.target.cloneNode(!1));return this.children.forEach(e=>{const r=e.clone();t.getTarget().appendChild(r.getTarget()),t.pushChild(r)}),t.mounted=!1,t.bindingData=this.bindingData,t.clearBindingDataCache(),t.visible=this.visible,t.display=this.display,t.template=this.template,t}remove(t=!0){const e=[];return this.children.forEach(r=>{e.push(r.remove(!1))}),this.children.length=0,this.attributeMap.clear(),this.bindingData=null,this.bindingDataCache=null,this.template&&(e.push(this.template.remove(!1)),this.template=null),e.push(super.remove(t)),Promise.all(e).then(()=>{})}getTarget(){return this.target}getBindingData(){return this.bindingDataCache?this.bindingDataCache:(this.bindingDataCache={},this.parent&&Object.assign(this.bindingDataCache,this.parent.getBindingData()),this.bindingData&&Object.assign(this.bindingDataCache,this.bindingData),this.bindingDataCache)}getRawBindingData(){return this.bindingData}setBindingData(t){this.bindingData=t,this.clearBindingDataCache()}clearBindingDataCache(){this.bindingDataCache=null,this.children.forEach(t=>{t instanceof x&&t.clearBindingDataCache()})}getTemplate(){return this.template}setTemplate(t){this.template=t}setListKey(t){this.listKey=t}getListKey(){return this.listKey}setValue(t){if(this.skipChangeValue||this.value===t)return Promise.resolve();const e=this.getTarget();if(e instanceof HTMLInputElement&&(e.type==="checkbox"||e.type==="radio")){const r=this.getAttribute("value");let n;return r==="true"?n=t===!0:r==="false"?n=t===!1:n=r===String(t),this.value=n?t:null,e.checked===n?Promise.resolve():(this.skipChangeValue=!0,v.enqueue(()=>{e.checked=n,e.dispatchEvent(new Event("change",{bubbles:!0}))}).finally(()=>{this.skipChangeValue=!1}))}else return e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement?(this.value=t,this.skipChangeValue=!0,v.enqueue(()=>{e.value=t===null?"":String(t),(e instanceof HTMLInputElement&&this.INPUT_EVENT_TYPES.includes(e.type)||e instanceof HTMLTextAreaElement)&&e.dispatchEvent(new Event("input",{bubbles:!0})),e.dispatchEvent(new Event("change",{bubbles:!0}))}).finally(()=>{this.skipChangeValue=!1})):(f.warn("[Haori]","setValue is not supported for this element type.",e),Promise.resolve())}getValue(){return this.value}clearValue(){this.value=null}syncValue(){const t=this.getTarget();if(t instanceof HTMLInputElement)if(t.type==="checkbox"||t.type==="radio")if(t.checked){const e=t.value;e==="true"?this.value=!0:e==="false"?this.value=!1:this.value=e}else{const e=t.value;e==="true"?this.value=!1:e==="false"?this.value=!0:this.value=null}else this.value=t.value;else t instanceof HTMLTextAreaElement?this.value=t.value:t instanceof HTMLSelectElement&&(this.value=t.value)}setAttribute(t,e){if(this.skipMutationAttributes)return Promise.resolve();if(e===null)return this.removeAttribute(t);const r=new L(t,e);this.attributeMap.set(t,r),this.skipMutationAttributes=!0;const n=this.getTarget(),s=r.isForceEvaluation()?e:this.getAttribute(t);return v.enqueue(()=>{if(s===null||s===!1)n.removeAttribute(t);else{const i=String(s);n.getAttribute(t)!==i&&n.setAttribute(t,i)}}).finally(()=>{this.skipMutationAttributes=!1})}removeAttribute(t){if(this.skipMutationAttributes)return Promise.resolve();this.attributeMap.delete(t),this.skipMutationAttributes=!0;const e=this.getTarget();return v.enqueue(()=>{e.removeAttribute(t)}).finally(()=>{this.skipMutationAttributes=!1})}getAttribute(t){const e=this.attributeMap.get(t);if(e===void 0)return null;const r=e.evaluate(this.getBindingData());return r.length===1?r[0]:B.joinEvaluateResults(r)}getRawAttribute(t){const e=this.attributeMap.get(t);return e===void 0?null:e.getValue()}getAttributeNames(){return Array.from(this.attributeMap.keys())}hasAttribute(t){return this.attributeMap.has(t)}resolveInsertionPointFromDom(t,e){const r=t.getTarget();if(r.parentNode!==this.target)return null;const n=e?r.nextSibling:r;let s=e?r.nextSibling:r;for(;s!==null;){const i=A.get(s);if(i!==null){const a=this.children.indexOf(i);if(a!==-1)return{index:a,referenceNode:n}}s=s.nextSibling}return{index:this.children.length,referenceNode:n}}insertBefore(t,e,r){if(this.skipMutationNodes)return Promise.resolve();if(t===this)return f.error("[Haori]","Cannot insert element as child of itself"),Promise.reject(new Error("Self-insertion not allowed"));const n=new Set;let s=this.parent;for(;s;)n.add(s),s=s.getParent();if(n.has(t))return f.error("[Haori]","Cannot create circular reference"),Promise.reject(new Error("Circular reference detected"));const i=t.getParent()===this;let a=-1,o=-1;i&&(a=this.children.indexOf(t),e!==null&&(o=this.children.indexOf(e)));const g=t.getParent();g!==null&&g.removeChild(t);let u=r===void 0?e?.getTarget()||null:r;if(e===null)this.children.push(t);else{let l;if(i?a!==-1&&a<o?l=o-1:l=o:l=this.children.indexOf(e),l===-1){const d=this.resolveInsertionPointFromDom(e,!1);d===null?(f.warn("[Haori]","Reference child not found in children.",e),this.children.push(t)):(this.children.splice(d.index,0,t),u=d.referenceNode)}else this.children.splice(l,0,t)}t.setParent(this),t.setMounted(this.mounted);const b=this.skipMutationNodes;return this.skipMutationNodes=!0,v.enqueue(()=>{this.target.insertBefore(t.getTarget(),u)}).finally(()=>{this.skipMutationNodes=b})}insertAfter(t,e){if(e==null)return this.insertBefore(t,null);const r=this.children.indexOf(e);if(r===-1){const n=this.resolveInsertionPointFromDom(e,!0);return n===null?(f.warn("[Haori]","Reference child not found in children.",e),this.insertBefore(t,null)):this.insertBefore(t,this.children[n.index]||null,n.referenceNode)}return this.insertBefore(t,this.children[r+1]||null)}getPrevious(){const t=this.getParent();if(t===null)return null;const e=t.getChildElementFragments(),r=e.indexOf(this);return r<=0?null:e[r-1]}getNext(){const t=this.getParent();if(t===null)return null;const e=t.getChildElementFragments(),r=e.indexOf(this);return r<0||r+1>=e.length?null:e[r+1]}isVisible(){return this.visible}hide(){return this.visible=!1,this.display=this.getTarget().style.display,this.getTarget().style.display="none",this.getTarget().setAttribute(`${h.prefix}if-false`,""),Promise.resolve()}show(){return this.getTarget().style.display=this.display??"",this.getTarget().removeAttribute(`${h.prefix}if-false`),this.visible=!0,Promise.resolve()}closestByAttribute(t){if(this.hasAttribute(t))return this;const e=this.getParent();return e===null?null:e.closestByAttribute(t)}}class H extends A{constructor(t){super(t),this.skipMutation=!1,this.text=t.textContent||"",this.contents=new B(this.text)}clone(){const t=new H(this.target.cloneNode(!0));return t.mounted=!1,t.text=this.text,t.contents=this.contents,t}getTarget(){return this.target}setContent(t){return this.skipMutation||this.text===t?Promise.resolve():(this.text=t,this.contents=new B(t),this.evaluate())}evaluate(){return this.contents.isRawEvaluate&&this.parent===null?Promise.reject(new Error("Parent fragment is required for raw evaluation")):v.enqueue(()=>{this.skipMutation=!0,this.contents.isRawEvaluate?this.parent.getTarget().innerHTML=this.contents.evaluate(this.parent.getBindingData())[0]:this.contents.isEvaluate?this.target.textContent=B.joinEvaluateResults(this.contents.evaluate(this.parent.getBindingData())):this.target.textContent=this.text}).finally(()=>{this.skipMutation=!1})}}class J extends A{constructor(t){super(t),this.skipMutation=!1,this.text=t.textContent||""}clone(){const t=new J(this.target.cloneNode(!0));return t.mounted=!1,t.text=this.text,t}getTarget(){return this.target}setContent(t){return this.skipMutation||this.text===t?Promise.resolve():(this.text=t,v.enqueue(()=>{this.skipMutation=!0,this.target.textContent=this.text}).finally(()=>{this.skipMutation=!1}))}}const q=class q{constructor(t){this.contents=[],this.isEvaluate=!1,this.isRawEvaluate=!1,this.value=t;const e=[...t.matchAll(q.PLACEHOLDER_REGEX)];let r=0,n=!1,s=!1;for(const i of e){i.index>r&&this.contents.push({text:t.slice(r,i.index),type:0});const a={text:i[1]??i[2],type:i[1]?2:1};n=!0,s=s||a.type===2,this.contents.push(a),r=i.index+i[0].length}r<t.length&&this.contents.push({text:t.slice(r),type:0}),this.isEvaluate=n,this.isRawEvaluate=s,this.checkRawExpressions()}static joinEvaluateResults(t){return t===null||t.length===0?"":t.map(e=>e==null||e===!1||Number.isNaN(e)?"":typeof e!="string"?String(e):e).join("")}getValue(){return this.value}checkRawExpressions(){for(let t=0;t<this.contents.length;t++)this.contents[t].type===2&&this.contents.length>1&&(f.error("[Haori]","Raw expressions are not allowed in multi-content expressions."),this.contents[t].type=1)}evaluate(t){if(!this.isEvaluate&&!this.isRawEvaluate)return this.contents.map(r=>r.text);const e=[];return this.contents.forEach(r=>{try{if(r.type===1||r.type===2){const n=I.evaluate(r.text,t);e.push(n)}else e.push(r.text)}catch(n){f.error("[Haori]",`Error evaluating text expression: ${r.text}`,n),e.push("")}}),e}};q.PLACEHOLDER_REGEX=/\{\{\{([\s\S]+?)\}\}\}|\{\{([\s\S]+?)\}\}/g;let B=q;const U=class U extends B{constructor(t,e){super(e),this.forceEvaluation=U.FORCE_EVALUATION_ATTRIBUTES.includes(t)}isForceEvaluation(){return this.forceEvaluation}evaluate(t){if(!this.isEvaluate&&!this.forceEvaluation)return this.contents.map(r=>r.text);const e=[];return this.contents.forEach(r=>{try{if(this.forceEvaluation&&r.type===0||r.type===1||r.type===2){const n=I.evaluate(r.text,t);e.push(n)}else e.push(r.text)}catch(n){f.error("[Haori]",`Error evaluating attribute expression: ${r.text}`,n),e.push("")}}),this.forceEvaluation&&e.length>1?(f.error("[Haori]","each or if expressions must have a single content.",e),[e[0]]):e}};U.FORCE_EVALUATION_ATTRIBUTES=["data-if","hor-if","data-each","hor-each"];let L=U;class y{static dispatch(t,e,r,n){const s=new CustomEvent(`haori:${e}`,{bubbles:n?.bubbles??!0,cancelable:n?.cancelable??!1,composed:n?.composed??!0,detail:r});return t.dispatchEvent(s)}static ready(t){y.dispatch(document,"ready",{version:t})}static render(t){y.dispatch(t,"render",{target:t})}static importStart(t,e){y.dispatch(t,"importstart",{url:e,startedAt:performance.now()})}static importEnd(t,e,r,n){y.dispatch(t,"importend",{url:e,bytes:r,durationMs:performance.now()-n})}static importError(t,e,r){y.dispatch(t,"importerror",{url:e,error:r})}static bindChange(t,e,r,n="other"){const s=[],i=new Set(Object.keys(e||{})),a=new Set(Object.keys(r)),o=new Set([...i,...a]);for(const g of o){const u=e?.[g],b=r[g];u!==b&&s.push(g)}y.dispatch(t,"bindchange",{previous:e||{},next:r,changedKeys:s,reason:n})}static eachUpdate(t,e,r,n){y.dispatch(t,"eachupdate",{added:e,removed:r,order:n,total:n.length})}static rowAdd(t,e,r,n){y.dispatch(t,"rowadd",{key:e,index:r,item:n})}static rowRemove(t,e,r){y.dispatch(t,"rowremove",{key:e,index:r})}static rowMove(t,e,r,n){y.dispatch(t,"rowmove",{key:e,from:r,to:n})}static show(t){y.dispatch(t,"show",{visible:!0})}static hide(t){y.dispatch(t,"hide",{visible:!1})}static fetchStart(t,e,r,n){y.dispatch(t,"fetchstart",{url:e,options:r||{},payload:n,startedAt:performance.now()})}static fetchEnd(t,e,r,n){y.dispatch(t,"fetchend",{url:e,status:r,durationMs:performance.now()-n})}static fetchError(t,e,r,n,s){y.dispatch(t,"fetcherror",{url:e,status:n,error:r,durationMs:s?performance.now()-s:void 0})}}const Z=["addErrorMessage","closeDialog","confirm","dialog","openDialog","toast"];function G(){const t=globalThis.window?.Haori;return Z.every(r=>typeof t?.[r]=="function")?t:K}const c=class c{static attrName(t,e,r=!1){return t?`${h.prefix}${t}-${e}`:r?`${h.prefix}fetch-${e}`:`${h.prefix}${e}`}static resolveDataParamString(t,e){return t.replace(c.DATA_PLACEHOLDER_REGEX,(r,n,s)=>{const i=I.evaluate(n??s??"",e);return i==null||Number.isNaN(i)?"":encodeURIComponent(typeof i=="object"?JSON.stringify(i):String(i))})}static isJsonStringContext(t,e){let r=!1,n=!1;for(let s=0;s<e;s+=1){const i=t[s];if(n){n=!1;continue}if(i==="\\"){n=!0;continue}i==='"'&&(r=!r)}return r}static stringifyJsonTemplateValue(t){if(t===void 0||Number.isNaN(t))return"null";try{return JSON.stringify(t)??JSON.stringify(String(t))}catch{return JSON.stringify(String(t))}}static stringifyJsonTemplateStringContent(t){if(t==null||Number.isNaN(t))return"";const e=typeof t=="object"?c.stringifyJsonTemplateValue(t):String(t);return JSON.stringify(e).slice(1,-1)}static resolveDataJsonString(t,e){return t.replace(c.DATA_PLACEHOLDER_REGEX,(r,n,s,i)=>{const a=I.evaluate(n??s??"",e);return c.isJsonStringContext(t,i)?c.stringifyJsonTemplateStringContent(a):c.stringifyJsonTemplateValue(a)})}static resolveDataAttribute(t,e){const r=t.getRawAttribute(e),n=t.getAttribute(e);if(n&&typeof n=="object"&&!Array.isArray(n))return n;if(typeof n!="string"||r===null)return null;const s=r.trim();return c.SINGLE_PLACEHOLDER_REGEX.test(s)?T.parseDataBind(n):s.startsWith("{")||s.startsWith("[")?T.parseDataBind(c.resolveDataJsonString(r,t.getBindingData())):T.parseDataBind(c.resolveDataParamString(r,t.getBindingData()))}static buildOptions(t,e){const r={targetFragment:t};if(e){if(t.hasAttribute(c.attrName(e,"validate"))&&(r.valid=!0),t.hasAttribute(c.attrName(e,"confirm"))&&(r.confirmMessage=t.getAttribute(c.attrName(e,"confirm"))),t.hasAttribute(c.attrName(e,"data"))&&(r.data=c.resolveDataAttribute(t,c.attrName(e,"data"))),t.hasAttribute(c.attrName(e,"form"))){const l=t.getRawAttribute(c.attrName(e,"form"));if(l){const d=document.body.querySelector(l);d!==null?r.formFragment=E.getFormFragment(A.get(d)):f.error("Haori",`Form element not found: ${l} (${c.attrName(e,"form")})`)}else r.formFragment=E.getFormFragment(t)}else e==="change"&&(r.formFragment=E.getFormFragment(t));if(t.hasAttribute(`${h.prefix}${e}-before-run`)){const l=t.getRawAttribute(`${h.prefix}${e}-before-run`);try{r.beforeCallback=new Function("fetchUrl","fetchOptions",`
|
|
5
|
+
return (${t});`;try{i=new Function(...r,a),this.EXPRESSION_CACHE.set(n,i)}catch(o){return h.error("[Haori]","Failed to compile expression:",t,o),null}}try{const s=[],a=this.wrapBoundValues(e);return r.forEach(o=>{s.push(a[o])}),this.withBlockedPropertyAccess(()=>i(...s))}catch(s){return h.error("[Haori]","Expression evaluation error:",t,s),s instanceof ReferenceError?void 0:null}}static containsDangerousPatterns(t){return this.hasAllowedSyntax(t)?[/\beval\s*\(/,/\barguments\s*\[/,/\barguments\s*\./].some(r=>r.test(t)):!0}static hasAllowedSyntax(t){const e=this.tokenizeExpression(t);if(e===null||e.length===0)return!1;const r=[];let n=null;for(let i=0;i<e.length;i++){const s=e[i],a=e[i+1]||null,o=r[r.length-1]||null;if(s.type==="identifier"&&(this.DISALLOWED_KEYWORDS.has(s.value)||this.STRICT_FORBIDDEN_NAMES.includes(s.value)||(n?.value==="."||n?.value==="?.")&&this.FORBIDDEN_PROPERTY_NAMES.has(s.value))||o==="member"&&s.value!=="]"&&s.type==="string"&&this.FORBIDDEN_PROPERTY_NAMES.has(this.decodeStringLiteral(s.value))||s.value==="."&&a?.type!=="identifier"||s.value==="?."&&a?.type!=="identifier"&&a?.value!=="["&&a?.value!=="(")return!1;switch(s.value){case"(":r.push("paren");break;case")":{if(r.pop()!=="paren")return!1;break}case"[":{const p=this.startsMemberAccess(n)?"member":"array";r.push(p);break}case"]":{if(r.pop()===void 0)return!1;break}}n=s}return r.length===0}static tokenizeExpression(t){const e=[],r=["===","!==","...","?.","&&","||",">=","<=","==","!=","=>"],n=new Set(["(",")","[","]",".",",","?",":","+","-","*","/","%","!",">","<"]);let i=0;for(;i<t.length;){const s=t[i];if(/\s/.test(s)){i+=1;continue}if(s==="/"&&(t[i+1]==="/"||t[i+1]==="*"))return null;if(s==='"'||s==="'"){const o=this.readStringToken(t,i);if(o===null)return null;e.push(o.token),i=o.nextIndex;continue}const a=r.find(o=>t.startsWith(o,i));if(a){e.push({type:"operator",value:a,position:i}),i+=a.length;continue}if(/[0-9]/.test(s)){const o=this.readNumberToken(t,i);e.push(o.token),i=o.nextIndex;continue}if(/[A-Za-z_$]/.test(s)){const o=this.readIdentifierToken(t,i);e.push(o.token),i=o.nextIndex;continue}if(n.has(s)){e.push({type:"operator",value:s,position:i}),i+=1;continue}return null}return e}static readStringToken(t,e){const r=t[e];let n=e+1;for(;n<t.length;){const i=t[n];if(i==="\\"){n+=2;continue}if(i===r)return{token:{type:"string",value:t.slice(e,n+1),position:e},nextIndex:n+1};n+=1}return null}static readNumberToken(t,e){let r=e;for(;r<t.length&&/[0-9_]/.test(t[r]);)r+=1;if(t[r]===".")for(r+=1;r<t.length&&/[0-9_]/.test(t[r]);)r+=1;return{token:{type:"number",value:t.slice(e,r),position:e},nextIndex:r}}static readIdentifierToken(t,e){let r=e;for(;r<t.length&&/[A-Za-z0-9_$]/.test(t[r]);)r+=1;return{token:{type:"identifier",value:t.slice(e,r),position:e},nextIndex:r}}static startsMemberAccess(t){return t===null?!1:t.type==="identifier"||t.type==="number"?!0:t.value===")"||t.value==="]"||t.value==="?."}static decodeStringLiteral(t){return t.slice(1,-1).replace(/\\u([0-9a-fA-F]{4})/g,(e,r)=>String.fromCharCode(parseInt(r,16))).replace(/\\x([0-9a-fA-F]{2})/g,(e,r)=>String.fromCharCode(parseInt(r,16))).replace(/\\(["'\\bfnrtv0])/g,(e,r)=>{switch(r){case"b":return"\b";case"f":return"\f";case"n":return`
|
|
6
|
+
`;case"r":return"\r";case"t":return" ";case"v":return"\v";case"0":return"\0";default:return r}})}static wrapBoundValues(t){const e=new WeakMap,r={};return Object.entries(t).forEach(([n,i])=>{r[n]=this.wrapBoundValue(i,e)}),r}static wrapBoundValue(t,e){if(!this.shouldWrapValue(t))return t;const r=t,n=e.get(r);if(n!==void 0)return n;const i=new Proxy(r,{get:(s,a,o)=>{if(typeof a=="string"&&this.FORBIDDEN_PROPERTY_NAMES.has(a))return;const p=Reflect.get(s,a,o);return typeof a=="symbol"?p:this.wrapBoundValue(p,e)},has:(s,a)=>typeof a=="string"&&this.FORBIDDEN_PROPERTY_NAMES.has(a)?!1:Reflect.has(s,a),getOwnPropertyDescriptor:(s,a)=>{if(!(typeof a=="string"&&this.FORBIDDEN_PROPERTY_NAMES.has(a)))return Reflect.getOwnPropertyDescriptor(s,a)},apply:(s,a,o)=>{const p=Reflect.apply(s,a,o);return this.isIteratorLike(p)?p:this.wrapBoundValue(p,e)},construct:(s,a,o)=>this.wrapBoundValue(Reflect.construct(s,a,o),e)});return e.set(r,i),i}static shouldWrapValue(t){if(typeof t=="function")return!0;if(t===null||typeof t!="object")return!1;if(Array.isArray(t))return!0;const e=Object.getPrototypeOf(t);return e===Object.prototype||e===null}static withBlockedPropertyAccess(t){const r=[{target:Object.prototype,property:"constructor"},{target:Function.prototype,property:"constructor"},{target:Object.prototype,property:"__proto__"}].map(n=>({...n,descriptor:Object.getOwnPropertyDescriptor(n.target,n.property)})).filter(n=>n.descriptor?.configurable===!0);r.forEach(({target:n,property:i})=>{Object.defineProperty(n,i,{configurable:!0,enumerable:!1,get:()=>{},set:()=>{}})});try{return t()}finally{r.forEach(({target:n,property:i,descriptor:s})=>{s!==void 0&&Object.defineProperty(n,i,s)})}}static isIteratorLike(t){return t===null||typeof t!="object"?!1:typeof t.next=="function"}static containsForbiddenKeys(t){if(!t||typeof t!="object")return!1;for(const e of Object.keys(t))if(this.FORBIDDEN_BINDING_NAMES.has(e))return!0;return!1}static containsForbiddenBindingValues(t,e=new WeakSet){if(!t||typeof t!="object"||e.has(t))return!1;if(e.add(t),this.getForbiddenBindingValues().some(r=>r===t))return!0;for(const r of Object.values(t)){if(typeof r=="function"){if(this.getForbiddenBindingValues().some(n=>n===r))return!0;continue}if(this.containsForbiddenBindingValues(r,e))return!0}return!1}};S.FORBIDDEN_NAMES=["window","self","globalThis","frames","parent","top","Function","setTimeout","setInterval","requestAnimationFrame","alert","confirm","prompt","fetch","XMLHttpRequest","Reflect","constructor","__proto__","prototype","Object","document","location","navigator","localStorage","sessionStorage","IndexedDB","history"],S.STRICT_FORBIDDEN_NAMES=["eval","arguments"],S.REBINDABLE_FORBIDDEN_NAMES=new Set(["location"]),S.FORBIDDEN_BINDING_NAMES=new Set([...S.FORBIDDEN_NAMES.filter(t=>!S.REBINDABLE_FORBIDDEN_NAMES.has(t)),"constructor","__proto__","prototype",...S.STRICT_FORBIDDEN_NAMES]),S.FORBIDDEN_PROPERTY_NAMES=new Set(["constructor","__proto__","prototype"]),S.DISALLOWED_KEYWORDS=new Set(["await","break","case","catch","class","const","continue","debugger","default","delete","do","else","export","finally","for","function","if","import","in","instanceof","let","new","return","switch","this","throw","try","typeof","var","void","while","with","yield"]),S.EXPRESSION_CACHE=new Map;let $=S;const k=class k{constructor(t){this.parent=null,this.mounted=!1,this.skipMutationNodes=!1,this.target=t,k.FRAGMENT_CACHE.set(t,this)}static get(t){if(t==null)return null;if(k.FRAGMENT_CACHE.has(t))return k.FRAGMENT_CACHE.get(t);let e;switch(t.nodeType){case Node.ELEMENT_NODE:e=new P(t);break;case Node.TEXT_NODE:e=new I(t);break;case Node.COMMENT_NODE:e=new J(t);break;default:return h.warn("[Haori]","Unsupported node type:",t.nodeType),null}return e}isSkipMutationNodes(){return this.skipMutationNodes}unmount(){if(!this.mounted||this.skipMutationNodes)return Promise.resolve();if(this.parent){const t=this.parent,e=t.skipMutationNodes;return v.enqueue(()=>{t.skipMutationNodes=!0,this.target.parentNode===t.getTarget()&&t.getTarget().removeChild(this.target),this.mounted=!1}).finally(()=>{t.skipMutationNodes=e})}else{const t=this.target.parentNode;if(t)return v.enqueue(()=>{this.target.parentNode===t&&t.removeChild(this.target),this.mounted=!1});this.mounted=!1}return Promise.resolve()}mount(){if(this.mounted||this.skipMutationNodes)return Promise.resolve();if(this.parent){const t=this.parent,e=t.skipMutationNodes;return v.enqueue(()=>{t.skipMutationNodes=!0,this.target.parentNode!==t.getTarget()&&t.getTarget().appendChild(this.target),this.mounted=!0}).finally(()=>{t.skipMutationNodes=e})}return Promise.resolve()}isMounted(){return this.mounted}setMounted(t){this.mounted=t}remove(t=!0){return this.parent&&this.parent.removeChild(this),k.FRAGMENT_CACHE.delete(this.target),t?this.unmount():Promise.resolve()}getTarget(){return this.target}getParent(){return this.parent}setParent(t){this.parent=t}};k.FRAGMENT_CACHE=new WeakMap;let A=k;class P extends A{constructor(t){super(t),this.INPUT_EVENT_TYPES=["text","password","email","url","tel","search","number","range","color","date","datetime-local","month","time","week"],this.children=[],this.attributeMap=new Map,this.bindingData=null,this.bindingDataCache=null,this.visible=!0,this.display=null,this.template=null,this.listKey=null,this.value=null,this.skipMutationAttributes=!1,this.skipChangeValue=!1,this.syncValue(),t.getAttributeNames().forEach(e=>{const r=t.getAttribute(e);if(r!==null&&!this.attributeMap.has(e)){const n=new j(e,r);this.attributeMap.set(e,n)}}),t.childNodes.forEach(e=>{const r=A.get(e);r.setParent(this),this.children.push(r)})}getChildren(){return this.children}getChildElementFragments(){return this.children.filter(t=>t instanceof P)}pushChild(t){this.children.push(t),t.setParent(this)}removeChild(t){const e=this.children.indexOf(t);if(e<0){h.warn("[Haori]","Child fragment not found.",t);return}this.children.splice(e,1),t.setParent(null)}clone(){const t=new P(this.target.cloneNode(!1));return this.attributeMap.forEach((e,r)=>{t.attributeMap.set(r,e)}),this.children.forEach(e=>{const r=e.clone();t.getTarget().appendChild(r.getTarget()),t.pushChild(r)}),t.mounted=!1,t.bindingData=this.bindingData,t.clearBindingDataCache(),t.visible=this.visible,t.display=this.display,t.template=this.template,t}remove(t=!0){const e=[];return this.children.forEach(r=>{e.push(r.remove(!1))}),this.children.length=0,this.attributeMap.clear(),this.bindingData=null,this.bindingDataCache=null,this.template&&(e.push(this.template.remove(!1)),this.template=null),e.push(super.remove(t)),Promise.all(e).then(()=>{})}getTarget(){return this.target}getBindingData(){return this.bindingDataCache?this.bindingDataCache:(this.bindingDataCache={},this.parent&&Object.assign(this.bindingDataCache,this.parent.getBindingData()),this.bindingData&&Object.assign(this.bindingDataCache,this.bindingData),this.bindingDataCache)}getRawBindingData(){return this.bindingData}setBindingData(t){this.bindingData=t,this.clearBindingDataCache()}setParent(t){this.parent!==t&&(this.parent=t,this.clearBindingDataCache())}clearBindingDataCache(){this.bindingDataCache=null,this.children.forEach(t=>{t instanceof P&&t.clearBindingDataCache()})}getTemplate(){return this.template}setTemplate(t){this.template=t}setListKey(t){this.listKey=t}getListKey(){return this.listKey}setValue(t){if(this.skipChangeValue||this.value===t)return Promise.resolve();const e=this.getTarget();if(e instanceof HTMLInputElement&&(e.type==="checkbox"||e.type==="radio")){const r=this.getAttribute("value");let n;return r==="true"?n=t===!0:r==="false"?n=t===!1:n=r===String(t),this.value=n?t:null,e.checked===n?Promise.resolve():(this.skipChangeValue=!0,v.enqueue(()=>{e.checked=n,e.dispatchEvent(new Event("change",{bubbles:!0}))}).finally(()=>{this.skipChangeValue=!1}))}else return e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement?(this.value=t,this.skipChangeValue=!0,v.enqueue(()=>{e.value=t===null?"":String(t),(e instanceof HTMLInputElement&&this.INPUT_EVENT_TYPES.includes(e.type)||e instanceof HTMLTextAreaElement)&&e.dispatchEvent(new Event("input",{bubbles:!0})),e.dispatchEvent(new Event("change",{bubbles:!0}))}).finally(()=>{this.skipChangeValue=!1})):(h.warn("[Haori]","setValue is not supported for this element type.",e),Promise.resolve())}getValue(){return this.value}clearValue(){this.value=null}syncValue(){const t=this.getTarget();if(t instanceof HTMLInputElement)if(t.type==="checkbox"||t.type==="radio")if(t.checked){const e=t.value;e==="true"?this.value=!0:e==="false"?this.value=!1:this.value=e}else{const e=t.value;e==="true"?this.value=!1:e==="false"?this.value=!0:this.value=null}else this.value=t.value;else t instanceof HTMLTextAreaElement?this.value=t.value:t instanceof HTMLSelectElement&&(this.value=t.value)}setAttribute(t,e){if(this.skipMutationAttributes)return Promise.resolve();if(e===null)return this.removeAttribute(t);const r=new j(t,e);this.attributeMap.set(t,r),this.skipMutationAttributes=!0;const n=this.getTarget(),i=r.isForceEvaluation()?e:this.getAttribute(t);return v.enqueue(()=>{if(i===null||i===!1)n.removeAttribute(t);else{const s=String(i);n.getAttribute(t)!==s&&n.setAttribute(t,s)}}).finally(()=>{this.skipMutationAttributes=!1})}removeAttribute(t){if(this.skipMutationAttributes)return Promise.resolve();this.attributeMap.delete(t),this.skipMutationAttributes=!0;const e=this.getTarget();return v.enqueue(()=>{e.removeAttribute(t)}).finally(()=>{this.skipMutationAttributes=!1})}getAttribute(t){const e=this.attributeMap.get(t);if(e===void 0)return null;const r=e.evaluate(this.getBindingData());return r.length===1?r[0]:H.joinEvaluateResults(r)}getRawAttribute(t){const e=this.attributeMap.get(t);return e===void 0?null:e.getValue()}getAttributeNames(){return Array.from(this.attributeMap.keys())}hasAttribute(t){return this.attributeMap.has(t)}resolveInsertionPointFromDom(t,e){const r=t.getTarget();if(r.parentNode!==this.target)return null;const n=e?r.nextSibling:r;let i=e?r.nextSibling:r;for(;i!==null;){const s=A.get(i);if(s!==null){const a=this.children.indexOf(s);if(a!==-1)return{index:a,referenceNode:n}}i=i.nextSibling}return{index:this.children.length,referenceNode:n}}insertBefore(t,e,r){if(this.skipMutationNodes)return Promise.resolve();if(t===this)return h.error("[Haori]","Cannot insert element as child of itself"),Promise.reject(new Error("Self-insertion not allowed"));const n=new Set;let i=this.parent;for(;i;)n.add(i),i=i.getParent();if(n.has(t))return h.error("[Haori]","Cannot create circular reference"),Promise.reject(new Error("Circular reference detected"));const s=t.getParent()===this;let a=-1,o=-1;s&&(a=this.children.indexOf(t),e!==null&&(o=this.children.indexOf(e)));const p=t.getParent();p!==null&&p.removeChild(t);let c=r===void 0?e?.getTarget()||null:r;if(e===null)this.children.push(t);else{let l;if(s?a!==-1&&a<o?l=o-1:l=o:l=this.children.indexOf(e),l===-1){const d=this.resolveInsertionPointFromDom(e,!1);d===null?(h.warn("[Haori]","Reference child not found in children.",e),this.children.push(t)):(this.children.splice(d.index,0,t),c=d.referenceNode)}else this.children.splice(l,0,t)}t.setParent(this),t.setMounted(this.mounted);const y=this.skipMutationNodes;return this.skipMutationNodes=!0,v.enqueue(()=>{this.target.insertBefore(t.getTarget(),c)}).finally(()=>{this.skipMutationNodes=y})}insertAfter(t,e){if(e==null)return this.insertBefore(t,null);const r=this.children.indexOf(e);if(r===-1){const n=this.resolveInsertionPointFromDom(e,!0);return n===null?(h.warn("[Haori]","Reference child not found in children.",e),this.insertBefore(t,null)):this.insertBefore(t,this.children[n.index]||null,n.referenceNode)}return this.insertBefore(t,this.children[r+1]||null)}getPrevious(){const t=this.getParent();if(t===null)return null;const e=t.getChildElementFragments(),r=e.indexOf(this);return r<=0?null:e[r-1]}getNext(){const t=this.getParent();if(t===null)return null;const e=t.getChildElementFragments(),r=e.indexOf(this);return r<0||r+1>=e.length?null:e[r+1]}isVisible(){return this.visible}hide(){return this.visible=!1,this.display=this.getTarget().style.display,this.getTarget().style.display="none",this.getTarget().setAttribute(`${f.prefix}if-false`,""),Promise.resolve()}show(){return this.getTarget().style.display=this.display??"",this.getTarget().removeAttribute(`${f.prefix}if-false`),this.visible=!0,Promise.resolve()}closestByAttribute(t){if(this.hasAttribute(t))return this;const e=this.getParent();return e===null?null:e.closestByAttribute(t)}}class I extends A{constructor(t){super(t),this.skipMutation=!1,this.text=t.textContent||"",this.contents=new H(this.text)}clone(){const t=new I(this.target.cloneNode(!0));return t.mounted=!1,t.text=this.text,t.contents=this.contents,t}getTarget(){return this.target}setContent(t){return this.skipMutation||this.text===t?Promise.resolve():(this.text=t,this.contents=new H(t),this.evaluate())}evaluate(){return this.contents.isRawEvaluate&&this.parent===null?Promise.reject(new Error("Parent fragment is required for raw evaluation")):v.enqueue(()=>{this.skipMutation=!0,this.contents.isRawEvaluate?this.parent.getTarget().innerHTML=this.contents.evaluate(this.parent.getBindingData())[0]:this.contents.isEvaluate?this.target.textContent=H.joinEvaluateResults(this.contents.evaluate(this.parent.getBindingData())):this.target.textContent=this.text}).finally(()=>{this.skipMutation=!1})}}class J extends A{constructor(t){super(t),this.skipMutation=!1,this.text=t.textContent||""}clone(){const t=new J(this.target.cloneNode(!0));return t.mounted=!1,t.text=this.text,t}getTarget(){return this.target}setContent(t){return this.skipMutation||this.text===t?Promise.resolve():(this.text=t,v.enqueue(()=>{this.skipMutation=!0,this.target.textContent=this.text}).finally(()=>{this.skipMutation=!1}))}}const U=class U{constructor(t){this.contents=[],this.isEvaluate=!1,this.isRawEvaluate=!1,this.value=t;const e=[...t.matchAll(U.PLACEHOLDER_REGEX)];let r=0,n=!1,i=!1;for(const s of e){s.index>r&&this.contents.push({text:t.slice(r,s.index),type:0});const a={text:s[1]??s[2],type:s[1]?2:1};n=!0,i=i||a.type===2,this.contents.push(a),r=s.index+s[0].length}r<t.length&&this.contents.push({text:t.slice(r),type:0}),this.isEvaluate=n,this.isRawEvaluate=i,this.checkRawExpressions()}static joinEvaluateResults(t){return t===null||t.length===0?"":t.map(e=>e==null||e===!1||Number.isNaN(e)?"":typeof e!="string"?String(e):e).join("")}getValue(){return this.value}checkRawExpressions(){for(let t=0;t<this.contents.length;t++)this.contents[t].type===2&&this.contents.length>1&&(h.error("[Haori]","Raw expressions are not allowed in multi-content expressions."),this.contents[t].type=1)}evaluate(t){if(!this.isEvaluate&&!this.isRawEvaluate)return this.contents.map(r=>r.text);const e=[];return this.contents.forEach(r=>{try{if(r.type===1||r.type===2){const n=$.evaluate(r.text,t);e.push(n)}else e.push(r.text)}catch(n){h.error("[Haori]",`Error evaluating text expression: ${r.text}`,n),e.push("")}}),e}};U.PLACEHOLDER_REGEX=/\{\{\{([\s\S]+?)\}\}\}|\{\{([\s\S]+?)\}\}/g;let H=U;const q=class q extends H{constructor(t,e){super(e),this.forceEvaluation=q.FORCE_EVALUATION_ATTRIBUTES.includes(t)}isForceEvaluation(){return this.forceEvaluation}evaluate(t){if(!this.isEvaluate&&!this.forceEvaluation)return this.contents.map(r=>r.text);const e=[];return this.contents.forEach(r=>{try{if(this.forceEvaluation&&r.type===0||r.type===1||r.type===2){const n=$.evaluate(r.text,t);e.push(n)}else e.push(r.text)}catch(n){h.error("[Haori]",`Error evaluating attribute expression: ${r.text}`,n),e.push("")}}),this.forceEvaluation&&e.length>1?(h.error("[Haori]","each or if expressions must have a single content.",e),[e[0]]):e}};q.FORCE_EVALUATION_ATTRIBUTES=["data-if","hor-if","data-each","hor-each"];let j=q;class E{static dispatch(t,e,r,n){const i=new CustomEvent(`haori:${e}`,{bubbles:n?.bubbles??!0,cancelable:n?.cancelable??!1,composed:n?.composed??!0,detail:r});return t.dispatchEvent(i)}static ready(t){E.dispatch(document,"ready",{version:t})}static render(t){E.dispatch(t,"render",{target:t})}static importStart(t,e){E.dispatch(t,"importstart",{url:e,startedAt:performance.now()})}static importEnd(t,e,r,n){E.dispatch(t,"importend",{url:e,bytes:r,durationMs:performance.now()-n})}static importError(t,e,r){E.dispatch(t,"importerror",{url:e,error:r})}static bindChange(t,e,r,n="other"){const i=[],s=new Set(Object.keys(e||{})),a=new Set(Object.keys(r)),o=new Set([...s,...a]);for(const p of o){const c=e?.[p],y=r[p];c!==y&&i.push(p)}E.dispatch(t,"bindchange",{previous:e||{},next:r,changedKeys:i,reason:n})}static eachUpdate(t,e,r,n){E.dispatch(t,"eachupdate",{added:e,removed:r,order:n,total:n.length})}static rowAdd(t,e,r,n){E.dispatch(t,"rowadd",{key:e,index:r,item:n})}static rowRemove(t,e,r){E.dispatch(t,"rowremove",{key:e,index:r})}static rowMove(t,e,r,n){E.dispatch(t,"rowmove",{key:e,from:r,to:n})}static show(t){E.dispatch(t,"show",{visible:!0})}static hide(t){E.dispatch(t,"hide",{visible:!1})}static fetchStart(t,e,r,n){E.dispatch(t,"fetchstart",{url:e,options:r||{},payload:n,startedAt:performance.now()})}static fetchEnd(t,e,r,n){E.dispatch(t,"fetchend",{url:e,status:r,durationMs:performance.now()-n})}static fetchError(t,e,r,n,i){E.dispatch(t,"fetcherror",{url:e,status:n,error:r,durationMs:i?performance.now()-i:void 0})}}const Z=["addErrorMessage","closeDialog","confirm","dialog","openDialog","toast"];function G(){const t=globalThis.window?.Haori;return Z.every(r=>typeof t?.[r]=="function")?t:K}const u=class u{static attrName(t,e,r=!1){return t?`${f.prefix}${t}-${e}`:r?`${f.prefix}fetch-${e}`:`${f.prefix}${e}`}static resolveDataParamString(t,e){return t.replace(u.DATA_PLACEHOLDER_REGEX,(r,n,i)=>{const s=$.evaluate(n??i??"",e);return s==null||Number.isNaN(s)?"":encodeURIComponent(typeof s=="object"?JSON.stringify(s):String(s))})}static isJsonStringContext(t,e){let r=!1,n=!1;for(let i=0;i<e;i+=1){const s=t[i];if(n){n=!1;continue}if(s==="\\"){n=!0;continue}s==='"'&&(r=!r)}return r}static stringifyJsonTemplateValue(t){if(t===void 0||Number.isNaN(t))return"null";try{return JSON.stringify(t)??JSON.stringify(String(t))}catch{return JSON.stringify(String(t))}}static stringifyJsonTemplateStringContent(t){if(t==null||Number.isNaN(t))return"";const e=typeof t=="object"?u.stringifyJsonTemplateValue(t):String(t);return JSON.stringify(e).slice(1,-1)}static resolveDataJsonString(t,e){return t.replace(u.DATA_PLACEHOLDER_REGEX,(r,n,i,s)=>{const a=$.evaluate(n??i??"",e);return u.isJsonStringContext(t,s)?u.stringifyJsonTemplateStringContent(a):u.stringifyJsonTemplateValue(a)})}static resolveDataAttribute(t,e){const r=t.getRawAttribute(e),n=t.getAttribute(e);if(n&&typeof n=="object"&&!Array.isArray(n))return n;if(typeof n!="string"||r===null)return null;const i=r.trim();return u.SINGLE_PLACEHOLDER_REGEX.test(i)?T.parseDataBind(n):i.startsWith("{")||i.startsWith("[")?T.parseDataBind(u.resolveDataJsonString(r,t.getBindingData())):T.parseDataBind(u.resolveDataParamString(r,t.getBindingData()))}static buildOptions(t,e){const r={targetFragment:t};if(e){if(t.hasAttribute(u.attrName(e,"validate"))&&(r.valid=!0),t.hasAttribute(u.attrName(e,"confirm"))&&(r.confirmMessage=t.getAttribute(u.attrName(e,"confirm"))),t.hasAttribute(u.attrName(e,"data"))&&(r.data=u.resolveDataAttribute(t,u.attrName(e,"data"))),t.hasAttribute(u.attrName(e,"form"))){const l=t.getRawAttribute(u.attrName(e,"form"));if(l){const d=document.body.querySelector(l);d!==null?r.formFragment=b.getFormFragment(A.get(d)):h.error("Haori",`Form element not found: ${l} (${u.attrName(e,"form")})`)}else r.formFragment=b.getFormFragment(t)}else e==="change"&&(r.formFragment=b.getFormFragment(t));if(t.hasAttribute(`${f.prefix}${e}-before-run`)){const l=t.getRawAttribute(`${f.prefix}${e}-before-run`);try{r.beforeCallback=new Function("fetchUrl","fetchOptions",`
|
|
7
7
|
"use strict";
|
|
8
8
|
${l}
|
|
9
|
-
`)}catch(d){
|
|
9
|
+
`)}catch(d){h.error("Haori",`Invalid before script: ${d}`)}}}const n=u.attrName(e,"fetch"),i=t.hasAttribute(n);i&&(r.fetchUrl=t.getAttribute(n));const s={};if(e){const l=u.attrName(e,"fetch-method");t.hasAttribute(l)&&(s.method=t.getAttribute(l))}else{const l=u.attrName(null,"method",!0);t.hasAttribute(l)&&(s.method=t.getAttribute(l))}if(e){const l=u.attrName(e,"fetch-headers");if(t.hasAttribute(l)){const d=t.getRawAttribute(l);try{s.headers=T.parseDataBind(d)}catch(g){h.error("Haori",`Invalid fetch headers: ${g}`)}}}else{const l=u.attrName(null,"headers",!0);if(t.hasAttribute(l)){const d=t.getRawAttribute(l);try{s.headers=T.parseDataBind(d)}catch(g){h.error("Haori",`Invalid fetch headers: ${g}`)}}}if(e){const l=u.attrName(e,"fetch-content-type");if(t.hasAttribute(l))s.headers={...s.headers,"Content-Type":t.getAttribute(l)};else if(s.method&&s.method!=="GET"&&s.method!=="HEAD"&&s.method!=="OPTIONS"){let d=!1;s.headers&&typeof s.headers=="object"&&(d="Content-Type"in s.headers),d||(s.headers={...s.headers,"Content-Type":"application/json"})}else s.method&&(s.method==="GET"||s.method==="HEAD"||s.method==="OPTIONS")&&(s.headers={...s.headers,"Content-Type":"application/x-www-form-urlencoded"})}else{const l=u.attrName(null,"content-type",!0);if(t.hasAttribute(l))s.headers={...s.headers,"Content-Type":t.getAttribute(l)};else if(s.method&&s.method!=="GET"&&s.method!=="HEAD"&&s.method!=="OPTIONS"){let d=!1;s.headers&&typeof s.headers=="object"&&(d="Content-Type"in s.headers),d||(s.headers={...s.headers,"Content-Type":"application/json"})}else s.method&&(s.method==="GET"||s.method==="HEAD"||s.method==="OPTIONS")&&(s.headers={...s.headers,"Content-Type":"application/x-www-form-urlencoded"})}Object.keys(s).length>0&&(r.fetchOptions=s);const a=e?u.attrName(e,"bind"):u.attrName(null,"bind",!0);if(t.hasAttribute(a)){const l=t.getRawAttribute(a);if(l){const d=document.body.querySelectorAll(l);d.length>0?(r.bindFragments=[],d.forEach(g=>{const w=A.get(g);w&&r.bindFragments.push(w)})):h.error("Haori",`Bind element not found: ${l} (${a})`)}}const o=u.attrName(e,"bind-arg"),p=u.attrName(null,"arg",!0),c=u.attrName(null,"bind-arg",!0);e?t.hasAttribute(o)&&(r.bindArg=t.getRawAttribute(o)):t.hasAttribute(p)?r.bindArg=t.getRawAttribute(p):t.hasAttribute(c)&&(r.bindArg=t.getRawAttribute(c));const y=e?u.attrName(e,"bind-params"):u.attrName(null,"bind-params",!0);if(t.hasAttribute(y)){const l=t.getRawAttribute(y);r.bindParams=l.split("&").map(d=>d.trim())}if(e){if(t.hasAttribute(u.attrName(e,"adjust"))){const d=t.getRawAttribute(u.attrName(e,"adjust"));if(d){const g=document.body.querySelectorAll(d);g.length>0?(r.adjustFragments=[],g.forEach(w=>{const N=A.get(w);N&&r.adjustFragments.push(N)})):h.error("Haori",`Adjust element not found: ${d} (${u.attrName(e,"adjust")})`)}if(t.hasAttribute(u.attrName(e,"adjust-value"))){const g=t.getRawAttribute(u.attrName(e,"adjust-value")),w=Number(g);isNaN(w)||(r.adjustValue=w)}}if(t.hasAttribute(u.attrName(e,"row-add"))&&(r.rowAdd=!0),t.hasAttribute(u.attrName(e,"row-remove"))&&(r.rowRemove=!0),t.hasAttribute(u.attrName(e,"row-prev"))&&(r.rowMovePrev=!0),t.hasAttribute(u.attrName(e,"row-next"))&&(r.rowMoveNext=!0),t.hasAttribute(`${f.prefix}${e}-after-run`)){const d=t.getRawAttribute(`${f.prefix}${e}-after-run`);try{r.afterCallback=new Function("response",`
|
|
10
10
|
"use strict";
|
|
11
11
|
${d}
|
|
12
|
-
`)}catch(
|
|
13
|
-
`)}):typeof o=="string"?
|
|
14
|
-
`)}):typeof o=="string"&&i.push({key:a,message:o}))}if(i.length===0){await r(`${t.status} ${t.statusText}`);return}for(const a of i)a.key&&e?await E.addErrorMessage(e,a.key,a.message):await r(a.message);return}catch{}try{const s=await t.text();s&&s.trim().length>0?await r(s.trim()):await r(`${t.status} ${t.statusText}`)}catch{await r(`${t.status} ${t.statusText}`)}}validate(t){if(this.options.valid!==!0)return!0;const e=t.getTarget();let r=this.validateOne(t);return r||e.focus(),t.getChildElementFragments().reverse().forEach(n=>{r&&=this.validate(n)}),r}validateOne(t){const e=t.getTarget();return e instanceof HTMLInputElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement?e.reportValidity():!0}confirm(){const t=this.options.confirmMessage;return t==null?Promise.resolve(!0):G().confirm(t)}bindResult(t){return!this.options.bindFragments||this.options.bindFragments.length===0?Promise.resolve():(t.headers.get("Content-Type")?.includes("application/json")?t.json():t.text()).then(r=>{if(this.options.bindParams){const s={};this.options.bindParams.forEach(i=>{r&&typeof r=="object"&&i in r&&(s[i]=r[i])}),r=s}const n=[];if(this.options.bindArg)this.options.bindFragments.forEach(s=>{const i=s.getBindingData();i[this.options.bindArg]=r,n.push(T.setBindingData(s.getTarget(),i))});else{if(typeof r=="string")return f.error("Haori","string data cannot be bound without a bindArg."),Promise.reject(new Error("string data cannot be bound without a bindArg."));this.options.bindFragments.forEach(s=>{n.push(T.setBindingData(s.getTarget(),r))})}return Promise.all(n).then(()=>{})})}adjust(){if(!this.options.adjustFragments||this.options.adjustFragments.length===0)return Promise.resolve();const t=this.options.adjustValue??0,e=[];for(const r of this.options.adjustFragments){let n=r.getValue();(n==null||n==="")&&(n="0");let s=Number(n);isNaN(s)&&(s=0),s+=t,e.push(r.setValue(String(s)))}return Promise.all(e).then(()=>{})}getRowFragment(){if(!this.options.targetFragment)return f.error("Haori","Target fragment is not specified for row operation."),null;const t=this.options.targetFragment.closestByAttribute(`${h.prefix}row`);return t||(f.error("Haori","Row fragment not found."),null)}addRow(){if(this.options.rowAdd!==!0)return Promise.resolve();const t=this.getRowFragment();if(!t)return Promise.reject(new Error("Row fragment not found."));const e=[],r=t.clone();return e.push(t.getParent().insertAfter(r,t)),e.push(T.evaluateAll(r)),e.push(E.reset(r)),Promise.all(e).then(()=>{})}removeRow(){if(this.options.rowRemove!==!0)return Promise.resolve();const t=this.getRowFragment();if(!t)return Promise.reject(new Error("Row fragment not found."));const e=t.getParent();return e&&e.getChildElementFragments().filter(n=>!n.hasAttribute(`${h.prefix}each-before`)&&!n.hasAttribute(`${h.prefix}each-after`)).length<=1?Promise.resolve():t.remove()}movePrevRow(){if(this.options.rowMovePrev!==!0)return Promise.resolve();const t=this.getRowFragment();if(!t)return Promise.reject(new Error("Row fragment not found."));const e=t.getPrevious();if(!e)return Promise.resolve();const r=t.getParent();return r?r.insertBefore(t,e):Promise.resolve()}moveNextRow(){if(this.options.rowMoveNext!==!0)return Promise.resolve();const t=this.getRowFragment();if(!t)return Promise.reject(new Error("Row fragment not found."));const e=t.getNext();if(!e)return Promise.resolve();const r=t.getParent();return r?r.insertAfter(t,e):Promise.resolve()}};c.DATA_PLACEHOLDER_REGEX=/\{\{\{([\s\S]+?)\}\}\}|\{\{([\s\S]+?)\}\}/g,c.SINGLE_PLACEHOLDER_REGEX=/^(\{\{\{[\s\S]+?\}\}\}|\{\{[\s\S]+?\}\})$/;let j=c;class _{static readParams(){const t={},e=window.location.search;return new URLSearchParams(e).forEach((n,s)=>{t[s]=n}),t}}class tt{static async load(t,e){let r;try{r=await fetch(t,e)}catch(s){throw f.error("[Haori]","Failed to fetch import source:",t,s),new Error(`Failed to fetch: ${t}`)}if(!r.ok){const s=`${r.status} ${r.statusText}`;throw f.error("[Haori]","Import HTTP error:",t,s),new Error(`Failed to load ${t}: ${s}`)}let n;try{n=await r.text()}catch(s){throw f.error("[Haori]","Failed to read response text:",t,s),new Error(`Failed to read response from: ${t}`)}try{const i=new DOMParser().parseFromString(n,"text/html");return i&&i.body?i.body.innerHTML:(f.warn("[Haori]","No body found in imported document:",t),n)}catch(s){return f.error("[Haori]","Failed to parse imported HTML:",t,s),n}}}const m=class m{static isDeferredAttributeName(t){return m.DEFERRED_ATTRIBUTE_SUFFIXES.some(e=>t===`${h.prefix}${e}`)}static scan(t){const e=A.get(t);if(!e)return Promise.resolve();t.parentNode&&(A.get(t.parentNode)?.isMounted()||document.body.contains(t)?e.setMounted(!0):e.setMounted(!1));const r=[],n=new Set;for(const s of m.PRIORITY_ATTRIBUTE_SUFFIXES){const i=h.prefix+s;e.hasAttribute(i)&&(r.push(m.setAttribute(e.getTarget(),i,e.getRawAttribute(i))),n.add(i))}for(const s of e.getAttributeNames()){if(n.has(s)||m.isDeferredAttributeName(s))continue;const i=e.getRawAttribute(s);i!==null&&r.push(m.setAttribute(e.getTarget(),s,i))}for(const s of m.DEFERRED_ATTRIBUTE_SUFFIXES){const i=h.prefix+s;e.hasAttribute(i)&&(r.push(m.setAttribute(e.getTarget(),i,e.getRawAttribute(i))),n.add(i))}return e.getChildren().forEach(s=>{s instanceof x?r.push(m.scan(s.getTarget())):s instanceof H&&r.push(m.evaluateText(s))}),Promise.all(r).then(()=>{})}static setAttribute(t,e,r){const n=A.get(t),s=[];switch(e){case`${h.prefix}bind`:{r===null?(n.clearBindingDataCache(),n.setBindingData({})):n.setBindingData(m.parseDataBind(r));break}case`${h.prefix}if`:s.push(m.evaluateIf(n));break;case`${h.prefix}each`:s.push(m.evaluateEach(n));break;case`${h.prefix}fetch`:s.push(new j(n,null).run());break;case`${h.prefix}import`:{if(typeof r=="string"){const i=n.getTarget(),a=performance.now();y.importStart(i,r),s.push(tt.load(r).then(o=>{const g=new TextEncoder().encode(o).length;return v.enqueue(()=>{i.innerHTML=o}).then(()=>{y.importEnd(i,r,g,a)})}).catch(o=>{y.importError(i,r,o),f.error("[Haori]","Failed to import HTML:",r,o)}))}break}case`${h.prefix}url-param`:{const i=n.getAttribute(`${h.prefix}url-arg`),a=_.readParams();if(i===null)m.setBindingData(t,a);else{const o=n.getRawBindingData()||{};o[String(i)]=a,m.setBindingData(t,o)}break}}return r===null?s.push(n.removeAttribute(e)):s.push(n.setAttribute(e,r)),Promise.all(s).then(()=>{})}static setBindingData(t,e){const r=A.get(t),n=r.getRawBindingData();r.setBindingData(e);const s=[];return s.push(r.setAttribute(`${h.prefix}bind`,JSON.stringify(e))),s.push(m.evaluateAll(r)),y.bindChange(t,n,e,"manual"),Promise.all(s).then(()=>{})}static parseDataBind(t){if(t.startsWith("{")||t.startsWith("["))try{return JSON.parse(t)}catch(e){return f.error("[Haori]","Invalid JSON in data-bind:",e),{}}else{const e=new URLSearchParams(t),r={};for(const[n,s]of e.entries())r[n]!==void 0?Array.isArray(r[n])?r[n].push(s):r[n]=[r[n],s]:r[n]=s;return r}}static addNode(t,e){const r=A.get(t);if(r.isSkipMutationNodes())return;const n=A.get(e.nextSibling),s=A.get(e);s&&(r.insertBefore(s,n),s instanceof x?m.scan(s.getTarget()):s instanceof H&&m.evaluateText(s))}static removeNode(t){const e=A.get(t);if(e){const r=e.getParent();if(r&&r.isSkipMutationNodes())return;e.remove()}}static changeText(t,e){const r=A.get(t);r&&r.setContent(e)}static changeValue(t,e){const r=A.get(t);if(r.getValue()===e)return Promise.resolve();const n=[];n.push(r.setValue(e));const s=m.getFormFragment(r);if(s){const i=E.getValues(s),a=s.getAttribute(`${h.prefix}form-arg`);let o;a?(o=s.getRawBindingData(),o||(o={}),o[String(a)]=i):o=i,n.push(m.setBindingData(s.getTarget(),o))}return Promise.all(n).then(()=>{})}static getFormFragment(t){if(t.getTarget()instanceof HTMLFormElement)return t;const e=t.getParent();return e?m.getFormFragment(e):null}static evaluateAll(t){const e=[];return t.hasAttribute(`${h.prefix}if`)&&e.push(m.evaluateIf(t)),t.hasAttribute(`${h.prefix}each`)&&e.push(m.evaluateEach(t)),t.getChildren().forEach(r=>{r instanceof x?e.push(m.evaluateAll(r)):r instanceof H&&e.push(m.evaluateText(r))}),Promise.all(e).then(()=>{})}static evaluateText(t){return t.evaluate()}static evaluateIf(t){const e=[],r=t.getAttribute(`${h.prefix}if`);return r===!1||r===void 0||r===null||Number.isNaN(r)?t.isVisible()&&e.push(t.hide().then(()=>{y.hide(t.getTarget())})):t.isVisible()||(e.push(t.show().then(()=>{y.show(t.getTarget())})),e.push(m.evaluateAll(t))),Promise.all(e).then(()=>{})}static evaluateEach(t){if(!t.isVisible()||!t.isMounted())return Promise.resolve();let e=t.getTemplate();if(e===null){let n=!1;t.getChildren().forEach(i=>{if(!n&&i instanceof x){if(i.hasAttribute(`${h.prefix}each-before`)||i.hasAttribute(`${h.prefix}each-after`))return;e=i.clone(),t.setTemplate(e),n=!0,t.removeChild(i);const a=i.getTarget();a.parentNode&&a.parentNode.removeChild(a),i.setMounted(!1)}});const s=t.getAttribute(`${h.prefix}each`);return Array.isArray(s)?this.updateDiff(t,s):(f.error("[Haori]","Invalid each attribute:",s),Promise.reject(new Error("Invalid each attribute.")))}const r=t.getAttribute(`${h.prefix}each`);return Array.isArray(r)?this.updateDiff(t,r):(f.error("[Haori]","Invalid each attribute:",r),Promise.reject(new Error("Invalid each attribute.")))}static updateDiff(t,e){const r=t.getTemplate();if(r===null)return f.error("[Haori]","Template is not set for each element."),Promise.resolve();let n=t.getAttribute(`${h.prefix}each-index`);n&&(n=String(n));const s=t.getAttribute(`${h.prefix}each-key`),i=t.getAttribute(`${h.prefix}each-arg`),a=new Map,o=[];e.forEach((p,w)=>{const N=m.createListKey(p,s?String(s):null,w);o.push(N),a.set(N,{item:p,itemIndex:w})});const g=[];let u=t.getChildren().filter(p=>p instanceof x).filter(p=>!p.hasAttribute(`${h.prefix}each-before`)&&!p.hasAttribute(`${h.prefix}each-after`));u=u.filter(p=>o.indexOf(String(p.getListKey()))===-1?(g.push(p.remove()),!1):!0);const b=u.map(p=>p.getListKey()),l=t.getChildren().filter(p=>p instanceof x).filter(p=>p.hasAttribute(`${h.prefix}each-before`)).length;let d=Promise.resolve();return o.forEach((p,w)=>{const N=b.indexOf(p),{item:$,itemIndex:S}=a.get(p);let F;if(N!==-1)F=u[N],m.updateRowFragment(F,$,n,S,i?String(i):null,p),typeof F.clearBindingDataCache=="function"&&F.clearBindingDataCache(),d=d.then(()=>m.evaluateAll(F));else{F=r.clone(),m.updateRowFragment(F,$,n,S,i?String(i):null,p),typeof F.clearBindingDataCache=="function"&&F.clearBindingDataCache();const Y=l+w;d=d.then(()=>t.insertBefore(F,t.getChildren()[Y]||null).then(()=>m.evaluateAll(F)))}}),Promise.all(g).then(()=>d).then(()=>{const p=o.filter(S=>S!==null),w=b.filter(S=>S!==null),N=p.filter(S=>!w.includes(S)),$=w.filter(S=>!p.includes(S));y.eachUpdate(t.getTarget(),N,$,p)})}static createListKey(t,e,r){let n;if(typeof t=="object"&&t!==null)if(e){const s=t[e];s==null?n=`__index_${r}`:typeof s=="object"?n=JSON.stringify(s):n=String(s)}else n=`__index_${r}`;else n=String(t);return n}static updateRowFragment(t,e,r,n,s,i){let a=e;if(typeof e=="object"&&e!==null)a={...e},r&&(a[r]=n),s&&(a={[s]:a});else if(s)a={[s]:e},r&&(a[r]=n);else{f.error("[Haori]",`Primitive value requires '${h.prefix}each-arg' attribute: ${e}`);return}t.setListKey(i),t.setAttribute(`${h.prefix}row`,i),t.setBindingData(a)}};m.PRIORITY_ATTRIBUTE_SUFFIXES=["bind","if","each"],m.DEFERRED_ATTRIBUTE_SUFFIXES=["fetch","url-param"];let T=m;class et{constructor(t=document){this.onClick=e=>this.delegate(e,"click"),this.onChange=e=>this.delegate(e,"change"),this.onLoadCapture=e=>this.delegate(e,"load"),this.onWindowLoad=()=>{const e=document.documentElement,r=A.get(e);r&&new j(r,"load").run()},this.root=t}start(){this.root.addEventListener("click",this.onClick),this.root.addEventListener("change",this.onChange),this.root.addEventListener("load",this.onLoadCapture,!0),window.addEventListener("load",this.onWindowLoad,{once:!0})}stop(){this.root.removeEventListener("click",this.onClick),this.root.removeEventListener("change",this.onChange),this.root.removeEventListener("load",this.onLoadCapture,!0),window.removeEventListener("load",this.onWindowLoad)}delegate(t,e){const r=this.getElementFromTarget(t.target);if(!r)return;const n=A.get(r);n&&(e==="change"&&n instanceof x&&n.syncValue(),new j(n,e).run().catch(s=>{f.error("[Haori]","Procedure execution error:",s)}))}getElementFromTarget(t){return t?t instanceof HTMLElement?t:t instanceof Node?t.parentElement:null:null}}const C=class C{static async init(){if(C._initialized)return;C._initialized=!0;const t=await Promise.allSettled([T.scan(document.head),T.scan(document.body)]),[e,r]=t;e.status!=="fulfilled"&&f.error("[Haori]","Failed to build head fragment:",e.reason),r.status!=="fulfilled"&&f.error("[Haori]","Failed to build body fragment:",r.reason),C.observe(document.head),C.observe(document.body),new et().start()}static observe(t){new MutationObserver(async r=>{for(const n of r)try{switch(n.type){case"attributes":{f.info("[Haori]","Attribute changed:",n.target,n.attributeName);const s=n.target;T.setAttribute(s,n.attributeName,s.getAttribute(n.attributeName));break}case"childList":{f.info("[Haori]","Child list changed:",Array.from(n.removedNodes).map(s=>s.nodeName),Array.from(n.addedNodes).map(s=>s.nodeName)),Array.from(n.removedNodes).forEach(s=>{T.removeNode(s)}),Array.from(n.addedNodes).forEach(s=>{s.parentElement instanceof HTMLElement&&T.addNode(s.parentElement,s)});break}case"characterData":{f.info("[Haori]","Character data changed:",n.target,n.target.textContent),n.target instanceof Text||n.target instanceof Comment?T.changeText(n.target,n.target.textContent):f.warn("[Haori]","Unsupported character data type:",n.target);break}default:f.warn("[Haori]","Unknown mutation type:",n.type);continue}}catch(s){f.error("[Haori]","Error processing mutation:",s)}}).observe(t,{childList:!0,subtree:!0,attributes:!0,characterData:!0}),f.info("[Haori]","Observer initialized for",t)}};C._initialized=!1;let V=C;document.readyState==="loading"?document.addEventListener("DOMContentLoaded",V.init):V.init();const rt="0.1.4";exports.Core=T;exports.Env=h;exports.Form=E;exports.Fragment=A;exports.Haori=K;exports.Log=f;exports.Queue=v;exports.default=K;exports.version=rt;
|
|
12
|
+
`)}catch(g){h.error("Haori",`Invalid after script: ${g}`)}}if(t.hasAttribute(u.attrName(e,"dialog"))&&(r.dialogMessage=t.getAttribute(u.attrName(e,"dialog"))),t.hasAttribute(u.attrName(e,"toast"))&&(r.toastMessage=t.getAttribute(u.attrName(e,"toast"))),t.hasAttribute(u.attrName(e,"redirect"))&&(r.redirectUrl=t.getAttribute(u.attrName(e,"redirect"))),t.hasAttribute(u.attrName(e,"history"))&&(r.historyUrl=t.getAttribute(u.attrName(e,"history"))),t.hasAttribute(u.attrName(e,"history-data"))&&(r.historyData=u.resolveDataAttribute(t,u.attrName(e,"history-data"))),t.hasAttribute(u.attrName(e,"history-form"))){const d=t.getRawAttribute(u.attrName(e,"history-form"));if(d){const g=document.body.querySelector(d);g!==null?r.historyFormFragment=b.getFormFragment(A.get(g)):h.error("Haori",`Form element not found: ${d} (${u.attrName(e,"history-form")})`)}else r.historyFormFragment=b.getFormFragment(t)}["reset","refetch","click","open","close"].forEach(d=>{const g=u.attrName(e,d);if(!t.hasAttribute(g))return;const w=t.getRawAttribute(g),N=[];if(w?(document.body.querySelectorAll(w).forEach(F=>{const x=A.get(F);x&&N.push(x)}),N.length===0&&h.error("Haori",`Element not found: ${w} (${g})`)):N.push(t),N.length>0)switch(d){case"reset":r.resetFragments=N;break;case"refetch":r.refetchFragments=N;break;case"click":r.clickFragments=N;break;case"open":r.openFragments=N;break;case"close":r.closeFragments=N;break}})}if(!e&&(t.hasAttribute(u.attrName(null,"data",!0))&&(r.data=u.resolveDataAttribute(t,u.attrName(null,"data",!0))),t.hasAttribute(u.attrName(null,"form",!0)))){const l=t.getRawAttribute(u.attrName(null,"form",!0));if(l){const d=document.body.querySelector(l);d!==null?r.formFragment=b.getFormFragment(A.get(d)):h.error("Haori",`Form element not found: ${l} (${u.attrName(null,"fetch-form",!0)})`)}else r.formFragment=b.getFormFragment(t)}return i&&(!r.bindFragments||r.bindFragments.length===0)&&(r.bindFragments=[t]),r}static isElementFragment(t){if(typeof t!="object"||t===null)return!1;const e=t;return typeof e.getTarget=="function"&&typeof e.getChildElementFragments=="function"}constructor(t,e=null){u.isElementFragment(t)?this.options=u.buildOptions(t,e):this.options=t}run(){return Object.keys(this.options).length===0||this.options.formFragment&&this.validate(this.options.formFragment)===!1?Promise.resolve():this.confirm().then(t=>{if(!t)return Promise.resolve();let e=this.options.fetchUrl,r=this.options.fetchOptions;if(this.options.beforeCallback){const s=this.options.beforeCallback(e||null,r||null);if(s!=null){if(s===!1||typeof s=="object"&&s.stop)return Promise.resolve();typeof s=="object"&&(e="fetchUrl"in s?s.fetchUrl:e,r="fetchOptions"in s?s.fetchOptions:r)}}const n={};if(this.options.formFragment){const s=b.getValues(this.options.formFragment);Object.assign(n,s)}this.options.data&&typeof this.options.data=="object"&&Object.assign(n,this.options.data);const i=Object.keys(n).length>0;if(e){const s={...r||{}},a=new Headers(s.headers||void 0),o=(s.method||"GET").toUpperCase();if(o==="GET"||o==="HEAD"||o==="OPTIONS"){if(i){const p=new URL(e,window.location.href),c=new URLSearchParams(p.search);for(const[y,l]of Object.entries(n))l!==void 0&&(l===null?c.append(y,""):Array.isArray(l)?l.forEach(d=>{c.append(y,String(d))}):typeof l=="object"||typeof l=="function"?c.append(y,JSON.stringify(l)):c.append(y,String(l)));p.search=c.toString(),e=p.toString()}}else if(i){const p=a.get("Content-Type")||"";if(/multipart\/form-data/i.test(p)){a.delete("Content-Type");const c=new FormData;for(const[y,l]of Object.entries(n))l==null?c.append(y,""):l instanceof Blob?c.append(y,l):Array.isArray(l)?l.forEach(d=>c.append(y,String(d))):typeof l=="object"?c.append(y,JSON.stringify(l)):c.append(y,String(l));s.body=c}else if(/application\/x-www-form-urlencoded/i.test(p)){const c=new URLSearchParams;for(const[y,l]of Object.entries(n))l!==void 0&&(l===null?c.append(y,""):Array.isArray(l)?l.forEach(d=>c.append(y,String(d))):typeof l=="object"?c.append(y,JSON.stringify(l)):c.append(y,String(l)));s.body=c}else a.set("Content-Type","application/json"),s.body=JSON.stringify(n)}if(s.headers=a,this.options.targetFragment&&e){const p=performance.now();return E.fetchStart(this.options.targetFragment.getTarget(),e,s,i?n:void 0),fetch(e,s).then(c=>this.handleFetchResult(c,e||void 0,p)).catch(c=>{throw e&&E.fetchError(this.options.targetFragment.getTarget(),e,c),c})}else return e?fetch(e,s).then(p=>this.handleFetchResult(p,e||void 0)):Promise.resolve()}else{if((!this.options.bindFragments||this.options.bindFragments.length===0)&&this.options.formFragment&&i){const o=this.options.formFragment,p=o.getTarget();p.setAttribute(`${f.prefix}bind`,JSON.stringify(n));const c=o.getBindingData();return Object.assign(c,n),T.setBindingData(p,c)}const s=i?n:{},a=new Response(JSON.stringify(s),{headers:{"Content-Type":"application/json"}});return this.handleFetchResult(a)}})}handleFetchResult(t,e,r){const n=G();if(!t.ok)return this.options.targetFragment&&e&&E.fetchError(this.options.targetFragment.getTarget(),e,new Error(`${t.status} ${t.statusText}`),t.status,r),this.handleFetchError(t);if(this.options.targetFragment&&e&&r&&E.fetchEnd(this.options.targetFragment.getTarget(),e,t.status,r),this.options.afterCallback){const s=this.options.afterCallback(t);if(s!=null){if(s===!1||typeof s=="object"&&s.stop)return Promise.resolve();typeof s=="object"&&"response"in s&&(t="response"in s?s.response:t)}}const i=[];return i.push(this.bindResult(t)),i.push(this.adjust()),i.push(this.addRow()),i.push(this.removeRow()),i.push(this.movePrevRow()),i.push(this.moveNextRow()),this.options.resetFragments&&this.options.resetFragments.length>0&&this.options.resetFragments.forEach(s=>{i.push(b.reset(s))}),this.options.refetchFragments&&this.options.refetchFragments.length>0&&this.options.refetchFragments.forEach(s=>{i.push(new u(s,null).run())}),this.options.clickFragments&&this.options.clickFragments.length>0&&this.options.clickFragments.forEach(s=>{const a=s.getTarget();typeof a.click=="function"?a.click():a.dispatchEvent(new MouseEvent("click",{bubbles:!0,cancelable:!0}))}),this.options.openFragments&&this.options.openFragments.length>0&&this.options.openFragments.forEach(s=>{const a=s.getTarget();a instanceof HTMLElement?i.push(n.openDialog(a)):h.error("Haori","Element is not an HTML element: ",a)}),this.options.closeFragments&&this.options.closeFragments.length>0&&this.options.closeFragments.forEach(s=>{const a=s.getTarget();a instanceof HTMLElement?i.push(n.closeDialog(a)):h.error("Haori","Element is not an HTML element: ",a)}),Promise.all(i).then(()=>this.options.dialogMessage?n.dialog(this.options.dialogMessage):Promise.resolve()).then(()=>this.options.toastMessage?n.toast(this.options.toastMessage,"info"):Promise.resolve()).then(()=>(this.pushHistory(),Promise.resolve())).then(()=>(this.options.redirectUrl&&(window.location.href=this.options.redirectUrl),Promise.resolve()))}pushHistory(){const t=this.options.historyUrl!==void 0&&this.options.historyUrl!==null,e=this.options.historyData!==void 0&&this.options.historyData!==null,r=this.options.historyFormFragment!==void 0&&this.options.historyFormFragment!==null;if(!(!t&&!e&&!r))try{const n=t?this.options.historyUrl:window.location.pathname,i=new URL(n,window.location.href);if(i.origin!==window.location.origin){const a="history.pushState: cross-origin URL is not allowed: "+i.toString();h.error("Haori",a);return}const s=a=>{for(const[o,p]of Object.entries(a))p!=null&&(Array.isArray(p)?p.forEach(c=>i.searchParams.append(o,String(c))):typeof p=="object"?i.searchParams.set(o,JSON.stringify(p)):i.searchParams.set(o,String(p)))};e&&s(this.options.historyData),r&&s(b.getValues(this.options.historyFormFragment)),history.pushState({},"",i.toString())}catch(n){h.error("Haori",`history.pushState failed: ${n}`)}}async handleFetchError(t){let e=null;this.options.formFragment?e=this.options.formFragment:this.options.targetFragment&&(e=b.getFormFragment(this.options.targetFragment)||this.options.targetFragment);const r=async i=>{const s=e?e.getTarget():document.body;await G().addErrorMessage(s,i)};if((t.headers.get("Content-Type")||"").includes("application/json"))try{const i=await t.json(),s=[];if(i&&typeof i=="object"){if(typeof i.message=="string"&&s.push({message:i.message}),Array.isArray(i.messages))for(const a of i.messages)typeof a=="string"&&s.push({message:a});if(i.errors&&typeof i.errors=="object")for(const[a,o]of Object.entries(i.errors))Array.isArray(o)?s.push({key:a,message:o.join(`
|
|
13
|
+
`)}):typeof o=="string"?s.push({key:a,message:o}):o!=null&&s.push({key:a,message:String(o)});if(s.length===0)for(const[a,o]of Object.entries(i))a==="message"||a==="messages"||a==="errors"||(Array.isArray(o)?s.push({key:a,message:o.join(`
|
|
14
|
+
`)}):typeof o=="string"&&s.push({key:a,message:o}))}if(s.length===0){await r(`${t.status} ${t.statusText}`);return}for(const a of s)a.key&&e?await b.addErrorMessage(e,a.key,a.message):await r(a.message);return}catch{}try{const i=await t.text();i&&i.trim().length>0?await r(i.trim()):await r(`${t.status} ${t.statusText}`)}catch{await r(`${t.status} ${t.statusText}`)}}validate(t){if(this.options.valid!==!0)return!0;const e=t.getTarget();let r=this.validateOne(t);return r||e.focus(),t.getChildElementFragments().reverse().forEach(n=>{r&&=this.validate(n)}),r}validateOne(t){const e=t.getTarget();return e instanceof HTMLInputElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement?e.reportValidity():!0}confirm(){const t=this.options.confirmMessage;return t==null?Promise.resolve(!0):G().confirm(t)}bindResult(t){return!this.options.bindFragments||this.options.bindFragments.length===0?Promise.resolve():(t.headers.get("Content-Type")?.includes("application/json")?t.json():t.text()).then(r=>{if(this.options.bindParams){const i={};this.options.bindParams.forEach(s=>{r&&typeof r=="object"&&s in r&&(i[s]=r[s])}),r=i}const n=[];if(this.options.bindArg)this.options.bindFragments.forEach(i=>{const s=i.getBindingData();s[this.options.bindArg]=r,n.push(T.setBindingData(i.getTarget(),s))});else{if(typeof r=="string")return h.error("Haori","string data cannot be bound without a bindArg."),Promise.reject(new Error("string data cannot be bound without a bindArg."));this.options.bindFragments.forEach(i=>{n.push(T.setBindingData(i.getTarget(),r))})}return Promise.all(n).then(()=>{})})}adjust(){if(!this.options.adjustFragments||this.options.adjustFragments.length===0)return Promise.resolve();const t=this.options.adjustValue??0,e=[];for(const r of this.options.adjustFragments){let n=r.getValue();(n==null||n==="")&&(n="0");let i=Number(n);isNaN(i)&&(i=0),i+=t,e.push(r.setValue(String(i)))}return Promise.all(e).then(()=>{})}getRowFragment(){if(!this.options.targetFragment)return h.error("Haori","Target fragment is not specified for row operation."),null;const t=this.options.targetFragment.closestByAttribute(`${f.prefix}row`);return t||(h.error("Haori","Row fragment not found."),null)}addRow(){if(this.options.rowAdd!==!0)return Promise.resolve();const t=this.getRowFragment();if(!t)return Promise.reject(new Error("Row fragment not found."));const e=[],r=t.clone();return e.push(t.getParent().insertAfter(r,t)),e.push(T.evaluateAll(r)),e.push(b.reset(r)),Promise.all(e).then(()=>{})}removeRow(){if(this.options.rowRemove!==!0)return Promise.resolve();const t=this.getRowFragment();if(!t)return Promise.reject(new Error("Row fragment not found."));const e=t.getParent();return e&&e.getChildElementFragments().filter(n=>!n.hasAttribute(`${f.prefix}each-before`)&&!n.hasAttribute(`${f.prefix}each-after`)).length<=1?Promise.resolve():t.remove()}movePrevRow(){if(this.options.rowMovePrev!==!0)return Promise.resolve();const t=this.getRowFragment();if(!t)return Promise.reject(new Error("Row fragment not found."));const e=t.getPrevious();if(!e)return Promise.resolve();const r=t.getParent();return r?r.insertBefore(t,e):Promise.resolve()}moveNextRow(){if(this.options.rowMoveNext!==!0)return Promise.resolve();const t=this.getRowFragment();if(!t)return Promise.reject(new Error("Row fragment not found."));const e=t.getNext();if(!e)return Promise.resolve();const r=t.getParent();return r?r.insertAfter(t,e):Promise.resolve()}};u.DATA_PLACEHOLDER_REGEX=/\{\{\{([\s\S]+?)\}\}\}|\{\{([\s\S]+?)\}\}/g,u.SINGLE_PLACEHOLDER_REGEX=/^(\{\{\{[\s\S]+?\}\}\}|\{\{[\s\S]+?\}\})$/;let L=u;class _{static readParams(){const t={},e=window.location.search;return new URLSearchParams(e).forEach((n,i)=>{t[i]=n}),t}}class tt{static async load(t,e){let r;try{r=await fetch(t,e)}catch(i){throw h.error("[Haori]","Failed to fetch import source:",t,i),new Error(`Failed to fetch: ${t}`)}if(!r.ok){const i=`${r.status} ${r.statusText}`;throw h.error("[Haori]","Import HTTP error:",t,i),new Error(`Failed to load ${t}: ${i}`)}let n;try{n=await r.text()}catch(i){throw h.error("[Haori]","Failed to read response text:",t,i),new Error(`Failed to read response from: ${t}`)}try{const s=new DOMParser().parseFromString(n,"text/html");return s&&s.body?s.body.innerHTML:(h.warn("[Haori]","No body found in imported document:",t),n)}catch(i){return h.error("[Haori]","Failed to parse imported HTML:",t,i),n}}}const m=class m{static isDeferredAttributeName(t){return m.DEFERRED_ATTRIBUTE_SUFFIXES.some(e=>t===`${f.prefix}${e}`)}static isEvaluateAllExcludedAttributeName(t){return m.EVALUATE_ALL_EXCLUDED_ATTRIBUTE_SUFFIXES.some(e=>t===`${f.prefix}${e}`)}static shouldReevaluateAttribute(t,e){return e!==null&&!m.isEvaluateAllExcludedAttributeName(t)&&m.ATTRIBUTE_PLACEHOLDER_REGEX.test(e)}static reevaluateInterpolatedAttributes(t){let e=Promise.resolve();for(const r of t.getAttributeNames()){const n=t.getRawAttribute(r);m.shouldReevaluateAttribute(r,n)&&(e=e.then(()=>t.setAttribute(r,n)))}return e.then(()=>{})}static scan(t){const e=A.get(t);if(!e)return Promise.resolve();t.parentNode&&(A.get(t.parentNode)?.isMounted()||document.body.contains(t)?e.setMounted(!0):e.setMounted(!1));const r=[],n=new Set;for(const i of m.PRIORITY_ATTRIBUTE_SUFFIXES){const s=f.prefix+i;e.hasAttribute(s)&&(r.push(m.setAttribute(e.getTarget(),s,e.getRawAttribute(s))),n.add(s))}for(const i of e.getAttributeNames()){if(n.has(i)||m.isDeferredAttributeName(i))continue;const s=e.getRawAttribute(i);s!==null&&r.push(m.setAttribute(e.getTarget(),i,s))}for(const i of m.DEFERRED_ATTRIBUTE_SUFFIXES){const s=f.prefix+i;e.hasAttribute(s)&&(r.push(m.setAttribute(e.getTarget(),s,e.getRawAttribute(s))),n.add(s))}return e.getChildren().forEach(i=>{i instanceof P?r.push(m.scan(i.getTarget())):i instanceof I&&r.push(m.evaluateText(i))}),Promise.all(r).then(()=>{})}static setAttribute(t,e,r){const n=A.get(t),i=[];switch(e){case`${f.prefix}bind`:{r===null?(n.clearBindingDataCache(),n.setBindingData({})):n.setBindingData(m.parseDataBind(r));break}case`${f.prefix}if`:i.push(m.evaluateIf(n));break;case`${f.prefix}each`:i.push(m.evaluateEach(n));break;case`${f.prefix}fetch`:i.push(new L(n,null).run());break;case`${f.prefix}import`:{if(typeof r=="string"){const s=n.getTarget(),a=performance.now();E.importStart(s,r),i.push(tt.load(r).then(o=>{const p=new TextEncoder().encode(o).length;return v.enqueue(()=>{s.innerHTML=o}).then(()=>{E.importEnd(s,r,p,a)})}).catch(o=>{E.importError(s,r,o),h.error("[Haori]","Failed to import HTML:",r,o)}))}break}case`${f.prefix}url-param`:{const s=n.getAttribute(`${f.prefix}url-arg`),a=_.readParams();if(s===null)m.setBindingData(t,a);else{const o=n.getRawBindingData()||{};o[String(s)]=a,m.setBindingData(t,o)}break}}return r===null?i.push(n.removeAttribute(e)):i.push(n.setAttribute(e,r)),Promise.all(i).then(()=>{})}static setBindingData(t,e){const r=A.get(t),n=r.getRawBindingData();r.setBindingData(e);const i=[];return i.push(r.setAttribute(`${f.prefix}bind`,JSON.stringify(e))),i.push(m.evaluateAll(r)),E.bindChange(t,n,e,"manual"),Promise.all(i).then(()=>{})}static parseDataBind(t){if(t.startsWith("{")||t.startsWith("["))try{return JSON.parse(t)}catch(e){return h.error("[Haori]","Invalid JSON in data-bind:",e),{}}else{const e=new URLSearchParams(t),r={};for(const[n,i]of e.entries())r[n]!==void 0?Array.isArray(r[n])?r[n].push(i):r[n]=[r[n],i]:r[n]=i;return r}}static addNode(t,e){const r=A.get(t);if(r.isSkipMutationNodes())return;const n=A.get(e.nextSibling),i=A.get(e);i&&(r.insertBefore(i,n),i instanceof P?m.scan(i.getTarget()):i instanceof I&&m.evaluateText(i))}static removeNode(t){const e=A.get(t);if(e){const r=e.getParent();if(r&&r.isSkipMutationNodes())return;e.remove()}}static changeText(t,e){const r=A.get(t);r&&r.setContent(e)}static changeValue(t,e){const r=A.get(t);if(r.getValue()===e)return Promise.resolve();const n=[];n.push(r.setValue(e));const i=m.getFormFragment(r);if(i){const s=b.getValues(i),a=i.getAttribute(`${f.prefix}form-arg`);let o;a?(o=i.getRawBindingData(),o||(o={}),o[String(a)]=s):o=s,n.push(m.setBindingData(i.getTarget(),o))}return Promise.all(n).then(()=>{})}static getFormFragment(t){if(t.getTarget()instanceof HTMLFormElement)return t;const e=t.getParent();return e?m.getFormFragment(e):null}static evaluateAll(t){const e=[];return e.push(m.reevaluateInterpolatedAttributes(t)),t.hasAttribute(`${f.prefix}if`)&&e.push(m.evaluateIf(t)),t.hasAttribute(`${f.prefix}each`)&&e.push(m.evaluateEach(t)),t.getChildren().forEach(r=>{r instanceof P?e.push(m.evaluateAll(r)):r instanceof I&&e.push(m.evaluateText(r))}),Promise.all(e).then(()=>{})}static evaluateText(t){return t.evaluate()}static evaluateIf(t){const e=[],r=t.getAttribute(`${f.prefix}if`);return r===!1||r===void 0||r===null||Number.isNaN(r)?t.isVisible()&&e.push(t.hide().then(()=>{E.hide(t.getTarget())})):t.isVisible()||(e.push(t.show().then(()=>{E.show(t.getTarget())})),e.push(m.evaluateAll(t))),Promise.all(e).then(()=>{})}static evaluateEach(t){if(!t.isVisible()||!t.isMounted())return Promise.resolve();let e=t.getTemplate();if(e===null){let n=!1;t.getChildren().forEach(s=>{if(!n&&s instanceof P){if(s.hasAttribute(`${f.prefix}each-before`)||s.hasAttribute(`${f.prefix}each-after`))return;e=s.clone(),t.setTemplate(e),n=!0,t.removeChild(s);const a=s.getTarget();a.parentNode&&a.parentNode.removeChild(a),s.setMounted(!1)}});const i=t.getAttribute(`${f.prefix}each`);return Array.isArray(i)?this.updateDiff(t,i):(h.error("[Haori]","Invalid each attribute:",i),Promise.reject(new Error("Invalid each attribute.")))}const r=t.getAttribute(`${f.prefix}each`);return Array.isArray(r)?this.updateDiff(t,r):(h.error("[Haori]","Invalid each attribute:",r),Promise.reject(new Error("Invalid each attribute.")))}static updateDiff(t,e){const r=t.getTemplate();if(r===null)return h.error("[Haori]","Template is not set for each element."),Promise.resolve();let n=t.getAttribute(`${f.prefix}each-index`);n&&(n=String(n));const i=t.getAttribute(`${f.prefix}each-key`),s=t.getAttribute(`${f.prefix}each-arg`),a=new Map,o=[];e.forEach((g,w)=>{const N=m.createListKey(g,i?String(i):null,w);o.push(N),a.set(N,{item:g,itemIndex:w})});const p=[];let c=t.getChildren().filter(g=>g instanceof P).filter(g=>!g.hasAttribute(`${f.prefix}each-before`)&&!g.hasAttribute(`${f.prefix}each-after`));c=c.filter(g=>o.indexOf(String(g.getListKey()))===-1?(p.push(g.remove()),!1):!0);const y=c.map(g=>g.getListKey()),l=t.getChildren().filter(g=>g instanceof P).filter(g=>g.hasAttribute(`${f.prefix}each-before`)).length;let d=Promise.resolve();return o.forEach((g,w)=>{const N=y.indexOf(g),{item:B,itemIndex:F}=a.get(g);let x;if(N!==-1)x=c[N],d=d.then(()=>m.updateRowFragment(x,B,n,F,s?String(s):null,g).then(()=>m.evaluateAll(x)));else{x=r.clone();const Y=l+w;d=d.then(()=>m.updateRowFragment(x,B,n,F,s?String(s):null,g).then(()=>t.insertBefore(x,t.getChildren()[Y]||null).then(()=>m.evaluateAll(x))))}}),Promise.all(p).then(()=>d).then(()=>{const g=o.filter(F=>F!==null),w=y.filter(F=>F!==null),N=g.filter(F=>!w.includes(F)),B=w.filter(F=>!g.includes(F));E.eachUpdate(t.getTarget(),N,B,g)})}static createListKey(t,e,r){let n;if(typeof t=="object"&&t!==null)if(e){const i=t[e];i==null?n=`__index_${r}`:typeof i=="object"?n=JSON.stringify(i):n=String(i)}else n=`__index_${r}`;else n=String(t);return n}static updateRowFragment(t,e,r,n,i,s){let a=e;if(typeof e=="object"&&e!==null)a={...e},r&&(a[r]=n),i&&(a={[i]:a});else if(i)a={[i]:e},r&&(a[r]=n);else return h.error("[Haori]",`Primitive value requires '${f.prefix}each-arg' attribute: ${e}`),Promise.resolve();return t.setListKey(s),t.setBindingData(a),t.setAttribute(`${f.prefix}row`,s)}};m.PRIORITY_ATTRIBUTE_SUFFIXES=["bind","if","each"],m.DEFERRED_ATTRIBUTE_SUFFIXES=["fetch","url-param"],m.EVALUATE_ALL_EXCLUDED_ATTRIBUTE_SUFFIXES=["bind","if","each","fetch","import","url-param"],m.ATTRIBUTE_PLACEHOLDER_REGEX=/\{\{\{[\s\S]+?\}\}\}|\{\{[\s\S]+?\}\}/;let T=m;class et{constructor(t=document){this.onClick=e=>this.delegate(e,"click"),this.onChange=e=>this.delegate(e,"change"),this.onLoadCapture=e=>this.delegate(e,"load"),this.onWindowLoad=()=>{const e=document.documentElement,r=A.get(e);r&&new L(r,"load").run()},this.root=t}start(){this.root.addEventListener("click",this.onClick),this.root.addEventListener("change",this.onChange),this.root.addEventListener("load",this.onLoadCapture,!0),window.addEventListener("load",this.onWindowLoad,{once:!0})}stop(){this.root.removeEventListener("click",this.onClick),this.root.removeEventListener("change",this.onChange),this.root.removeEventListener("load",this.onLoadCapture,!0),window.removeEventListener("load",this.onWindowLoad)}delegate(t,e){const r=this.getElementFromTarget(t.target);if(!r)return;const n=A.get(r);n&&(e==="change"&&n instanceof P&&n.syncValue(),new L(n,e).run().catch(i=>{h.error("[Haori]","Procedure execution error:",i)}))}getElementFromTarget(t){return t?t instanceof HTMLElement?t:t instanceof Node?t.parentElement:null:null}}const O=class O{static async init(){if(O._initialized)return;O._initialized=!0;const t=await Promise.allSettled([T.scan(document.head),T.scan(document.body)]),[e,r]=t;e.status!=="fulfilled"&&h.error("[Haori]","Failed to build head fragment:",e.reason),r.status!=="fulfilled"&&h.error("[Haori]","Failed to build body fragment:",r.reason),O.observe(document.head),O.observe(document.body),new et().start()}static observe(t){new MutationObserver(async r=>{for(const n of r)try{switch(n.type){case"attributes":{h.info("[Haori]","Attribute changed:",n.target,n.attributeName);const i=n.target;T.setAttribute(i,n.attributeName,i.getAttribute(n.attributeName));break}case"childList":{h.info("[Haori]","Child list changed:",Array.from(n.removedNodes).map(i=>i.nodeName),Array.from(n.addedNodes).map(i=>i.nodeName)),Array.from(n.removedNodes).forEach(i=>{T.removeNode(i)}),Array.from(n.addedNodes).forEach(i=>{i.parentElement instanceof HTMLElement&&T.addNode(i.parentElement,i)});break}case"characterData":{h.info("[Haori]","Character data changed:",n.target,n.target.textContent),n.target instanceof Text||n.target instanceof Comment?T.changeText(n.target,n.target.textContent):h.warn("[Haori]","Unsupported character data type:",n.target);break}default:h.warn("[Haori]","Unknown mutation type:",n.type);continue}}catch(i){h.error("[Haori]","Error processing mutation:",i)}}).observe(t,{childList:!0,subtree:!0,attributes:!0,characterData:!0}),h.info("[Haori]","Observer initialized for",t)}};O._initialized=!1;let V=O;document.readyState==="loading"?document.addEventListener("DOMContentLoaded",V.init):V.init();const rt="0.2.0";exports.Core=T;exports.Env=f;exports.Form=b;exports.Fragment=A;exports.Haori=K;exports.Log=h;exports.Queue=v;exports.default=K;exports.version=rt;
|
|
15
15
|
//# sourceMappingURL=haori.cjs.js.map
|