kdu-router 2.7.0 → 3.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.
- package/LICENSE +1 -1
- package/README.md +2 -25
- package/dist/kdu-router.common.js +164 -57
- package/dist/kdu-router.esm.js +164 -57
- package/dist/kdu-router.js +164 -57
- package/dist/kdu-router.min.js +2 -2
- package/package.json +22 -37
- package/types/index.d.ts +13 -18
- package/types/kdu.d.ts +2 -3
- package/types/router.d.ts +5 -5
- package/src/components/link.js +0 -138
- package/src/components/view.js +0 -96
- package/src/create-matcher.js +0 -201
- package/src/create-route-map.js +0 -165
- package/src/history/abstract.js +0 -51
- package/src/history/base.js +0 -328
- package/src/history/hash.js +0 -97
- package/src/history/html5.js +0 -69
- package/src/index.js +0 -235
- package/src/install.js +0 -52
- package/src/util/async.js +0 -18
- package/src/util/dom.js +0 -3
- package/src/util/location.js +0 -68
- package/src/util/params.js +0 -26
- package/src/util/path.js +0 -74
- package/src/util/push-state.js +0 -59
- package/src/util/query.js +0 -96
- package/src/util/resolve-components.js +0 -100
- package/src/util/route.js +0 -110
- package/src/util/scroll.js +0 -111
- package/src/util/warn.js +0 -17
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kdu-router",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Official router for Kdu",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Official router for Kdu.js 2",
|
|
5
5
|
"author": "NKDuy",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "dist/kdu-router.common.js",
|
|
@@ -9,11 +9,10 @@
|
|
|
9
9
|
"unpkg": "dist/kdu-router.js",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/
|
|
12
|
+
"url": "https://github.com/kdujs/kdu-router.git"
|
|
13
13
|
},
|
|
14
14
|
"typings": "types/index.d.ts",
|
|
15
15
|
"files": [
|
|
16
|
-
"src",
|
|
17
16
|
"dist/*.js",
|
|
18
17
|
"types/*.d.ts"
|
|
19
18
|
],
|
|
@@ -23,57 +22,43 @@
|
|
|
23
22
|
"routing"
|
|
24
23
|
],
|
|
25
24
|
"scripts": {
|
|
26
|
-
"dev": "
|
|
27
|
-
"dev:dist": "rollup -wm -c build/dev.config.js",
|
|
25
|
+
"dev:dist": "rollup -wm -c build/rollup.dev.config.js",
|
|
28
26
|
"build": "node build/build.js",
|
|
29
|
-
"lint": "eslint src
|
|
30
|
-
"test": "npm run lint && flow check && npm run test:unit && npm run test:e2e && npm run test:types",
|
|
31
|
-
"test:unit": "jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
|
|
32
|
-
"test:e2e": "node test/e2e/runner.js",
|
|
33
|
-
"test:types": "tsc -p types/test",
|
|
34
|
-
"docs": "cd docs && gitbook install && gitbook serve",
|
|
35
|
-
"docs:deploy": "bash ./build/update-docs.sh",
|
|
27
|
+
"lint": "eslint src",
|
|
36
28
|
"release": "bash build/release.sh"
|
|
37
29
|
},
|
|
38
30
|
"devDependencies": {
|
|
39
31
|
"babel-core": "^6.24.1",
|
|
40
|
-
"babel-eslint": "^
|
|
32
|
+
"babel-eslint": "^8.0.1",
|
|
41
33
|
"babel-loader": "^7.0.0",
|
|
42
34
|
"babel-plugin-syntax-dynamic-import": "^6.18.0",
|
|
43
35
|
"babel-preset-es2015": "^6.24.1",
|
|
44
36
|
"babel-preset-flow-kdu": "^1.0.0",
|
|
45
|
-
"buble": "^0.
|
|
46
|
-
"chromedriver": "^2.
|
|
37
|
+
"buble": "^0.16.0",
|
|
38
|
+
"chromedriver": "^2.33.1",
|
|
47
39
|
"cross-spawn": "^5.0.1",
|
|
48
40
|
"css-loader": "^0.28.0",
|
|
49
41
|
"es6-promise": "^4.0.5",
|
|
50
|
-
"eslint": "^3.0
|
|
42
|
+
"eslint": "^3.19.0",
|
|
51
43
|
"eslint-plugin-flowtype": "^2.34.0",
|
|
52
|
-
"eslint-plugin-kdu-libs": "^1.
|
|
53
|
-
"express": "^4.
|
|
44
|
+
"eslint-plugin-kdu-libs": "^1.2.1",
|
|
45
|
+
"express": "^4.16.2",
|
|
54
46
|
"express-urlrewrite": "^1.2.0",
|
|
55
|
-
"flow-bin": "^0.
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"phantomjs-prebuilt": "^2.1.14",
|
|
63
|
-
"rollup": "^0.43.0",
|
|
64
|
-
"rollup-plugin-buble": "^0.15.0",
|
|
65
|
-
"rollup-plugin-commonjs": "^8.0.2",
|
|
47
|
+
"flow-bin": "^0.56.0",
|
|
48
|
+
"jasmine": "2.8.0",
|
|
49
|
+
"kdu": "^2.5.0",
|
|
50
|
+
"path-to-regexp": "^1.7.0",
|
|
51
|
+
"rollup": "^0.50.0",
|
|
52
|
+
"rollup-plugin-buble": "^0.16.0",
|
|
53
|
+
"rollup-plugin-commonjs": "^8.2.1",
|
|
66
54
|
"rollup-plugin-flow-no-whitespace": "^1.0.0",
|
|
67
55
|
"rollup-plugin-node-resolve": "^3.0.0",
|
|
68
|
-
"rollup-plugin-replace": "^
|
|
56
|
+
"rollup-plugin-replace": "^2.0.0",
|
|
69
57
|
"rollup-watch": "^4.0.0",
|
|
70
58
|
"selenium-server": "^2.53.1",
|
|
71
|
-
"typescript": "^2.
|
|
72
|
-
"uglify-js": "^3.
|
|
73
|
-
"
|
|
74
|
-
"kdu-loader": "^13.5.0",
|
|
75
|
-
"kdu-template-compiler": "^2.5.13",
|
|
76
|
-
"webpack": "^2.4.1",
|
|
59
|
+
"typescript": "^2.5.3",
|
|
60
|
+
"uglify-js": "^3.1.3",
|
|
61
|
+
"webpack": "^3.7.1",
|
|
77
62
|
"webpack-dev-middleware": "^1.9.0"
|
|
78
63
|
}
|
|
79
64
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
import "./kdu";
|
|
2
|
-
import
|
|
2
|
+
import { KduRouter } from "./router";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
// All available types are exported via this namespace
|
|
6
|
-
declare namespace KduRouter {
|
|
7
|
-
export type RouterMode = R.RouterMode;
|
|
8
|
-
export type RawLocation = R.RawLocation;
|
|
9
|
-
export type RedirectOption = R.RedirectOption;
|
|
10
|
-
export type RouterOptions = R.RouterOptions;
|
|
11
|
-
export type RouteConfig = R.RouteConfig;
|
|
12
|
-
export type RouteRecord = R.RouteRecord;
|
|
13
|
-
export type Location = R.Location;
|
|
14
|
-
export type Route = R.Route;
|
|
15
|
-
export type NavigationGuard = R.NavigationGuard;
|
|
16
|
-
}
|
|
4
|
+
export default KduRouter;
|
|
17
5
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
6
|
+
export {
|
|
7
|
+
RouterMode,
|
|
8
|
+
RawLocation,
|
|
9
|
+
RedirectOption,
|
|
10
|
+
RouterOptions,
|
|
11
|
+
RouteConfig,
|
|
12
|
+
RouteRecord,
|
|
13
|
+
Location,
|
|
14
|
+
Route,
|
|
15
|
+
NavigationGuard
|
|
16
|
+
} from "./router";
|
package/types/kdu.d.ts
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
* Augment the typings of Kdu.js
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import Kdu
|
|
6
|
-
import KduRouter
|
|
7
|
-
import { Route, RawLocation, NavigationGuard } from "./index";
|
|
5
|
+
import Kdu from "kdu";
|
|
6
|
+
import KduRouter, { Route, RawLocation, NavigationGuard } from "./index";
|
|
8
7
|
|
|
9
8
|
declare module "kdu/types/kdu" {
|
|
10
9
|
interface Kdu {
|
package/types/router.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import Kdu
|
|
2
|
-
import { ComponentOptions, PluginFunction } from "types/kdu";
|
|
1
|
+
import Kdu, { ComponentOptions, PluginFunction, AsyncComponent } from "kdu";
|
|
3
2
|
|
|
4
|
-
type Component = ComponentOptions<Kdu> | typeof Kdu;
|
|
3
|
+
type Component = ComponentOptions<Kdu> | typeof Kdu | AsyncComponent;
|
|
5
4
|
type Dictionary<T> = { [key: string]: T };
|
|
6
5
|
|
|
7
6
|
export type RouterMode = "hash" | "history" | "abstract";
|
|
@@ -13,7 +12,7 @@ export type NavigationGuard = (
|
|
|
13
12
|
next: (to?: RawLocation | false | ((vm: Kdu) => any) | void) => void
|
|
14
13
|
) => any
|
|
15
14
|
|
|
16
|
-
declare class KduRouter {
|
|
15
|
+
export declare class KduRouter {
|
|
17
16
|
constructor (options?: RouterOptions);
|
|
18
17
|
|
|
19
18
|
app: Kdu;
|
|
@@ -45,6 +44,7 @@ declare class KduRouter {
|
|
|
45
44
|
}
|
|
46
45
|
|
|
47
46
|
type Position = { x: number, y: number };
|
|
47
|
+
type PositionResult = Position | { selector: string, offset?: Position } | void;
|
|
48
48
|
|
|
49
49
|
export interface RouterOptions {
|
|
50
50
|
routes?: RouteConfig[];
|
|
@@ -59,7 +59,7 @@ export interface RouterOptions {
|
|
|
59
59
|
to: Route,
|
|
60
60
|
from: Route,
|
|
61
61
|
savedPosition: Position | void
|
|
62
|
-
) =>
|
|
62
|
+
) => PositionResult | Promise<PositionResult>;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
type RoutePropsFunction = (route: Route) => Object;
|
package/src/components/link.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/* @flow */
|
|
2
|
-
|
|
3
|
-
import { createRoute, isSameRoute, isIncludedRoute } from '../util/route'
|
|
4
|
-
import { _Kdu } from '../install'
|
|
5
|
-
|
|
6
|
-
// work around weird flow bug
|
|
7
|
-
const toTypes: Array<Function> = [String, Object]
|
|
8
|
-
const eventTypes: Array<Function> = [String, Array]
|
|
9
|
-
|
|
10
|
-
export default {
|
|
11
|
-
name: 'router-link',
|
|
12
|
-
props: {
|
|
13
|
-
to: {
|
|
14
|
-
type: toTypes,
|
|
15
|
-
required: true
|
|
16
|
-
},
|
|
17
|
-
tag: {
|
|
18
|
-
type: String,
|
|
19
|
-
default: 'a'
|
|
20
|
-
},
|
|
21
|
-
exact: Boolean,
|
|
22
|
-
append: Boolean,
|
|
23
|
-
replace: Boolean,
|
|
24
|
-
activeClass: String,
|
|
25
|
-
exactActiveClass: String,
|
|
26
|
-
event: {
|
|
27
|
-
type: eventTypes,
|
|
28
|
-
default: 'click'
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
render (h: Function) {
|
|
32
|
-
const router = this.$router
|
|
33
|
-
const current = this.$route
|
|
34
|
-
const { location, route, href } = router.resolve(this.to, current, this.append)
|
|
35
|
-
|
|
36
|
-
const classes = {}
|
|
37
|
-
const globalActiveClass = router.options.linkActiveClass
|
|
38
|
-
const globalExactActiveClass = router.options.linkExactActiveClass
|
|
39
|
-
// Support global empty active class
|
|
40
|
-
const activeClassFallback = globalActiveClass == null
|
|
41
|
-
? 'router-link-active'
|
|
42
|
-
: globalActiveClass
|
|
43
|
-
const exactActiveClassFallback = globalExactActiveClass == null
|
|
44
|
-
? 'router-link-exact-active'
|
|
45
|
-
: globalExactActiveClass
|
|
46
|
-
const activeClass = this.activeClass == null
|
|
47
|
-
? activeClassFallback
|
|
48
|
-
: this.activeClass
|
|
49
|
-
const exactActiveClass = this.exactActiveClass == null
|
|
50
|
-
? exactActiveClassFallback
|
|
51
|
-
: this.exactActiveClass
|
|
52
|
-
const compareTarget = location.path
|
|
53
|
-
? createRoute(null, location, null, router)
|
|
54
|
-
: route
|
|
55
|
-
|
|
56
|
-
classes[exactActiveClass] = isSameRoute(current, compareTarget)
|
|
57
|
-
classes[activeClass] = this.exact
|
|
58
|
-
? classes[exactActiveClass]
|
|
59
|
-
: isIncludedRoute(current, compareTarget)
|
|
60
|
-
|
|
61
|
-
const handler = e => {
|
|
62
|
-
if (guardEvent(e)) {
|
|
63
|
-
if (this.replace) {
|
|
64
|
-
router.replace(location)
|
|
65
|
-
} else {
|
|
66
|
-
router.push(location)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const on = { click: guardEvent }
|
|
72
|
-
if (Array.isArray(this.event)) {
|
|
73
|
-
this.event.forEach(e => { on[e] = handler })
|
|
74
|
-
} else {
|
|
75
|
-
on[this.event] = handler
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const data: any = {
|
|
79
|
-
class: classes
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (this.tag === 'a') {
|
|
83
|
-
data.on = on
|
|
84
|
-
data.attrs = { href }
|
|
85
|
-
} else {
|
|
86
|
-
// find the first <a> child and apply listener and href
|
|
87
|
-
const a = findAnchor(this.$slots.default)
|
|
88
|
-
if (a) {
|
|
89
|
-
// in case the <a> is a static node
|
|
90
|
-
a.isStatic = false
|
|
91
|
-
const extend = _Kdu.util.extend
|
|
92
|
-
const aData = a.data = extend({}, a.data)
|
|
93
|
-
aData.on = on
|
|
94
|
-
const aAttrs = a.data.attrs = extend({}, a.data.attrs)
|
|
95
|
-
aAttrs.href = href
|
|
96
|
-
} else {
|
|
97
|
-
// doesn't have <a> child, apply listener to self
|
|
98
|
-
data.on = on
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return h(this.tag, data, this.$slots.default)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function guardEvent (e) {
|
|
107
|
-
// don't redirect with control keys
|
|
108
|
-
if (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) return
|
|
109
|
-
// don't redirect when preventDefault called
|
|
110
|
-
if (e.defaultPrevented) return
|
|
111
|
-
// don't redirect on right click
|
|
112
|
-
if (e.button !== undefined && e.button !== 0) return
|
|
113
|
-
// don't redirect if `target="_blank"`
|
|
114
|
-
if (e.currentTarget && e.currentTarget.getAttribute) {
|
|
115
|
-
const target = e.currentTarget.getAttribute('target')
|
|
116
|
-
if (/\b_blank\b/i.test(target)) return
|
|
117
|
-
}
|
|
118
|
-
// this may be a Weex event which doesn't have this method
|
|
119
|
-
if (e.preventDefault) {
|
|
120
|
-
e.preventDefault()
|
|
121
|
-
}
|
|
122
|
-
return true
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function findAnchor (children) {
|
|
126
|
-
if (children) {
|
|
127
|
-
let child
|
|
128
|
-
for (let i = 0; i < children.length; i++) {
|
|
129
|
-
child = children[i]
|
|
130
|
-
if (child.tag === 'a') {
|
|
131
|
-
return child
|
|
132
|
-
}
|
|
133
|
-
if (child.children && (child = findAnchor(child.children))) {
|
|
134
|
-
return child
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
package/src/components/view.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { warn } from '../util/warn'
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'router-view',
|
|
5
|
-
functional: true,
|
|
6
|
-
props: {
|
|
7
|
-
name: {
|
|
8
|
-
type: String,
|
|
9
|
-
default: 'default'
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
render (_, { props, children, parent, data }) {
|
|
13
|
-
data.routerView = true
|
|
14
|
-
|
|
15
|
-
// directly use parent context's createElement() function
|
|
16
|
-
// so that components rendered by router-view can resolve named slots
|
|
17
|
-
const h = parent.$createElement
|
|
18
|
-
const name = props.name
|
|
19
|
-
const route = parent.$route
|
|
20
|
-
const cache = parent._routerViewCache || (parent._routerViewCache = {})
|
|
21
|
-
|
|
22
|
-
// determine current view depth, also check to see if the tree
|
|
23
|
-
// has been toggled inactive but kept-alive.
|
|
24
|
-
let depth = 0
|
|
25
|
-
let inactive = false
|
|
26
|
-
while (parent && parent._routerRoot !== parent) {
|
|
27
|
-
if (parent.$vnode && parent.$vnode.data.routerView) {
|
|
28
|
-
depth++
|
|
29
|
-
}
|
|
30
|
-
if (parent._inactive) {
|
|
31
|
-
inactive = true
|
|
32
|
-
}
|
|
33
|
-
parent = parent.$parent
|
|
34
|
-
}
|
|
35
|
-
data.routerViewDepth = depth
|
|
36
|
-
|
|
37
|
-
// render previous view if the tree is inactive and kept-alive
|
|
38
|
-
if (inactive) {
|
|
39
|
-
return h(cache[name], data, children)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const matched = route.matched[depth]
|
|
43
|
-
// render empty node if no matched route
|
|
44
|
-
if (!matched) {
|
|
45
|
-
cache[name] = null
|
|
46
|
-
return h()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const component = cache[name] = matched.components[name]
|
|
50
|
-
|
|
51
|
-
// attach instance registration hook
|
|
52
|
-
// this will be called in the instance's injected lifecycle hooks
|
|
53
|
-
data.registerRouteInstance = (vm, val) => {
|
|
54
|
-
// val could be undefined for unregistration
|
|
55
|
-
const current = matched.instances[name]
|
|
56
|
-
if (
|
|
57
|
-
(val && current !== vm) ||
|
|
58
|
-
(!val && current === vm)
|
|
59
|
-
) {
|
|
60
|
-
matched.instances[name] = val
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// also regiseter instance in prepatch hook
|
|
65
|
-
// in case the same component instance is reused across different routes
|
|
66
|
-
;(data.hook || (data.hook = {})).prepatch = (_, vnode) => {
|
|
67
|
-
matched.instances[name] = vnode.componentInstance
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// resolve props
|
|
71
|
-
data.props = resolveProps(route, matched.props && matched.props[name])
|
|
72
|
-
|
|
73
|
-
return h(component, data, children)
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function resolveProps (route, config) {
|
|
78
|
-
switch (typeof config) {
|
|
79
|
-
case 'undefined':
|
|
80
|
-
return
|
|
81
|
-
case 'object':
|
|
82
|
-
return config
|
|
83
|
-
case 'function':
|
|
84
|
-
return config(route)
|
|
85
|
-
case 'boolean':
|
|
86
|
-
return config ? route.params : undefined
|
|
87
|
-
default:
|
|
88
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
89
|
-
warn(
|
|
90
|
-
false,
|
|
91
|
-
`props in "${route.path}" is a ${typeof config}, ` +
|
|
92
|
-
`expecting an object, function or boolean.`
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
package/src/create-matcher.js
DELETED
|
@@ -1,201 +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
|
-
if (record) {
|
|
57
|
-
location.path = fillParams(record.path, location.params, `named route "${name}"`)
|
|
58
|
-
return _createRoute(record, location, redirectedFrom)
|
|
59
|
-
}
|
|
60
|
-
} else if (location.path) {
|
|
61
|
-
location.params = {}
|
|
62
|
-
for (let i = 0; i < pathList.length; i++) {
|
|
63
|
-
const path = pathList[i]
|
|
64
|
-
const record = pathMap[path]
|
|
65
|
-
if (matchRoute(record.regex, location.path, location.params)) {
|
|
66
|
-
return _createRoute(record, location, redirectedFrom)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
// no match
|
|
71
|
-
return _createRoute(null, location)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function redirect (
|
|
75
|
-
record: RouteRecord,
|
|
76
|
-
location: Location
|
|
77
|
-
): Route {
|
|
78
|
-
const originalRedirect = record.redirect
|
|
79
|
-
let redirect = typeof originalRedirect === 'function'
|
|
80
|
-
? originalRedirect(createRoute(record, location, null, router))
|
|
81
|
-
: originalRedirect
|
|
82
|
-
|
|
83
|
-
if (typeof redirect === 'string') {
|
|
84
|
-
redirect = { path: redirect }
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (!redirect || typeof redirect !== 'object') {
|
|
88
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
89
|
-
warn(
|
|
90
|
-
false, `invalid redirect option: ${JSON.stringify(redirect)}`
|
|
91
|
-
)
|
|
92
|
-
}
|
|
93
|
-
return _createRoute(null, location)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const re: Object = redirect
|
|
97
|
-
const { name, path } = re
|
|
98
|
-
let { query, hash, params } = location
|
|
99
|
-
query = re.hasOwnProperty('query') ? re.query : query
|
|
100
|
-
hash = re.hasOwnProperty('hash') ? re.hash : hash
|
|
101
|
-
params = re.hasOwnProperty('params') ? re.params : params
|
|
102
|
-
|
|
103
|
-
if (name) {
|
|
104
|
-
// resolved named direct
|
|
105
|
-
const targetRecord = nameMap[name]
|
|
106
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
107
|
-
assert(targetRecord, `redirect failed: named route "${name}" not found.`)
|
|
108
|
-
}
|
|
109
|
-
return match({
|
|
110
|
-
_normalized: true,
|
|
111
|
-
name,
|
|
112
|
-
query,
|
|
113
|
-
hash,
|
|
114
|
-
params
|
|
115
|
-
}, undefined, location)
|
|
116
|
-
} else if (path) {
|
|
117
|
-
// 1. resolve relative redirect
|
|
118
|
-
const rawPath = resolveRecordPath(path, record)
|
|
119
|
-
// 2. resolve params
|
|
120
|
-
const resolvedPath = fillParams(rawPath, params, `redirect route with path "${rawPath}"`)
|
|
121
|
-
// 3. rematch with existing query and hash
|
|
122
|
-
return match({
|
|
123
|
-
_normalized: true,
|
|
124
|
-
path: resolvedPath,
|
|
125
|
-
query,
|
|
126
|
-
hash
|
|
127
|
-
}, undefined, location)
|
|
128
|
-
} else {
|
|
129
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
130
|
-
warn(false, `invalid redirect option: ${JSON.stringify(redirect)}`)
|
|
131
|
-
}
|
|
132
|
-
return _createRoute(null, location)
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function alias (
|
|
137
|
-
record: RouteRecord,
|
|
138
|
-
location: Location,
|
|
139
|
-
matchAs: string
|
|
140
|
-
): Route {
|
|
141
|
-
const aliasedPath = fillParams(matchAs, location.params, `aliased route with path "${matchAs}"`)
|
|
142
|
-
const aliasedMatch = match({
|
|
143
|
-
_normalized: true,
|
|
144
|
-
path: aliasedPath
|
|
145
|
-
})
|
|
146
|
-
if (aliasedMatch) {
|
|
147
|
-
const matched = aliasedMatch.matched
|
|
148
|
-
const aliasedRecord = matched[matched.length - 1]
|
|
149
|
-
location.params = aliasedMatch.params
|
|
150
|
-
return _createRoute(aliasedRecord, location)
|
|
151
|
-
}
|
|
152
|
-
return _createRoute(null, location)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function _createRoute (
|
|
156
|
-
record: ?RouteRecord,
|
|
157
|
-
location: Location,
|
|
158
|
-
redirectedFrom?: Location
|
|
159
|
-
): Route {
|
|
160
|
-
if (record && record.redirect) {
|
|
161
|
-
return redirect(record, redirectedFrom || location)
|
|
162
|
-
}
|
|
163
|
-
if (record && record.matchAs) {
|
|
164
|
-
return alias(record, location, record.matchAs)
|
|
165
|
-
}
|
|
166
|
-
return createRoute(record, location, redirectedFrom, router)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return {
|
|
170
|
-
match,
|
|
171
|
-
addRoutes
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function matchRoute (
|
|
176
|
-
regex: RouteRegExp,
|
|
177
|
-
path: string,
|
|
178
|
-
params: Object
|
|
179
|
-
): boolean {
|
|
180
|
-
const m = path.match(regex)
|
|
181
|
-
|
|
182
|
-
if (!m) {
|
|
183
|
-
return false
|
|
184
|
-
} else if (!params) {
|
|
185
|
-
return true
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
for (let i = 1, len = m.length; i < len; ++i) {
|
|
189
|
-
const key = regex.keys[i - 1]
|
|
190
|
-
const val = typeof m[i] === 'string' ? decodeURIComponent(m[i]) : m[i]
|
|
191
|
-
if (key) {
|
|
192
|
-
params[key.name] = val
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return true
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function resolveRecordPath (path: string, record: RouteRecord): string {
|
|
200
|
-
return resolvePath(path, record.parent ? record.parent.path : '/', true)
|
|
201
|
-
}
|