kdu-router 3.4.0-beta.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +8 -6
  3. package/dist/kdu-router.cjs.js +2848 -0
  4. package/dist/kdu-router.cjs.prod.js +2610 -0
  5. package/dist/kdu-router.d.ts +1255 -0
  6. package/dist/kdu-router.esm-browser.js +3332 -0
  7. package/dist/kdu-router.esm-bundler.js +3343 -0
  8. package/dist/kdu-router.global.js +3354 -0
  9. package/dist/kdu-router.global.prod.js +6 -0
  10. package/ketur/attributes.json +8 -14
  11. package/ketur/tags.json +2 -12
  12. package/package.json +64 -92
  13. package/dist/kdu-router.common.js +0 -3040
  14. package/dist/kdu-router.esm.browser.js +0 -3005
  15. package/dist/kdu-router.esm.browser.min.js +0 -11
  16. package/dist/kdu-router.esm.js +0 -3038
  17. package/dist/kdu-router.js +0 -3046
  18. package/dist/kdu-router.min.js +0 -11
  19. package/src/components/link.js +0 -197
  20. package/src/components/view.js +0 -149
  21. package/src/create-matcher.js +0 -200
  22. package/src/create-route-map.js +0 -205
  23. package/src/history/abstract.js +0 -68
  24. package/src/history/base.js +0 -400
  25. package/src/history/hash.js +0 -163
  26. package/src/history/html5.js +0 -94
  27. package/src/index.js +0 -277
  28. package/src/install.js +0 -52
  29. package/src/util/async.js +0 -18
  30. package/src/util/dom.js +0 -3
  31. package/src/util/errors.js +0 -85
  32. package/src/util/location.js +0 -69
  33. package/src/util/misc.js +0 -6
  34. package/src/util/params.js +0 -37
  35. package/src/util/path.js +0 -74
  36. package/src/util/push-state.js +0 -46
  37. package/src/util/query.js +0 -96
  38. package/src/util/resolve-components.js +0 -109
  39. package/src/util/route.js +0 -132
  40. package/src/util/scroll.js +0 -165
  41. package/src/util/state-key.js +0 -22
  42. package/src/util/warn.js +0 -14
  43. package/types/index.d.ts +0 -17
  44. package/types/kdu.d.ts +0 -22
  45. package/types/router.d.ts +0 -170
