omi-router
Version:
Router for Omi
2 lines (1 loc) • 6.96 kB
JavaScript
(function(w,v){typeof exports=="object"&&typeof module<"u"?v(exports,require("omi")):typeof define=="function"&&define.amd?define(["exports","omi"],v):(w=typeof globalThis<"u"?globalThis:w||self,v(w.index={},w.Omi))})(this,function(w,v){"use strict";function D(e){for(var t=[],r=0;r<e.length;){var i=e[r];if(i==="*"||i==="+"||i==="?"){t.push({type:"MODIFIER",index:r,value:e[r++]});continue}if(i==="\\"){t.push({type:"ESCAPED_CHAR",index:r++,value:e[r++]});continue}if(i==="{"){t.push({type:"OPEN",index:r,value:e[r++]});continue}if(i==="}"){t.push({type:"CLOSE",index:r,value:e[r++]});continue}if(i===":"){for(var c="",n=r+1;n<e.length;){var a=e.charCodeAt(n);if(a>=48&&a<=57||a>=65&&a<=90||a>=97&&a<=122||a===95){c+=e[n++];continue}break}if(!c)throw new TypeError("Missing parameter name at ".concat(r));t.push({type:"NAME",index:r,value:c}),r=n;continue}if(i==="("){var h=1,l="",n=r+1;if(e[n]==="?")throw new TypeError('Pattern cannot start with "?" at '.concat(n));for(;n<e.length;){if(e[n]==="\\"){l+=e[n++]+e[n++];continue}if(e[n]===")"){if(h--,h===0){n++;break}}else if(e[n]==="("&&(h++,e[n+1]!=="?"))throw new TypeError("Capturing groups are not allowed at ".concat(n));l+=e[n++]}if(h)throw new TypeError("Unbalanced pattern at ".concat(r));if(!l)throw new TypeError("Missing pattern at ".concat(r));t.push({type:"PATTERN",index:r,value:l}),r=n;continue}t.push({type:"CHAR",index:r,value:e[r++]})}return t.push({type:"END",index:r,value:""}),t}function N(e,t){t===void 0&&(t={});for(var r=D(e),i=t.prefixes,c=i===void 0?"./":i,n="[^".concat(R(t.delimiter||"/#?"),"]+?"),a=[],h=0,l=0,d="",o=function(f){if(l<r.length&&r[l].type===f)return r[l++].value},y=function(f){var m=o(f);if(m!==void 0)return m;var E=r[l],M=E.type,O=E.index;throw new TypeError("Unexpected ".concat(M," at ").concat(O,", expected ").concat(f))},C=function(){for(var f="",m;m=o("CHAR")||o("ESCAPED_CHAR");)f+=m;return f};l<r.length;){var b=o("CHAR"),T=o("NAME"),x=o("PATTERN");if(T||x){var p=b||"";c.indexOf(p)===-1&&(d+=p,p=""),d&&(a.push(d),d=""),a.push({name:T||h++,prefix:p,suffix:"",pattern:x||n,modifier:o("MODIFIER")||""});continue}var u=b||o("ESCAPED_CHAR");if(u){d+=u;continue}d&&(a.push(d),d="");var P=o("OPEN");if(P){var p=C(),A=o("NAME")||"",s=o("PATTERN")||"",g=C();y("CLOSE"),a.push({name:A||(s?h++:""),pattern:A&&!s?n:s,prefix:p,suffix:g,modifier:o("MODIFIER")||""});continue}y("END")}return a}function R(e){return e.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function H(e){return e&&e.sensitive?"":"i"}function I(e,t){if(!t)return e;for(var r=/\((?:\?<(.*?)>)?(?!\?)/g,i=0,c=r.exec(e.source);c;)t.push({name:c[1]||i++,prefix:"",suffix:"",modifier:"",pattern:""}),c=r.exec(e.source);return e}function q(e,t,r){var i=e.map(function(c){return S(c,t,r).source});return new RegExp("(?:".concat(i.join("|"),")"),H(r))}function $(e,t,r){return j(N(e,r),t,r)}function j(e,t,r){r===void 0&&(r={});for(var i=r.strict,c=i===void 0?!1:i,n=r.start,a=n===void 0?!0:n,h=r.end,l=h===void 0?!0:h,d=r.encode,o=d===void 0?function(O){return O}:d,y=r.delimiter,C=y===void 0?"/#?":y,b=r.endsWith,T=b===void 0?"":b,x="[".concat(R(T),"]|$"),p="[".concat(R(C),"]"),u=a?"^":"",P=0,A=e;P<A.length;P++){var s=A[P];if(typeof s=="string")u+=R(o(s));else{var g=R(o(s.prefix)),f=R(o(s.suffix));if(s.pattern)if(t&&t.push(s),g||f)if(s.modifier==="+"||s.modifier==="*"){var m=s.modifier==="*"?"?":"";u+="(?:".concat(g,"((?:").concat(s.pattern,")(?:").concat(f).concat(g,"(?:").concat(s.pattern,"))*)").concat(f,")").concat(m)}else u+="(?:".concat(g,"(").concat(s.pattern,")").concat(f,")").concat(s.modifier);else s.modifier==="+"||s.modifier==="*"?u+="((?:".concat(s.pattern,")").concat(s.modifier,")"):u+="(".concat(s.pattern,")").concat(s.modifier);else u+="(?:".concat(g).concat(f,")").concat(s.modifier)}}if(l)c||(u+="".concat(p,"?")),u+=r.endsWith?"(?=".concat(x,")"):"$";else{var E=e[e.length-1],M=typeof E=="string"?p.indexOf(E[E.length-1])>-1:E===void 0;c||(u+="(?:".concat(p,"(?=").concat(x,"))?")),M||(u+="(?=".concat(p,"|").concat(x,")"))}return new RegExp(u,H(r))}function S(e,t,r){return e instanceof RegExp?I(e,t):Array.isArray(e)?q(e,t,r):$(e,t,r)}var W=Object.defineProperty,F=Object.getOwnPropertyDescriptor,L=(e,t,r,i)=>{for(var c=i>1?void 0:i?F(t,r):t,n=e.length-1,a;n>=0;n--)(a=e[n])&&(c=(i?a(t,r,c):a(c))||c);return i&&c&&W(t,r,c),c};v.mixin({router:null});let _=class extends v.Component{constructor(){super(...arguments),this.currentRoute=null,this.routes=[],this.isHashMode=!0,this.params={},this.query={},this.hash="",this.base=""}install(){v.mixin({router:this}),this.base=this.props.base||"",this.isHashMode=this.props.hash!==!1,this.routes=this.props.routes.map(e=>{const t=[];if(e.render&&(e.render=e.render.bind(this)),e.path==="*")return{...e,regex:/(.*)/};const r=S(e.path,t);return{...e,keys:t,regex:r}}),window.onpopstate=e=>{this.matchAndRender(this.getRoutePath())},this.matchAndRender(this.getRoutePath())}getRoutePath(){return this.isHashMode?window.location.hash.split("?")[0].replace("#","")||"/":window.location.pathname}getQueryPath(){return this.isHashMode?window.location.hash.split("?")[1]:window.location.search}beforeEach(e){this.beforeEachCallback=e}afterEach(e){this.afterEachCallback=e}push(e){history.pushState({},"",this.isHashMode?`#${e}`:e),this.matchAndRender(e)}replace(e){history.replaceState({},"",e),this.matchAndRender(e)}go(e){history.go(e)}back(){history.back()}forward(){history.forward()}matchAndRender(e){var t,r;for(const i of this.routes){const c=(t=i.regex)==null?void 0:t.exec(e);if(c){if(i.redirect){if(this.isHashMode)window.location.hash=i.redirect;else{const a=this.base+i.redirect;window.location.href=window.location.origin+a}return}if(this.beforeEachCallback){const a=this.beforeEachCallback({path:e},{path:window.location.pathname});if(a===!1)return;if(typeof a=="string"){this.push(a);return}else if(typeof a=="object"){this.push(a.path);return}}if(i.beforeEnter&&i.beforeEnter({path:e},{path:window.location.pathname})===!1)return;this.currentRoute=i,this.params={},(r=i.keys)==null||r.forEach((a,h)=>{this.params[a.name]=c[h+1]});const n=new URLSearchParams(this.getQueryPath());this.query={},Array.from(n.entries()).forEach(([a,h])=>{this.query[a]=h}),this.hash=window.location.hash,this.afterEachCallback&&this.afterEachCallback({path:e},{path:window.location.pathname}),this.update();break}}}render(){return this.currentRoute&&this.currentRoute.render?this.currentRoute.render(this):null}};_=L([v.tag("router-view")],_);class U{constructor(t){this.params={},this.query={},this.hash="",this.currentRoute=null,this.el=v.render(v.h("router-view",{onInstall:r=>{r.detail.constructor.css=t.css},routes:t.routes,base:t.base,hash:t.hash}),t.renderTo)}beforeEach(t){this.el.beforeEach(t)}afterEach(t){this.el.afterEach(t)}push(t){this.el.push(t)}replace(t){this.el.replace(t)}go(t){this.el.go(t)}back(){this.el.back()}forward(){this.el.forward()}}w.Router=U,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});