@deijose/nix-js 2.5.0 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/lib/component.cjs +1 -1
- package/dist/lib/component.js +1 -1
- package/dist/lib/form.cjs +1 -1
- package/dist/lib/form.js +1 -1
- package/dist/lib/nix/router.d.ts +4 -2
- package/dist/lib/router.cjs +5 -5
- package/dist/lib/router.js +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
[]()
|
|
6
6
|
[]()
|
|
7
|
-
[]()
|
|
8
8
|
[]()
|
|
9
9
|
[](https://nix-js.dev/)
|
|
10
10
|
|
|
@@ -13,7 +13,7 @@ A lightweight, fully reactive micro-framework for building modern web UIs — no
|
|
|
13
13
|
**[→ Documentation & Live Demo](https://nix-js.dev/)**
|
|
14
14
|
|
|
15
15
|
```
|
|
16
|
-
~24 KB minified · ~
|
|
16
|
+
~24 KB minified · ~12 KB gzipped · zero dependencies · TypeScript-first · ES2022
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Installation
|
package/dist/lib/component.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./lifecycle.cjs"),t=require("./context.cjs"),n=require("./router.cjs");function r(t){let o="string"==typeof t?document.querySelector(t):t;if(!o)throw Error(`[Nix] mount: container not found: ${t}`);return o}function i(o,u,i){if(e.isNixComponent(o)){let c
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./lifecycle.cjs"),t=require("./context.cjs"),n=require("./router.cjs");function r(t){let o="string"==typeof t?document.querySelector(t):t;if(!o)throw Error(`[Nix] mount: container not found: ${t}`);return o}function i(o,u,i){if(e.isNixComponent(o)){let c=r(u);e._debugComponentMountStart(o),t._pushComponentContext();let p,l=()=>{},f=!1;try{i?.router&&t.provide(n.RouterKey,i.router);try{o.onInit?.()}catch(t){if(!o.onError)throw t;o.onError(t)}try{l=o.render()._render(c,null)}catch(t){if(!o.onError)throw t;o.onError(t),l=()=>{},f=!0}}finally{e._debugComponentMountEnd(o),t._popComponentContext()}if(!f)try{let t=o.onMount?.();"function"==typeof t&&(p=t)}catch(t){if(!o.onError)throw t;o.onError(t)}return{unmount(){try{o.onUnmount?.()}catch{}try{p?.()}catch{}l(),e._debugComponentUnmount(o)}}}if(!i?.router)return o.mount(u);let c,p=r(u);t._pushComponentContext();try{t.provide(n.RouterKey,i.router),c=o._render(p,null)}finally{t._popComponentContext()}return{unmount(){c()}}}exports.mount=i;
|
package/dist/lib/component.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{_debugComponentMountEnd as e,_debugComponentMountStart as t,_debugComponentUnmount as n,isNixComponent as r}from"./lifecycle.js";import{_popComponentContext as i,_pushComponentContext as a,provide as o}from"./context.js";import{RouterKey as s}from"./router.js";function c(o){let
|
|
1
|
+
import{_debugComponentMountEnd as e,_debugComponentMountStart as t,_debugComponentUnmount as n,isNixComponent as r}from"./lifecycle.js";import{_popComponentContext as i,_pushComponentContext as a,provide as o}from"./context.js";import{RouterKey as s}from"./router.js";function c(o){let r="string"==typeof o?document.querySelector(o):o;if(!r)throw Error(`[Nix] mount: container not found: ${o}`);return r}function l(u,m,f){if(r(u)){let r=c(m);t(u),a();let l,p=()=>{},y=!1;try{f?.router&&o(s,f.router);try{u.onInit?.()}catch(o){if(!u.onError)throw o;u.onError(o)}try{p=u.render()._render(r,null)}catch(o){if(!u.onError)throw o;u.onError(o),p=()=>{},y=!0}}finally{e(u),i()}if(!y)try{let o=u.onMount?.();"function"==typeof o&&(l=o)}catch(o){if(!u.onError)throw o;u.onError(o)}return{unmount(){try{u.onUnmount?.()}catch{}try{l?.()}catch{}p(),n(u)}}}if(!f?.router)return u.mount(m);let l,p=c(m);a();try{o(s,f.router),l=u._render(p,null)}finally{i()}return{unmount(){l()}}}export{l as mount};
|
package/dist/lib/form.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");function t(e="Required"){return t=>null==t||""===t||Array.isArray(t)&&0===t.length?e:null}function n(e,t){return r=>"string"==typeof r&&r.length<e?t??`Minimum ${e} characters`:null}function r(e,t){return r=>"string"==typeof r&&r.length>e?t??`Maximum ${e} characters`:null}function i(e,t="Invalid format"){return r=>"string"!=typeof r||e.test(r)?null:t}function a(e="Invalid email"){return i(/^[^\s@]+@[^\s@]+\.[^\s@]+$/,e)}function o(e,t){return r=>"number"==typeof r&&r<e?t??`Minimum value is ${e}`:null}function s(e,t){return r=>"number"==typeof r&&r>e?t??`Maximum value is ${e}`:null}function c(e){return e}var l={required:t,minLength:n,maxLength:r,email:a,pattern:i,min:o,max:s};function u(e,t){return{...e,...t}}function d(t,r=[],n="blur",l){let u=e.signal(t),i=e.signal(!1),o=e.signal(!1),a=e.signal(null),s=e.signal(!1),f=!0,c=e.effect(()=>{u.value,f?f=!1:null!==a.peek()&&(a.value=null)}),
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");function t(e="Required"){return t=>null==t||""===t||Array.isArray(t)&&0===t.length?e:null}function n(e,t){return r=>"string"==typeof r&&r.length<e?t??`Minimum ${e} characters`:null}function r(e,t){return r=>"string"==typeof r&&r.length>e?t??`Maximum ${e} characters`:null}function i(e,t="Invalid format"){return r=>"string"!=typeof r||e.test(r)?null:t}function a(e="Invalid email"){return i(/^[^\s@]+@[^\s@]+\.[^\s@]+$/,e)}function o(e,t){return r=>"number"==typeof r&&r<e?t??`Minimum value is ${e}`:null}function s(e,t){return r=>"number"==typeof r&&r>e?t??`Maximum value is ${e}`:null}function c(e){return e}var l={required:t,minLength:n,maxLength:r,email:a,pattern:i,min:o,max:s};function u(e,t){return{...e,...t}}function d(t,r=[],n="blur",l){let u=e.signal(t),i=e.signal(!1),o=e.signal(!1),a=e.signal(null),s=e.signal(!1),f=!0,c=e.effect(()=>{u.value,f?f=!1:null!==a.peek()&&(a.value=null)}),p=e.computed(()=>{if(a.value)return a.value;let e=l?.();for(let t of r){let r=t(u.value,e);if(r)return r}return null}),v=e.computed(()=>("input"===n?o.value||i.value:"submit"===n?s.value:i.value)?p.value:null);return{value:u,error:v,rawError:p,touched:i,dirty:o,onInput:e=>{u.value=function(e){if(!e||!("value"in e))return t;let r=e,n=r.value;return"boolean"==typeof t?!0===r.checked||!function(e){return"checkbox"===e.type||"radio"===e.type}(r)&&("true"===n||"1"===n||"false"!==n&&"0"!==n&&""!==n&&t):"number"==typeof t?""===n?NaN:Number(n):n}(e.target),o.value=!0},onBlur:()=>{i.value=!0},reset:function(){e.batch(()=>{u.value=t,i.value=!1,o.value=!1,a.value=null,s.value=!1})},_setExternalError:function(e){a.value=e,e&&(i.value=!0)},_forceVisible:function(){i.value=!0,s.value=!0},_dispose:function(){c(),v.dispose(),p.dispose()}}}function f(t,r={},n="blur"){function l(e){let t={};for(let l in e){let u=r[l]??[];t[l]=d(e[l],u,n)}return t}let u=e.signal(t.map(l)),i=e.computed(()=>u.value.length);return{fields:u,append:function(e){u.value=[...u.value,l(e)]},remove:function(e){let t=u.value;if(!(e<0||e>=t.length)){for(let r in t[e])t[e][r]._dispose();u.value=t.filter((t,r)=>r!==e)}},move:function(e,t){let r=[...u.value];if(e<0||e>=r.length||t<0||t>=r.length||e===t)return;let[n]=r.splice(e,1);r.splice(t,0,n),u.value=r},replace:function(e,t){let r=[...u.value];if(!(e<0||e>=r.length)){for(let t in r[e])r[e][t]._dispose();r[e]=l(t),u.value=r}},length:i,reset:function(){for(let e of u.value)for(let t in e)e[t]._dispose();u.value=t.map(l)},_dispose:function(){for(let e of u.value)for(let t in e)e[t]._dispose();i.dispose()}}}function p(e){return"object"==typeof e&&!!e&&!Array.isArray(e)}function m(e,t="",r=[]){for(let[n,l]of Object.entries(e)){let e=t?`${t}.${n}`:n;p(l)&&Object.keys(l).length>0?m(l,e,r):r.push([e,l])}return r}function h(e,t,r){let n=t.split("."),l=e;for(let e=0;e<n.length-1;e++){let t=n[e];p(l[t])||(l[t]={}),l=l[t]}l[n[n.length-1]]=r}function g(t,r={}){let n=r.validateOn??"blur",l={},u=r.validators;function i(){let e={};for(let t in l)h(e,t,l[t].value.value);return e}for(let[e,r]of m(t))l[e]=d(r,u?.[e]??[],n,i);let o=e.signal(!1),a=e.signal(0),s=e.computed(()=>i()),f=e.computed(()=>{let e={};for(let t in l){let r=l[t].error.value;r&&(e[t]=r)}return e}),c=e.computed(()=>{for(let e in l)if(l[e].rawError.value)return!1;return!0}),p=e.computed(()=>{for(let e in l)if(l[e].error.value)return!1;return!0}),v=e.computed(()=>{for(let e in l)if(l[e].dirty.value)return!0;return!1}),g=e.computed(()=>{for(let e in l)if(l[e].touched.value)return!0;return!1});function x(e){for(let t in e)l[t]?._setExternalError(e[t]??null)}return{fields:l,values:s,errors:f,canSubmit:c,valid:p,dirty:v,touched:g,isSubmitting:o,submitCount:a,handleSubmit:function(e){return t=>{t.preventDefault(),a.value++;for(let e in l)l[e]._forceVisible();let n=s.value;if(r.validate){let e=r.validate(n);if(e){let t={},r=!1;for(let n in e){let l=e[n],u=Array.isArray(l)?l[0]??null:l??null;u&&(t[n]=u,r=!0)}if(r)return void x(t)}}for(let e in l)if(l[e].rawError.value)return;let u=e(n);u instanceof Promise&&(o.value=!0,u.finally(()=>{o.value=!1}).catch(()=>{}))}},reset:function(){for(let e in l)l[e].reset();o.value=!1,a.value=0},setErrors:x,dispose:function(){s.dispose(),f.dispose(),c.dispose(),p.dispose(),v.dispose(),g.dispose();for(let e in l)l[e]._dispose()}}}exports.createForm=g,exports.createValidator=c,exports.email=a,exports.extendValidators=u,exports.max=s,exports.maxLength=r,exports.min=o,exports.minLength=n,exports.nixField=d,exports.nixFieldArray=f,exports.pattern=i,exports.required=t,exports.validators=l;
|
package/dist/lib/form.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{batch as e,computed as t,effect as n,signal as r}from"./signals.js";function i(e="Required"){return t=>null==t||""===t||Array.isArray(t)&&0===t.length?e:null}function a(e,t){return r=>"string"==typeof r&&r.length<e?t??`Minimum ${e} characters`:null}function o(e,t){return r=>"string"==typeof r&&r.length>e?t??`Maximum ${e} characters`:null}function s(e,t="Invalid format"){return r=>"string"!=typeof r||e.test(r)?null:t}function c(e="Invalid email"){return s(/^[^\s@]+@[^\s@]+\.[^\s@]+$/,e)}function l(e,t){return r=>"number"==typeof r&&r<e?t??`Minimum value is ${e}`:null}function u(e,t){return r=>"number"==typeof r&&r>e?t??`Maximum value is ${e}`:null}function d(e){return e}var f={required:i,minLength:a,maxLength:o,email:c,pattern:s,min:l,max:u};function p(e,t){return{...e,...t}}function m(l,u=[],i="blur",a){let o=r(l),s=r(!1),f=r(!1),v=r(null),c=r(!1),d=!0,p=n(()=>{o.value,d?d=!1:null!==v.peek()&&(v.value=null)}),m=t(()=>{if(v.value)return v.value;let e=a?.();for(let t of u){let r=t(o.value,e);if(r)return r}return null}),h=t(()=>("input"===i?f.value||s.value:"submit"===i?c.value:s.value)?m.value:null);return{value:o,error:h,rawError:m,touched:s,dirty:f,onInput:e=>{o.value=function(e){if(!e||!("value"in e))return l;let t=e;return"boolean"==typeof l
|
|
1
|
+
import{batch as e,computed as t,effect as n,signal as r}from"./signals.js";function i(e="Required"){return t=>null==t||""===t||Array.isArray(t)&&0===t.length?e:null}function a(e,t){return r=>"string"==typeof r&&r.length<e?t??`Minimum ${e} characters`:null}function o(e,t){return r=>"string"==typeof r&&r.length>e?t??`Maximum ${e} characters`:null}function s(e,t="Invalid format"){return r=>"string"!=typeof r||e.test(r)?null:t}function c(e="Invalid email"){return s(/^[^\s@]+@[^\s@]+\.[^\s@]+$/,e)}function l(e,t){return r=>"number"==typeof r&&r<e?t??`Minimum value is ${e}`:null}function u(e,t){return r=>"number"==typeof r&&r>e?t??`Maximum value is ${e}`:null}function d(e){return e}var f={required:i,minLength:a,maxLength:o,email:c,pattern:s,min:l,max:u};function p(e,t){return{...e,...t}}function m(l,u=[],i="blur",a){let o=r(l),s=r(!1),f=r(!1),v=r(null),c=r(!1),d=!0,p=n(()=>{o.value,d?d=!1:null!==v.peek()&&(v.value=null)}),m=t(()=>{if(v.value)return v.value;let e=a?.();for(let t of u){let r=t(o.value,e);if(r)return r}return null}),h=t(()=>("input"===i?f.value||s.value:"submit"===i?c.value:s.value)?m.value:null);return{value:o,error:h,rawError:m,touched:s,dirty:f,onInput:e=>{o.value=function(e){if(!e||!("value"in e))return l;let t=e,r=t.value;return"boolean"==typeof l?!0===t.checked||!function(e){return"checkbox"===e.type||"radio"===e.type}(t)&&("true"===r||"1"===r||"false"!==r&&"0"!==r&&""!==r&&l):"number"==typeof l?""===r?NaN:Number(r):r}(e.target),f.value=!0},onBlur:()=>{s.value=!0},reset:function(){e(()=>{o.value=l,s.value=!1,f.value=!1,v.value=null,c.value=!1})},_setExternalError:function(e){v.value=e,e&&(s.value=!0)},_forceVisible:function(){s.value=!0,c.value=!0},_dispose:function(){p(),h.dispose(),m.dispose()}}}function h(e,n={},l="blur"){function u(e){let t={};for(let r in e){let u=n[r]??[];t[r]=m(e[r],u,l)}return t}let i=r(e.map(u)),a=t(()=>i.value.length);return{fields:i,append:function(e){i.value=[...i.value,u(e)]},remove:function(e){let t=i.value;if(!(e<0||e>=t.length)){for(let r in t[e])t[e][r]._dispose();i.value=t.filter((t,r)=>r!==e)}},move:function(e,t){let r=[...i.value];if(e<0||e>=r.length||t<0||t>=r.length||e===t)return;let[n]=r.splice(e,1);r.splice(t,0,n),i.value=r},replace:function(e,t){let r=[...i.value];if(!(e<0||e>=r.length)){for(let t in r[e])r[e][t]._dispose();r[e]=u(t),i.value=r}},length:a,reset:function(){for(let e of i.value)for(let t in e)e[t]._dispose();i.value=e.map(u)},_dispose:function(){for(let e of i.value)for(let t in e)e[t]._dispose();a.dispose()}}}function g(e){return"object"==typeof e&&!!e&&!Array.isArray(e)}function _(e,t="",r=[]){for(let[n,l]of Object.entries(e)){let e=t?`${t}.${n}`:n;g(l)&&Object.keys(l).length>0?_(l,e,r):r.push([e,l])}return r}function v(e,t,r){let n=t.split("."),l=e;for(let e=0;e<n.length-1;e++){let t=n[e];g(l[t])||(l[t]={}),l=l[t]}l[n[n.length-1]]=r}function y(e,n={}){let l=n.validateOn??"blur",u={},i=n.validators;function a(){let e={};for(let t in u)v(e,t,u[t].value.value);return e}for(let[t,r]of _(e))u[t]=m(r,i?.[t]??[],l,a);let o=r(!1),s=r(0),f=t(()=>a()),c=t(()=>{let e={};for(let t in u){let r=u[t].error.value;r&&(e[t]=r)}return e}),d=t(()=>{for(let e in u)if(u[e].rawError.value)return!1;return!0}),p=t(()=>{for(let e in u)if(u[e].error.value)return!1;return!0}),h=t(()=>{for(let e in u)if(u[e].dirty.value)return!0;return!1}),g=t(()=>{for(let e in u)if(u[e].touched.value)return!0;return!1});function y(e){for(let t in e)u[t]?._setExternalError(e[t]??null)}return{fields:u,values:f,errors:c,canSubmit:d,valid:p,dirty:h,touched:g,isSubmitting:o,submitCount:s,handleSubmit:function(e){return t=>{t.preventDefault(),s.value++;for(let e in u)u[e]._forceVisible();let r=f.value;if(n.validate){let e=n.validate(r);if(e){let t={},r=!1;for(let n in e){let l=e[n],u=Array.isArray(l)?l[0]??null:l??null;u&&(t[n]=u,r=!0)}if(r)return void y(t)}}for(let e in u)if(u[e].rawError.value)return;let l=e(r);l instanceof Promise&&(o.value=!0,l.finally(()=>{o.value=!1}).catch(()=>{}))}},reset:function(){for(let e in u)u[e].reset();o.value=!1,s.value=0},setErrors:y,dispose:function(){f.dispose(),c.dispose(),d.dispose(),p.dispose(),h.dispose(),g.dispose();for(let e in u)u[e]._dispose()}}}export{y as createForm,d as createValidator,c as email,p as extendValidators,u as max,o as maxLength,l as min,a as minLength,m as nixField,h as nixFieldArray,s as pattern,i as required,f as validators};
|
package/dist/lib/nix/router.d.ts
CHANGED
|
@@ -106,13 +106,15 @@ export declare function nixRouter(): Router;
|
|
|
106
106
|
export declare function _resetRouter(): void;
|
|
107
107
|
export declare class RouterView extends NixComponent {
|
|
108
108
|
private _depth;
|
|
109
|
-
|
|
109
|
+
private _router?;
|
|
110
|
+
constructor(depth?: number, router?: Router);
|
|
110
111
|
render(): NixTemplate;
|
|
111
112
|
}
|
|
112
113
|
export declare class Link extends NixComponent {
|
|
113
114
|
private _to;
|
|
114
115
|
private _label;
|
|
115
|
-
|
|
116
|
+
private _router?;
|
|
117
|
+
constructor(to: string, label: string, router?: Router);
|
|
116
118
|
render(): NixTemplate;
|
|
117
119
|
}
|
|
118
120
|
export interface _RouterDebugInternal {
|
package/dist/lib/router.cjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs"),t=require("./template2.cjs"),n=require("./lifecycle.cjs"),r=require("./context.cjs");var i=r.createInjectionKey("nix:router"),a=null,o=null,s="__nix_scroll",c="__nix_pos";function l(){if(!a)throw Error("[Nix] No active router. Call createRouter() first, or instantiate an outlet that auto-bootstraps one (e.g. IonRouterOutlet).");return a}function u(){return null!==a}function d(){return{left:window.scrollX??window.pageXOffset??0,top:window.scrollY??window.pageYOffset??0}}function ee(e){if(!e||"object"!=typeof e)return null;let t=e[s];if(!t||"object"!=typeof t)return null;let r=t.left,n=t.top;return"number"!=typeof r||"number"!=typeof n?null:{left:r,top:n}}function f(e){if(!e||"object"!=typeof e)return null;let t=e[c];return"number"==typeof t?t:null}function p(e,t,r){let n=e&&"object"==typeof e?{...e}:{};return n[s]={left:t.left,top:t.top},n[c]=r,n}function m(e){let t={};return new URLSearchParams(e).forEach((e,r)=>{t[r]=e}),t}function h(e){let t=new URLSearchParams;for(let[r,n]of Object.entries(e))null!=n&&!1!==n&&t.set(r,String(n));let r=t.toString();return r?"?"+r:""}function g(e){return"*"===e?[{kind:"wildcard"}]:e.split("/").filter(Boolean).map(e=>"*"===e?{kind:"wildcard"}:e.startsWith(":")?{kind:"param",name:e.slice(1)}:{kind:"literal",value:e})}function _(e,t){return"*"===t?""===e?"*":e+"/*":(e+(t.startsWith("/")?t:"/"+t)).replace(/\/+/g,"/")||"/"}function v(e,t="",r=[]){let n=[];for(let o of e){let e=_(t,o.path),a=[...r,o.component],i=g(e);n.push({fullPath:e,segments:i,chain:a,name:o.name,meta:o.meta,beforeEnter:o.beforeEnter,record:o}),o.children?.length&&n.push(...v(o.children,e,a))}return n}function y(e,t){let r=e.split("/").filter(Boolean),n=t.segments;if(1===n.length&&"wildcard"===n[0].kind)return{};let o=n.length>0&&"wildcard"===n[n.length-1].kind,a=o?n.slice(0,-1):n;if(o){if(r.length<a.length)return null}else if(r.length!==a.length)return null;let i={};for(let e=0;e<a.length;e++){let t=a[e];if("literal"===t.kind){if(r[e]!==t.value)return null}else if("param"===t.kind)try{i[t.name]=decodeURIComponent(r[e]??"")}catch{i[t.name]=r[e]??""}}return i}function b(e){return e.segments.reduce((e,t)=>"literal"===t.kind?e+2:"param"===t.kind?e+1:e,0)}function x(e,t){let r,n={},o=-1;for(let a of t){let t=y(e,a);if(null===t)continue;let i=b(a);i>o&&(r=a,n=t,o=i)}return r?{route:r,params:n}:void 0}function S(e){let t=e.trim();return t&&"/"!==t?(t.startsWith("/")||(t="/"+t),t.endsWith("/")&&(t=t.slice(0,-1)),t):""}function te(){if(typeof document>"u")return"";let e=document.querySelector("base");if(!e)return"";let t=e.getAttribute("href")||"";try{return S(new URL(t,window.location.origin).pathname)}catch{return S(t)}}function C(e){return!1===e?{allow:!1}:!0===e||null==e?{allow:!0}:"string"==typeof e?{allow:!1,redirect:e}:"object"==typeof e&&"redirect"in e&&"string"==typeof e.redirect?{allow:!1,redirect:e.redirect}:{allow:!0}}function w(t,r){let n=null==r?.base?te():S(r.base),i=r?.mode??"history",l="hash"===i,u=r?.scrollBehavior,s=new Map,c=!1;function g(e){return e?e.startsWith("/")?e:"/"+e:"/"}function w(e){let t=g(e||"/");if(n&&t.startsWith(n)){let e=t.slice(n.length);return""===e?"/":g(e)}return t}function y(){return l?function(){let e=window.location.hash||"";if(e.startsWith("#")&&(e=e.slice(1)),!e)return{pathname:"/",search:""};e.startsWith("/")||(e="/"+e);let t=e.indexOf("?"),r=-1===t?e:e.slice(0,t),n=-1===t?"":e.slice(t);return{pathname:w(r),search:n}}():{pathname:w(window.location.pathname||"/"),search:window.location.search||""}}function b(e,t){let r=function(e){let t=g(e);return n?(n+t).replace(/\/+/g,"/")||"/":t}(e)+h(t);return l?"#"+r:r}function _(e,t){return g(e)+h(t)}let k=y(),E=k.pathname,R=m(k.search),j=v(t),q=new Map;for(let e of j)e.name&&(q.has(e.name)&&console.warn(`[Nix Router] Duplicate route name: "${e.name}"`),q.set(e.name,e));let O=x(E,j),W=e.signal(E),$=e.signal(O?.params??{}),N=e.signal(R),L=f(history.state)??0,P=e.signal({action:"initial",direction:"none"}),M=e.signal(L>0);function T(e){window.scrollTo(e.left,e.top)}function B(e,t,r){if(u){let n=u(e,t,r);if(!n)return;return void T(n)}T(r??{left:0,top:0})}l?s.set(_(E,R),d()):history.replaceState(p(history.state,d(),L),"");let I=[],U=[],A=0;function D(e,t,r,n,o){let a=[...I];r&&a.push(r);let i=++A;if(0===a.length)return void n();let l=0;!function r(u){if(i!==A)return;let s=C(u);if(!s.allow)return s.redirect&&s.redirect!==e?void z(s.redirect):s.redirect===e?void n():void o?.();if(l>=a.length)return void n();let c=a[l++](e,t);c instanceof Promise?c.then(r):r(c)}(void 0)}let G=!1;function Q(e,t){let r=e.indexOf("?"),n=g((-1===r?e:e.slice(0,r))||"/"),o=-1===r?{}:m(e.slice(r)),a=t?{...o,...t}:o,i={};for(let[e,t]of Object.entries(a))null!=t&&!1!==t&&(i[e]=String(t));return{pathname:n,stringQuery:i}}function K(e,t){return"string"==typeof e?Q(e,t?.query):Q(function(e){let t=q.get(e.name);if(!t)throw Error(`[Nix Router] No route with name "${e.name}"`);return"/"+t.segments.map(t=>{if("literal"===t.kind)return t.value;if("wildcard"===t.kind)return"";let r=e.params?.[t.name];if(null==r)throw Error(`[Nix Router] Missing param "${t.name}" for route "${e.name}"`);return encodeURIComponent(String(r))}).filter(Boolean).join("/")}(e),{...e.query??{},...t?.query??{}})}o&&=(o(),null);let X,Y=(e,t,r,n,o)=>{let a=W.value,i={...N.value},l=x(e,j),u="none";null!=n&&(n<L?u="back":n>L&&(u="forward")),D(e,a,l?.route.beforeEnter,()=>{null!=n&&(L=n);let o=X;X=void 0,P.value={action:"pop",direction:u,animation:o},$.value=l?.params??{},N.value=t,W.value=e,M.value=L>0,B(e,a,r);for(let t of U)try{t(e,a)}catch{}},()=>o(a,i))};if(l){let e=()=>{if(c)return void(c=!1);let e=y(),t=m(e.search),r=s.get(_(e.pathname,t))??null;Y(e.pathname,t,r,null,(e,t)=>{c=!0,window.location.hash=b(e,t).slice(1),queueMicrotask(()=>{c=!1})})};window.addEventListener("hashchange",e),o=()=>window.removeEventListener("hashchange",e)}else{let e=e=>{let t=y(),r=m(t.search),n=ee(e.state??history.state),o=f(e.state??history.state);Y(t.pathname,r,n,o,(e,t)=>{history.pushState(p({},d(),L),"",b(e,t))})};window.addEventListener("popstate",e),o=()=>window.removeEventListener("popstate",e)}function V(e,t,r,n,o,a,i){i||(function(e,t){let r=d();l?s.set(_(e,t),r):history.replaceState(p(history.state,r,L),"")}(r,n),L+=1),P.value=a,$.value=o?.params??{},N.value=t,W.value=e,M.value=L>0;let u=b(e,t);if(l)s.set(_(e,t),{left:0,top:0}),i?history.replaceState(history.state,"",u):(c=!0,window.location.hash=u.slice(1),queueMicrotask(()=>{c=!1}));else{let e=p({},{left:0,top:0},L);i?history.replaceState(e,"",u):history.pushState(e,"",u)}B(e,r,null);for(let t of U)try{t(e,r)}catch{}}function z(e,t){G=!0;let{pathname:r,stringQuery:n}=K(e,t),o=W.value,a={...N.value},i=x(r,j),l={action:"push",direction:t?.direction??"forward",animation:t?.animation};D(r,o,i?.route.beforeEnter,()=>V(r,n,o,a,i,l,!1))}let F={current:W,params:$,query:N,intent:P,canGoBack:M,base:n||"/",navigate:z,replace:function(e,t){G=!0;let{pathname:r,stringQuery:n}=K(e,t),o=W.value,a={...N.value},i=x(r,j),l={action:"replace",direction:t?.direction??"root",animation:t?.animation};D(r,o,i?.route.beforeEnter,()=>V(r,n,o,a,i,l,!0))},back:function(e){void 0!==e&&(X=e),history.back()},forward:function(e){void 0!==e&&(X=e),history.forward()},go:function(e){history.go(e)},isActive:function(e,t=!0){let r=W.value;return t?r===e:r===e||r.startsWith(e.endsWith("/")?e:e+"/")},resolve:function(e){let t=x(e,j);return t?{matched:!0,params:t.params,route:t.route.record}:{matched:!1,params:{},route:void 0}},beforeEach:function(e){return I.push(e),()=>{let t=I.indexOf(e);-1!==t&&I.splice(t,1)}},afterEach:function(e){return U.push(e),()=>{let t=U.indexOf(e);-1!==t&&U.splice(t,1)}},routes:t,_flat:j,_guards:I,_base:n,_mode:i};return a&&console.warn("[Nix] A router already exists. The previous router is being replaced. Only one router instance should be active at a time."),a=F,queueMicrotask(()=>{G||D(E,"",x(E,j)?.route.beforeEnter,()=>{},()=>{let e=b("/",{});l?(s.set(_("/",{}),{left:0,top:0}),history.replaceState(history.state,"",e)):history.replaceState(p({},{left:0,top:0},L),"",e);let t=x("/",j);P.value={action:"replace",direction:"root"},W.value="/",$.value=t?.params??{},N.value={},M.value=L>0,B("/",E,null)})}),F}function T(){return r.inject(i)||l()}function E(){o&&=(o(),null),a=null}var D=class extends n.NixComponent{_depth;constructor(e=0){super(),this._depth=e}render(){let e=this._depth;return t.l`<div class="router-view">${()=>{let r
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs"),t=require("./template2.cjs"),n=require("./lifecycle.cjs"),r=require("./context.cjs");var i=r.createInjectionKey("nix:router"),a=null,o=null,s="__nix_scroll",c="__nix_pos";function l(){if(!a)throw Error("[Nix] No active router. Call createRouter() first, or instantiate an outlet that auto-bootstraps one (e.g. IonRouterOutlet).");return a}function u(){return null!==a}function d(){return{left:window.scrollX??window.pageXOffset??0,top:window.scrollY??window.pageYOffset??0}}function ee(e){if(!e||"object"!=typeof e)return null;let t=e[s];if(!t||"object"!=typeof t)return null;let r=t.left,n=t.top;return"number"!=typeof r||"number"!=typeof n?null:{left:r,top:n}}function f(e){if(!e||"object"!=typeof e)return null;let t=e[c];return"number"==typeof t?t:null}function p(e,t,r){let n=e&&"object"==typeof e?{...e}:{};return n[s]={left:t.left,top:t.top},n[c]=r,n}function m(e){let t={};return new URLSearchParams(e).forEach((e,r)=>{t[r]=e}),t}function h(e){let t=new URLSearchParams;for(let[r,n]of Object.entries(e))null!=n&&!1!==n&&t.set(r,String(n));let r=t.toString();return r?"?"+r:""}function g(e){return"*"===e?[{kind:"wildcard"}]:e.split("/").filter(Boolean).map(e=>"*"===e?{kind:"wildcard"}:e.startsWith(":")?{kind:"param",name:e.slice(1)}:{kind:"literal",value:e})}function _(e,t){return"*"===t?""===e?"*":e+"/*":(e+(t.startsWith("/")?t:"/"+t)).replace(/\/+/g,"/")||"/"}function v(e,t="",r=[]){let n=[];for(let o of e){let e=_(t,o.path),a=[...r,o.component],i=g(e);n.push({fullPath:e,segments:i,chain:a,name:o.name,meta:o.meta,beforeEnter:o.beforeEnter,record:o}),o.children?.length&&n.push(...v(o.children,e,a))}return n}function y(e,t){let r=e.split("/").filter(Boolean),n=t.segments;if(1===n.length&&"wildcard"===n[0].kind)return{};let o=n.length>0&&"wildcard"===n[n.length-1].kind,a=o?n.slice(0,-1):n;if(o){if(r.length<a.length)return null}else if(r.length!==a.length)return null;let i={};for(let e=0;e<a.length;e++){let t=a[e];if("literal"===t.kind){if(r[e]!==t.value)return null}else if("param"===t.kind)try{i[t.name]=decodeURIComponent(r[e]??"")}catch{i[t.name]=r[e]??""}}return i}function b(e){return e.segments.reduce((e,t)=>"literal"===t.kind?e+2:"param"===t.kind?e+1:e,0)}function x(e,t){let r,n={},o=-1;for(let a of t){let t=y(e,a);if(null===t)continue;let i=b(a);i>o&&(r=a,n=t,o=i)}return r?{route:r,params:n}:void 0}function S(e){let t=e.trim();return t&&"/"!==t?(t.startsWith("/")||(t="/"+t),t.endsWith("/")&&(t=t.slice(0,-1)),t):""}function te(){if(typeof document>"u")return"";let e=document.querySelector("base");if(!e)return"";let t=e.getAttribute("href")||"";try{return S(new URL(t,window.location.origin).pathname)}catch{return S(t)}}function C(e){return!1===e?{allow:!1}:!0===e||null==e?{allow:!0}:"string"==typeof e?{allow:!1,redirect:e}:"object"==typeof e&&"redirect"in e&&"string"==typeof e.redirect?{allow:!1,redirect:e.redirect}:{allow:!0}}function w(t,r){let n=null==r?.base?te():S(r.base),i=r?.mode??"history",l="hash"===i,u=r?.scrollBehavior,s=new Map,c=!1;function g(e){return e?e.startsWith("/")?e:"/"+e:"/"}function w(e){let t=g(e||"/");if(n&&t.startsWith(n)){let e=t.slice(n.length);return""===e?"/":g(e)}return t}function y(){return l?function(){let e=window.location.hash||"";if(e.startsWith("#")&&(e=e.slice(1)),!e)return{pathname:"/",search:""};e.startsWith("/")||(e="/"+e);let t=e.indexOf("?"),r=-1===t?e:e.slice(0,t),n=-1===t?"":e.slice(t);return{pathname:w(r),search:n}}():{pathname:w(window.location.pathname||"/"),search:window.location.search||""}}function b(e,t){let r=function(e){let t=g(e);return n?(n+t).replace(/\/+/g,"/")||"/":t}(e)+h(t);return l?"#"+r:r}function _(e,t){return g(e)+h(t)}let k=y(),E=k.pathname,R=m(k.search),j=v(t),q=new Map;for(let e of j)e.name&&(q.has(e.name)&&console.warn(`[Nix Router] Duplicate route name: "${e.name}"`),q.set(e.name,e));let O=x(E,j),W=e.signal(E),$=e.signal(O?.params??{}),N=e.signal(R),L=f(history.state)??0,P=e.signal({action:"initial",direction:"none"}),M=e.signal(L>0);function T(e){window.scrollTo(e.left,e.top)}function B(e,t,r){if(u){let n=u(e,t,r);if(!n)return;return void T(n)}T(r??{left:0,top:0})}l?s.set(_(E,R),d()):history.replaceState(p(history.state,d(),L),"");let I=[],U=[],A=0;function D(e,t,r,n,o){let a=[...I];r&&a.push(r);let i=++A;if(0===a.length)return void n();let l=0;!function r(u){if(i!==A)return;let s=C(u);if(!s.allow)return s.redirect&&s.redirect!==e?void z(s.redirect):s.redirect===e?void n():void o?.();if(l>=a.length)return void n();let c=a[l++](e,t);c instanceof Promise?c.then(r):r(c)}(void 0)}let G=!1;function Q(e,t){let r=e.indexOf("?"),n=g((-1===r?e:e.slice(0,r))||"/"),o=-1===r?{}:m(e.slice(r)),a=t?{...o,...t}:o,i={};for(let[e,t]of Object.entries(a))null!=t&&!1!==t&&(i[e]=String(t));return{pathname:n,stringQuery:i}}function K(e,t){return"string"==typeof e?Q(e,t?.query):Q(function(e){let t=q.get(e.name);if(!t)throw Error(`[Nix Router] No route with name "${e.name}"`);return"/"+t.segments.map(t=>{if("literal"===t.kind)return t.value;if("wildcard"===t.kind)return"";let r=e.params?.[t.name];if(null==r)throw Error(`[Nix Router] Missing param "${t.name}" for route "${e.name}"`);return encodeURIComponent(String(r))}).filter(Boolean).join("/")}(e),{...e.query??{},...t?.query??{}})}o&&=(o(),null);let X,Y=(e,t,r,n,o)=>{let a=W.value,i={...N.value},l=x(e,j),u="none";null!=n&&(n<L?u="back":n>L&&(u="forward")),D(e,a,l?.route.beforeEnter,()=>{null!=n&&(L=n);let o=X;X=void 0,P.value={action:"pop",direction:u,animation:o},$.value=l?.params??{},N.value=t,W.value=e,M.value=L>0,B(e,a,r);for(let t of U)try{t(e,a)}catch{}},()=>o(a,i))};if(l){let e=()=>{if(c)return void(c=!1);let e=y(),t=m(e.search),r=s.get(_(e.pathname,t))??null;Y(e.pathname,t,r,null,(e,t)=>{c=!0,window.location.hash=b(e,t).slice(1),queueMicrotask(()=>{c=!1})})};window.addEventListener("hashchange",e),o=()=>window.removeEventListener("hashchange",e)}else{let e=e=>{let t=y(),r=m(t.search),n=ee(e.state??history.state),o=f(e.state??history.state);Y(t.pathname,r,n,o,(e,t)=>{history.pushState(p({},d(),L),"",b(e,t))})};window.addEventListener("popstate",e),o=()=>window.removeEventListener("popstate",e)}function V(e,t,r,n,o,a,i){i||(function(e,t){let r=d();l?s.set(_(e,t),r):history.replaceState(p(history.state,r,L),"")}(r,n),L+=1),P.value=a,$.value=o?.params??{},N.value=t,W.value=e,M.value=L>0;let u=b(e,t);if(l)s.set(_(e,t),{left:0,top:0}),i?history.replaceState(history.state,"",u):(c=!0,window.location.hash=u.slice(1),queueMicrotask(()=>{c=!1}));else{let e=p({},{left:0,top:0},L);i?history.replaceState(e,"",u):history.pushState(e,"",u)}B(e,r,null);for(let t of U)try{t(e,r)}catch{}}function z(e,t){G=!0;let{pathname:r,stringQuery:n}=K(e,t),o=W.value,a={...N.value},i=x(r,j),l={action:"push",direction:t?.direction??"forward",animation:t?.animation};D(r,o,i?.route.beforeEnter,()=>V(r,n,o,a,i,l,!1))}let F={current:W,params:$,query:N,intent:P,canGoBack:M,base:n||"/",navigate:z,replace:function(e,t){G=!0;let{pathname:r,stringQuery:n}=K(e,t),o=W.value,a={...N.value},i=x(r,j),l={action:"replace",direction:t?.direction??"root",animation:t?.animation};D(r,o,i?.route.beforeEnter,()=>V(r,n,o,a,i,l,!0))},back:function(e){void 0!==e&&(X=e),history.back()},forward:function(e){void 0!==e&&(X=e),history.forward()},go:function(e){history.go(e)},isActive:function(e,t=!0){let r=W.value;return t?r===e:r===e||r.startsWith(e.endsWith("/")?e:e+"/")},resolve:function(e){let t=x(e,j);return t?{matched:!0,params:t.params,route:t.route.record}:{matched:!1,params:{},route:void 0}},beforeEach:function(e){return I.push(e),()=>{let t=I.indexOf(e);-1!==t&&I.splice(t,1)}},afterEach:function(e){return U.push(e),()=>{let t=U.indexOf(e);-1!==t&&U.splice(t,1)}},routes:t,_flat:j,_guards:I,_base:n,_mode:i};return a&&console.warn("[Nix] A router already exists. The previous router is being replaced. Only one router instance should be active at a time."),a=F,queueMicrotask(()=>{G||D(E,"",x(E,j)?.route.beforeEnter,()=>{},()=>{let e=b("/",{});l?(s.set(_("/",{}),{left:0,top:0}),history.replaceState(history.state,"",e)):history.replaceState(p({},{left:0,top:0},L),"",e);let t=x("/",j);P.value={action:"replace",direction:"root"},W.value="/",$.value=t?.params??{},N.value={},M.value=L>0,B("/",E,null)})}),F}function T(){return r.inject(i)||l()}function E(){o&&=(o(),null),a=null}var D=class extends n.NixComponent{_depth;_router;constructor(e=0,t){super(),this._depth=e,this._router=t}render(){let e=this._depth,r=this._router;return t.l`<div class="router-view">${()=>{let n=r??T(),o=x(n.current.value,n._flat);if(!o)return t.l`
|
|
2
2
|
<div style="color:#f87171;padding:16px 0">
|
|
3
|
-
404 — Route not found: <strong>${
|
|
3
|
+
404 — Route not found: <strong>${n.current.value}</strong>
|
|
4
4
|
</div>
|
|
5
|
-
`;if(e>=
|
|
5
|
+
`;if(e>=o.route.chain.length)return t.l`
|
|
6
6
|
<span></span>
|
|
7
|
-
`;let o
|
|
7
|
+
`;let a=o.route.chain[e];return a?a():t.l`
|
|
8
8
|
<span></span>
|
|
9
|
-
`}}</div>`}},O=class extends n.NixComponent{_to;_label;constructor(e,t){super(),this._to=e,this._label=t}render(){let e=this._to,r=this._label,n=T(),o=e.startsWith("/")?e:"/"+e,a=(n._base?n._base+o:o).replace(/\/+/g,"/");return t.l`
|
|
9
|
+
`}}</div>`}},O=class extends n.NixComponent{_to;_label;_router;constructor(e,t,r){super(),this._to=e,this._label=t,this._router=r}render(){let e=this._to,r=this._label,n=this._router??T(),o=e.startsWith("/")?e:"/"+e,a=(n._base?n._base+o:o).replace(/\/+/g,"/");return t.l`
|
|
10
10
|
<a href=${"hash"===n._mode?"#"+a:a} style=${()=>n.current.value===e?"color:#38bdf8;font-weight:700;text-decoration:none;cursor:pointer;padding:4px 10px;border-radius:4px;background:#0c2a3a":"color:#a3a3a3;text-decoration:none;cursor:pointer;padding:4px 10px;border-radius:4px"} @click=${t=>{t.preventDefault(),n.navigate(e)}}>${r}</a>
|
|
11
11
|
`}};function k(){if(!a)return null;let e=a.current.value,t=x(e,a._flat),r=t?.route.beforeEnter,n=a._guards.map((e,t)=>e.name||`beforeEach#${t+1}`);return r&&n.push(r.name||"beforeEnter"),{mode:a._mode,base:a._base||"/",currentPath:e,params:{...a.params.value},query:{...a.query.value},matchedPath:t?.route.fullPath??null,activeGuards:{globalCount:a._guards.length,hasRouteGuard:!!r,names:n}}}exports.Link=O,exports.RouterKey=i,exports.RouterView=D,exports._debugGetRouterInternal=k,exports._hasActiveRouter=u,exports._resetRouter=E,exports.createRouter=w,exports.nixRouter=T;
|
package/dist/lib/router.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import{signal as e}from"./signals.js";import{l as t}from"./template2.js";import{NixComponent as n}from"./lifecycle.js";import{createInjectionKey as r,inject as i}from"./context.js";var a=r("nix:router"),o=null,s=null,c="__nix_scroll",l="__nix_pos";function u(){if(!o)throw Error("[Nix] No active router. Call createRouter() first, or instantiate an outlet that auto-bootstraps one (e.g. IonRouterOutlet).");return o}function d(){return null!==o}function f(){return{left:window.scrollX??window.pageXOffset??0,top:window.scrollY??window.pageYOffset??0}}function ee(e){if(!e||"object"!=typeof e)return null;let t=e[c];if(!t||"object"!=typeof t)return null;let r=t.left,n=t.top;return"number"!=typeof r||"number"!=typeof n?null:{left:r,top:n}}function p(e){if(!e||"object"!=typeof e)return null;let t=e[l];return"number"==typeof t?t:null}function m(e,t,r){let n=e&&"object"==typeof e?{...e}:{};return n[c]={left:t.left,top:t.top},n[l]=r,n}function h(e){let t={};return new URLSearchParams(e).forEach((e,r)=>{t[r]=e}),t}function g(e){let t=new URLSearchParams;for(let[r,n]of Object.entries(e))null!=n&&!1!==n&&t.set(r,String(n));let r=t.toString();return r?"?"+r:""}function _(e){return"*"===e?[{kind:"wildcard"}]:e.split("/").filter(Boolean).map(e=>"*"===e?{kind:"wildcard"}:e.startsWith(":")?{kind:"param",name:e.slice(1)}:{kind:"literal",value:e})}function v(e,t){return"*"===t?""===e?"*":e+"/*":(e+(t.startsWith("/")?t:"/"+t)).replace(/\/+/g,"/")||"/"}function y(e,t="",r=[]){let n=[];for(let o of e){let e=v(t,o.path),a=[...r,o.component],i=_(e);n.push({fullPath:e,segments:i,chain:a,name:o.name,meta:o.meta,beforeEnter:o.beforeEnter,record:o}),o.children?.length&&n.push(...y(o.children,e,a))}return n}function b(e,t){let r=e.split("/").filter(Boolean),n=t.segments;if(1===n.length&&"wildcard"===n[0].kind)return{};let o=n.length>0&&"wildcard"===n[n.length-1].kind,a=o?n.slice(0,-1):n;if(o){if(r.length<a.length)return null}else if(r.length!==a.length)return null;let i={};for(let e=0;e<a.length;e++){let t=a[e];if("literal"===t.kind){if(r[e]!==t.value)return null}else if("param"===t.kind)try{i[t.name]=decodeURIComponent(r[e]??"")}catch{i[t.name]=r[e]??""}}return i}function x(e){return e.segments.reduce((e,t)=>"literal"===t.kind?e+2:"param"===t.kind?e+1:e,0)}function S(e,t){let r,n={},o=-1;for(let a of t){let t=b(e,a);if(null===t)continue;let i=x(a);i>o&&(r=a,n=t,o=i)}return r?{route:r,params:n}:void 0}function C(e){let t=e.trim();return t&&"/"!==t?(t.startsWith("/")||(t="/"+t),t.endsWith("/")&&(t=t.slice(0,-1)),t):""}function te(){if(typeof document>"u")return"";let e=document.querySelector("base");if(!e)return"";let t=e.getAttribute("href")||"";try{return C(new URL(t,window.location.origin).pathname)}catch{return C(t)}}function ne(e){return!1===e?{allow:!1}:!0===e||null==e?{allow:!0}:"string"==typeof e?{allow:!1,redirect:e}:"object"==typeof e&&"redirect"in e&&"string"==typeof e.redirect?{allow:!1,redirect:e.redirect}:{allow:!0}}function w(t,r){let n=null==r?.base?te():C(r.base),a=r?.mode??"history",i="hash"===a,l=r?.scrollBehavior,u=new Map,c=!1;function d(e){return e?e.startsWith("/")?e:"/"+e:"/"}function v(e){let t=d(e||"/");if(n&&t.startsWith(n)){let e=t.slice(n.length);return""===e?"/":d(e)}return t}function w(){return i?function(){let e=window.location.hash||"";if(e.startsWith("#")&&(e=e.slice(1)),!e)return{pathname:"/",search:""};e.startsWith("/")||(e="/"+e);let t=e.indexOf("?"),r=-1===t?e:e.slice(0,t),n=-1===t?"":e.slice(t);return{pathname:v(r),search:n}}():{pathname:v(window.location.pathname||"/"),search:window.location.search||""}}function b(e,t){let r=function(e){let t=d(e);return n?(n+t).replace(/\/+/g,"/")||"/":t}(e)+g(t);return i?"#"+r:r}function _(e,t){return d(e)+g(t)}let x=w(),k=x.pathname,E=h(x.search),R=y(t),j=new Map;for(let e of R)e.name&&(j.has(e.name)&&console.warn(`[Nix Router] Duplicate route name: "${e.name}"`),j.set(e.name,e));let O=S(k,R),W=e(k),$=e(O?.params??{}),q=e(E),L=p(history.state)??0,N=e({action:"initial",direction:"none"}),P=e(L>0);function M(e){window.scrollTo(e.left,e.top)}function T(e,t,r){if(l){let n=l(e,t,r);if(!n)return;return void M(n)}M(r??{left:0,top:0})}i?u.set(_(k,E),f()):history.replaceState(m(history.state,f(),L),"");let B=[],I=[],U=0;function A(e,t,r,n,o){let a=[...B];r&&a.push(r);let i=++U;if(0===a.length)return void n();let l=0;!function r(u){if(i!==U)return;let s=ne(u);if(!s.allow)return s.redirect&&s.redirect!==e?void V(s.redirect):s.redirect===e?void n():void o?.();if(l>=a.length)return void n();let c=a[l++](e,t);c instanceof Promise?c.then(r):r(c)}(void 0)}let D=!1;function G(e,t){let r=e.indexOf("?"),n=d((-1===r?e:e.slice(0,r))||"/"),o=-1===r?{}:h(e.slice(r)),a=t?{...o,...t}:o,i={};for(let[e,t]of Object.entries(a))null!=t&&!1!==t&&(i[e]=String(t));return{pathname:n,stringQuery:i}}function Q(e,t){return"string"==typeof e?G(e,t?.query):G(function(e){let t=j.get(e.name);if(!t)throw Error(`[Nix Router] No route with name "${e.name}"`);return"/"+t.segments.map(t=>{if("literal"===t.kind)return t.value;if("wildcard"===t.kind)return"";let r=e.params?.[t.name];if(null==r)throw Error(`[Nix Router] Missing param "${t.name}" for route "${e.name}"`);return encodeURIComponent(String(r))}).filter(Boolean).join("/")}(e),{...e.query??{},...t?.query??{}})}s&&=(s(),null);let K,X=(e,t,r,n,o)=>{let a=W.value,i={...q.value},l=S(e,R),u="none";null!=n&&(n<L?u="back":n>L&&(u="forward")),A(e,a,l?.route.beforeEnter,()=>{null!=n&&(L=n);let o=K;K=void 0,N.value={action:"pop",direction:u,animation:o},$.value=l?.params??{},q.value=t,W.value=e,P.value=L>0,T(e,a,r);for(let t of I)try{t(e,a)}catch{}},()=>o(a,i))};if(i){let e=()=>{if(c)return void(c=!1);let e=w(),t=h(e.search),r=u.get(_(e.pathname,t))??null;X(e.pathname,t,r,null,(e,t)=>{c=!0,window.location.hash=b(e,t).slice(1),queueMicrotask(()=>{c=!1})})};window.addEventListener("hashchange",e),s=()=>window.removeEventListener("hashchange",e)}else{let e=e=>{let t=w(),r=h(t.search),n=ee(e.state??history.state),o=p(e.state??history.state);X(t.pathname,r,n,o,(e,t)=>{history.pushState(m({},f(),L),"",b(e,t))})};window.addEventListener("popstate",e),s=()=>window.removeEventListener("popstate",e)}function Y(e,t,r,n,o,a,l){l||(function(e,t){let r=f();i?u.set(_(e,t),r):history.replaceState(m(history.state,r,L),"")}(r,n),L+=1),N.value=a,$.value=o?.params??{},q.value=t,W.value=e,P.value=L>0;let s=b(e,t);if(i)u.set(_(e,t),{left:0,top:0}),l?history.replaceState(history.state,"",s):(c=!0,window.location.hash=s.slice(1),queueMicrotask(()=>{c=!1}));else{let e=m({},{left:0,top:0},L);l?history.replaceState(e,"",s):history.pushState(e,"",s)}T(e,r,null);for(let t of I)try{t(e,r)}catch{}}function V(e,t){D=!0;let{pathname:r,stringQuery:n}=Q(e,t),o=W.value,a={...q.value},i=S(r,R),l={action:"push",direction:t?.direction??"forward",animation:t?.animation};A(r,o,i?.route.beforeEnter,()=>Y(r,n,o,a,i,l,!1))}let z={current:W,params:$,query:q,intent:N,canGoBack:P,base:n||"/",navigate:V,replace:function(e,t){D=!0;let{pathname:r,stringQuery:n}=Q(e,t),o=W.value,a={...q.value},i=S(r,R),l={action:"replace",direction:t?.direction??"root",animation:t?.animation};A(r,o,i?.route.beforeEnter,()=>Y(r,n,o,a,i,l,!0))},back:function(e){void 0!==e&&(K=e),history.back()},forward:function(e){void 0!==e&&(K=e),history.forward()},go:function(e){history.go(e)},isActive:function(e,t=!0){let r=W.value;return t?r===e:r===e||r.startsWith(e.endsWith("/")?e:e+"/")},resolve:function(e){let t=S(e,R);return t?{matched:!0,params:t.params,route:t.route.record}:{matched:!1,params:{},route:void 0}},beforeEach:function(e){return B.push(e),()=>{let t=B.indexOf(e);-1!==t&&B.splice(t,1)}},afterEach:function(e){return I.push(e),()=>{let t=I.indexOf(e);-1!==t&&I.splice(t,1)}},routes:t,_flat:R,_guards:B,_base:n,_mode:a};return o&&console.warn("[Nix] A router already exists. The previous router is being replaced. Only one router instance should be active at a time."),o=z,queueMicrotask(()=>{D||A(k,"",S(k,R)?.route.beforeEnter,()=>{},()=>{let e=b("/",{});i?(u.set(_("/",{}),{left:0,top:0}),history.replaceState(history.state,"",e)):history.replaceState(m({},{left:0,top:0},L),"",e);let t=S("/",R);N.value={action:"replace",direction:"root"},W.value="/",$.value=t?.params??{},q.value={},P.value=L>0,T("/",k,null)})}),z}function T(){return i(a)||u()}function E(){s&&=(s(),null),o=null}var D=class extends n{_depth;constructor(e=0){super(),this._depth=e}render(){let e=this._depth;return t`<div class="router-view">${()=>{let r
|
|
1
|
+
import{signal as e}from"./signals.js";import{l as t}from"./template2.js";import{NixComponent as n}from"./lifecycle.js";import{createInjectionKey as r,inject as i}from"./context.js";var a=r("nix:router"),o=null,s=null,c="__nix_scroll",l="__nix_pos";function u(){if(!o)throw Error("[Nix] No active router. Call createRouter() first, or instantiate an outlet that auto-bootstraps one (e.g. IonRouterOutlet).");return o}function d(){return null!==o}function f(){return{left:window.scrollX??window.pageXOffset??0,top:window.scrollY??window.pageYOffset??0}}function ee(e){if(!e||"object"!=typeof e)return null;let t=e[c];if(!t||"object"!=typeof t)return null;let r=t.left,n=t.top;return"number"!=typeof r||"number"!=typeof n?null:{left:r,top:n}}function p(e){if(!e||"object"!=typeof e)return null;let t=e[l];return"number"==typeof t?t:null}function m(e,t,r){let n=e&&"object"==typeof e?{...e}:{};return n[c]={left:t.left,top:t.top},n[l]=r,n}function h(e){let t={};return new URLSearchParams(e).forEach((e,r)=>{t[r]=e}),t}function g(e){let t=new URLSearchParams;for(let[r,n]of Object.entries(e))null!=n&&!1!==n&&t.set(r,String(n));let r=t.toString();return r?"?"+r:""}function _(e){return"*"===e?[{kind:"wildcard"}]:e.split("/").filter(Boolean).map(e=>"*"===e?{kind:"wildcard"}:e.startsWith(":")?{kind:"param",name:e.slice(1)}:{kind:"literal",value:e})}function v(e,t){return"*"===t?""===e?"*":e+"/*":(e+(t.startsWith("/")?t:"/"+t)).replace(/\/+/g,"/")||"/"}function y(e,t="",r=[]){let n=[];for(let o of e){let e=v(t,o.path),a=[...r,o.component],i=_(e);n.push({fullPath:e,segments:i,chain:a,name:o.name,meta:o.meta,beforeEnter:o.beforeEnter,record:o}),o.children?.length&&n.push(...y(o.children,e,a))}return n}function b(e,t){let r=e.split("/").filter(Boolean),n=t.segments;if(1===n.length&&"wildcard"===n[0].kind)return{};let o=n.length>0&&"wildcard"===n[n.length-1].kind,a=o?n.slice(0,-1):n;if(o){if(r.length<a.length)return null}else if(r.length!==a.length)return null;let i={};for(let e=0;e<a.length;e++){let t=a[e];if("literal"===t.kind){if(r[e]!==t.value)return null}else if("param"===t.kind)try{i[t.name]=decodeURIComponent(r[e]??"")}catch{i[t.name]=r[e]??""}}return i}function x(e){return e.segments.reduce((e,t)=>"literal"===t.kind?e+2:"param"===t.kind?e+1:e,0)}function S(e,t){let r,n={},o=-1;for(let a of t){let t=b(e,a);if(null===t)continue;let i=x(a);i>o&&(r=a,n=t,o=i)}return r?{route:r,params:n}:void 0}function C(e){let t=e.trim();return t&&"/"!==t?(t.startsWith("/")||(t="/"+t),t.endsWith("/")&&(t=t.slice(0,-1)),t):""}function te(){if(typeof document>"u")return"";let e=document.querySelector("base");if(!e)return"";let t=e.getAttribute("href")||"";try{return C(new URL(t,window.location.origin).pathname)}catch{return C(t)}}function ne(e){return!1===e?{allow:!1}:!0===e||null==e?{allow:!0}:"string"==typeof e?{allow:!1,redirect:e}:"object"==typeof e&&"redirect"in e&&"string"==typeof e.redirect?{allow:!1,redirect:e.redirect}:{allow:!0}}function w(t,r){let n=null==r?.base?te():C(r.base),a=r?.mode??"history",i="hash"===a,l=r?.scrollBehavior,u=new Map,c=!1;function d(e){return e?e.startsWith("/")?e:"/"+e:"/"}function v(e){let t=d(e||"/");if(n&&t.startsWith(n)){let e=t.slice(n.length);return""===e?"/":d(e)}return t}function w(){return i?function(){let e=window.location.hash||"";if(e.startsWith("#")&&(e=e.slice(1)),!e)return{pathname:"/",search:""};e.startsWith("/")||(e="/"+e);let t=e.indexOf("?"),r=-1===t?e:e.slice(0,t),n=-1===t?"":e.slice(t);return{pathname:v(r),search:n}}():{pathname:v(window.location.pathname||"/"),search:window.location.search||""}}function b(e,t){let r=function(e){let t=d(e);return n?(n+t).replace(/\/+/g,"/")||"/":t}(e)+g(t);return i?"#"+r:r}function _(e,t){return d(e)+g(t)}let x=w(),k=x.pathname,E=h(x.search),R=y(t),j=new Map;for(let e of R)e.name&&(j.has(e.name)&&console.warn(`[Nix Router] Duplicate route name: "${e.name}"`),j.set(e.name,e));let O=S(k,R),W=e(k),$=e(O?.params??{}),q=e(E),L=p(history.state)??0,N=e({action:"initial",direction:"none"}),P=e(L>0);function M(e){window.scrollTo(e.left,e.top)}function T(e,t,r){if(l){let n=l(e,t,r);if(!n)return;return void M(n)}M(r??{left:0,top:0})}i?u.set(_(k,E),f()):history.replaceState(m(history.state,f(),L),"");let B=[],I=[],U=0;function A(e,t,r,n,o){let a=[...B];r&&a.push(r);let i=++U;if(0===a.length)return void n();let l=0;!function r(u){if(i!==U)return;let s=ne(u);if(!s.allow)return s.redirect&&s.redirect!==e?void V(s.redirect):s.redirect===e?void n():void o?.();if(l>=a.length)return void n();let c=a[l++](e,t);c instanceof Promise?c.then(r):r(c)}(void 0)}let D=!1;function G(e,t){let r=e.indexOf("?"),n=d((-1===r?e:e.slice(0,r))||"/"),o=-1===r?{}:h(e.slice(r)),a=t?{...o,...t}:o,i={};for(let[e,t]of Object.entries(a))null!=t&&!1!==t&&(i[e]=String(t));return{pathname:n,stringQuery:i}}function Q(e,t){return"string"==typeof e?G(e,t?.query):G(function(e){let t=j.get(e.name);if(!t)throw Error(`[Nix Router] No route with name "${e.name}"`);return"/"+t.segments.map(t=>{if("literal"===t.kind)return t.value;if("wildcard"===t.kind)return"";let r=e.params?.[t.name];if(null==r)throw Error(`[Nix Router] Missing param "${t.name}" for route "${e.name}"`);return encodeURIComponent(String(r))}).filter(Boolean).join("/")}(e),{...e.query??{},...t?.query??{}})}s&&=(s(),null);let K,X=(e,t,r,n,o)=>{let a=W.value,i={...q.value},l=S(e,R),u="none";null!=n&&(n<L?u="back":n>L&&(u="forward")),A(e,a,l?.route.beforeEnter,()=>{null!=n&&(L=n);let o=K;K=void 0,N.value={action:"pop",direction:u,animation:o},$.value=l?.params??{},q.value=t,W.value=e,P.value=L>0,T(e,a,r);for(let t of I)try{t(e,a)}catch{}},()=>o(a,i))};if(i){let e=()=>{if(c)return void(c=!1);let e=w(),t=h(e.search),r=u.get(_(e.pathname,t))??null;X(e.pathname,t,r,null,(e,t)=>{c=!0,window.location.hash=b(e,t).slice(1),queueMicrotask(()=>{c=!1})})};window.addEventListener("hashchange",e),s=()=>window.removeEventListener("hashchange",e)}else{let e=e=>{let t=w(),r=h(t.search),n=ee(e.state??history.state),o=p(e.state??history.state);X(t.pathname,r,n,o,(e,t)=>{history.pushState(m({},f(),L),"",b(e,t))})};window.addEventListener("popstate",e),s=()=>window.removeEventListener("popstate",e)}function Y(e,t,r,n,o,a,l){l||(function(e,t){let r=f();i?u.set(_(e,t),r):history.replaceState(m(history.state,r,L),"")}(r,n),L+=1),N.value=a,$.value=o?.params??{},q.value=t,W.value=e,P.value=L>0;let s=b(e,t);if(i)u.set(_(e,t),{left:0,top:0}),l?history.replaceState(history.state,"",s):(c=!0,window.location.hash=s.slice(1),queueMicrotask(()=>{c=!1}));else{let e=m({},{left:0,top:0},L);l?history.replaceState(e,"",s):history.pushState(e,"",s)}T(e,r,null);for(let t of I)try{t(e,r)}catch{}}function V(e,t){D=!0;let{pathname:r,stringQuery:n}=Q(e,t),o=W.value,a={...q.value},i=S(r,R),l={action:"push",direction:t?.direction??"forward",animation:t?.animation};A(r,o,i?.route.beforeEnter,()=>Y(r,n,o,a,i,l,!1))}let z={current:W,params:$,query:q,intent:N,canGoBack:P,base:n||"/",navigate:V,replace:function(e,t){D=!0;let{pathname:r,stringQuery:n}=Q(e,t),o=W.value,a={...q.value},i=S(r,R),l={action:"replace",direction:t?.direction??"root",animation:t?.animation};A(r,o,i?.route.beforeEnter,()=>Y(r,n,o,a,i,l,!0))},back:function(e){void 0!==e&&(K=e),history.back()},forward:function(e){void 0!==e&&(K=e),history.forward()},go:function(e){history.go(e)},isActive:function(e,t=!0){let r=W.value;return t?r===e:r===e||r.startsWith(e.endsWith("/")?e:e+"/")},resolve:function(e){let t=S(e,R);return t?{matched:!0,params:t.params,route:t.route.record}:{matched:!1,params:{},route:void 0}},beforeEach:function(e){return B.push(e),()=>{let t=B.indexOf(e);-1!==t&&B.splice(t,1)}},afterEach:function(e){return I.push(e),()=>{let t=I.indexOf(e);-1!==t&&I.splice(t,1)}},routes:t,_flat:R,_guards:B,_base:n,_mode:a};return o&&console.warn("[Nix] A router already exists. The previous router is being replaced. Only one router instance should be active at a time."),o=z,queueMicrotask(()=>{D||A(k,"",S(k,R)?.route.beforeEnter,()=>{},()=>{let e=b("/",{});i?(u.set(_("/",{}),{left:0,top:0}),history.replaceState(history.state,"",e)):history.replaceState(m({},{left:0,top:0},L),"",e);let t=S("/",R);N.value={action:"replace",direction:"root"},W.value="/",$.value=t?.params??{},q.value={},P.value=L>0,T("/",k,null)})}),z}function T(){return i(a)||u()}function E(){s&&=(s(),null),o=null}var D=class extends n{_depth;_router;constructor(e=0,t){super(),this._depth=e,this._router=t}render(){let e=this._depth,r=this._router;return t`<div class="router-view">${()=>{let n=r??T(),o=S(n.current.value,n._flat);if(!o)return t`
|
|
2
2
|
<div style="color:#f87171;padding:16px 0">
|
|
3
|
-
404 — Route not found: <strong>${
|
|
3
|
+
404 — Route not found: <strong>${n.current.value}</strong>
|
|
4
4
|
</div>
|
|
5
|
-
`;if(e>=
|
|
5
|
+
`;if(e>=o.route.chain.length)return t`
|
|
6
6
|
<span></span>
|
|
7
|
-
`;let o
|
|
7
|
+
`;let a=o.route.chain[e];return a?a():t`
|
|
8
8
|
<span></span>
|
|
9
|
-
`}}</div>`}},O=class extends n{_to;_label;constructor(e,t){super(),this._to=e,this._label=t}render(){let e=this._to,r=this._label,n=T(),o=e.startsWith("/")?e:"/"+e,a=(n._base?n._base+o:o).replace(/\/+/g,"/");return t`
|
|
9
|
+
`}}</div>`}},O=class extends n{_to;_label;_router;constructor(e,t,r){super(),this._to=e,this._label=t,this._router=r}render(){let e=this._to,r=this._label,n=this._router??T(),o=e.startsWith("/")?e:"/"+e,a=(n._base?n._base+o:o).replace(/\/+/g,"/");return t`
|
|
10
10
|
<a href=${"hash"===n._mode?"#"+a:a} style=${()=>n.current.value===e?"color:#38bdf8;font-weight:700;text-decoration:none;cursor:pointer;padding:4px 10px;border-radius:4px;background:#0c2a3a":"color:#a3a3a3;text-decoration:none;cursor:pointer;padding:4px 10px;border-radius:4px"} @click=${t=>{t.preventDefault(),n.navigate(e)}}>${r}</a>
|
|
11
11
|
`}};function k(){if(!o)return null;let e=o.current.value,t=S(e,o._flat),r=t?.route.beforeEnter,n=o._guards.map((e,t)=>e.name||`beforeEach#${t+1}`);return r&&n.push(r.name||"beforeEnter"),{mode:o._mode,base:o._base||"/",currentPath:e,params:{...o.params.value},query:{...o.query.value},matchedPath:t?.route.fullPath??null,activeGuards:{globalCount:o._guards.length,hasRouteGuard:!!r,names:n}}}export{O as Link,a as RouterKey,D as RouterView,k as _debugGetRouterInternal,d as _hasActiveRouter,E as _resetRouter,w as createRouter,T as nixRouter};
|
package/package.json
CHANGED