@@ -1,11 +0,0 @@
1
- /*!
2
- * kdu-router v3.4.0-beta.0
3
- * (c) 2022 NKDuy
4
- * @license MIT
5
- */
6
- /*!
7
- * kdu-router v3.4.0-beta.0
8
- * (c) 2022 NKDuy
9
- * @license MIT
10
- */
11
- var t,e;t=this,e=function(){"use strict";function t(t,e){for(var r in e)t[r]=e[r];return t}var e={name:"RouterView",functional:!0,props:{name:{type:String,default:"default"}},render:function(e,n){var o=n.props,i=n.children,a=n.parent,s=n.data;s.routerView=!0;for(var u=a.$createElement,c=o.name,p=a.$route,f=a._routerViewCache||(a._routerViewCache={}),h=0,l=!1;a&&a._routerRoot!==a;){var d=a.$knode?a.$knode.data:{};d.routerView&&h++,d.keepAlive&&a._directInactive&&a._inactive&&(l=!0),a=a.$parent}if(s.routerViewDepth=h,l){var v=f[c],y=v&&v.component;return y?(v.configProps&&r(y,s,v.route,v.configProps),u(y,s,i)):u()}var m=p.matched[h],g=m&&m.components[c];if(!m||!g)return f[c]=null,u();f[c]={component:g},s.registerRouteInstance=function(t,e){var r=m.instances[c];(e&&r!==t||!e&&r===t)&&(m.instances[c]=e)},(s.hook||(s.hook={})).prepatch=function(t,e){m.instances[c]=e.componentInstance},s.hook.init=function(t){t.data.keepAlive&&t.componentInstance&&t.componentInstance!==m.instances[c]&&(m.instances[c]=t.componentInstance)};var w=m.props&&m.props[c];return w&&(t(f[c],{route:p,configProps:w}),r(g,s,p,w)),u(g,s,i)}};function r(e,r,n,o){var i=r.props=function(t,e){switch(typeof e){case"undefined":return;case"object":return e;case"function":return e(t);case"boolean":return e?t.params:void 0}}(n,o);if(i){i=r.props=t({},i);var a=r.attrs=r.attrs||{};for(var s in i)e.props&&s in e.props||(a[s]=i[s],delete i[s])}}var n=/[!'()*]/g,o=function(t){return"%"+t.charCodeAt(0).toString(16)},i=/%2C/g,a=function(t){return encodeURIComponent(t).replace(n,o).replace(i,",")},s=decodeURIComponent;function u(t){var e={};return(t=t.trim().replace(/^(\?|#|&)/,""))?(t.split("&").forEach((function(t){var r=t.replace(/\+/g," ").split("="),n=s(r.shift()),o=r.length>0?s(r.join("=")):null;void 0===e[n]?e[n]=o:Array.isArray(e[n])?e[n].push(o):e[n]=[e[n],o]})),e):e}function c(t){var e=t?Object.keys(t).map((function(e){var r=t[e];if(void 0===r)return"";if(null===r)return a(e);if(Array.isArray(r)){var n=[];return r.forEach((function(t){void 0!==t&&(null===t?n.push(a(e)):n.push(a(e)+"="+a(t)))})),n.join("&")}return a(e)+"="+a(r)})).filter((function(t){return t.length>0})).join("&"):null;return e?"?"+e:""}var p=/\/?$/;function f(t,e,r,n){var o=n&&n.options.stringifyQuery,i=e.query||{};try{i=h(i)}catch(t){}var a={name:e.name||t&&t.name,meta:t&&t.meta||{},path:e.path||"/",hash:e.hash||"",query:i,params:e.params||{},fullPath:v(e,o),matched:t?d(t):[]};return r&&(a.redirectedFrom=v(r,o)),Object.freeze(a)}function h(t){if(Array.isArray(t))return t.map(h);if(t&&"object"==typeof t){var e={};for(var r in t)e[r]=h(t[r]);return e}return t}var l=f(null,{path:"/"});function d(t){for(var e=[];t;)e.unshift(t),t=t.parent;return e}function v(t,e){var r=t.path,n=t.query;void 0===n&&(n={});var o=t.hash;return void 0===o&&(o=""),(r||"/")+(e||c)(n)+o}function y(t,e){return e===l?t===e:!!e&&(t.path&&e.path?t.path.replace(p,"")===e.path.replace(p,"")&&t.hash===e.hash&&m(t.query,e.query):!(!t.name||!e.name)&&t.name===e.name&&t.hash===e.hash&&m(t.query,e.query)&&m(t.params,e.params))}function m(t,e){if(void 0===t&&(t={}),void 0===e&&(e={}),!t||!e)return t===e;var r=Object.keys(t),n=Object.keys(e);return r.length===n.length&&r.every((function(r){var n=t[r],o=e[r];return"object"==typeof n&&"object"==typeof o?m(n,o):String(n)===String(o)}))}function g(t,e,r){var n=t.charAt(0);if("/"===n)return t;if("?"===n||"#"===n)return e+t;var o=e.split("/");r&&o[o.length-1]||o.pop();for(var i=t.replace(/^\//,"").split("/"),a=0;a<i.length;a++){var s=i[a];".."===s?o.pop():"."!==s&&o.push(s)}return""!==o[0]&&o.unshift(""),o.join("/")}function w(t){return t.replace(/\/\//g,"/")}var b=Array.isArray||function(t){return"[object Array]"==Object.prototype.toString.call(t)},x=q,R=C,k=function(t,e){return j(C(t,e),e)},E=j,A=T,O=new RegExp(["(\\\\.)","([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))"].join("|"),"g");function C(t,e){for(var r,n=[],o=0,i=0,a="",s=e&&e.delimiter||"/";null!=(r=O.exec(t));){var u=r[0],c=r[1],p=r.index;if(a+=t.slice(i,p),i=p+u.length,c)a+=c[1];else{var f=t[i],h=r[2],l=r[3],d=r[4],v=r[5],y=r[6],m=r[7];a&&(n.push(a),a="");var g=null!=h&&null!=f&&f!==h,w="+"===y||"*"===y,b="?"===y||"*"===y,x=r[2]||s,R=d||v;n.push({name:l||o++,prefix:h||"",delimiter:x,optional:b,repeat:w,partial:g,asterisk:!!m,pattern:R?L(R):m?".*":"[^"+S(x)+"]+?"})}}return i<t.length&&(a+=t.substr(i)),a&&n.push(a),n}function _(t){return encodeURI(t).replace(/[\/?#]/g,(function(t){return"%"+t.charCodeAt(0).toString(16).toUpperCase()}))}function j(t,e){for(var r=new Array(t.length),n=0;n<t.length;n++)"object"==typeof t[n]&&(r[n]=new RegExp("^(?:"+t[n].pattern+")$",P(e)));return function(e,n){for(var o="",i=e||{},a=(n||{}).pretty?_:encodeURIComponent,s=0;s<t.length;s++){var u=t[s];if("string"!=typeof u){var c,p=i[u.name];if(null==p){if(u.optional){u.partial&&(o+=u.prefix);continue}throw new TypeError('Expected "'+u.name+'" to be defined')}if(b(p)){if(!u.repeat)throw new TypeError('Expected "'+u.name+'" to not repeat, but received `'+JSON.stringify(p)+"`");if(0===p.length){if(u.optional)continue;throw new TypeError('Expected "'+u.name+'" to not be empty')}for(var f=0;f<p.length;f++){if(c=a(p[f]),!r[s].test(c))throw new TypeError('Expected all "'+u.name+'" to match "'+u.pattern+'", but received `'+JSON.stringify(c)+"`");o+=(0===f?u.prefix:u.delimiter)+c}}else{if(c=u.asterisk?encodeURI(p).replace(/[?#]/g,(function(t){return"%"+t.charCodeAt(0).toString(16).toUpperCase()})):a(p),!r[s].test(c))throw new TypeError('Expected "'+u.name+'" to match "'+u.pattern+'", but received "'+c+'"');o+=u.prefix+c}}else o+=u}return o}}function S(t){return t.replace(/([.+*?=^!:${}()[\]|\/\\])/g,"\\$1")}function L(t){return t.replace(/([=!:$\/()])/g,"\\$1")}function $(t,e){return t.keys=e,t}function P(t){return t&&t.sensitive?"":"i"}function T(t,e,r){b(e)||(r=e||r,e=[]);for(var n=(r=r||{}).strict,o=!1!==r.end,i="",a=0;a<t.length;a++){var s=t[a];if("string"==typeof s)i+=S(s);else{var u=S(s.prefix),c="(?:"+s.pattern+")";e.push(s),s.repeat&&(c+="(?:"+u+c+")*"),i+=c=s.optional?s.partial?u+"("+c+")?":"(?:"+u+"("+c+"))?":u+"("+c+")"}}var p=S(r.delimiter||"/"),f=i.slice(-p.length)===p;return n||(i=(f?i.slice(0,-p.length):i)+"(?:"+p+"(?=$))?"),i+=o?"$":n&&f?"":"(?="+p+"|$)",$(new RegExp("^"+i,P(r)),e)}function q(t,e,r){return b(e)||(r=e||r,e=[]),r=r||{},t instanceof RegExp?function(t,e){var r=t.source.match(/\((?!\?)/g);if(r)for(var n=0;n<r.length;n++)e.push({name:n,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,asterisk:!1,pattern:null});return $(t,e)}(t,e):b(t)?function(t,e,r){for(var n=[],o=0;o<t.length;o++)n.push(q(t[o],e,r).source);return $(new RegExp("(?:"+n.join("|")+")",P(r)),e)}(t,e,r):function(t,e,r){return T(C(t,r),e,r)}(t,e,r)}x.parse=R,x.compile=k,x.tokensToFunction=E,x.tokensToRegExp=A;var U=Object.create(null);function I(t,e,r){e=e||{};try{var n=U[t]||(U[t]=x.compile(t));return"string"==typeof e.pathMatch&&(e[0]=e.pathMatch),n(e,{pretty:!0})}catch(t){return""}finally{delete e[0]}}function M(e,r,n,o){var i="string"==typeof e?{path:e}:e;if(i._normalized)return i;if(i.name){var a=(i=t({},e)).params;return a&&"object"==typeof a&&(i.params=t({},a)),i}if(!i.path&&i.params&&r){(i=t({},i))._normalized=!0;var s=t(t({},r.params),i.params);if(r.name)i.name=r.name,i.params=s;else if(r.matched.length){var c=r.matched[r.matched.length-1].path;i.path=I(c,s,r.path)}return i}var p=function(t){var e="",r="",n=t.indexOf("#");n>=0&&(e=t.slice(n),t=t.slice(0,n));var o=t.indexOf("?");return o>=0&&(r=t.slice(o+1),t=t.slice(0,o)),{path:t,query:r,hash:e}}(i.path||""),f=r&&r.path||"/",h=p.path?g(p.path,f,n||i.append):f,l=function(t,e,r){void 0===e&&(e={});var n,o=r||u;try{n=o(t||"")}catch(t){n={}}for(var i in e){var a=e[i];n[i]=Array.isArray(a)?a.map((function(t){return""+t})):""+a}return n}(p.query,i.query,o&&o.options.parseQuery),d=i.hash||p.hash;return d&&"#"!==d.charAt(0)&&(d="#"+d),{_normalized:!0,path:h,query:l,hash:d}}var B,F=function(){},H={name:"RouterLink",props:{to:{type:[String,Object],required:!0},tag:{type:String,default:"a"},exact:Boolean,append:Boolean,replace:Boolean,activeClass:String,exactActiveClass:String,ariaCurrentValue:{type:String,default:"page"},event:{type:[String,Array],default:"click"}},render:function(e){var r=this,n=this.$router,o=this.$route,i=n.resolve(this.to,o,this.append),a=i.location,s=i.route,u=i.href,c={},h=n.options.linkActiveClass,l=n.options.linkExactActiveClass,d=null==h?"router-link-active":h,v=null==l?"router-link-exact-active":l,m=null==this.activeClass?d:this.activeClass,g=null==this.exactActiveClass?v:this.exactActiveClass,w=s.redirectedFrom?f(null,M(s.redirectedFrom),null,n):s;c[g]=y(o,w),c[m]=this.exact?c[g]:function(t,e){return 0===t.path.replace(p,"/").indexOf(e.path.replace(p,"/"))&&(!e.hash||t.hash===e.hash)&&function(t,e){for(var r in e)if(!(r in t))return!1;return!0}(t.query,e.query)}(o,w);var b=c[g]?this.ariaCurrentValue:null,x=function(t){N(t)&&(r.replace?n.replace(a,F):n.push(a,F))},R={click:N};Array.isArray(this.event)?this.event.forEach((function(t){R[t]=x})):R[this.event]=x;var k={class:c},E=!this.$scopedSlots.$hasNormal&&this.$scopedSlots.default&&this.$scopedSlots.default({href:u,route:s,navigate:x,isActive:c[m],isExactActive:c[g]});if(E){if(1===E.length)return E[0];if(E.length>1||!E.length)return 0===E.length?e():e("span",{},E)}if("a"===this.tag)k.on=R,k.attrs={href:u,"aria-current":b};else{var A=function t(e){var r;if(e)for(var n=0;n<e.length;n++){if("a"===(r=e[n]).tag)return r;if(r.children&&(r=t(r.children)))return r}}(this.$slots.default);if(A){A.isStatic=!1;var O=A.data=t({},A.data);for(var C in O.on=O.on||{},O.on){var _=O.on[C];C in R&&(O.on[C]=Array.isArray(_)?_:[_])}for(var j in R)j in O.on?O.on[j].push(R[j]):O.on[j]=x;var S=A.data.attrs=t({},A.data.attrs);S.href=u,S["aria-current"]=b}else k.on=R}return e(this.tag,k,this.$slots.default)}};function N(t){if(!(t.metaKey||t.altKey||t.ctrlKey||t.shiftKey||t.defaultPrevented||void 0!==t.button&&0!==t.button)){if(t.currentTarget&&t.currentTarget.getAttribute){var e=t.currentTarget.getAttribute("target");if(/\b_blank\b/i.test(e))return}return t.preventDefault&&t.preventDefault(),!0}}var V="undefined"!=typeof window;function z(t,e,r,n){var o=e||[],i=r||Object.create(null),a=n||Object.create(null);t.forEach((function(t){!function t(e,r,n,o,i,a){var s=o.path,u=o.name,c=o.pathToRegexpOptions||{},p=function(t,e,r){return r||(t=t.replace(/\/$/,"")),"/"===t[0]||null==e?t:w(e.path+"/"+t)}(s,i,c.strict);"boolean"==typeof o.caseSensitive&&(c.sensitive=o.caseSensitive);var f={path:p,regex:K(p,c),components:o.components||{default:o.component},instances:{},name:u,parent:i,matchAs:a,redirect:o.redirect,beforeEnter:o.beforeEnter,meta:o.meta||{},props:null==o.props?{}:o.components?o.props:{default:o.props}};if(o.children&&o.children.forEach((function(o){var i=a?w(a+"/"+o.path):void 0;t(e,r,n,o,f,i)})),r[f.path]||(e.push(f.path),r[f.path]=f),void 0!==o.alias)for(var h=Array.isArray(o.alias)?o.alias:[o.alias],l=0;l<h.length;++l){var d={path:h[l],children:o.children};t(e,r,n,d,i,f.path||"/")}u&&(n[u]||(n[u]=f))}(o,i,a,t)}));for(var s=0,u=o.length;s<u;s++)"*"===o[s]&&(o.push(o.splice(s,1)[0]),u--,s--);return{pathList:o,pathMap:i,nameMap:a}}function K(t,e){return x(t,[],e)}function D(t,e){var r=z(t),n=r.pathList,o=r.pathMap,i=r.nameMap;function a(t,r,a){var s=M(t,r,!1,e),c=s.name;if(c){var p=i[c];if(!p)return u(null,s);var f=p.regex.keys.filter((function(t){return!t.optional})).map((function(t){return t.name}));if("object"!=typeof s.params&&(s.params={}),r&&"object"==typeof r.params)for(var h in r.params)!(h in s.params)&&f.indexOf(h)>-1&&(s.params[h]=r.params[h]);return s.path=I(p.path,s.params),u(p,s,a)}if(s.path){s.params={};for(var l=0;l<n.length;l++){var d=n[l],v=o[d];if(J(v.regex,s.path,s.params))return u(v,s,a)}}return u(null,s)}function s(t,r){var n=t.redirect,o="function"==typeof n?n(f(t,r,null,e)):n;if("string"==typeof o&&(o={path:o}),!o||"object"!=typeof o)return u(null,r);var s=o,c=s.name,p=s.path,h=r.query,l=r.hash,d=r.params;return h=s.hasOwnProperty("query")?s.query:h,l=s.hasOwnProperty("hash")?s.hash:l,d=s.hasOwnProperty("params")?s.params:d,c?(i[c],a({_normalized:!0,name:c,query:h,hash:l,params:d},void 0,r)):p?a({_normalized:!0,path:I(function(t,e){return g(t,e.parent?e.parent.path:"/",!0)}(p,t),d),query:h,hash:l},void 0,r):u(null,r)}function u(t,r,n){return t&&t.redirect?s(t,n||r):t&&t.matchAs?function(t,e,r){var n=a({_normalized:!0,path:I(r,e.params)});if(n){var o=n.matched,i=o[o.length-1];return e.params=n.params,u(i,e)}return u(null,e)}(0,r,t.matchAs):f(t,r,n,e)}return{match:a,addRoutes:function(t){z(t,n,o,i)}}}function J(t,e,r){var n=e.match(t);if(!n)return!1;if(!r)return!0;for(var o=1,i=n.length;o<i;++o){var a=t.keys[o-1],s="string"==typeof n[o]?decodeURIComponent(n[o]):n[o];a&&(r[a.name||"pathMatch"]=s)}return!0}var Q=V&&window.performance&&window.performance.now?window.performance:Date;function X(){return Q.now().toFixed(3)}var Y=X();function W(){return Y}function G(t){return Y=t}var Z=Object.create(null);function tt(){"scrollRestoration"in window.history&&(window.history.scrollRestoration="manual");var e=window.location.protocol+"//"+window.location.host,r=window.location.href.replace(e,""),n=t({},window.history.state);return n.key=W(),window.history.replaceState(n,"",r),window.addEventListener("popstate",nt),function(){window.removeEventListener("popstate",nt)}}function et(t,e,r,n){if(t.app){var o=t.options.scrollBehavior;o&&t.app.$nextTick((function(){var i=function(){var t=W();if(t)return Z[t]}(),a=o.call(t,e,r,n?i:null);a&&("function"==typeof a.then?a.then((function(t){ut(t,i)})).catch((function(t){})):ut(a,i))}))}}function rt(){var t=W();t&&(Z[t]={x:window.pageXOffset,y:window.pageYOffset})}function nt(t){rt(),t.state&&t.state.key&&G(t.state.key)}function ot(t){return at(t.x)||at(t.y)}function it(t){return{x:at(t.x)?t.x:window.pageXOffset,y:at(t.y)?t.y:window.pageYOffset}}function at(t){return"number"==typeof t}var st=/^#\d/;function ut(t,e){var r,n="object"==typeof t;if(n&&"string"==typeof t.selector){var o=st.test(t.selector)?document.getElementById(t.selector.slice(1)):document.querySelector(t.selector);if(o){var i=t.offset&&"object"==typeof t.offset?t.offset:{};e=function(t,e){var r=document.documentElement.getBoundingClientRect(),n=t.getBoundingClientRect();return{x:n.left-r.left-e.x,y:n.top-r.top-e.y}}(o,i={x:at((r=i).x)?r.x:0,y:at(r.y)?r.y:0})}else ot(t)&&(e=it(t))}else n&&ot(t)&&(e=it(t));e&&window.scrollTo(e.x,e.y)}var ct,pt=V&&(-1===(ct=window.navigator.userAgent).indexOf("Android 2.")&&-1===ct.indexOf("Android 4.0")||-1===ct.indexOf("Mobile Safari")||-1!==ct.indexOf("Chrome")||-1!==ct.indexOf("Windows Phone"))&&window.history&&"function"==typeof window.history.pushState;function ft(e,r){rt();var n=window.history;try{if(r){var o=t({},n.state);o.key=W(),n.replaceState(o,"",e)}else n.pushState({key:G(X())},"",e)}catch(t){window.location[r?"replace":"assign"](e)}}function ht(t){ft(t,!0)}function lt(t,e,r){var n=function(o){o>=t.length?r():t[o]?e(t[o],(function(){n(o+1)})):n(o+1)};n(0)}var dt={redirected:2,aborted:4,cancelled:8,duplicated:16};function vt(t,e){return mt(t,e,dt.redirected,'Redirected when going from "'+t.fullPath+'" to "'+function(t){if("string"==typeof t)return t;if("path"in t)return t.path;var e={};return gt.forEach((function(r){r in t&&(e[r]=t[r])})),JSON.stringify(e,null,2)}(e)+'" via a navigation guard.')}function yt(t,e){return mt(t,e,dt.cancelled,'Navigation cancelled from "'+t.fullPath+'" to "'+e.fullPath+'" with a new navigation.')}function mt(t,e,r,n){var o=new Error(n);return o._isRouter=!0,o.from=t,o.to=e,o.type=r,o}var gt=["params","query","hash"];function wt(t){return Object.prototype.toString.call(t).indexOf("Error")>-1}function bt(t,e){return wt(t)&&t._isRouter&&(null==e||t.type===e)}function xt(t){return function(e,r,n){var o=!1,i=0,a=null;Rt(t,(function(t,e,r,s){if("function"==typeof t&&void 0===t.cid){o=!0,i++;var u,c=At((function(e){var o;((o=e).__esModule||Et&&"Module"===o[Symbol.toStringTag])&&(e=e.default),t.resolved="function"==typeof e?e:B.extend(e),r.components[s]=e,--i<=0&&n()})),p=At((function(t){var e="Failed to resolve async component "+s+": "+t;a||(a=wt(t)?t:new Error(e),n(a))}));try{u=t(c,p)}catch(t){p(t)}if(u)if("function"==typeof u.then)u.then(c,p);else{var f=u.component;f&&"function"==typeof f.then&&f.then(c,p)}}})),o||n()}}function Rt(t,e){return kt(t.map((function(t){return Object.keys(t.components).map((function(r){return e(t.components[r],t.instances[r],t,r)}))})))}function kt(t){return Array.prototype.concat.apply([],t)}var Et="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag;function At(t){var e=!1;return function(){for(var r=[],n=arguments.length;n--;)r[n]=arguments[n];if(!e)return e=!0,t.apply(this,r)}}var Ot=function(t,e){this.router=t,this.base=function(t){if(!t)if(V){var e=document.querySelector("base");t=(t=e&&e.getAttribute("href")||"/").replace(/^https?:\/\/[^\/]+/,"")}else t="/";return"/"!==t.charAt(0)&&(t="/"+t),t.replace(/\/$/,"")}(e),this.current=l,this.pending=null,this.ready=!1,this.readyCbs=[],this.readyErrorCbs=[],this.errorCbs=[],this.listeners=[]};function Ct(t,e,r,n){var o=Rt(t,(function(t,n,o,i){var a=function(t,e){return"function"!=typeof t&&(t=B.extend(t)),t.options[e]}(t,e);if(a)return Array.isArray(a)?a.map((function(t){return r(t,n,o,i)})):r(a,n,o,i)}));return kt(n?o.reverse():o)}function _t(t,e){if(e)return function(){return t.apply(e,arguments)}}Ot.prototype.listen=function(t){this.cb=t},Ot.prototype.onReady=function(t,e){this.ready?t():(this.readyCbs.push(t),e&&this.readyErrorCbs.push(e))},Ot.prototype.onError=function(t){this.errorCbs.push(t)},Ot.prototype.transitionTo=function(t,e,r){var n,o=this;try{n=this.router.match(t,this.current)}catch(t){throw this.errorCbs.forEach((function(e){e(t)})),t}this.confirmTransition(n,(function(){var t=o.current;o.updateRoute(n),e&&e(n),o.ensureURL(),o.router.afterHooks.forEach((function(e){e&&e(n,t)})),o.ready||(o.ready=!0,o.readyCbs.forEach((function(t){t(n)})))}),(function(t){r&&r(t),t&&!o.ready&&(o.ready=!0,bt(t,dt.redirected)?o.readyCbs.forEach((function(t){t(n)})):o.readyErrorCbs.forEach((function(e){e(t)})))}))},Ot.prototype.confirmTransition=function(t,e,r){var n,o,i=this,a=this.current,s=function(t){!bt(t)&&wt(t)&&(i.errorCbs.length?i.errorCbs.forEach((function(e){e(t)})):console.error(t)),r&&r(t)},u=t.matched.length-1,c=a.matched.length-1;if(y(t,a)&&u===c&&t.matched[u]===a.matched[c])return this.ensureURL(),s(((o=mt(n=a,t,dt.duplicated,'Avoided redundant navigation to current location: "'+n.fullPath+'".')).name="NavigationDuplicated",o));var p=function(t,e){var r,n=Math.max(t.length,e.length);for(r=0;r<n&&t[r]===e[r];r++);return{updated:e.slice(0,r),activated:e.slice(r),deactivated:t.slice(r)}}(this.current.matched,t.matched),f=p.updated,h=p.deactivated,l=p.activated,d=[].concat(function(t){return Ct(t,"beforeRouteLeave",_t,!0)}(h),this.router.beforeHooks,function(t){return Ct(t,"beforeRouteUpdate",_t)}(f),l.map((function(t){return t.beforeEnter})),xt(l));this.pending=t;var v=function(e,r){if(i.pending!==t)return s(yt(a,t));try{e(t,a,(function(e){!1===e?(i.ensureURL(!0),s(function(t,e){return mt(t,e,dt.aborted,'Navigation aborted from "'+t.fullPath+'" to "'+e.fullPath+'" via a navigation guard.')}(a,t))):wt(e)?(i.ensureURL(!0),s(e)):"string"==typeof e||"object"==typeof e&&("string"==typeof e.path||"string"==typeof e.name)?(s(vt(a,t)),"object"==typeof e&&e.replace?i.replace(e):i.push(e)):r(e)}))}catch(t){s(t)}};lt(d,v,(function(){var r=[];lt(function(t,e,r){return Ct(t,"beforeRouteEnter",(function(t,n,o,i){return function(t,e,r,n,o){return function(i,a,s){return t(i,a,(function(t){"function"==typeof t&&n.push((function(){!function t(e,r,n,o){r[n]&&!r[n]._isBeingDestroyed?e(r[n]):o()&&setTimeout((function(){t(e,r,n,o)}),16)}(t,e.instances,r,o)})),s(t)}))}}(t,o,i,e,r)}))}(l,r,(function(){return i.current===t})).concat(i.router.resolveHooks),v,(function(){if(i.pending!==t)return s(yt(a,t));i.pending=null,e(t),i.router.app&&i.router.app.$nextTick((function(){r.forEach((function(t){t()}))}))}))}))},Ot.prototype.updateRoute=function(t){this.current=t,this.cb&&this.cb(t)},Ot.prototype.setupListeners=function(){},Ot.prototype.teardownListeners=function(){this.listeners.forEach((function(t){t()})),this.listeners=[]};var jt=function(t){function e(e,r){t.call(this,e,r),this._startLocation=St(this.base)}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e.prototype.setupListeners=function(){var t=this;if(!(this.listeners.length>0)){var e=this.router,r=e.options.scrollBehavior,n=pt&&r;n&&this.listeners.push(tt());var o=function(){var r=t.current,o=St(t.base);t.current===l&&o===t._startLocation||t.transitionTo(o,(function(t){n&&et(e,t,r,!0)}))};window.addEventListener("popstate",o),this.listeners.push((function(){window.removeEventListener("popstate",o)}))}},e.prototype.go=function(t){window.history.go(t)},e.prototype.push=function(t,e,r){var n=this,o=this.current;this.transitionTo(t,(function(t){ft(w(n.base+t.fullPath)),et(n.router,t,o,!1),e&&e(t)}),r)},e.prototype.replace=function(t,e,r){var n=this,o=this.current;this.transitionTo(t,(function(t){ht(w(n.base+t.fullPath)),et(n.router,t,o,!1),e&&e(t)}),r)},e.prototype.ensureURL=function(t){if(St(this.base)!==this.current.fullPath){var e=w(this.base+this.current.fullPath);t?ft(e):ht(e)}},e.prototype.getCurrentLocation=function(){return St(this.base)},e}(Ot);function St(t){var e=decodeURI(window.location.pathname);return t&&0===e.toLowerCase().indexOf(t.toLowerCase())&&(e=e.slice(t.length)),(e||"/")+window.location.search+window.location.hash}var Lt=function(t){function e(e,r,n){t.call(this,e,r),n&&function(t){var e=St(t);if(!/^\/#/.test(e))return window.location.replace(w(t+"/#"+e)),!0}(this.base)||$t()}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e.prototype.setupListeners=function(){var t=this;if(!(this.listeners.length>0)){var e=this.router.options.scrollBehavior,r=pt&&e;r&&this.listeners.push(tt());var n=function(){var e=t.current;$t()&&t.transitionTo(Pt(),(function(n){r&&et(t.router,n,e,!0),pt||Ut(n.fullPath)}))},o=pt?"popstate":"hashchange";window.addEventListener(o,n),this.listeners.push((function(){window.removeEventListener(o,n)}))}},e.prototype.push=function(t,e,r){var n=this,o=this.current;this.transitionTo(t,(function(t){qt(t.fullPath),et(n.router,t,o,!1),e&&e(t)}),r)},e.prototype.replace=function(t,e,r){var n=this,o=this.current;this.transitionTo(t,(function(t){Ut(t.fullPath),et(n.router,t,o,!1),e&&e(t)}),r)},e.prototype.go=function(t){window.history.go(t)},e.prototype.ensureURL=function(t){var e=this.current.fullPath;Pt()!==e&&(t?qt(e):Ut(e))},e.prototype.getCurrentLocation=function(){return Pt()},e}(Ot);function $t(){var t=Pt();return"/"===t.charAt(0)||(Ut("/"+t),!1)}function Pt(){var t=window.location.href,e=t.indexOf("#");if(e<0)return"";var r=(t=t.slice(e+1)).indexOf("?");if(r<0){var n=t.indexOf("#");t=n>-1?decodeURI(t.slice(0,n))+t.slice(n):decodeURI(t)}else t=decodeURI(t.slice(0,r))+t.slice(r);return t}function Tt(t){var e=window.location.href,r=e.indexOf("#");return(r>=0?e.slice(0,r):e)+"#"+t}function qt(t){pt?ft(Tt(t)):window.location.hash=t}function Ut(t){pt?ht(Tt(t)):window.location.replace(Tt(t))}var It=function(t){function e(e,r){t.call(this,e,r),this.stack=[],this.index=-1}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e.prototype.push=function(t,e,r){var n=this;this.transitionTo(t,(function(t){n.stack=n.stack.slice(0,n.index+1).concat(t),n.index++,e&&e(t)}),r)},e.prototype.replace=function(t,e,r){var n=this;this.transitionTo(t,(function(t){n.stack=n.stack.slice(0,n.index).concat(t),e&&e(t)}),r)},e.prototype.go=function(t){var e=this,r=this.index+t;if(!(r<0||r>=this.stack.length)){var n=this.stack[r];this.confirmTransition(n,(function(){e.index=r,e.updateRoute(n)}),(function(t){bt(t,dt.duplicated)&&(e.index=r)}))}},e.prototype.getCurrentLocation=function(){var t=this.stack[this.stack.length-1];return t?t.fullPath:"/"},e.prototype.ensureURL=function(){},e}(Ot),Mt=function(t){void 0===t&&(t={}),this.app=null,this.apps=[],this.options=t,this.beforeHooks=[],this.resolveHooks=[],this.afterHooks=[],this.matcher=D(t.routes||[],this);var e=t.mode||"hash";switch(this.fallback="history"===e&&!pt&&!1!==t.fallback,this.fallback&&(e="hash"),V||(e="abstract"),this.mode=e,e){case"history":this.history=new jt(this,t.base);break;case"hash":this.history=new Lt(this,t.base,this.fallback);break;case"abstract":this.history=new It(this,t.base)}},Bt={currentRoute:{configurable:!0}};function Ft(t,e){return t.push(e),function(){var r=t.indexOf(e);r>-1&&t.splice(r,1)}}return Mt.prototype.match=function(t,e,r){return this.matcher.match(t,e,r)},Bt.currentRoute.get=function(){return this.history&&this.history.current},Mt.prototype.init=function(t){var e=this;if(this.apps.push(t),t.$once("hook:destroyed",(function(){var r=e.apps.indexOf(t);r>-1&&e.apps.splice(r,1),e.app===t&&(e.app=e.apps[0]||null),e.app||e.history.teardownListeners()})),!this.app){this.app=t;var r=this.history;if(r instanceof jt||r instanceof Lt){var n=function(t){r.setupListeners(),function(t){var n=r.current,o=e.options.scrollBehavior;pt&&o&&"fullPath"in t&&et(e,t,n,!1)}(t)};r.transitionTo(r.getCurrentLocation(),n,n)}r.listen((function(t){e.apps.forEach((function(e){e._route=t}))}))}},Mt.prototype.beforeEach=function(t){return Ft(this.beforeHooks,t)},Mt.prototype.beforeResolve=function(t){return Ft(this.resolveHooks,t)},Mt.prototype.afterEach=function(t){return Ft(this.afterHooks,t)},Mt.prototype.onReady=function(t,e){this.history.onReady(t,e)},Mt.prototype.onError=function(t){this.history.onError(t)},Mt.prototype.push=function(t,e,r){var n=this;if(!e&&!r&&"undefined"!=typeof Promise)return new Promise((function(e,r){n.history.push(t,e,r)}));this.history.push(t,e,r)},Mt.prototype.replace=function(t,e,r){var n=this;if(!e&&!r&&"undefined"!=typeof Promise)return new Promise((function(e,r){n.history.replace(t,e,r)}));this.history.replace(t,e,r)},Mt.prototype.go=function(t){this.history.go(t)},Mt.prototype.back=function(){this.go(-1)},Mt.prototype.forward=function(){this.go(1)},Mt.prototype.getMatchedComponents=function(t){var e=t?t.matched?t:this.resolve(t).route:this.currentRoute;return e?[].concat.apply([],e.matched.map((function(t){return Object.keys(t.components).map((function(e){return t.components[e]}))}))):[]},Mt.prototype.resolve=function(t,e,r){var n=M(t,e=e||this.history.current,r,this),o=this.match(n,e),i=o.redirectedFrom||o.fullPath;return{location:n,route:o,href:function(t,e,r){var n="hash"===r?"#"+e:e;return t?w(t+"/"+n):n}(this.history.base,i,this.mode),normalizedTo:n,resolved:o}},Mt.prototype.addRoutes=function(t){this.matcher.addRoutes(t),this.history.current!==l&&this.history.transitionTo(this.history.getCurrentLocation())},Object.defineProperties(Mt.prototype,Bt),Mt.install=function t(r){if(!t.installed||B!==r){t.installed=!0,B=r;var n=function(t){return void 0!==t},o=function(t,e){var r=t.$options._parentKnode;n(r)&&n(r=r.data)&&n(r=r.registerRouteInstance)&&r(t,e)};r.mixin({beforeCreate:function(){n(this.$options.router)?(this._routerRoot=this,this._router=this.$options.router,this._router.init(this),r.util.defineReactive(this,"_route",this._router.history.current)):this._routerRoot=this.$parent&&this.$parent._routerRoot||this,o(this,this)},destroyed:function(){o(this)}}),Object.defineProperty(r.prototype,"$router",{get:function(){return this._routerRoot._router}}),Object.defineProperty(r.prototype,"$route",{get:function(){return this._routerRoot._route}}),r.component("RouterView",e),r.component("RouterLink",H);var i=r.config.optionMergeStrategies;i.beforeRouteEnter=i.beforeRouteLeave=i.beforeRouteUpdate=i.created}},Mt.version="3.4.0-beta.0",Mt.isNavigationFailure=bt,Mt.NavigationFailureType=dt,V&&window.Kdu&&window.Kdu.use(Mt),Mt},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).KduRouter=e();
@@ -1,197 +0,0 @@
1
- /* @flow */
2
-
3
- import { createRoute, isSameRoute, isIncludedRoute } from '../util/route'
4
- import { extend } from '../util/misc'
5
- import { normalizeLocation } from '../util/location'
6
- import { warn } from '../util/warn'
7
-
8
- // work around weird flow bug
9
- const toTypes: Array<Function> = [String, Object]
10
- const eventTypes: Array<Function> = [String, Array]
11
-
12
- const noop = () => {}
13
-
14
- export default {
15
- name: 'RouterLink',
16
- props: {
17
- to: {
18
- type: toTypes,
19
- required: true
20
- },
21
- tag: {
22
- type: String,
23
- default: 'a'
24
- },
25
- exact: Boolean,
26
- append: Boolean,
27
- replace: Boolean,
28
- activeClass: String,
29
- exactActiveClass: String,
30
- ariaCurrentValue: {
31
- type: String,
32
- default: 'page'
33
- },
34
- event: {
35
- type: eventTypes,
36
- default: 'click'
37
- }
38
- },
39
- render (h: Function) {
40
- const router = this.$router
41
- const current = this.$route
42
- const { location, route, href } = router.resolve(
43
- this.to,
44
- current,
45
- this.append
46
- )
47
-
48
- const classes = {}
49
- const globalActiveClass = router.options.linkActiveClass
50
- const globalExactActiveClass = router.options.linkExactActiveClass
51
- // Support global empty active class
52
- const activeClassFallback =
53
- globalActiveClass == null ? 'router-link-active' : globalActiveClass
54
- const exactActiveClassFallback =
55
- globalExactActiveClass == null
56
- ? 'router-link-exact-active'
57
- : globalExactActiveClass
58
- const activeClass =
59
- this.activeClass == null ? activeClassFallback : this.activeClass
60
- const exactActiveClass =
61
- this.exactActiveClass == null
62
- ? exactActiveClassFallback
63
- : this.exactActiveClass
64
-
65
- const compareTarget = route.redirectedFrom
66
- ? createRoute(null, normalizeLocation(route.redirectedFrom), null, router)
67
- : route
68
-
69
- classes[exactActiveClass] = isSameRoute(current, compareTarget)
70
- classes[activeClass] = this.exact
71
- ? classes[exactActiveClass]
72
- : isIncludedRoute(current, compareTarget)
73
-
74
- const ariaCurrentValue = classes[exactActiveClass] ? this.ariaCurrentValue : null
75
-
76
- const handler = e => {
77
- if (guardEvent(e)) {
78
- if (this.replace) {
79
- router.replace(location, noop)
80
- } else {
81
- router.push(location, noop)
82
- }
83
- }
84
- }
85
-
86
- const on = { click: guardEvent }
87
- if (Array.isArray(this.event)) {
88
- this.event.forEach(e => {
89
- on[e] = handler
90
- })
91
- } else {
92
- on[this.event] = handler
93
- }
94
-
95
- const data: any = { class: classes }
96
-
97
- const scopedSlot =
98
- !this.$scopedSlots.$hasNormal &&
99
- this.$scopedSlots.default &&
100
- this.$scopedSlots.default({
101
- href,
102
- route,
103
- navigate: handler,
104
- isActive: classes[activeClass],
105
- isExactActive: classes[exactActiveClass]
106
- })
107
-
108
- if (scopedSlot) {
109
- if (scopedSlot.length === 1) {
110
- return scopedSlot[0]
111
- } else if (scopedSlot.length > 1 || !scopedSlot.length) {
112
- if (process.env.NODE_ENV !== 'production') {
113
- warn(
114
- false,
115
- `RouterLink with to="${
116
- this.to
117
- }" is trying to use a scoped slot but it didn't provide exactly one child. Wrapping the content with a span element.`
118
- )
119
- }
120
- return scopedSlot.length === 0 ? h() : h('span', {}, scopedSlot)
121
- }
122
- }
123
-
124
- if (this.tag === 'a') {
125
- data.on = on
126
- data.attrs = { href, 'aria-current': ariaCurrentValue }
127
- } else {
128
- // find the first <a> child and apply listener and href
129
- const a = findAnchor(this.$slots.default)
130
- if (a) {
131
- // in case the <a> is a static node
132
- a.isStatic = false
133
- const aData = (a.data = extend({}, a.data))
134
- aData.on = aData.on || {}
135
- // transform existing events in both objects into arrays so we can push later
136
- for (const event in aData.on) {
137
- const handler = aData.on[event]
138
- if (event in on) {
139
- aData.on[event] = Array.isArray(handler) ? handler : [handler]
140
- }
141
- }
142
- // append new listeners for router-link
143
- for (const event in on) {
144
- if (event in aData.on) {
145
- // on[event] is always a function
146
- aData.on[event].push(on[event])
147
- } else {
148
- aData.on[event] = handler
149
- }
150
- }
151
-
152
- const aAttrs = (a.data.attrs = extend({}, a.data.attrs))
153
- aAttrs.href = href
154
- aAttrs['aria-current'] = ariaCurrentValue
155
- } else {
156
- // doesn't have <a> child, apply listener to self
157
- data.on = on
158
- }
159
- }
160
-
161
- return h(this.tag, data, this.$slots.default)
162
- }
163
- }
164
-
165
- function guardEvent (e) {
166
- // don't redirect with control keys
167
- if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) return
168
- // don't redirect when preventDefault called
169
- if (e.defaultPrevented) return
170
- // don't redirect on right click
171
- if (e.button !== undefined && e.button !== 0) return
172
- // don't redirect if `target="_blank"`
173
- if (e.currentTarget && e.currentTarget.getAttribute) {
174
- const target = e.currentTarget.getAttribute('target')
175
- if (/\b_blank\b/i.test(target)) return
176
- }
177
- // this may be a Weex event which doesn't have this method
178
- if (e.preventDefault) {
179
- e.preventDefault()
180
- }
181
- return true
182
- }
183
-
184
- function findAnchor (children) {
185
- if (children) {
186
- let child
187
- for (let i = 0; i < children.length; i++) {
188
- child = children[i]
189
- if (child.tag === 'a') {
190
- return child
191
- }
192
- if (child.children && (child = findAnchor(child.children))) {
193
- return child
194
- }
195
- }
196
- }
197
- }
@@ -1,149 +0,0 @@
1
- import { warn } from '../util/warn'
2
- import { extend } from '../util/misc'
3
-
4
- export default {
5
- name: 'RouterView',
6
- functional: true,
7
- props: {
8
- name: {
9
- type: String,
10
- default: 'default'
11
- }
12
- },
13
- render (_, { props, children, parent, data }) {
14
- // used by devtools to display a router-view badge
15
- data.routerView = true
16
-
17
- // directly use parent context's createElement() function
18
- // so that components rendered by router-view can resolve named slots
19
- const h = parent.$createElement
20
- const name = props.name
21
- const route = parent.$route
22
- const cache = parent._routerViewCache || (parent._routerViewCache = {})
23
-
24
- // determine current view depth, also check to see if the tree
25
- // has been toggled inactive but kept-alive.
26
- let depth = 0
27
- let inactive = false
28
- while (parent && parent._routerRoot !== parent) {
29
- const knodeData = parent.$knode ? parent.$knode.data : {}
30
- if (knodeData.routerView) {
31
- depth++
32
- }
33
- if (knodeData.keepAlive && parent._directInactive && parent._inactive) {
34
- inactive = true
35
- }
36
- parent = parent.$parent
37
- }
38
- data.routerViewDepth = depth
39
-
40
- // render previous view if the tree is inactive and kept-alive
41
- if (inactive) {
42
- const cachedData = cache[name]
43
- const cachedComponent = cachedData && cachedData.component
44
- if (cachedComponent) {
45
- // #2301
46
- // pass props
47
- if (cachedData.configProps) {
48
- fillPropsinData(cachedComponent, data, cachedData.route, cachedData.configProps)
49
- }
50
- return h(cachedComponent, data, children)
51
- } else {
52
- // render previous empty view
53
- return h()
54
- }
55
- }
56
-
57
- const matched = route.matched[depth]
58
- const component = matched && matched.components[name]
59
-
60
- // render empty node if no matched route or no config component
61
- if (!matched || !component) {
62
- cache[name] = null
63
- return h()
64
- }
65
-
66
- // cache component
67
- cache[name] = { component }
68
-
69
- // attach instance registration hook
70
- // this will be called in the instance's injected lifecycle hooks
71
- data.registerRouteInstance = (vm, val) => {
72
- // val could be undefined for unregistration
73
- const current = matched.instances[name]
74
- if (
75
- (val && current !== vm) ||
76
- (!val && current === vm)
77
- ) {
78
- matched.instances[name] = val
79
- }
80
- }
81
-
82
- // also register instance in prepatch hook
83
- // in case the same component instance is reused across different routes
84
- ;(data.hook || (data.hook = {})).prepatch = (_, knode) => {
85
- matched.instances[name] = knode.componentInstance
86
- }
87
-
88
- // register instance in init hook
89
- // in case kept-alive component be actived when routes changed
90
- data.hook.init = (knode) => {
91
- if (knode.data.keepAlive &&
92
- knode.componentInstance &&
93
- knode.componentInstance !== matched.instances[name]
94
- ) {
95
- matched.instances[name] = knode.componentInstance
96
- }
97
- }
98
-
99
- const configProps = matched.props && matched.props[name]
100
- // save route and configProps in cache
101
- if (configProps) {
102
- extend(cache[name], {
103
- route,
104
- configProps
105
- })
106
- fillPropsinData(component, data, route, configProps)
107
- }
108
-
109
- return h(component, data, children)
110
- }
111
- }
112
-
113
- function fillPropsinData (component, data, route, configProps) {
114
- // resolve props
115
- let propsToPass = data.props = resolveProps(route, configProps)
116
- if (propsToPass) {
117
- // clone to prevent mutation
118
- propsToPass = data.props = extend({}, propsToPass)
119
- // pass non-declared props as attrs
120
- const attrs = data.attrs = data.attrs || {}
121
- for (const key in propsToPass) {
122
- if (!component.props || !(key in component.props)) {
123
- attrs[key] = propsToPass[key]
124
- delete propsToPass[key]
125
- }
126
- }
127
- }
128
- }
129
-
130
- function resolveProps (route, config) {
131
- switch (typeof config) {
132
- case 'undefined':
133
- return
134
- case 'object':
135
- return config
136
- case 'function':
137
- return config(route)
138
- case 'boolean':
139
- return config ? route.params : undefined
140
- default:
141
- if (process.env.NODE_ENV !== 'production') {
142
- warn(
143
- false,
144
- `props in "${route.path}" is a ${typeof config}, ` +
145
- `expecting an object, function or boolean.`
146
- )
147
- }
148
- }
149
- }
@@ -1,200 +0,0 @@
1
- /* @flow */
2
-
3
- import type KduRouter from './index'
4
- import { resolvePath } from './util/path'
5
- import { assert, warn } from './util/warn'
6
- import { createRoute } from './util/route'
7
- import { fillParams } from './util/params'
8
- import { createRouteMap } from './create-route-map'
9
- import { normalizeLocation } from './util/location'
10
-
11
- export type Matcher = {
12
- match: (raw: RawLocation, current?: Route, redirectedFrom?: Location) => Route;
13
- addRoutes: (routes: Array<RouteConfig>) => void;
14
- };
15
-
16
- export function createMatcher (
17
- routes: Array<RouteConfig>,
18
- router: KduRouter
19
- ): Matcher {
20
- const { pathList, pathMap, nameMap } = createRouteMap(routes)
21
-
22
- function addRoutes (routes) {
23
- createRouteMap(routes, pathList, pathMap, nameMap)
24
- }
25
-
26
- function match (
27
- raw: RawLocation,
28
- currentRoute?: Route,
29
- redirectedFrom?: Location
30
- ): Route {
31
- const location = normalizeLocation(raw, currentRoute, false, router)
32
- const { name } = location
33
-
34
- if (name) {
35
- const record = nameMap[name]
36
- if (process.env.NODE_ENV !== 'production') {
37
- warn(record, `Route with name '${name}' does not exist`)
38
- }
39
- if (!record) return _createRoute(null, location)
40
- const paramNames = record.regex.keys
41
- .filter(key => !key.optional)
42
- .map(key => key.name)
43
-
44
- if (typeof location.params !== 'object') {
45
- location.params = {}
46
- }
47
-
48
- if (currentRoute && typeof currentRoute.params === 'object') {
49
- for (const key in currentRoute.params) {
50
- if (!(key in location.params) && paramNames.indexOf(key) > -1) {
51
- location.params[key] = currentRoute.params[key]
52
- }
53
- }
54
- }
55
-
56
- location.path = fillParams(record.path, location.params, `named route "${name}"`)
57
- return _createRoute(record, location, redirectedFrom)
58
- } else if (location.path) {
59
- location.params = {}
60
- for (let i = 0; i < pathList.length; i++) {
61
- const path = pathList[i]
62
- const record = pathMap[path]
63
- if (matchRoute(record.regex, location.path, location.params)) {
64
- return _createRoute(record, location, redirectedFrom)
65
- }
66
- }
67
- }
68
- // no match
69
- return _createRoute(null, location)
70
- }
71
-
72
- function redirect (
73
- record: RouteRecord,
74
- location: Location
75
- ): Route {
76
- const originalRedirect = record.redirect
77
- let redirect = typeof originalRedirect === 'function'
78
- ? originalRedirect(createRoute(record, location, null, router))
79
- : originalRedirect
80
-
81
- if (typeof redirect === 'string') {
82
- redirect = { path: redirect }
83
- }
84
-
85
- if (!redirect || typeof redirect !== 'object') {
86
- if (process.env.NODE_ENV !== 'production') {
87
- warn(
88
- false, `invalid redirect option: ${JSON.stringify(redirect)}`
89
- )
90
- }
91
- return _createRoute(null, location)
92
- }
93
-
94
- const re: Object = redirect
95
- const { name, path } = re
96
- let { query, hash, params } = location
97
- query = re.hasOwnProperty('query') ? re.query : query
98
- hash = re.hasOwnProperty('hash') ? re.hash : hash
99
- params = re.hasOwnProperty('params') ? re.params : params
100
-
101
- if (name) {
102
- // resolved named direct
103
- const targetRecord = nameMap[name]
104
- if (process.env.NODE_ENV !== 'production') {
105
- assert(targetRecord, `redirect failed: named route "${name}" not found.`)
106
- }
107
- return match({
108
- _normalized: true,
109
- name,
110
- query,
111
- hash,
112
- params
113
- }, undefined, location)
114
- } else if (path) {
115
- // 1. resolve relative redirect
116
- const rawPath = resolveRecordPath(path, record)
117
- // 2. resolve params
118
- const resolvedPath = fillParams(rawPath, params, `redirect route with path "${rawPath}"`)
119
- // 3. rematch with existing query and hash
120
- return match({
121
- _normalized: true,
122
- path: resolvedPath,
123
- query,
124
- hash
125
- }, undefined, location)
126
- } else {
127
- if (process.env.NODE_ENV !== 'production') {
128
- warn(false, `invalid redirect option: ${JSON.stringify(redirect)}`)
129
- }
130
- return _createRoute(null, location)
131
- }
132
- }
133
-
134
- function alias (
135
- record: RouteRecord,
136
- location: Location,
137
- matchAs: string
138
- ): Route {
139
- const aliasedPath = fillParams(matchAs, location.params, `aliased route with path "${matchAs}"`)
140
- const aliasedMatch = match({
141
- _normalized: true,
142
- path: aliasedPath
143
- })
144
- if (aliasedMatch) {
145
- const matched = aliasedMatch.matched
146
- const aliasedRecord = matched[matched.length - 1]
147
- location.params = aliasedMatch.params
148
- return _createRoute(aliasedRecord, location)
149
- }
150
- return _createRoute(null, location)
151
- }
152
-
153
- function _createRoute (
154
- record: ?RouteRecord,
155
- location: Location,
156
- redirectedFrom?: Location
157
- ): Route {
158
- if (record && record.redirect) {
159
- return redirect(record, redirectedFrom || location)
160
- }
161
- if (record && record.matchAs) {
162
- return alias(record, location, record.matchAs)
163
- }
164
- return createRoute(record, location, redirectedFrom, router)
165
- }
166
-
167
- return {
168
- match,
169
- addRoutes
170
- }
171
- }
172
-
173
- function matchRoute (
174
- regex: RouteRegExp,
175
- path: string,
176
- params: Object
177
- ): boolean {
178
- const m = path.match(regex)
179
-
180
- if (!m) {
181
- return false
182
- } else if (!params) {
183
- return true
184
- }
185
-
186
- for (let i = 1, len = m.length; i < len; ++i) {
187
- const key = regex.keys[i - 1]
188
- const val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i]
189
- if (key) {
190
- // Fix #1994: using * with props: true generates a param named 0
191
- params[key.name || 'pathMatch'] = val
192
- }
193
- }
194
-
195
- return true
196
- }
197
-
198
- function resolveRecordPath (path: string, record: RouteRecord): string {
199
- return resolvePath(path, record.parent ? record.parent.path : '/', true)
200
- }