UNPKG

svelte-pathfinder

Version:

Tiny, state-based, advanced router for SvelteJS.

3 lines (2 loc) 13.8 kB
var pathfinder=function(e){"use strict";function t(){}function n(e){return e()}function o(e){e.forEach(n)}function r(e,...n){if(null==e){for(const e of n)e(void 0);return t}const o=e.subscribe(...n);return o.unsubscribe?()=>o.unsubscribe():o}function i(e){let t;return r(e,(e=>t=e))(),t}const c=[],s=[];let u=[];const l=[],a=Promise.resolve();let f=!1;function d(){return f||(f=!0,a.then(b)),a}function h(e){u.push(e)}const g=new Set;let p=0;function b(){if(0===p){do{try{for(;p<c.length;){const e=c[p];p++,m(e.$$)}}catch(e){throw c.length=0,p=0,e}for(c.length=0,p=0;s.length;)s.pop()();for(let e=0;e<u.length;e+=1){const t=u[e];g.has(t)||(g.add(t),t())}u.length=0}while(c.length);for(;l.length;)l.pop()();f=!1,g.clear()}}function m(e){if(null!==e.fragment){e.update(),o(e.before_update);const t=e.dirty;e.dirty=[-1],e.fragment&&e.fragment.p(e.ctx,t),e.after_update.forEach(h)}}const y=[];function v(e,n=t){let o;const r=new Set;function i(t){if(i=t,((n=e)!=n?i==i:n!==i||n&&"object"==typeof n||"function"==typeof n)&&(e=t,o)){const t=!y.length;for(const t of r)t[1](),y.push(t,e);if(t){for(let e=0;e<y.length;e+=2)y[e][0](y[e+1]);y.length=0}}var n,i}function c(t){i(t(e))}return{set:i,update:c,subscribe:function(s,u=t){const l=[s,u];return r.add(l),1===r.size&&(o=n(i,c)||t),s(e),()=>{r.delete(l),0===r.size&&o&&(o(),o=null)}}}}function w(e,n,i){const c=!Array.isArray(e),s=c?[e]:e;if(!s.every(Boolean))throw new Error("derived() expects stores as input, got a falsy value");const u=n.length<2;return l=(e,i)=>{let l=!1;const a=[];let f=0,d=t;const h=()=>{if(f)return;d();const o=n(c?a[0]:a,e,i);u?e(o):d="function"==typeof o?o:t},g=s.map(((e,t)=>r(e,(e=>{a[t]=e,f&=~(1<<t),l&&h()}),(()=>{f|=1<<t}))));return l=!0,h(),function(){o(g),d(),l=!1}},{subscribe:v(i,l).subscribe};var l}const O=/^((mailto:)|(tel:)|(sms:)|(data:)|(blob:)|(javascript:)|(ftp(s?):\/\/)|(file:\/\/))/,A="undefined"!=typeof location,$="undefined"!=typeof process,E="undefined"!=typeof history,j=E&&V(history.pushState),x="undefined"!=typeof window,S=x&&window!==window.parent,R=A&&("file:"===location.protocol||/[-_\w]+[.][\w]+$/i.test(location.pathname)),k=x&&E&&A&&!S,I=!j||R,L="#!",N={array:{separator:",",format:"bracket"},convertTypes:!0,breakHooks:!0,hashbang:!1,anchor:!1,scroll:!1,focus:!1,nesting:3,sideEffect:k,base:""};function C(){if(!A)return{};if(N.hashbang||I){const e=location.hash;return new URL(0===e.indexOf(L)?e.substring(2):e.substring(1),"file:")}return location}function P(){return N.base?N.base:A&&(N.hashbang||I)?location.pathname:"/"}function T(e){(N.hashbang||I)&&(e=L+e);const t=P();return("/"===t[t.length-1]?t.substring(0,t.length-1):t)+e}function U(e){e=q(e,C().origin);return e=q(e,P()),(N.hashbang||I)&&(e=q(e,L)),H(e)}function _(e){const t=e.tagName.toLowerCase(),n=e.type&&e.type.toLowerCase();return"button"===t||"input"===t&&["button","submit","image"].includes(n)}function z(e){const t=q(D(arguments.length>1&&void 0!==arguments[1]?arguments[1]:""),"#");if(e&&N.scroll){const t=M(N.scroll)?{...N.scroll,...e}:e,{top:n=0,left:o=0}=e,{scrollHeight:r,scrollWidth:i}=document.documentElement;if(n<=r&&o<=i)return Z(t);const c=function(e,t){let n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:5e3;const o=new ResizeObserver(e);o.observe(t);const r=()=>o.unobserve(t);return setTimeout(r,n),r}((e=>{if(!e[0])return c();(!n||e[0].contentRect.height>=n)&&(!o||e[0].contentRect.width>=o)&&(c(),Z(t))}),document.documentElement)}else if(t&&N.anchor){const e=M(N.anchor)?N.anchor:{},n=document.getElementById(t);if(n)return Z(e,n);const o=X((()=>{const n=document.getElementById(t);n&&(o(),Z(e,n))}))}else N.scroll&&Z()}function B(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},{encode:t=encodeURIComponent}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return Object.keys(e).reduce(((n,o)=>{if(Object.prototype.hasOwnProperty.call(e,o)&&isNaN(parseInt(o,10)))if(Array.isArray(e[o]))"separator"===N.array.format?n.push(`${o}=${e[o].join(N.array.separator)}`):e[o].forEach((e=>n.push(`${o}[]=${t(e)}`)));else if(M(e[o])){let t=F(o,e[o]);n.push(G(t))}else n.push(`${o}=${t(e[o])}`);return n}),[]).join("&")}function K(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"*",{loose:n=!1,sensitive:o=!1,blank:r=!1,decode:i=decodeURIComponent}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const c={},s=t instanceof RegExp?t:t.split("/").reduce(((e,t,o,r)=>{let{length:i}=r;if(t){const n=t[0];if("*"===n)c.wild=void 0,e+="/(?<wild>.*)";else if(":"===n){const n=t.indexOf("?",1),o=t.indexOf(".",1),r=!!~n,i=!!~o,s=t.substring(1,r?n:i?o:t.length);c[s]=void 0,e+=r&&!i?`(?:/(?<${s}>[^/]+?))?`:`/(?<${s}>[^/]+?)`,i&&(e+=`${r?"?":""}\\${t.substring(o)}`)}else e+=`/${t}`}return o===i-1&&(e+=n?"(?:$|/)":"/?$"),e}),"^"),u=new RegExp(s,o?"":"i").exec(e);return u?Object.entries(u.groups||{}).reduce(((e,t)=>{let[n,o]=t;const r=i(o);return e[n]=N.convertTypes?Y(r):r,e}),{}):r?c:null}function D(e){let{decode:t=decodeURIComponent}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return t(e)}function H(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"/";return!(e+="")&&(arguments.length>2&&void 0!==arguments[2]&&arguments[2])?e:0!==e.indexOf(t)?t+e:e}function q(e,t){return 0===(e+"").indexOf(t)?e.substring(t.length):e}function M(e){return!Array.isArray(e)&&"object"==typeof e&&null!==e}function V(e){return"function"==typeof e}function W(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return window.addEventListener(...t),()=>window.removeEventListener(...t)}function Z(){let{top:e=0,left:t=0,...n}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},o=arguments.length>1?arguments[1]:void 0;o?document.documentElement.scrollIntoView?o.scrollIntoView({behavior:"smooth",...n}):window.scrollTo({top:o.offsetTop-e,behavior:"smooth",...n}):window.scrollTo({top:e,left:t,behavior:"smooth",...n})}function X(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:5e3;const n=new MutationObserver(e);n.observe(document.body,{childList:!0,subtree:!0});const o=()=>n.disconnect();return setTimeout(o,t),o}function Y(e){if(Array.isArray(e))return e[e.length-1]=Y(e[e.length-1]),e;if("object"==typeof e)return Object.entries(e).reduce(((e,t)=>{let[n,o]=t;return e[n]=Y(o),e}),{});if("true"===e||"false"===e)return"true"===e;if("null"===e)return null;if("undefined"!==e){if(""!==e&&!isNaN(Number(e))&&Number(e).toString()===e)return Number(e);if("separator"===N.array.format&&"string"==typeof e){const t=e.split(N.array.separator);return t.length>1?t:e}return e}}function F(e,t){const n=/(\[[^[\]]*])/g;let o=/(\[[^[\]]*])/.exec(e),r=o?e.slice(0,o.index):e,i=[];r&&i.push(r);let c=0;for(;(o=n.exec(e))&&c<N.nesting;)c++,i.push(o[1]);return o&&i.push(`[${e.slice(o.index)}]`),function(e,t){let n=t;for(let t=e.length-1;t>=0;--t){let o,r=e[t];if("[]"===r)o=[].concat(n);else{o={};const e="["===r.charAt(0)&&"]"===r.charAt(r.length-1)?r.slice(1,-1):r,t=parseInt(e,10);!isNaN(t)&&r!==e&&String(t)===e&&t>=0?(o=[],o[t]=N.convertTypes?Y(n):n):o[e]=n}n=o}return n}(i,t)}function G(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";return Object.entries(e).map((e=>{let[n,o]=e;return"object"==typeof o?G(o,t?`${t}[${n}]`:n):`${t}[${n}]=${o}`})).join("&")}const J=te((function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return"string"==typeof e&&(e=q(e,"/").split("/")),Object.prototype.hasOwnProperty.call(e,"toString")?e:Object.defineProperty(e,"toString",{value(){return H(this.join("/"))},configurable:!1,writable:!1})})),Q=te((function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";return"string"==typeof e&&(e=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",{decode:t=decodeURIComponent}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return e?e.replace("?","").replace(/\+/g," ").split("&").filter(Boolean).reduce(((e,n)=>{let[o,r]=n.split(/=(.*)/,2);o=t(o||""),r=t(r||"");let i=F(o,r);return e=Object.keys(i).reduce(((e,t)=>{const n=N.convertTypes?Y(i[t]):i[t];return e[t]?Array.isArray(e[t])?e[t]=e[t].concat(n):Object.assign(e[t],n):e[t]=n,e}),e),e}),{}):{}}(e)),Object.prototype.hasOwnProperty.call(e,"toString")?e:Object.defineProperty(e,"toString",{value(){return H(B(this),"?",!0)},configurable:!1,writable:!1})})),ee=te((function(){return H(D(arguments.length>0&&void 0!==arguments[0]?arguments[0]:""),"#",!0)}));function te(e){return(t,n)=>{let o=t&&t.toString();!Array.isArray(n)&&(n=[n]);const r=new Set(n),c=function(e){return function(){for(var t=arguments.length,n=new Array(t),o=0;o<t;o++)n[o]=arguments[o];const r=[...e];return!(N.breakHooks?r.some((e=>!1===e(...n))):r.reduce(((e,t)=>!1===t(...n)||e),!1))}}(r),{subscribe:s,set:u}=v(t=e(t),(()=>()=>r.clear()));function l(n){(n=e(n)).toString()!==o&&!1!==c(n,t,e.name)&&(o=n.toString(),u(t=n))}return c(null,t,e.name),{subscribe:s,update(e){l(e(i(this)))},set(e){l(e)},hook:n=>(V(n)&&(r.add(n),n(null,t,e.name)),()=>r.delete(n))}}}const ne=function(){const e=C().pathname;if(!e)return;return H(q(e,P()))}(),{search:oe,hash:re}=C();let ie=!0,ce=!1,se=!1,ue=0;const le=J(ne,pe),ae=Q(oe,pe),fe=ee(re,pe),de=v({}),he=w([le,ae,fe],((e,t)=>{let[n,o,r]=e,i=!1;return d().then((()=>{i||t(n+o+r)})),()=>i=!0}),ne+oe+re),ge=w(le,(e=>K.bind(null,e.toString())));function pe(){(N.scroll||N.focus)&&de.update((function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return N.scroll&&(e._scroll={top:window.pageYOffset,left:window.pageXOffset}),N.focus&&(e._focus=document.activeElement.id),e}))}function be(e,t){const n=e.indexOf("#")>=0?e.slice(e.indexOf("#")):"",o=document.activeElement;!M(t)&&(t={}),d().then((()=>function(e,t){function n(){return e?document.getElementById(e):document.activeElement!==t&&document.activeElement!==document.body?document.activeElement:document.querySelector("[autofocus]")}N.focus&&setTimeout((()=>{const e=n();if(e)return e.focus();const t=X((()=>{const e=n();e&&(t(),e.focus())})),o=document.body,r=o.getAttribute("tabindex");o.tabIndex=-1,o.focus({preventScroll:!0}),null!==r?o.setAttribute("tabindex",r):o.removeAttribute("tabindex"),getSelection().removeAllRanges()}))}(t._focus,o))).then((()=>z(t._scroll,n)))}if(k||S){const e=new Set;e.add(he.subscribe((e=>{ie||ce||!N.sideEffect||(j?history[se?"replaceState":"pushState"]({},null,T(e)):location.hash=T(e)),!ce&&be(e),!se&&ue++,ie=se=ce=!1}))),j?(e.add(de.subscribe((e=>{!ie&&N.sideEffect&&history.replaceState(e,null,location.pathname+location.search+location.hash)}))),e.add(W("popstate",(e=>{ce=!0,me(location.href,e.state),be(U(location.href),e.state)})))):e.add(W("hashchange",(()=>{if(ce=!0,!N.hashbang&&!I)return fe.set(location.hash);me(location.hash),be(U(location.hash))}))),e.add(W("beforeunload",(()=>{e.forEach((e=>e())),e.clear()}),!0))}function me(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const{pathname:n,search:o,hash:r}=e instanceof URL?e:new URL(U(e),"file:");le.set(n),ae.set(o),fe.set(r),d().then((()=>de.set(t||{})))}const ye=function(e){return function(t){let n,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(t instanceof RegExp)throw new Error("Paramable does not support RegExp patterns.");t=t.replace(/\/$/,"");const{subscribe:r}=v({},(r=>e.subscribe((e=>{var i;n=K(e.toString(),t,{blank:!0,...o}),r("object"!=typeof(i=n)||null===i?i:Object.create(Object.getPrototypeOf(i),Object.getOwnPropertyDescriptors(i)))}))));function c(){let r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};Object.entries(n).some((e=>{let[t,n]=e;return n!==r[t]}))&&e.update((e=>{const n=o.loose?H(e.slice(t.split("/").length-1).join("/")):"";return function(e,t){let{encode:n=encodeURIComponent}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return e.replace(/(\/|^)([:*][^/]*?)(\?)?(?=[/.]|$)/g,((e,o,r)=>(e=t["*"===r?"wild":r.substring(1)])?`/${n(e)}`:""))}(t+n,r)}))}return{get(){return i(this)},update(e){c(e(this.get()))},subscribe:r,set:c}}}(le);return e.back=function(e){ue>0&&k&&N.sideEffect?(history.back(),ue--):d().then((()=>me(e)))},e.click=function(e){if(!e.target||e.ctrlKey||e.metaKey||e.altKey||e.shiftKey||e.button||1!==e.which||e.defaultPrevented)return;const t=function(e,t){for(;e&&e.nodeName.toLowerCase()!==t;)e=e.parentNode;return e&&e.nodeName.toLowerCase()===t?e:null}(e.target,"a");if(!t||t.target||t.hasAttribute("download")||t.hasAttribute("rel")&&t.getAttribute("rel").includes("external"))return;const n=t.getAttribute("href"),o=t.href;!n||0!==o.indexOf(location.origin)||O.test(n)||!N.hashbang&&!I&&n.startsWith("#")||(e.preventDefault(),me(o,Object.assign({},t.dataset)))},e.fragment=fe,e.goto=me,e.paramable=ye,e.path=le,e.pattern=ge,e.prefs=N,e.query=ae,e.redirect=function(e,t){d().then((()=>{se=!0,me(e,t)}))},e.state=de,e.submit=function(e){if(!e.target||e.defaultPrevented)return;const t=e.target,n=e.submitter||_(document.activeElement)&&document.activeElement;let o=t.action,r=t.method,i=t.target;if(n&&(n.hasAttribute("formaction")&&(o=n.formAction),n.hasAttribute("formmethod")&&(r=n.formMethod),n.hasAttribute("formtarget")&&(i=n.formTarget)),r&&"get"!==r.toLowerCase())return;if(i&&"_self"!==i.toLowerCase())return;const{pathname:c,hash:s}=new URL(o),u=[],l={},a=t.elements,f=a.length;for(let e=0;e<f;e++){const t=a[e];t.name&&!t.disabled&&(["checkbox","radio"].includes(t.type)&&!t.checked||_(t)&&t!==n||("hidden"!==t.type?u.push(`${t.name}=${t.value}`):l[t.name]=t.value))}let d=H(`${c}?${u.join("&")}${s}`);$&&d.match(/^\/[a-zA-Z]:\//)&&(d=d.replace(/^\/[a-zA-Z]:\//,"/")),e.preventDefault(),me(d,l)},e.url=he,e}({}); //# sourceMappingURL=pathfinder.min.js.map