frontend/.pnpm-store/v3/files/a0/90aee4e8b273e024f6a3fd09ea3ed606ed99ee4d4c6a93481c58ab027ad1cb189fbbf43842c1fd01baddc27ee43e8e06d3884af3a0d2372057ab02e1b8ba92

2 lines
5.8 KiB
Plaintext

import{getContext as D}from"svelte";import{writable as F}from"svelte/store";function b(e,r=!1){return e=e.slice(e.startsWith("/#")?2:0,e.endsWith("/*")?-2:void 0),e.startsWith("/")||(e="/"+e),e==="/"&&(e=""),r&&!e.endsWith("/")&&(e+="/"),e}function g(e,r){e=b(e,!0),r=b(r,!0);let a=[],n={},t=!0,c=e.split("/").map(s=>s.startsWith(":")?(a.push(s.slice(1)),"([^\\/]+)"):s).join("\\/"),o=r.match(new RegExp(`^${c}$`));return o||(t=!1,o=r.match(new RegExp(`^${c}`))),o?(a.forEach((s,h)=>n[s]=o[h+1]),{exact:t,params:n,part:o[0].slice(0,-1)}):null}function k(e,r,a){if(a==="")return e;if(a[0]==="/")return a;let n=o=>o.split("/").filter(s=>s!==""),t=n(e),c=r?n(r):[];return"/"+c.map((o,s)=>t[s]).join("/")+"/"+a}function p(e,r,a,n){let t=[r,"data-"+r].reduce((c,o)=>{let s=e.getAttribute(o);return a&&e.removeAttribute(o),s===null?c:s},!1);return!n&&t===""?!0:t||n||!1}function M(e){let r=e.split("&").map(a=>a.split("=")).reduce((a,n)=>{let t=n[0];if(!t)return a;let c=n.length>1?n[n.length-1]:!0;return typeof c=="string"&&c.includes(",")&&(c=c.split(",")),a[t]===void 0?a[t]=[c]:a[t].push(c),a},{});return Object.entries(r).reduce((a,n)=>(a[n[0]]=n[1].length>1?n[1]:n[1][0],a),{})}function v(e){return Object.entries(e).map(([r,a])=>a?a===!0?r:`${r}=${Array.isArray(a)?a.join(","):a}`:null).filter(r=>r).join("&")}function R(e,r){return e?r+e:""}function y(e){throw new Error("[Tinro] "+e)}var i={HISTORY:1,HASH:2,MEMORY:3,OFF:4,run(e,r,a,n){return e===this.HISTORY?r&&r():e===this.HASH?a&&a():n&&n()},getDefault(){return!window||window.location.pathname==="srcdoc"?this.MEMORY:this.HISTORY}};var O,H,$,u=C();function C(){let e=i.getDefault(),r,a=o=>window.onhashchange=window.onpopstate=O=null,n=o=>r&&r(S(e)),t=o=>{o&&(e=o),a(),e!==i.OFF&&i.run(e,s=>window.onpopstate=n,s=>window.onhashchange=n)&&n()},c=o=>{let s=Object.assign(S(e),o);return s.path+R(v(s.query),"?")+R(s.hash,"#")};return{mode:t,get:o=>S(e),go(o,s){Y(e,o,s),n()},start(o){r=o,t()},stop(){r=null,t(i.OFF)},set(o){this.go(c(o),!o.path)},methods(){return Q(this)}}}function Y(e,r,a){!a&&(H=$);let n=t=>history[`${a?"replace":"push"}State`]({},"",t);i.run(e,t=>n(r),t=>n(`#${r}`),t=>O=r)}function S(e){let r=window.location,a=i.run(e,t=>r.pathname+r.search+r.hash,t=>String(r.hash.slice(1)||"/"),t=>O||"/"),n=a.match(/^([^?#]+)(?:\?([^#]+))?(?:\#(.+))?$/);return $=a,{url:a,from:H,path:n[1]||"",query:M(n[2]||""),hash:n[3]||""}}function Q(e){let r=()=>e.get().query,a=o=>e.set({query:o}),n=o=>a(o(r())),t=()=>e.get().hash,c=o=>e.set({hash:o});return{hash:{get:t,set:c,clear:()=>c("")},query:{replace:a,clear:()=>a(""),get(o){return o?r()[o]:r()},set(o,s){n(h=>(h[o]=s,h))},delete(o){n(s=>(s[o]&&delete s[o],s))}}}}import{hasContext as T,getContext as E,setContext as U,onMount as W,tick as L}from"svelte";import{writable as z}from"svelte/store";var x="tinro";function _(e){let r=E(x);r&&(r.exact||r.fallback)&&y(`${e.fallback?"<Route fallback>":`<Route path="${e.path}">`} can't be inside ${r.fallback?"<Route fallback>":`<Route path="${r.path||"/"}"> with exact path`}`);let a=e.fallback?"fallbacks":"childs",n=z({}),t={router:{},exact:!1,pattern:null,meta:{},parent:r,fallback:e.fallback,redirect:!1,firstmatch:!1,breadcrumb:null,matched:!1,childs:new Set,activeChilds:new Set,fallbacks:new Set,update(c){t.exact=!c.path.endsWith("/*"),t.pattern=b(`${t.parent&&t.parent.pattern||""}${c.path}`),t.redirect=c.redirect,t.firstmatch=c.firstmatch,t.breadcrumb=c.breadcrumb,t.match()},register:()=>{if(!!t.parent)return t.parent[a].add(t),()=>{t.parent[a].delete(t),t.router.un&&t.router.un()}},show:()=>{e.onShow(),!t.fallback&&t.parent&&t.parent.activeChilds.add(t)},hide:()=>{e.onHide(),!t.fallback&&t.parent&&t.parent.activeChilds.delete(t)},match:async()=>{t.matched=!1;let{path:c,url:o,from:s,query:h}=t.router.location,l=g(t.pattern,c);if(!t.fallback&&l&&t.redirect&&(!t.exact||t.exact&&l.exact)){await L();let m=k(c,t.parent&&t.parent.pattern,t.redirect);return f.goto(m,!0)}if(t.meta=l&&{from:s,url:o,query:h,match:l.part,pattern:t.pattern,breadcrumbs:t.parent&&t.parent.meta&&t.parent.meta.breadcrumbs.slice()||[],params:l.params,subscribe:n.subscribe},t.breadcrumb&&t.meta&&t.meta.breadcrumbs.push({name:t.breadcrumb,path:l.part}),n.set(t.meta),l&&!t.fallback&&(!t.exact||t.exact&&l.exact)&&(!t.parent||!t.parent.firstmatch||!t.parent.matched)?(e.onMeta(t.meta),t.parent&&(t.parent.matched=!0),t.show()):t.hide(),await L(),l&&!t.fallback&&(t.childs.size>0&&t.activeChilds.size==0||t.childs.size==0&&t.fallbacks.size>0)){let m=t;for(;m.fallbacks.size==0;)if(m=m.parent,!m)return;m&&m.fallbacks.forEach(d=>{if(d.redirect){let A=k("/",d.parent&&d.parent.pattern,d.redirect);f.goto(A,!0)}else d.show()})}}};return U(x,t),W(()=>t.register()),t.router.un=f.subscribe(c=>{t.router.location=c,t.pattern!==null&&t.match()}),t}function w(){return T(x)?E(x).meta:y("meta() function must be run inside any `<Route>` child component only")}var f=P();function P(){let{subscribe:e}=F(u.get(),r=>{u.start(r);let a=I(u.go);return()=>{u.stop(),a()}});return{subscribe:e,goto:u.go,params:K,meta:w,useHashNavigation:r=>u.mode(r?i.HASH:i.HISTORY),mode:{hash:()=>u.mode(i.HASH),history:()=>u.mode(i.HISTORY),memory:()=>u.mode(i.MEMORY)},location:u.methods()}}function j(e){let r=p(e,"href"),a=p(e,"exact",!0),n=p(e,"active-class",!0,"active");return{destroy:f.subscribe(t=>{let c=g(r,t.path);c&&(c.exact&&a||!a)?e.classList.add(n):e.classList.remove(n)})}}function I(e){let r=a=>{let n=a.target.closest("a[href]"),t=n&&p(n,"target",!1,"_self"),c=n&&p(n,"tinro-ignore"),o=a.ctrlKey||a.metaKey||a.altKey||a.shiftKey;if(t=="_self"&&!c&&!o&&n){let s=n.getAttribute("href").replace(/^\/#/,"");/^\/\/|^[a-zA-Z]+:/.test(s)||(a.preventDefault(),e(s.startsWith("/")?s:n.href.replace(window.location.origin,"")))}};return addEventListener("click",r),()=>removeEventListener("click",r)}function K(){return D("tinro").meta.params}export{j as active,_ as createRouteObject,w as meta,f as router};