UNPKG

vue-router

Version:

## Status: Alpha

8 lines (7 loc) 18.2 kB
/*! * vue-router v4.0.0-alpha.4 * (c) 2020 Eduardo San Martin Morote * @license MIT */ var VueRouter=function(e,t){"use strict";var n,r;!function(e){e.pop="pop",e.push="push"}(n||(n={})),function(e){e.back="back",e.forward="forward",e.unknown=""}(r||(r={}));const o={fullPath:""};function a(e,t){let n=t.query?e(t.query):"";return t.path+(n&&"?")+n+(t.hash||"")}function c(e,t){return t&&0===e.indexOf(t)?e.replace(t,"")||"/":e}function i(e){return{fullPath:e.fullPath||e}}function s(e){return e?{x:e.scrollLeft,y:e.scrollTop}:{x:window.pageXOffset,y:window.pageYOffset}}const u=/^#\d/;function l(e){let t=null;if("selector"in e){const n=u.test(e.selector)?document.getElementById(e.selector.slice(1)):document.querySelector(e.selector);if(n){t=function(e,t){const n=document.documentElement.getBoundingClientRect(),r=e.getBoundingClientRect();return{x:r.left-n.left-t.x,y:r.top-n.top-t.y}}(n,e.offset||{x:0,y:0})}}else t={x:e.x,y:e.y};t&&window.scrollTo(t.x,t.y)}function f(e,t){const{pathname:n,search:r,hash:o}=t;if(e.indexOf("#")>-1){let e=o.slice(1);return"/"!==e.charAt(0)&&(e="/"+e),i(c(e,""))}return i(c(n,e)+r+o)}function p(e,t,n,r=!1,o=!1){return{back:e,current:t,forward:n,replaced:r,position:window.history.length,scroll:o?s():null}}function d(e=""){const o=function(e){const{history:n}=window;let r={value:f(e,window.location)},o={value:n.state};function a(r,a,c){const i=e+r.fullPath;try{n[c?"replaceState":"pushState"](a,"",i),o.value=a}catch(e){t.warn("[vue-router]: Error with push/replace State",e),window.location[c?"replace":"assign"](i)}}return o.value||a(r.value,{back:null,current:r.value,forward:null,position:n.length-1,replaced:!0,scroll:s()},!0),{location:r,state:o,push:function(e,t){const o=i(e),c={...n.state,forward:o,scroll:s()};a(c.current,c,!0),a(o,{...p(r.value,o,null),position:c.position+1,...t},!1),r.value=o},replace:function(e,t){const c=i(e);a(c,{...p(o.value.back,c,o.value.forward,!0),...n.state,...t,position:o.value.position},!0),r.value=c}}}(e),a=function(e,t,o,a){let c=[],i=[],u=null;const l=({state:i})=>{const s=f(e,window.location);if(!i)return a(s.fullPath);const l=o.value,p=t.value;if(o.value=s,t.value=i,u&&u.fullPath===l.fullPath)return void(u=null);const d=(p?i.position-p.position:"")||0;c.forEach(e=>{e(o.value,l,{distance:d,type:n.pop,direction:d?d>0?r.forward:r.back:r.unknown})})};function p(){const{history:e}=window;e.state&&e.replaceState({...e.state,scroll:s()},"")}return window.addEventListener("popstate",l),window.addEventListener("beforeunload",p),{pauseListeners:function(){u=o.value},listen:function(e){c.push(e);const t=()=>{const t=c.indexOf(e);t>-1&&c.splice(t,1)};return i.push(t),t},destroy:function(){for(const e of i)e();i=[],window.removeEventListener("popstate",l),window.removeEventListener("beforeunload",p)}}}(e,o.state,o.location,o.replace);function c(e,t=!0){t||a.pauseListeners(),history.go(e)}const u={location:o.location.value,base:e,back:function(e=!0){c(-1,e)},forward:function(e=!0){c(1,e)},go:c,...o,...a};return Object.defineProperty(u,"location",{get:()=>o.location.value}),Object.defineProperty(u,"state",{get:()=>o.state.value}),u}const h="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag,m=e=>h?Symbol(e):"_vr_"+e,g=m("rvlm"),v=m("rvd"),y=m("r"),w=m("rl"),b=/#/g,R=/&/g,E=/\//g,x=/=/g,O=/\?/g,k=/%5B/g,A=/%5D/g,P=/%5E/g,$=/%60/g,j=/%7B/g,L=/%7C/g,S=/%7D/g;function T(e){return encodeURI(""+e).replace(L,"|").replace(k,"[").replace(A,"]")}function C(e){return T(e).replace(b,"%23").replace(R,"%26").replace(x,"%3D").replace($,"`").replace(j,"{").replace(S,"}").replace(P,"^")}function M(e){return function(e){return T(e).replace(b,"%23").replace(O,"%3F")}(e).replace(E,"%2F")}function q(e){try{return decodeURIComponent(e)}catch(e){}return e}function I(e){const t={};if(""===e||"?"===e)return t;const n=("?"===e[0]?e.slice(1):e).split("&");for(let e=0;e<n.length;++e){let[r,o]=n[e].split("=");r=q(r);let a=null==o?null:q(o);if(r in t){let e=t[r];Array.isArray(e)||(e=t[r]=[e]),e.push(a)}else t[r]=a}return t}function B(e){let t="";for(let n in e){t.length&&(t+="&");const r=e[n];if(n=C(n),null==r){void 0!==r&&(t+=n);continue}let o=Array.isArray(r)?r.map(e=>e&&C(e)):[r&&C(r)];for(let e=0;e<o.length;e++)t+=(e?"&":"")+n,null!=o[e]&&(t+="="+o[e])}return t}function V(e){const t={};for(let n in e){let r=e[n];void 0!==r&&(t[n]=Array.isArray(r)?r.map(e=>null==e?null:""+e):null==r?r:""+r)}return t}const _=t.markNonReactive({path:"/",name:void 0,params:{},query:{},hash:"",fullPath:"/",matched:[],meta:{},redirectedFrom:void 0});function D(e,t){return Object.assign(new Error,{type:e},t)}const F={sensitive:!1,strict:!1,start:!0,end:!0},H=/[.+*?^${}()[\]/\\]/g;function U(e,t){let n=0;for(;n<e.length&&n<t.length;){const r=t[n]-e[n];if(r)return r;n++}return e.length<t.length?1===e.length&&80===e[0]?-1:1:e.length>t.length?1===t.length&&80===t[0]?1:-1:0}function G(e,t){let n=0;const r=e.score,o=t.score;for(;n<r.length&&n<o.length;){const e=U(r[n],o[n]);if(e)return e;n++}return o.length-r.length}const K={type:0,value:""},Q=/[a-zA-Z0-9_]/;function N(e,t,n){const r={...function(e,t){const n={...F,...t};let r=[],o=n.start?"^":"";const a=[];for(const t of e){const e=t.length?[]:[90];n.strict&&!t.length&&(o+="/");for(let r=0;r<t.length;r++){const c=t[r];let i=40+(n.sensitive?.25:0);if(0===c.type)r||(o+="/"),o+=c.value.replace(H,"\\$&"),i+=40;else if(1===c.type){const{value:e,repeatable:t,optional:n,regexp:s}=c;a.push({name:e,repeatable:t,optional:n});const u=s||"[^/]+?";if("[^/]+?"!==u){i+=10;try{new RegExp(`(${u})`)}catch(t){throw new Error(`Invalid custom RegExp for param "${e}" (${u}): `+t.message)}}let l=t?`((?:${u})(?:/(?:${u}))*)`:`(${u})`;r||(l=n?`(?:/${l})`:"/"+l),n&&(l+="?"),o+=l,i+=20,n&&(i+=-8),t&&(i+=-20),".*"===u&&(i+=-50)}e.push(i)}r.push(e)}if(n.strict&&n.end){const e=r.length-1;r[e][r[e].length-1]+=.7000000000000001}n.strict||(o+="/?"),n.end?o+="$":n.strict&&(o+="(?:/|$)");const c=new RegExp(o,n.sensitive?"":"i");return{re:c,score:r,keys:a,parse:function(e){const t=e.match(c),n={};if(!t)return null;for(let e=1;e<t.length;e++){const r=t[e]||"",o=a[e-1];n[o.name]=r&&o.repeatable?r.split("/"):r}return n},stringify:function(t){let n="",r=!1;for(const o of e){r&&"/"===n[n.length-1]||(n+="/"),r=!1;for(const e of o)if(0===e.type)n+=e.value;else if(1===e.type){const{value:o,repeatable:a,optional:c}=e,i=o in t?t[o]:"";if(Array.isArray(i)&&!a)throw new Error(`Provided param "${o}" is an array but it is not repeatable (* or + modifiers)`);const s=Array.isArray(i)?i.join("/"):i;if(!s){if(!c)throw new Error(`Missing required param "${o}"`);r=!0}n+=s}}return n}}}(function(e){if(!e)return[[]];if("/"===e)return[[K]];if("/"!==e[0])throw new Error('A non-empty path must start with "/"');function t(e){throw new Error(`ERR (${n})/"${u}": ${e}`)}let n=0,r=n;const o=[];let a;function c(){a&&o.push(a),a=[]}let i,s=0,u="",l="";function f(){u&&(0===n?a.push({type:0,value:u}):1===n||2===n||3===n?(a.length>1&&("*"===i||"+"===i)&&t(`A repeatable param (${u}) must be alone in its segment. eg: '/:ids+.`),a.push({type:1,value:u,regexp:l,repeatable:"*"===i||"+"===i,optional:"*"===i||"?"===i})):t("Invalid state to consume buffer"),u="")}function p(){u+=i}for(;s<e.length;)if(i=e[s++],"\\"!==i||2===n)switch(n){case 0:"/"===i?(u&&f(),c()):":"===i?(f(),n=1):p();break;case 4:p(),n=r;break;case 1:"("===i?(n=2,l=""):Q.test(i)?p():(f(),n=0,"*"!==i&&"?"!==i&&"+"!==i&&s--);break;case 2:")"===i?"\\"==l[l.length-1]?l=l.slice(0,-1)+i:n=3:l+=i;break;case 3:f(),n=0,"*"!==i&&"?"!==i&&"+"!==i&&s--;break;default:t("Unknown state")}else r=n,n=4;return 2===n&&t(`Unfinished custom RegExp for param "${u}"`),f(),c(),o}(e.path),n),record:e,parent:t,children:[],alias:[]};return t&&!r.record.aliasOf==!t.record.aliasOf&&t.children.push(r),r}let W=()=>{};function z(e,t){const n=[],r=new Map;function o(e,n,r){let i=function(e){let t,n;if("redirect"in e){t={};let{redirect:r}=e;n=(e,t,n)=>{n("function"==typeof r?r(e):r)}}else t="components"in e?e.components:{default:e.component},n=e.beforeEnter;return{path:e.path,components:t,children:e.children||[],name:e.name,beforeEnter:n,props:e.props||!1,meta:e.meta||{},leaveGuards:[],instances:{},aliasOf:void 0}}(e);i.aliasOf=r&&r.record;const s={...t,...e.options},u=[i];if("alias"in e){const t="string"==typeof e.alias?[e.alias]:e.alias;for(const e of t)u.push({...i,components:r?r.record.components:i.components,path:e,aliasOf:r?r.record:i})}let l,f;for(const e of u){let{path:t}=e;if(n&&"/"!==t[0]){let r=n.record.path,o="/"===r[r.length-1]?"":"/";e.path=n.record.path+(t&&o+t)}l=N(e,n,s),r?r.alias.push(l):(f=f||l,f!==l&&f.alias.push(l));let a=i.children;for(let e=0;e<a.length;e++)o(a[e],l,r&&r.children[e]);r=r||l,c(l)}return f?()=>{a(f)}:W}function a(e){if("string"==typeof e){const t=r.get(e);t&&(r.delete(e),n.splice(n.indexOf(t),1),t.children.forEach(a),t.alias.forEach(a))}else{let t=n.indexOf(e);t>-1&&(n.splice(t,1),e.record.name&&r.delete(e.record.name),e.children.forEach(a),e.alias.forEach(a))}}function c(e){let t=0;for(;t<n.length&&G(e,n[t])>=0;)t++;n.splice(t,0,e),e.record.name&&!function(e){for(;e;){if(e.record.aliasOf)return!0;e=e.parent}return!1}(e)&&r.set(e.record.name,e)}return e.forEach(e=>o(e)),{addRoute:o,resolve:function(e,t){let o,a,c,i={};if("name"in e&&e.name){if(o=r.get(e.name),!o)throw D(0,{location:e});c=o.record.name,i=e.params||t.params,a=o.stringify(i)}else if("path"in e)o=n.find(t=>t.re.test(e.path)),a=e.path,o&&(i=o.parse(e.path),c=o.record.name);else{if(o=t.name?r.get(t.name):n.find(e=>e.re.test(t.path)),!o)throw D(0,{location:e,currentLocation:t});c=o.record.name,i=e.params||t.params,a=o.stringify(i)}const s=[];let u=o;for(;u;)s.unshift(u.record),u=u.parent;return{name:c,path:a,params:i,matched:s,meta:o?o.record.meta:{}}},removeRoute:a,getRoutes:function(){return n},getRecordMatcher:function(e){return r.get(e)}}}function X(e,t,n,r){return()=>new Promise((o,a)=>{e.call(r,t,n,e=>{var r;!1===e?a(D(2,{from:n,to:t})):"string"==typeof(r=e)||r&&"object"==typeof r?a(D(1,{from:t,to:e})):o()})})}function Y(e,t,n,r){const o=[];for(const a of e)for(const e in a.components){const c=a.components[e];if("function"==typeof c){const i=c().catch(()=>null);o.push(async()=>{const o=await i;if(!o)throw new Error("TODO: error while fetching");const c=(s=o).__esModule||h&&"Module"===s[Symbol.toStringTag]?o.default:o;var s;a.components[e]=c;const u=c[t];return u&&X(u,n,r,a.instances[e])()})}else{const i=c[t];i&&o.push(X(i,n,r,a.instances[e]))}}return o}function Z(e,t){const n={};for(const r in t){const o=t[r];n[r]=Array.isArray(o)?o.map(e):e(o)}return n}function J(e,t){return(e.aliasOf||e)===(t.aliasOf||t)}function ee(e,t){const n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;let o,a=0;for(;a<n.length;){if(o=n[a],o!==r[a])return!1;if(!te(e[o],t[o]))return!1;a++}return!0}function te(e,t){return typeof e==typeof t&&(Array.isArray(e)?e.length===t.length&&e.every((e,n)=>e===t[n]):e===t)}function ne(){let e=[];return{add:function(t){return e.push(t),()=>{const n=e.indexOf(t);n>-1&&e.splice(n,1)}},list:()=>e,reset:function(){e=[]}}}function re(e){const n=t.inject(y),r=n.currentRoute,o=t.computed(()=>n.resolve(t.unref(e.to))),a=t.computed(()=>n.createHref(o.value)),c=t.computed(()=>{const e=o.value.matched[o.value.matched.length-1];return e?r.value.matched.findIndex(J.bind(null,e)):-1}),i=t.computed(()=>c.value>-1&&function(e,t){for(let n in t){let r=t[n],o=e[n];if("string"==typeof r){if(r!==o)return!1}else if(!Array.isArray(o)||o.length!==r.length||r.some((e,t)=>e!==o[t]))return!1}return!0}(r.value.params,o.value.params)),s=t.computed(()=>c.value===r.value.matched.length-1&&ee(r.value.params,o.value.params));return{route:o,href:a,isActive:i,isExactActive:s,navigate:function(e={}){(function(e){if(e.metaKey||e.altKey||e.ctrlKey||e.shiftKey)return;if(e.defaultPrevented)return;if(void 0!==e.button&&0!==e.button)return;if(e.currentTarget&&e.currentTarget.getAttribute){const t=e.currentTarget.getAttribute("target");if(/\b_blank\b/i.test(t))return}e.preventDefault&&e.preventDefault();return!0})(e)&&n.push(o.value)}}}const oe=t.defineComponent({name:"RouterLink",props:{to:{type:[String,Object],required:!0}},setup(e,{slots:n,attrs:r}){const o=t.reactive(re(e)),a=t.computed(()=>({"router-link-active":o.isActive,"router-link-exact-active":o.isExactActive}));return()=>t.h("a",{"aria-current":o.isExactActive?"page":null,onClick:o.navigate,href:o.href,...r,class:a.value},n.default&&n.default(o))}});function ae(e){const n=t.inject(v,0);t.provide(v,n+1);const r=t.computed(()=>t.unref(e.route).matched[n]),o=t.computed(()=>r.value&&r.value.components[t.unref(e.name)]),a=t.computed(()=>{const{props:n}=r.value;if(!n)return{};const o=t.unref(e.route);return!0===n?o.params:"object"==typeof n?n:n(o)});t.provide(g,r);const c=t.ref();function i(){r.value.instances[t.unref(e.name)]=c.value}return e=>o.value?t.h(o.value,{...a.value,...e,onVnodeMounted:i,ref:c}):null}const ce=t.defineComponent({name:"RouterView",props:{name:{type:String,default:"default"}},setup(e,{attrs:n}){const r=ae({route:t.inject(w),name:t.toRefs(e).name});return()=>r(n)}}),ie="undefined"!=typeof window;async function se(e){for(const t of e)await t()}function ue(e,t){const n=[],r=[],o=[];for(const o of t.matched)e.matched.indexOf(o)<0?n.push(o):r.push(o);for(const n of e.matched)t.matched.indexOf(n)<0&&o.push(n);return[n,r,o]}return e.Link=oe,e.START_LOCATION=_,e.View=ce,e.createMemoryHistory=function(e=""){let t=[],a=[o],c=0;function s(e){c++,c===a.length||a.splice(c),a.push(e)}const u={location:o,state:{},base:e,replace(e){const t=i(e);a.splice(c--,1),s(t)},push(e,t){s(i(e))},listen:e=>(t.push(e),()=>{const n=t.indexOf(e);n>-1&&t.splice(n,1)}),destroy(){t=[]},back(e=!0){this.go(-1,e)},forward(e=!0){this.go(1,e)},go(e,o=!0){const i=this.location,s=e<0?r.back:r.forward;c=Math.max(0,Math.min(c+e,a.length-1)),o&&function(e,r,{direction:o,distance:a}){const c={direction:o,distance:a,type:n.pop};for(let n of t)n(e,r,c)}(this.location,i,{direction:s,distance:e})}};return Object.defineProperty(u,"location",{get:()=>a[c]}),u},e.createRouter=function({history:e,routes:n,scrollBehavior:r,parseQuery:o=I,stringifyQuery:c=B}){const i=z(n,{}),s=ne(),u=ne(),f=t.ref(_);let p=_;ie&&"scrollRestoration"in window.history&&(window.history.scrollRestoration="manual");const d=Z.bind(null,M),h=Z.bind(null,q);function m(e,t){if(t=t||f.value,"string"==typeof e){let n=function(e,t){let n="",r={},o="",a="";const c=t.indexOf("?"),i=t.indexOf("#",c>-1?c:0);return c>-1&&(n=t.slice(0,c),o=t.slice(c+1,i>-1?i:t.length),r=e(o)),i>-1&&(n=n||t.slice(0,i),a=t.slice(i,t.length)),n=n||t,{fullPath:t,path:n,query:r,hash:a}}(o,e),r=i.resolve({path:n.path},t);return{...n,...r,params:h(r.params),redirectedFrom:void 0}}let n=i.resolve("params"in e?{...e,params:d(e.params)}:e,t);return n.params="params"in e?e.params:h(n.params),{fullPath:a(c,{...e,path:n.path}),hash:e.hash||"",query:V(e.query),...n,redirectedFrom:void 0}}function g(e){return v(e,void 0)}async function v(e,t){const n=p="object"==typeof e&&"matched"in e?e:m(e),r=f.value,o=e.state;if(!e.force&&function(e,t){let n=e.matched.length-1,r=t.matched.length-1;return n>-1&&n===r&&J(e.matched[n],t.matched[r])&&ee(e.params,t.params)}(r,n))return r;n.redirectedFrom=t;try{await b(n,r)}catch(e){if(p!==n&&k(D(3,{from:r,to:n})),1===e.type)return v(e.to,t||n);k(e)}return R(n,r,!0,!0===e.replace,o),f.value}async function b(e,t){let n;n=Y(t.matched.filter(t=>e.matched.indexOf(t)<0).reverse(),"beforeRouteLeave",e,t);const[r]=ue(e,t);for(const o of r)for(const r of o.leaveGuards)n.push(X(r,e,t));await se(n),n=[];for(const r of s.list())n.push(X(r,e,t));await se(n),n=Y(e.matched.filter(e=>t.matched.indexOf(e)>-1),"beforeRouteUpdate",e,t),await se(n),n=[];for(const r of e.matched)if(r.beforeEnter&&t.matched.indexOf(r)<0)if(Array.isArray(r.beforeEnter))for(const o of r.beforeEnter)n.push(X(o,e,t));else n.push(X(r.beforeEnter,e,t));await se(n),n=Y(e.matched.filter(e=>t.matched.indexOf(e)<0),"beforeRouteEnter",e,t),await se(n);for(const e of r)e.instances={}}function R(n,o,a,c,i){if(p!==n)return k(D(3,{from:o,to:n}),a);const[s]=ue(n,o);for(const e of s)e.leaveGuards=[];a&&(c||o===_?e.replace(n,i):e.push(n,i)),f.value=t.markNonReactive(n);const d=a||!ie?{}:window.history.state;(async function(e,n,o){if(!r)return;await t.nextTick();const a=await r(e,n,o||null);console.log("scrolling to",a),l(a)})(n,o,d&&d.scroll).catch(e=>k(e,!1));for(const e of u.list())e(n,o);A()}e.listen(async(t,n,r)=>{const o=m(t.fullPath);p=o;const a=f.value;try{await b(o,a),R(o,a,!1)}catch(t){if(1===t.type){if(p!==o)return k(D(3,{from:a,to:o}),!1);k(t,!1),v(t.to,o).catch(()=>{})}else 2===t.type?(console.log("Cancelled, going to",-r.distance),e.go(-r.distance,!1)):k(t,!1)}});let E,x=ne(),O=ne();function k(e,t=!0){if(A(e),O.list().forEach(t=>t(e)),t)throw e}function A(e){E||(E=!0,x.list().forEach(([t,n])=>e?n(e):t()),x.reset())}return{currentRoute:f,addRoute:function(e,t){let n,r;return"string"==typeof e?(n=i.getRecordMatcher(e),r=t):r=e,i.addRoute(r,n)},removeRoute:function(e){let t=i.getRecordMatcher(e);t&&i.removeRoute(t)},getRoutes:function(){return i.getRoutes().map(e=>e.record)},push:g,replace:function(e){return g({..."string"==typeof e?{path:e}:e,replace:!0})},resolve:m,beforeEach:s.add,afterEach:u.add,createHref:function(t){return e.base+t.fullPath},onError:O.add,isReady:function(){return E&&f.value!==_?Promise.resolve():new Promise((e,t)=>{x.add([e,t])})},history:e,install(e){!function(e,n){e.component("RouterLink",oe),e.component("RouterView",ce);let r=!1;ie&&e.mixin({beforeCreate(){r||(n.push(n.history.location.fullPath).catch(e=>{console.error("Unhandled error",e)}),r=!0)}});const o={};for(let e in _)o[e]=t.computed(()=>n.currentRoute.value[e]);e.provide(y,n),e.provide(w,t.reactive(o))}(e,this)}}},e.createWebHashHistory=function(e=""){return d(e+"/#")},e.createWebHistory=d,e.onBeforeRouteLeave=function(e){const n=t.getCurrentInstance();if(!n)return;const r=t.inject(g,{}).value;r&&r.leaveGuards.push(e.bind(n.proxy))},e.parseQuery=I,e.stringifyQuery=B,e.useLink=re,e.useRoute=function(){return t.inject(w)},e.useRouter=function(){return t.inject(y)},e.useView=ae,e}({},Vue); //# sourceMappingURL=vue-router.global.prod.js.map