UNPKG

svelte-router-spa

Version:

A full featured router component for Svelte and your Single Page Applications.

2 lines (1 loc) 15.9 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).SvelteRouterSpa={})}(this,(function(t){"use strict";function e(){}function n(t){return t()}function r(){return Object.create(null)}function o(t){t.forEach(n)}function a(t){return"function"==typeof t}function u(t,e){return t!=t?e==e:t!==e||t&&"object"==typeof t||"function"==typeof t}function s(t,n,r){t.$$.on_destroy.push(function(t,...n){if(null==t)return e;const r=t.subscribe(...n);return r.unsubscribe?()=>r.unsubscribe():r}(n,r))}function c(t,e,n,r){return t[1]&&r?function(t,e){for(const n in e)t[n]=e[n];return t}(n.ctx.slice(),t[1](r(e))):n.ctx}function i(t,e,n,r,o,a,u){const s=function(t,e,n,r){if(t[2]&&r){const o=t[2](r(n));if(void 0===e.dirty)return o;if("object"==typeof o){const t=[],n=Math.max(e.dirty.length,o.length);for(let r=0;r<n;r+=1)t[r]=e.dirty[r]|o[r];return t}return e.dirty|o}return e.dirty}(e,r,o,a);if(s){const o=c(e,n,r,u);t.p(o,s)}}function l(t,e,n){t.insertBefore(e,n||null)}function f(t){t.parentNode.removeChild(t)}function h(){return t="",document.createTextNode(t);var t}function d(t,e,n){null==n?t.removeAttribute(e):t.getAttribute(e)!==n&&t.setAttribute(e,n)}function p(t,e,n){t.classList[n?"add":"remove"](e)}let g;function m(t){g=t}function $(t){(function(){if(!g)throw new Error("Function called outside component initialization");return g})().$$.on_mount.push(t)}const y=[],b=[],v=[],P=[],w=Promise.resolve();let R=!1;function x(t){v.push(t)}let N=!1;const _=new Set;function j(){if(!N){N=!0;do{for(let t=0;t<y.length;t+=1){const e=y[t];m(e),L(e.$$)}for(m(null),y.length=0;b.length;)b.pop()();for(let t=0;t<v.length;t+=1){const e=v[t];_.has(e)||(_.add(e),e())}v.length=0}while(y.length);for(;P.length;)P.pop()();R=!1,N=!1,_.clear()}}function L(t){if(null!==t.fragment){t.update(),o(t.before_update);const e=t.dirty;t.dirty=[-1],t.fragment&&t.fragment.p(t.ctx,e),t.after_update.forEach(x)}}const O=new Set;let k;function A(){k={r:0,c:[],p:k}}function E(){k.r||o(k.c),k=k.p}function T(t,e){t&&t.i&&(O.delete(t),t.i(e))}function C(t,e,n,r){if(t&&t.o){if(O.has(t))return;O.add(t),k.c.push((()=>{O.delete(t),r&&(n&&t.d(1),r())})),t.o(e)}}function z(t){t&&t.c()}function q(t,e,r){const{fragment:u,on_mount:s,on_destroy:c,after_update:i}=t.$$;u&&u.m(e,r),x((()=>{const e=s.map(n).filter(a);c?c.push(...e):o(e),t.$$.on_mount=[]})),i.forEach(x)}function K(t,e){const n=t.$$;null!==n.fragment&&(o(n.on_destroy),n.fragment&&n.fragment.d(e),n.on_destroy=n.fragment=null,n.ctx=[])}function S(t,e){-1===t.$$.dirty[0]&&(y.push(t),R||(R=!0,w.then(j)),t.$$.dirty.fill(0)),t.$$.dirty[e/31|0]|=1<<e%31}function W(t,n,a,u,s,c,i=[-1]){const l=g;m(t);const h=t.$$={fragment:null,ctx:null,props:c,update:e,not_equal:s,bound:r(),on_mount:[],on_destroy:[],before_update:[],after_update:[],context:new Map(l?l.$$.context:[]),callbacks:r(),dirty:i,skip_bound:!1};let d=!1;if(h.ctx=a?a(t,n.props||{},((e,n,...r)=>{const o=r.length?r[0]:n;return h.ctx&&s(h.ctx[e],h.ctx[e]=o)&&(!h.skip_bound&&h.bound[e]&&h.bound[e](o),d&&S(t,e)),n})):[],h.update(),d=!0,o(h.before_update),h.fragment=!!u&&u(h.ctx),n.target){if(n.hydrate){const t=function(t){return Array.from(t.childNodes)}(n.target);h.fragment&&h.fragment.l(t),t.forEach(f)}else h.fragment&&h.fragment.c();n.intro&&T(t.$$.fragment),q(t,n.target,n.anchor),j()}m(l)}class I{$destroy(){K(this,1),this.$destroy=e}$on(t,e){const n=this.$$.callbacks[t]||(this.$$.callbacks[t]=[]);return n.push(e),()=>{const t=n.indexOf(e);-1!==t&&n.splice(t,1)}}$set(t){var e;this.$$set&&(e=t,0!==Object.keys(e).length)&&(this.$$.skip_bound=!0,this.$$set(t),this.$$.skip_bound=!1)}}const M=[];const{set:U,subscribe:D}=function(t,n=e){let r;const o=[];function a(e){if(u(t,e)&&(t=e,r)){const e=!M.length;for(let e=0;e<o.length;e+=1){const n=o[e];n[1](),M.push(n,t)}if(e){for(let t=0;t<M.length;t+=2)M[t][0](M[t+1]);M.length=0}}}return{set:a,update:function(e){a(e(t))},subscribe:function(u,s=e){const c=[u,s];return o.push(c),1===o.length&&(r=n(a)||e),u(t),()=>{const t=o.indexOf(c);-1!==t&&o.splice(t,1),0===o.length&&(r(),r=null)}}}}({}),V={subscribe:D,set:U,remove:()=>{U({})}},B=(t,e="")=>{const n=new URL(t);function r(){return a(e).reduce(((t,e,n)=>(":"===e[0]&&t.push({value:e.slice(1),index:n}),t)),[])}function o(){return a(n.pathname)}function a(t){return"/"===t||0===t.trim().length?[t]:("/"===t.slice(-1)&&(t=t.slice(0,-1)),"/"===t[0]&&(t=t.slice(1)),t.split("/"))}return Object.freeze({hash:n.hash,host:n.host,hostname:n.hostname,namedParams:function(){const t=o();return r().reduce(((e,n)=>(e[n.value]=t[n.index],e)),{})}(),namedParamsKeys:r().reduce(((t,e)=>(t.push(e.value),t)),[]),namedParamsValues:function(){const t=o();return r().reduce(((e,n)=>(e.push(t[n.index]),e)),[])}(),pathNames:o(),port:n.port,pathname:n.pathname,protocol:n.protocol,search:n.search,queryParams:function(){const t={};return n.searchParams.forEach(((e,n)=>{t[n]=e})),t}(),queryParamsKeys:function(){const t=[];return n.searchParams.forEach(((e,n)=>{t.push(n)})),t}(),queryParamsValues:function(){const t=[];return n.searchParams.forEach((e=>{t.push(e)})),t}()})},F=t=>{let e=!1;return 0===Object.keys(t).length||(t.childRoute&&0===Object.keys(t.childRoute).length?e=!0:t.childRoute&&(e=F(t.childRoute)),e)},G=(t,e)=>(e=X(e)).includes(":")?e.includes(t):e.startsWith(t),H=(t,e,n)=>{let r=!1;if(n)return{exists:e.lang&&e.lang[n]&&e.lang[n].includes(t),language:n};if(r=G(t,e.name),!r&&e.lang&&"object"==typeof e.lang)for(const[o,a]of Object.entries(e.lang))G(t,a)&&(r=!0,n=o);return{exists:r,language:n}},J=t=>"/"===t||0===t.trim().length?[t]:(t=X(t,"both")).split("/"),Q=(t="")=>{let e;return"/"===t||0===t.trim().length?t:(e=(t=X(t,"lead")).split(":")[0],e=X(e,"trail"),e.toLowerCase())},X=(t,e="lead")=>t.trim().length<1?"":("trail"!==e&&"both"!==e||"/"===t.slice(-1)&&(t=t.slice(0,-1)),"lead"!==e&&"both"!==e||"/"===t[0]&&(t=t.slice(1)),t),Y=(t,e=null)=>e&&t.lang&&t.lang[e]?t.lang[e]:t.name,Z=t=>{const e=t||!1;let n="";const r=(t,n)=>{if("undefined"!=typeof window){const r=(t=>{let e=[];if(t.queryParams)for(let[n,r]of Object.entries(t.queryParams))e.push(`${n}=${r}`);const n=t.hash?t.hash:"";return e.length>0?`${t.path}?${e.join("&")}${n}`:t.path+n})(t);n&&window.history.pushState({page:r},"",r),e&&o(r)}},o=t=>{"undefined"!=typeof ga&&(ga("set","page",t),ga("send","pageview"))};return Object.freeze({active:()=>n,isActive:(t,e=!1)=>{"/"!==t[0]&&(t="/"+t);let r=B(`http://fake.com${t}`).pathname,o=B(`http://fake.com${n}`).pathname;return r=X(r,"trail"),o=X(o,"trail"),e?o.includes(r):o===r},setActive:(t,e)=>{n=t.path,r(t,e)}})},tt=(t,e)=>{const n=(t=>{const e=t;return Object.freeze({valid:()=>e&&e.guard&&"function"==typeof e.guard,redirect:()=>!e.guard(),redirectPath:()=>{let t="/";return e.redirect&&e.redirect.length>0&&(t=e.redirect),t}})})(t.onlyIf);return Object.freeze({path:()=>{let r=e;return t.redirectTo&&t.redirectTo.length>0&&(r=t.redirectTo),n.valid()&&n.redirect()&&(r=n.redirectPath()),r}})};function et({basePath:t,basePathName:e,pathNames:n,convert:r,currentLanguage:o}){let a,u,s=o;function c(){return Y(u,s)}function i(){return Q(c())}function l(){return Q(a.result)}return Object.freeze({basePathSameAsLocalised:function(){return l()===i()},updatedPath:function(t){return u=t,a=((t,e,n,r,o=!1)=>{if("/"===t||0===t.trim().length)return{result:t,language:null};let a=t,u=n.name,s=r;if(o&&(s=""),u=X(u),a=X(a),n.childRoute)return{result:t,language:s};{let t=H(a,n,s);t.exists&&o&&(a=Y(n,r));let c=u.split(":")[0];return c=X(c,"trail"),c=c.split("/"),c.shift(),c.forEach((()=>{const u=e[0];if(t=H(`${a}/${u}`,n,s),!u||!t.exists)return{result:a,language:t.language};a=o?Y(n,r):`${a}/${u}`,e.shift()})),{result:a,language:t.language}}})(e,n,u,s,r),s=r?o:a.language,a},basePathNameWithoutNamedParams:l,localisedPathName:c,localisedRouteWithoutNamedParams:i,namedPath:function(){const e=c();return t?`${t}/${e}`:e},pathNames:n,routeLanguage:function(){return s},routePath:function(){let e=`${t}/${l()}`;"//"===e&&(e="/"),s&&(n=((t,e)=>{const n=e.split("/");return n.length>1&&n.forEach((function(e,n){e.length>0&&n>0&&t.shift()})),t})(n,i()));const r=((t="")=>0===t.trim().length?[]:J(t).reduce(((t,e)=>(":"===e[0]&&t.push(e.slice(1)),t)),[]))(c());return r&&r.length>0&&r.forEach((function(){n.length>0&&(e+=`/${n.shift()}`)})),e}})}function nt({routes:t,currentUrl:e,routerOptions:n,convert:r}){const o=n.defaultLanguage,a=n.prefix?n.prefix.toLowerCase():"",u=function(t,e){if(e&&e.trim().length>0){const n=t.replace(e+"/","");return B(n)}return B(t)}(e,a);let s="",c={},i=!1;function l(t,e,n,r,a){let f={};const h=et({basePath:e,basePathName:n.shift().toLowerCase(),pathNames:n,convert:a,currentLanguage:r});return i=!1,t.forEach((function(t){if(h.updatedPath(t),function(t,e){const n=t.basePathSameAsLocalised();n&&(i=!0);return n||!i&&(t=>X(t).startsWith(":"))(e)}(h,t.name)){let e=h.routePath();if(s=tt(t,s).path(),f.name!==e&&(f=function({route:t,routePath:e,routeLanguage:n,urlParser:r,namedPath:a}){const u=function({routeInfo:t,path:e,routeNamedParams:n,urlParser:r,namedPath:o,language:a}){const u=()=>{const t=B(`https://fake.com${r.pathname}`,o).namedParams;return{...n,...t}};return Object.freeze({get:()=>({name:e,component:t.component,hash:r.hash,layout:t.layout,queryParams:r.queryParams,namedParams:u(),path:e,language:a}),namedParams:u})}({routeInfo:t,urlParser:r,path:e,routeNamedParams:c,namedPath:a,language:n||o});return c=u.namedParams(),u.get()}({route:t,routePath:e,routeLanguage:h.routeLanguage(),urlParser:u,namedPath:h.namedPath()})),t.nestedRoutes&&t.nestedRoutes.length>0&&h.pathNames.length>0)f.childRoute=l(t.nestedRoutes,e,h.pathNames,h.routeLanguage(),a),f.path=f.childRoute.path,f.language=f.childRoute.language;else if(function(t,e){return t.nestedRoutes&&t.nestedRoutes.length>0&&0===e.length}(t,h.pathNames)){const n=l(t.nestedRoutes,e,["index"],h.routeLanguage(),a);n&&Object.keys(n).length>0&&(f.childRoute=n,f.language=f.childRoute.language)}}})),s&&(f.redirectTo=s),f}const f=e=>{const n=t.find((t=>"404"==t.name)),r=e||o||"";return n?{...n,language:r,path:"404"}:{name:"404",component:"",path:"404",redirectTo:"/404.html"}};return Object.freeze({findActiveRoute:function(){let e=l(t,"",u.pathNames,n.lang,r);return e&&Object.keys(e).length&&!F(e)?(e.path=e.path.split("?")[0],a&&(e.path=`/${a}${e.path}`)):"undefined"!=typeof window&&(e=f(n.lang)),e}})}const rt="/404.html";let ot,at=[],ut={};const st=(t,e,n={})=>{ut={...n},void 0!==e&&""!==e||(e=document.location.href),ot=Z(ut.gaPageviews),e=X(e,"trail"),at=t;const r=()=>{let n=!1;return ut.langConvertTo&&(ut.lang=ut.langConvertTo,n=!0),nt({routes:t,currentUrl:e,routerOptions:ut,convert:n}).findActiveRoute()};return Object.freeze({setActiveRoute:(t=!0)=>{const e=r();return e.redirectTo?((t,e)=>("undefined"!=typeof window&&(t===rt?ot.setActive({path:rt},e):it(t)),t))(e.redirectTo,t):(ot.setActive(e,t),V.set(e),e)},findActiveRoute:r})},ct=(t,e)=>(t=X(t,"lead"),ut.langConvertTo=e,st(at,"http://fake.com/"+t,ut).findActiveRoute()),it=(t,e=null,n=!0)=>(t=X(t,"lead"),e&&(ut.langConvertTo=e),st(at,"http://fake.com/"+t,ut).setActiveRoute(n)),lt=(t,e=!1)=>ot.isActive(t,e);function ft(t){let e,n;return e=new mt({props:{currentRoute:t[0].childRoute,params:t[1]}}),{c(){z(e.$$.fragment)},m(t,r){q(e,t,r),n=!0},p(t,n){const r={};1&n&&(r.currentRoute=t[0].childRoute),2&n&&(r.params=t[1]),e.$set(r)},i(t){n||(T(e.$$.fragment,t),n=!0)},o(t){C(e.$$.fragment,t),n=!1},d(t){K(e,t)}}}function ht(t){let e,n,r;var o=t[0].component;function a(t){return{props:{currentRoute:{...t[0],component:""},params:t[1]}}}return o&&(e=new o(a(t))),{c(){e&&z(e.$$.fragment),n=h()},m(t,o){e&&q(e,t,o),l(t,n,o),r=!0},p(t,r){const u={};if(1&r&&(u.currentRoute={...t[0],component:""}),2&r&&(u.params=t[1]),o!==(o=t[0].component)){if(e){A();const t=e;C(t.$$.fragment,1,0,(()=>{K(t,1)})),E()}o?(e=new o(a(t)),z(e.$$.fragment),T(e.$$.fragment,1),q(e,n.parentNode,n)):e=null}else o&&e.$set(u)},i(t){r||(e&&T(e.$$.fragment,t),r=!0)},o(t){e&&C(e.$$.fragment,t),r=!1},d(t){t&&f(n),e&&K(e,t)}}}function dt(t){let e,n,r;var o=t[0].layout;function a(t){return{props:{currentRoute:{...t[0],layout:""},params:t[1]}}}return o&&(e=new o(a(t))),{c(){e&&z(e.$$.fragment),n=h()},m(t,o){e&&q(e,t,o),l(t,n,o),r=!0},p(t,r){const u={};if(1&r&&(u.currentRoute={...t[0],layout:""}),2&r&&(u.params=t[1]),o!==(o=t[0].layout)){if(e){A();const t=e;C(t.$$.fragment,1,0,(()=>{K(t,1)})),E()}o?(e=new o(a(t)),z(e.$$.fragment),T(e.$$.fragment,1),q(e,n.parentNode,n)):e=null}else o&&e.$set(u)},i(t){r||(e&&T(e.$$.fragment,t),r=!0)},o(t){e&&C(e.$$.fragment,t),r=!1},d(t){t&&f(n),e&&K(e,t)}}}function pt(t){let e,n,r,o;const a=[dt,ht,ft],u=[];function s(t,e){return t[0].layout?0:t[0].component?1:t[0].childRoute?2:-1}return~(e=s(t))&&(n=u[e]=a[e](t)),{c(){n&&n.c(),r=h()},m(t,n){~e&&u[e].m(t,n),l(t,r,n),o=!0},p(t,[o]){let c=e;e=s(t),e===c?~e&&u[e].p(t,o):(n&&(A(),C(u[c],1,1,(()=>{u[c]=null})),E()),~e?(n=u[e],n?n.p(t,o):(n=u[e]=a[e](t),n.c()),T(n,1),n.m(r.parentNode,r)):n=null)},i(t){o||(T(n),o=!0)},o(t){C(n),o=!1},d(t){~e&&u[e].d(t),t&&f(r)}}}function gt(t,e,n){let{currentRoute:r={}}=e,{params:o={}}=e;return t.$$set=t=>{"currentRoute"in t&&n(0,r=t.currentRoute),"params"in t&&n(1,o=t.params)},[r,o]}"undefined"!=typeof window&&(window.addEventListener("click",(t=>{if("a"!==t.target.localName.toLowerCase())return;if(t.metaKey||t.ctrlKey||t.shiftKey)return;const e=ut.prefix?`/${ut.prefix.toLowerCase()}`:"",n=t.target.pathname&&t.target.host===window.location.host,r=!(e.length>1)||t.target.pathname.startsWith(e);if(n&&r){t.preventDefault();const e=t.target.pathname+t.target.search+t.target.search+t.target.hash;"_blank"===t.target.target?window.open(e,"newTab"):it(e)}})),window.onpopstate=function(t){let e=window.location.pathname+window.location.search+window.location.hash;it(e,null,!1)});class mt extends I{constructor(t){super(),W(this,t,gt,pt,u,{currentRoute:0,params:1})}}function $t(t){let e,n;return e=new mt({props:{currentRoute:t[0]}}),{c(){z(e.$$.fragment)},m(t,r){q(e,t,r),n=!0},p(t,[n]){const r={};1&n&&(r.currentRoute=t[0]),e.$set(r)},i(t){n||(T(e.$$.fragment,t),n=!0)},o(t){C(e.$$.fragment,t),n=!1},d(t){K(e,t)}}}function yt(t,e,n){let r;s(t,V,(t=>n(0,r=t)));let{routes:o=[]}=e,{options:a={}}=e;return $((()=>{st(o,document.location.href,a).setActiveRoute()})),t.$$set=t=>{"routes"in t&&n(1,o=t.routes),"options"in t&&n(2,a=t.options)},[r,o,a]}function bt(t){let e,n,r,o;const a=t[6].default,u=function(t,e,n,r){if(t){const o=c(t,e,n,r);return t[0](o)}}(a,t,t[5],null);return{c(){var n;n="a",e=document.createElement(n),u&&u.c(),d(e,"href",t[0]),d(e,"title",t[1]),d(e,"class",t[2]),p(e,"active",lt(t[0]))},m(a,s){var c,i,f,h;l(a,e,s),u&&u.m(e,null),n=!0,r||(c=e,i="click",f=t[3],c.addEventListener(i,f,h),o=()=>c.removeEventListener(i,f,h),r=!0)},p(t,[r]){u&&u.p&&32&r&&i(u,a,t,t[5],r,null,null),(!n||1&r)&&d(e,"href",t[0]),(!n||2&r)&&d(e,"title",t[1]),(!n||4&r)&&d(e,"class",t[2]),5&r&&p(e,"active",lt(t[0]))},i(t){n||(T(u,t),n=!0)},o(t){C(u,t),n=!1},d(t){t&&f(e),u&&u.d(t),r=!1,o()}}}function vt(t,e,n){let{$$slots:r={},$$scope:o}=e,{to:a="/"}=e,{title:u=""}=e,{styles:s=""}=e,{lang:c=null}=e;$((()=>{if(c){const t=ct(a,c);t&&n(0,a=t.path)}}));return t.$$set=t=>{"to"in t&&n(0,a=t.to),"title"in t&&n(1,u=t.title),"styles"in t&&n(2,s=t.styles),"lang"in t&&n(4,c=t.lang),"$$scope"in t&&n(5,o=t.$$scope)},[a,u,s,t=>{t.metaKey||t.ctrlKey||t.shiftKey||(t.preventDefault(),t.stopPropagation(),it(a))},c,o,r]}t.Navigate=class extends I{constructor(t){super(),W(this,t,vt,bt,u,{to:0,title:1,styles:2,lang:4})}},t.Route=mt,t.Router=class extends I{constructor(t){super(),W(this,t,yt,$t,u,{routes:1,options:2})}},t.SpaRouter=st,t.localisedRoute=ct,t.navigateTo=it,t.routeIsActive=lt,Object.defineProperty(t,"__esModule",{value:!0})}));