forked from givingwu/vue-mfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnhancedRouter.js
180 lines (157 loc) · 5.18 KB
/
EnhancedRouter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import VueRouter from 'vue-router'
import { isDev, isString, isObject, getWarning } from '../utils'
import { findRoute, completePath } from '../utils/route'
/**
* @class EnhancedRouter
* @description Dynamically add child routes to an existing route & provides some `helpers` method
*/
export default class EnhancedRouter {
static warn() {
return getWarning(EnhancedRouter.name)(arguments)
}
constructor(router) {
if (router.addRoutes !== this.addRoutes) {
router.addRoutes = this.addRoutes.bind(this)
}
this.router = router
this.routes = router.options.routes
this.pathMap = {}
this.pathList = []
this._init()
}
_init() {
this.refreshAndCheckState(this.routes)
}
/**
* @description Add new routes into current router, and supports dynamic nest
* @see
* + [Dynamically add child routes to an existing route](https://github.com/vuejs/vue-router/issues/1156)
* + [Feature request: replace routes dynamically](https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465)
* @param {Array<Route>} routes VueRoute route option
* @param {?String} parentPath
*/
addRoutes(routes, parentPath) {
if (isDev) {
console.log(this.pathList)
console.log(this.pathMap)
}
this.refreshAndCheckState(routes, parentPath)
this.router.matcher = new VueRouter(
this.normalizeOptions(this.router.options, { routes }, parentPath)
).matcher
}
/**
* @description normalize the options between oldRouter and newRouter with diff config options
* @param {Object} oldOpts oldRouter VueRouter configuration options
* @param {Object} newOpts newROuter VueRouter configuration options
* @param {?String} parentPath
* @returns {Object}
*/
normalizeOptions(oldOpts, newOpts, parentPath) {
const { routes: oldRoutes, ...oldProps } = oldOpts
const { routes: newRoutes, ...newProps } = newOpts
return Object.assign(
{
routes: this.mergeRoutes(oldRoutes, newRoutes, parentPath)
},
newProps,
oldProps
)
}
/**
* @description before merge new routes we need to check it out does its path or name duplicate in old routes
* @param {Array<Route>} oldRoutes
* @param {Array<Route>} newRoutes
* @param {?String} parentPath
* @returns {Array<Route>} oldRoutes
*/
mergeRoutes(oldRoutes, newRoutes, parentPath) {
const needMatchPath = parentPath
newRoutes.forEach((route) => {
if (isString(route.parentPath)) {
parentPath = route.parentPath
delete route.parentPath
} else {
parentPath = needMatchPath
}
if (isString(parentPath)) {
if (parentPath === '') {
oldRoutes.push(route)
} else {
const oldRoute = findRoute(oldRoutes, parentPath)
let path = route.path
if (oldRoute) {
;(oldRoute.children || (oldRoute.children = [])).push(
Object.assign({}, route, {
path:
parentPath && path.startsWith('/')
? (path = path.replace(/^\/*/, ''))
: path /* fix: @issue that nested paths that start with `/` will be treated as a root path */
})
)
}
}
} else {
oldRoutes.push(route)
}
})
return oldRoutes
}
/**
* @description 递归刷新路径 pathList 和 pathMap 并检查路由 path 和 name 是否重复
* @param {Array<Route>} newRoutes
* @param {String} parentPath
* 1. from method calls: addRoutes(routes, parentPath)
* 2. from route property: { path: '/bar', parentPath: '/foo', template: '<a href="/foo/bar">/foo/bar</a>' }
*/
refreshAndCheckState(routes, parentPath) {
routes.forEach(({ path, parentPath: selfParentPath, name, children }) => {
/* 优先匹配 route self parentPath */
if (selfParentPath) {
path = this.getParentPath(path, selfParentPath, name)
} else if (parentPath) {
path = this.getParentPath(path, parentPath, name)
}
if (path) {
if (!this.pathExists(path)) {
this.pathList.push(path)
} else {
EnhancedRouter.warn(`The path ${path} in pathList has been existed`)
}
}
if (name) {
if (!this.nameExists(name)) {
this.pathMap[name] = path
} else {
EnhancedRouter.warn(`The name ${name} in pathMap has been existed`)
}
}
if (children && children.length) {
return this.refreshAndCheckState(children, path)
}
})
}
getParentPath(path, parentPath, name) {
if (this.pathExists(parentPath)) {
return (path = completePath(path, parentPath))
} else {
EnhancedRouter.warn(
`Cannot found the parent path ${parentPath} ${
name ? 'of ' + name : ''
} in Vue-MFE MasterRouter`
)
return ''
}
}
pathExists(path) {
return this.pathList.includes(path)
}
nameExists(name) {
return this.pathMap[name]
}
findRoute(route) {
let path = (isString(route) && route) || (isObject(route) && route.path)
return (path && findRoute(this.routes, path)) || null
}
}
export const createEnhancedRouter = (router) => new EnhancedRouter(router)