kdu-router 3.1.3 → 3.4.0-beta.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.
@@ -1,205 +1,205 @@
1
- /* @flow */
2
-
3
- import Regexp from 'path-to-regexp'
4
- import { cleanPath } from './util/path'
5
- import { assert, warn } from './util/warn'
6
-
7
- export function createRouteMap (
8
- routes: Array<RouteConfig>,
9
- oldPathList?: Array<string>,
10
- oldPathMap?: Dictionary<RouteRecord>,
11
- oldNameMap?: Dictionary<RouteRecord>
12
- ): {
13
- pathList: Array<string>,
14
- pathMap: Dictionary<RouteRecord>,
15
- nameMap: Dictionary<RouteRecord>
16
- } {
17
- // the path list is used to control path matching priority
18
- const pathList: Array<string> = oldPathList || []
19
- // $flow-disable-line
20
- const pathMap: Dictionary<RouteRecord> = oldPathMap || Object.create(null)
21
- // $flow-disable-line
22
- const nameMap: Dictionary<RouteRecord> = oldNameMap || Object.create(null)
23
-
24
- routes.forEach(route => {
25
- addRouteRecord(pathList, pathMap, nameMap, route)
26
- })
27
-
28
- // ensure wildcard routes are always at the end
29
- for (let i = 0, l = pathList.length; i < l; i++) {
30
- if (pathList[i] === '*') {
31
- pathList.push(pathList.splice(i, 1)[0])
32
- l--
33
- i--
34
- }
35
- }
36
-
37
- if (process.env.NODE_ENV === 'development') {
38
- // warn if routes do not include leading slashes
39
- const found = pathList
40
- // check for missing leading slash
41
- .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')
42
-
43
- if (found.length > 0) {
44
- const pathNames = found.map(path => `- ${path}`).join('\n')
45
- warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
46
- }
47
- }
48
-
49
- return {
50
- pathList,
51
- pathMap,
52
- nameMap
53
- }
54
- }
55
-
56
- function addRouteRecord (
57
- pathList: Array<string>,
58
- pathMap: Dictionary<RouteRecord>,
59
- nameMap: Dictionary<RouteRecord>,
60
- route: RouteConfig,
61
- parent?: RouteRecord,
62
- matchAs?: string
63
- ) {
64
- const { path, name } = route
65
- if (process.env.NODE_ENV !== 'production') {
66
- assert(path != null, `"path" is required in a route configuration.`)
67
- assert(
68
- typeof route.component !== 'string',
69
- `route config "component" for path: ${String(
70
- path || name
71
- )} cannot be a ` + `string id. Use an actual component instead.`
72
- )
73
- }
74
-
75
- const pathToRegexpOptions: PathToRegexpOptions =
76
- route.pathToRegexpOptions || {}
77
- const normalizedPath = normalizePath(path, parent, pathToRegexpOptions.strict)
78
-
79
- if (typeof route.caseSensitive === 'boolean') {
80
- pathToRegexpOptions.sensitive = route.caseSensitive
81
- }
82
-
83
- const record: RouteRecord = {
84
- path: normalizedPath,
85
- regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
86
- components: route.components || { default: route.component },
87
- instances: {},
88
- name,
89
- parent,
90
- matchAs,
91
- redirect: route.redirect,
92
- beforeEnter: route.beforeEnter,
93
- meta: route.meta || {},
94
- props:
95
- route.props == null
96
- ? {}
97
- : route.components
98
- ? route.props
99
- : { default: route.props }
100
- }
101
-
102
- if (route.children) {
103
- // Warn if route is named, does not redirect and has a default child route.
104
- // If users navigate to this route by name, the default child will
105
- // not be rendered (GH Issue #629)
106
- if (process.env.NODE_ENV !== 'production') {
107
- if (
108
- route.name &&
109
- !route.redirect &&
110
- route.children.some(child => /^\/?$/.test(child.path))
111
- ) {
112
- warn(
113
- false,
114
- `Named Route '${route.name}' has a default child route. ` +
115
- `When navigating to this named route (:to="{name: '${
116
- route.name
117
- }'"), ` +
118
- `the default child route will not be rendered. Remove the name from ` +
119
- `this route and use the name of the default child route for named ` +
120
- `links instead.`
121
- )
122
- }
123
- }
124
- route.children.forEach(child => {
125
- const childMatchAs = matchAs
126
- ? cleanPath(`${matchAs}/${child.path}`)
127
- : undefined
128
- addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
129
- })
130
- }
131
-
132
- if (!pathMap[record.path]) {
133
- pathList.push(record.path)
134
- pathMap[record.path] = record
135
- }
136
-
137
- if (route.alias !== undefined) {
138
- const aliases = Array.isArray(route.alias) ? route.alias : [route.alias]
139
- for (let i = 0; i < aliases.length; ++i) {
140
- const alias = aliases[i]
141
- if (process.env.NODE_ENV !== 'production' && alias === path) {
142
- warn(
143
- false,
144
- `Found an alias with the same value as the path: "${path}". You have to remove that alias. It will be ignored in development.`
145
- )
146
- // skip in dev to make it work
147
- continue
148
- }
149
-
150
- const aliasRoute = {
151
- path: alias,
152
- children: route.children
153
- }
154
- addRouteRecord(
155
- pathList,
156
- pathMap,
157
- nameMap,
158
- aliasRoute,
159
- parent,
160
- record.path || '/' // matchAs
161
- )
162
- }
163
- }
164
-
165
- if (name) {
166
- if (!nameMap[name]) {
167
- nameMap[name] = record
168
- } else if (process.env.NODE_ENV !== 'production' && !matchAs) {
169
- warn(
170
- false,
171
- `Duplicate named routes definition: ` +
172
- `{ name: "${name}", path: "${record.path}" }`
173
- )
174
- }
175
- }
176
- }
177
-
178
- function compileRouteRegex (
179
- path: string,
180
- pathToRegexpOptions: PathToRegexpOptions
181
- ): RouteRegExp {
182
- const regex = Regexp(path, [], pathToRegexpOptions)
183
- if (process.env.NODE_ENV !== 'production') {
184
- const keys: any = Object.create(null)
185
- regex.keys.forEach(key => {
186
- warn(
187
- !keys[key.name],
188
- `Duplicate param keys in route with path: "${path}"`
189
- )
190
- keys[key.name] = true
191
- })
192
- }
193
- return regex
194
- }
195
-
196
- function normalizePath (
197
- path: string,
198
- parent?: RouteRecord,
199
- strict?: boolean
200
- ): string {
201
- if (!strict) path = path.replace(/\/$/, '')
202
- if (path[0] === '/') return path
203
- if (parent == null) return path
204
- return cleanPath(`${parent.path}/${path}`)
205
- }
1
+ /* @flow */
2
+
3
+ import Regexp from 'path-to-regexp'
4
+ import { cleanPath } from './util/path'
5
+ import { assert, warn } from './util/warn'
6
+
7
+ export function createRouteMap (
8
+ routes: Array<RouteConfig>,
9
+ oldPathList?: Array<string>,
10
+ oldPathMap?: Dictionary<RouteRecord>,
11
+ oldNameMap?: Dictionary<RouteRecord>
12
+ ): {
13
+ pathList: Array<string>,
14
+ pathMap: Dictionary<RouteRecord>,
15
+ nameMap: Dictionary<RouteRecord>
16
+ } {
17
+ // the path list is used to control path matching priority
18
+ const pathList: Array<string> = oldPathList || []
19
+ // $flow-disable-line
20
+ const pathMap: Dictionary<RouteRecord> = oldPathMap || Object.create(null)
21
+ // $flow-disable-line
22
+ const nameMap: Dictionary<RouteRecord> = oldNameMap || Object.create(null)
23
+
24
+ routes.forEach(route => {
25
+ addRouteRecord(pathList, pathMap, nameMap, route)
26
+ })
27
+
28
+ // ensure wildcard routes are always at the end
29
+ for (let i = 0, l = pathList.length; i < l; i++) {
30
+ if (pathList[i] === '*') {
31
+ pathList.push(pathList.splice(i, 1)[0])
32
+ l--
33
+ i--
34
+ }
35
+ }
36
+
37
+ if (process.env.NODE_ENV === 'development') {
38
+ // warn if routes do not include leading slashes
39
+ const found = pathList
40
+ // check for missing leading slash
41
+ .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')
42
+
43
+ if (found.length > 0) {
44
+ const pathNames = found.map(path => `- ${path}`).join('\n')
45
+ warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
46
+ }
47
+ }
48
+
49
+ return {
50
+ pathList,
51
+ pathMap,
52
+ nameMap
53
+ }
54
+ }
55
+
56
+ function addRouteRecord (
57
+ pathList: Array<string>,
58
+ pathMap: Dictionary<RouteRecord>,
59
+ nameMap: Dictionary<RouteRecord>,
60
+ route: RouteConfig,
61
+ parent?: RouteRecord,
62
+ matchAs?: string
63
+ ) {
64
+ const { path, name } = route
65
+ if (process.env.NODE_ENV !== 'production') {
66
+ assert(path != null, `"path" is required in a route configuration.`)
67
+ assert(
68
+ typeof route.component !== 'string',
69
+ `route config "component" for path: ${String(
70
+ path || name
71
+ )} cannot be a ` + `string id. Use an actual component instead.`
72
+ )
73
+ }
74
+
75
+ const pathToRegexpOptions: PathToRegexpOptions =
76
+ route.pathToRegexpOptions || {}
77
+ const normalizedPath = normalizePath(path, parent, pathToRegexpOptions.strict)
78
+
79
+ if (typeof route.caseSensitive === 'boolean') {
80
+ pathToRegexpOptions.sensitive = route.caseSensitive
81
+ }
82
+
83
+ const record: RouteRecord = {
84
+ path: normalizedPath,
85
+ regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
86
+ components: route.components || { default: route.component },
87
+ instances: {},
88
+ name,
89
+ parent,
90
+ matchAs,
91
+ redirect: route.redirect,
92
+ beforeEnter: route.beforeEnter,
93
+ meta: route.meta || {},
94
+ props:
95
+ route.props == null
96
+ ? {}
97
+ : route.components
98
+ ? route.props
99
+ : { default: route.props }
100
+ }
101
+
102
+ if (route.children) {
103
+ // Warn if route is named, does not redirect and has a default child route.
104
+ // If users navigate to this route by name, the default child will
105
+ // not be rendered (GH Issue #629)
106
+ if (process.env.NODE_ENV !== 'production') {
107
+ if (
108
+ route.name &&
109
+ !route.redirect &&
110
+ route.children.some(child => /^\/?$/.test(child.path))
111
+ ) {
112
+ warn(
113
+ false,
114
+ `Named Route '${route.name}' has a default child route. ` +
115
+ `When navigating to this named route (:to="{name: '${
116
+ route.name
117
+ }'"), ` +
118
+ `the default child route will not be rendered. Remove the name from ` +
119
+ `this route and use the name of the default child route for named ` +
120
+ `links instead.`
121
+ )
122
+ }
123
+ }
124
+ route.children.forEach(child => {
125
+ const childMatchAs = matchAs
126
+ ? cleanPath(`${matchAs}/${child.path}`)
127
+ : undefined
128
+ addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
129
+ })
130
+ }
131
+
132
+ if (!pathMap[record.path]) {
133
+ pathList.push(record.path)
134
+ pathMap[record.path] = record
135
+ }
136
+
137
+ if (route.alias !== undefined) {
138
+ const aliases = Array.isArray(route.alias) ? route.alias : [route.alias]
139
+ for (let i = 0; i < aliases.length; ++i) {
140
+ const alias = aliases[i]
141
+ if (process.env.NODE_ENV !== 'production' && alias === path) {
142
+ warn(
143
+ false,
144
+ `Found an alias with the same value as the path: "${path}". You have to remove that alias. It will be ignored in development.`
145
+ )
146
+ // skip in dev to make it work
147
+ continue
148
+ }
149
+
150
+ const aliasRoute = {
151
+ path: alias,
152
+ children: route.children
153
+ }
154
+ addRouteRecord(
155
+ pathList,
156
+ pathMap,
157
+ nameMap,
158
+ aliasRoute,
159
+ parent,
160
+ record.path || '/' // matchAs
161
+ )
162
+ }
163
+ }
164
+
165
+ if (name) {
166
+ if (!nameMap[name]) {
167
+ nameMap[name] = record
168
+ } else if (process.env.NODE_ENV !== 'production' && !matchAs) {
169
+ warn(
170
+ false,
171
+ `Duplicate named routes definition: ` +
172
+ `{ name: "${name}", path: "${record.path}" }`
173
+ )
174
+ }
175
+ }
176
+ }
177
+
178
+ function compileRouteRegex (
179
+ path: string,
180
+ pathToRegexpOptions: PathToRegexpOptions
181
+ ): RouteRegExp {
182
+ const regex = Regexp(path, [], pathToRegexpOptions)
183
+ if (process.env.NODE_ENV !== 'production') {
184
+ const keys: any = Object.create(null)
185
+ regex.keys.forEach(key => {
186
+ warn(
187
+ !keys[key.name],
188
+ `Duplicate param keys in route with path: "${path}"`
189
+ )
190
+ keys[key.name] = true
191
+ })
192
+ }
193
+ return regex
194
+ }
195
+
196
+ function normalizePath (
197
+ path: string,
198
+ parent?: RouteRecord,
199
+ strict?: boolean
200
+ ): string {
201
+ if (!strict) path = path.replace(/\/$/, '')
202
+ if (path[0] === '/') return path
203
+ if (parent == null) return path
204
+ return cleanPath(`${parent.path}/${path}`)
205
+ }
@@ -0,0 +1,68 @@
1
+ /* @flow */
2
+
3
+ import type Router from '../index'
4
+ import { History } from './base'
5
+ import { NavigationFailureType, isNavigationFailure } from '../util/errors'
6
+
7
+ export class AbstractHistory extends History {
8
+ index: number
9
+ stack: Array<Route>
10
+
11
+ constructor (router: Router, base: ?string) {
12
+ super(router, base)
13
+ this.stack = []
14
+ this.index = -1
15
+ }
16
+
17
+ push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
18
+ this.transitionTo(
19
+ location,
20
+ route => {
21
+ this.stack = this.stack.slice(0, this.index + 1).concat(route)
22
+ this.index++
23
+ onComplete && onComplete(route)
24
+ },
25
+ onAbort
26
+ )
27
+ }
28
+
29
+ replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
30
+ this.transitionTo(
31
+ location,
32
+ route => {
33
+ this.stack = this.stack.slice(0, this.index).concat(route)
34
+ onComplete && onComplete(route)
35
+ },
36
+ onAbort
37
+ )
38
+ }
39
+
40
+ go (n: number) {
41
+ const targetIndex = this.index + n
42
+ if (targetIndex < 0 || targetIndex >= this.stack.length) {
43
+ return
44
+ }
45
+ const route = this.stack[targetIndex]
46
+ this.confirmTransition(
47
+ route,
48
+ () => {
49
+ this.index = targetIndex
50
+ this.updateRoute(route)
51
+ },
52
+ err => {
53
+ if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
54
+ this.index = targetIndex
55
+ }
56
+ }
57
+ )
58
+ }
59
+
60
+ getCurrentLocation () {
61
+ const current = this.stack[this.stack.length - 1]
62
+ return current ? current.fullPath : '/'
63
+ }
64
+
65
+ ensureURL () {
66
+ // noop
67
+ }
68
+ }