silkrouter
Version:
Silk router is an app routing library
2 lines (1 loc) • 3.23 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("rxjs")):"function"==typeof define&&define.amd?define(["exports","rxjs"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).silkrouter={},t.rxjs)}(this,(function(t,e){"use strict";function s(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var n,o;var r=s(o?n:(o=1,n=function(t){return"object"==typeof t&&null!==t}));const i=/:[^/]+/g,a=/^\/(?=[^?]*)/,c="vpushstate",h="?",u="",l="State",p=`push${l}`,f=`replace${l}`;function b(t,e,s){t.dispatchEvent(new CustomEvent(e,{bubbles:!0,cancelable:!0,detail:s||[]}))}function d(t){return function(t){return"string"==typeof t}(t)?t.trim():u}function g(t,e){const s=r(e)?e:{},[n,o]=t.split(h),{replace:i=!1,preventDefault:l=!1,queryString:g=o,data:y,pageTitle:x=null}=s,{preservePath:m,hashRouting:v,history:w,context:R}=this.config;let E=d(n);if(!function(t){return a.test(t)}(E))throw new TypeError("Route format is incorrect!");{const t=E;v&&(E=`${m?"":"/"}#${E}`),E=`${E}${d(g?`${h+g}`:u)}`;const e=w.state||{idx:0};w[i?f:p]({data:y,idx:e.idx+1},x,E),!l&&t&&b(R,c,[{path:t,hash:v},void 0,this])}}const y=(t,e)=>d(t?e.hash.substring(1).split(h)[0]:e.pathname);class x{constructor(t,e){const[s,n,o]=t,{location:r,history:i}=o.config;this.route=s.path,this.isHashRoute=s.hash,this.router=o,this.currentEvent=n||e,this.query={path:d(r.search.substring(1)),hash:d(r.hash.split(h)[1])};const{state:a}=n||{},{data:c,idx:u=0}=a||i.state||{};this.data=c,this.index=+u}}function m(t){let s=t;const{hashRouting:n,location:o,init:r}=this.config,i=y(n,o);return t=>new e.Observable((e=>{const o=t.subscribe(e);return s||(s=!0,r&&i&&e.next(new x([{path:i,hash:n},void 0,this]))),()=>{o.unsubscribe()}}))}function v(){return t=>new e.Observable((e=>{const s=t.subscribe({next:t=>{const[,,s]=t.detail;s===this&&e.next(new x(t.detail,t))},error:e.error,complete:e.complete});return()=>{s.unsubscribe()}}))}class w{static get global(){return"undefined"!=typeof globalThis?globalThis:global||self}constructor({history:t,context:s,location:n,hash:o}){if(!t[p])throw new Error("History is not supported in this environment!");this.popStateSubscription=e.fromEvent(w.global,"popstate").subscribe((t=>{const e=y(o,n);e&&b(s,c,[{path:e,hash:o},t,this])})),this.listeners=e.fromEvent(s,c).pipe(v.apply(this))}pipe(...t){return this.listeners.pipe(m.apply(this),...t)}subscribe(t){return this.pipe().subscribe(t)}destroy(t){"function"==typeof t&&t(),this.popStateSubscription.unsubscribe()}}t.Router=class extends w{constructor(t){t=r(t)?t:{};const{history:e,location:s,document:n}=w.global,o=n.body;super({history:e,location:s,context:o,hash:t.hashRouting}),this.config=Object.freeze({init:!0,hashRouting:!1,preservePath:!1,context:o,history:e,location:s,...t}),t.hashRouting&&!s.hash&&this.set("/",{replace:!0,preventDefault:!0})}set(t,e){g.apply(this,[t,e])}},t.RouterCore=w,t.resolveParams=function(t,e){const s={};if(i.test(t)){const n=new RegExp(t.replace(/\//g,"\\/").replace(/:[^/\\]+/g,"([^\\/]+)"));if(i.lastIndex=0,n.test(e)){const o=Array.from(t.match(i)).map((t=>t.replace(":",u))),r=Array.from(e.match(n));r.shift(),o.forEach(((t,e)=>{s[t]=r[e]}))}}return s}}));