kdu-router 3.5.4 → 4.0.16

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 +10 -4
  3. package/dist/kdu-router.cjs.js +3484 -0
  4. package/dist/kdu-router.cjs.prod.js +2759 -0
  5. package/dist/kdu-router.d.ts +1349 -0
  6. package/dist/kdu-router.esm-browser.js +3460 -0
  7. package/dist/kdu-router.esm-bundler.js +3478 -0
  8. package/dist/kdu-router.global.js +3625 -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 +77 -71
  13. package/dist/kdu-router.common.js +0 -3147
  14. package/dist/kdu-router.esm.browser.js +0 -3113
  15. package/dist/kdu-router.esm.browser.min.js +0 -11
  16. package/dist/kdu-router.esm.js +0 -3145
  17. package/dist/kdu-router.js +0 -3152
  18. package/dist/kdu-router.min.js +0 -11
  19. package/src/components/link.js +0 -224
  20. package/src/components/view.js +0 -155
  21. package/src/create-matcher.js +0 -226
  22. package/src/create-route-map.js +0 -220
  23. package/src/history/abstract.js +0 -72
  24. package/src/history/base.js +0 -379
  25. package/src/history/hash.js +0 -152
  26. package/src/history/html5.js +0 -99
  27. package/src/index.js +0 -293
  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 -86
  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 -113
  38. package/src/util/resolve-components.js +0 -109
  39. package/src/util/route.js +0 -151
  40. package/src/util/scroll.js +0 -175
  41. package/src/util/state-key.js +0 -22
  42. package/src/util/warn.js +0 -14
  43. package/types/index.d.ts +0 -21
  44. package/types/kdu.d.ts +0 -22
  45. package/types/router.d.ts +0 -211
@@ -1,220 +0,0 @@
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
- parentRoute?: RouteRecord
13
- ): {
14
- pathList: Array<string>,
15
- pathMap: Dictionary<RouteRecord>,
16
- nameMap: Dictionary<RouteRecord>
17
- } {
18
- // the path list is used to control path matching priority
19
- const pathList: Array<string> = oldPathList || []
20
- // $flow-disable-line
21
- const pathMap: Dictionary<RouteRecord> = oldPathMap || Object.create(null)
22
- // $flow-disable-line
23
- const nameMap: Dictionary<RouteRecord> = oldNameMap || Object.create(null)
24
-
25
- routes.forEach(route => {
26
- addRouteRecord(pathList, pathMap, nameMap, route, parentRoute)
27
- })
28
-
29
- // ensure wildcard routes are always at the end
30
- for (let i = 0, l = pathList.length; i < l; i++) {
31
- if (pathList[i] === '*') {
32
- pathList.push(pathList.splice(i, 1)[0])
33
- l--
34
- i--
35
- }
36
- }
37
-
38
- if (process.env.NODE_ENV === 'development') {
39
- // warn if routes do not include leading slashes
40
- const found = pathList
41
- // check for missing leading slash
42
- .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')
43
-
44
- if (found.length > 0) {
45
- const pathNames = found.map(path => `- ${path}`).join('\n')
46
- warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
47
- }
48
- }
49
-
50
- return {
51
- pathList,
52
- pathMap,
53
- nameMap
54
- }
55
- }
56
-
57
- function addRouteRecord (
58
- pathList: Array<string>,
59
- pathMap: Dictionary<RouteRecord>,
60
- nameMap: Dictionary<RouteRecord>,
61
- route: RouteConfig,
62
- parent?: RouteRecord,
63
- matchAs?: string
64
- ) {
65
- const { path, name } = route
66
- if (process.env.NODE_ENV !== 'production') {
67
- assert(path != null, `"path" is required in a route configuration.`)
68
- assert(
69
- typeof route.component !== 'string',
70
- `route config "component" for path: ${String(
71
- path || name
72
- )} cannot be a ` + `string id. Use an actual component instead.`
73
- )
74
-
75
- warn(
76
- // eslint-disable-next-line no-control-regex
77
- !/[^\u0000-\u007F]+/.test(path),
78
- `Route with path "${path}" contains unencoded characters, make sure ` +
79
- `your path is correctly encoded before passing it to the router. Use ` +
80
- `encodeURI to encode static segments of your path.`
81
- )
82
- }
83
-
84
- const pathToRegexpOptions: PathToRegexpOptions =
85
- route.pathToRegexpOptions || {}
86
- const normalizedPath = normalizePath(path, parent, pathToRegexpOptions.strict)
87
-
88
- if (typeof route.caseSensitive === 'boolean') {
89
- pathToRegexpOptions.sensitive = route.caseSensitive
90
- }
91
-
92
- const record: RouteRecord = {
93
- path: normalizedPath,
94
- regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
95
- components: route.components || { default: route.component },
96
- alias: route.alias
97
- ? typeof route.alias === 'string'
98
- ? [route.alias]
99
- : route.alias
100
- : [],
101
- instances: {},
102
- enteredCbs: {},
103
- name,
104
- parent,
105
- matchAs,
106
- redirect: route.redirect,
107
- beforeEnter: route.beforeEnter,
108
- meta: route.meta || {},
109
- props:
110
- route.props == null
111
- ? {}
112
- : route.components
113
- ? route.props
114
- : { default: route.props }
115
- }
116
-
117
- if (route.children) {
118
- // Warn if route is named, does not redirect and has a default child route.
119
- // If users navigate to this route by name, the default child will
120
- // not be rendered (GH Issue #629)
121
- if (process.env.NODE_ENV !== 'production') {
122
- if (
123
- route.name &&
124
- !route.redirect &&
125
- route.children.some(child => /^\/?$/.test(child.path))
126
- ) {
127
- warn(
128
- false,
129
- `Named Route '${route.name}' has a default child route. ` +
130
- `When navigating to this named route (:to="{name: '${
131
- route.name
132
- }'}"), ` +
133
- `the default child route will not be rendered. Remove the name from ` +
134
- `this route and use the name of the default child route for named ` +
135
- `links instead.`
136
- )
137
- }
138
- }
139
- route.children.forEach(child => {
140
- const childMatchAs = matchAs
141
- ? cleanPath(`${matchAs}/${child.path}`)
142
- : undefined
143
- addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
144
- })
145
- }
146
-
147
- if (!pathMap[record.path]) {
148
- pathList.push(record.path)
149
- pathMap[record.path] = record
150
- }
151
-
152
- if (route.alias !== undefined) {
153
- const aliases = Array.isArray(route.alias) ? route.alias : [route.alias]
154
- for (let i = 0; i < aliases.length; ++i) {
155
- const alias = aliases[i]
156
- if (process.env.NODE_ENV !== 'production' && alias === path) {
157
- warn(
158
- false,
159
- `Found an alias with the same value as the path: "${path}". You have to remove that alias. It will be ignored in development.`
160
- )
161
- // skip in dev to make it work
162
- continue
163
- }
164
-
165
- const aliasRoute = {
166
- path: alias,
167
- children: route.children
168
- }
169
- addRouteRecord(
170
- pathList,
171
- pathMap,
172
- nameMap,
173
- aliasRoute,
174
- parent,
175
- record.path || '/' // matchAs
176
- )
177
- }
178
- }
179
-
180
- if (name) {
181
- if (!nameMap[name]) {
182
- nameMap[name] = record
183
- } else if (process.env.NODE_ENV !== 'production' && !matchAs) {
184
- warn(
185
- false,
186
- `Duplicate named routes definition: ` +
187
- `{ name: "${name}", path: "${record.path}" }`
188
- )
189
- }
190
- }
191
- }
192
-
193
- function compileRouteRegex (
194
- path: string,
195
- pathToRegexpOptions: PathToRegexpOptions
196
- ): RouteRegExp {
197
- const regex = Regexp(path, [], pathToRegexpOptions)
198
- if (process.env.NODE_ENV !== 'production') {
199
- const keys: any = Object.create(null)
200
- regex.keys.forEach(key => {
201
- warn(
202
- !keys[key.name],
203
- `Duplicate param keys in route with path: "${path}"`
204
- )
205
- keys[key.name] = true
206
- })
207
- }
208
- return regex
209
- }
210
-
211
- function normalizePath (
212
- path: string,
213
- parent?: RouteRecord,
214
- strict?: boolean
215
- ): string {
216
- if (!strict) path = path.replace(/\/$/, '')
217
- if (path[0] === '/') return path
218
- if (parent == null) return path
219
- return cleanPath(`${parent.path}/${path}`)
220
- }
@@ -1,72 +0,0 @@
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
- const prev = this.current
50
- this.index = targetIndex
51
- this.updateRoute(route)
52
- this.router.afterHooks.forEach(hook => {
53
- hook && hook(route, prev)
54
- })
55
- },
56
- err => {
57
- if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
58
- this.index = targetIndex
59
- }
60
- }
61
- )
62
- }
63
-
64
- getCurrentLocation () {
65
- const current = this.stack[this.stack.length - 1]
66
- return current ? current.fullPath : '/'
67
- }
68
-
69
- ensureURL () {
70
- // noop
71
- }
72
- }
@@ -1,379 +0,0 @@
1
- /* @flow */
2
-
3
- import { _Kdu } from '../install'
4
- import type Router from '../index'
5
- import { inBrowser } from '../util/dom'
6
- import { runQueue } from '../util/async'
7
- import { warn } from '../util/warn'
8
- import { START, isSameRoute, handleRouteEntered } from '../util/route'
9
- import {
10
- flatten,
11
- flatMapComponents,
12
- resolveAsyncComponents
13
- } from '../util/resolve-components'
14
- import {
15
- createNavigationDuplicatedError,
16
- createNavigationCancelledError,
17
- createNavigationRedirectedError,
18
- createNavigationAbortedError,
19
- isError,
20
- isNavigationFailure,
21
- NavigationFailureType
22
- } from '../util/errors'
23
- import { handleScroll } from '../util/scroll'
24
-
25
- export class History {
26
- router: Router
27
- base: string
28
- current: Route
29
- pending: ?Route
30
- cb: (r: Route) => void
31
- ready: boolean
32
- readyCbs: Array<Function>
33
- readyErrorCbs: Array<Function>
34
- errorCbs: Array<Function>
35
- listeners: Array<Function>
36
- cleanupListeners: Function
37
-
38
- // implemented by sub-classes
39
- +go: (n: number) => void
40
- +push: (loc: RawLocation, onComplete?: Function, onAbort?: Function) => void
41
- +replace: (
42
- loc: RawLocation,
43
- onComplete?: Function,
44
- onAbort?: Function
45
- ) => void
46
- +ensureURL: (push?: boolean) => void
47
- +getCurrentLocation: () => string
48
- +setupListeners: Function
49
-
50
- constructor (router: Router, base: ?string) {
51
- this.router = router
52
- this.base = normalizeBase(base)
53
- // start with a route object that stands for "nowhere"
54
- this.current = START
55
- this.pending = null
56
- this.ready = false
57
- this.readyCbs = []
58
- this.readyErrorCbs = []
59
- this.errorCbs = []
60
- this.listeners = []
61
- }
62
-
63
- listen (cb: Function) {
64
- this.cb = cb
65
- }
66
-
67
- onReady (cb: Function, errorCb: ?Function) {
68
- if (this.ready) {
69
- cb()
70
- } else {
71
- this.readyCbs.push(cb)
72
- if (errorCb) {
73
- this.readyErrorCbs.push(errorCb)
74
- }
75
- }
76
- }
77
-
78
- onError (errorCb: Function) {
79
- this.errorCbs.push(errorCb)
80
- }
81
-
82
- transitionTo (
83
- location: RawLocation,
84
- onComplete?: Function,
85
- onAbort?: Function
86
- ) {
87
- let route
88
- // catch redirect option
89
- try {
90
- route = this.router.match(location, this.current)
91
- } catch (e) {
92
- this.errorCbs.forEach(cb => {
93
- cb(e)
94
- })
95
- // Exception should still be thrown
96
- throw e
97
- }
98
- const prev = this.current
99
- this.confirmTransition(
100
- route,
101
- () => {
102
- this.updateRoute(route)
103
- onComplete && onComplete(route)
104
- this.ensureURL()
105
- this.router.afterHooks.forEach(hook => {
106
- hook && hook(route, prev)
107
- })
108
-
109
- // fire ready cbs once
110
- if (!this.ready) {
111
- this.ready = true
112
- this.readyCbs.forEach(cb => {
113
- cb(route)
114
- })
115
- }
116
- },
117
- err => {
118
- if (onAbort) {
119
- onAbort(err)
120
- }
121
- if (err && !this.ready) {
122
- // Initial redirection should not mark the history as ready yet
123
- // because it's triggered by the redirection instead
124
- if (!isNavigationFailure(err, NavigationFailureType.redirected) || prev !== START) {
125
- this.ready = true
126
- this.readyErrorCbs.forEach(cb => {
127
- cb(err)
128
- })
129
- }
130
- }
131
- }
132
- )
133
- }
134
-
135
- confirmTransition (route: Route, onComplete: Function, onAbort?: Function) {
136
- const current = this.current
137
- this.pending = route
138
- const abort = err => {
139
- // changed after adding errors
140
- // before that change, redirect and aborted navigation would produce an err == null
141
- if (!isNavigationFailure(err) && isError(err)) {
142
- if (this.errorCbs.length) {
143
- this.errorCbs.forEach(cb => {
144
- cb(err)
145
- })
146
- } else {
147
- if (process.env.NODE_ENV !== 'production') {
148
- warn(false, 'uncaught error during route navigation:')
149
- }
150
- console.error(err)
151
- }
152
- }
153
- onAbort && onAbort(err)
154
- }
155
- const lastRouteIndex = route.matched.length - 1
156
- const lastCurrentIndex = current.matched.length - 1
157
- if (
158
- isSameRoute(route, current) &&
159
- // in the case the route map has been dynamically appended to
160
- lastRouteIndex === lastCurrentIndex &&
161
- route.matched[lastRouteIndex] === current.matched[lastCurrentIndex]
162
- ) {
163
- this.ensureURL()
164
- if (route.hash) {
165
- handleScroll(this.router, current, route, false)
166
- }
167
- return abort(createNavigationDuplicatedError(current, route))
168
- }
169
-
170
- const { updated, deactivated, activated } = resolveQueue(
171
- this.current.matched,
172
- route.matched
173
- )
174
-
175
- const queue: Array<?NavigationGuard> = [].concat(
176
- // in-component leave guards
177
- extractLeaveGuards(deactivated),
178
- // global before hooks
179
- this.router.beforeHooks,
180
- // in-component update hooks
181
- extractUpdateHooks(updated),
182
- // in-config enter guards
183
- activated.map(m => m.beforeEnter),
184
- // async components
185
- resolveAsyncComponents(activated)
186
- )
187
-
188
- const iterator = (hook: NavigationGuard, next) => {
189
- if (this.pending !== route) {
190
- return abort(createNavigationCancelledError(current, route))
191
- }
192
- try {
193
- hook(route, current, (to: any) => {
194
- if (to === false) {
195
- // next(false) -> abort navigation, ensure current URL
196
- this.ensureURL(true)
197
- abort(createNavigationAbortedError(current, route))
198
- } else if (isError(to)) {
199
- this.ensureURL(true)
200
- abort(to)
201
- } else if (
202
- typeof to === 'string' ||
203
- (typeof to === 'object' &&
204
- (typeof to.path === 'string' || typeof to.name === 'string'))
205
- ) {
206
- // next('/') or next({ path: '/' }) -> redirect
207
- abort(createNavigationRedirectedError(current, route))
208
- if (typeof to === 'object' && to.replace) {
209
- this.replace(to)
210
- } else {
211
- this.push(to)
212
- }
213
- } else {
214
- // confirm transition and pass on the value
215
- next(to)
216
- }
217
- })
218
- } catch (e) {
219
- abort(e)
220
- }
221
- }
222
-
223
- runQueue(queue, iterator, () => {
224
- // wait until async components are resolved before
225
- // extracting in-component enter guards
226
- const enterGuards = extractEnterGuards(activated)
227
- const queue = enterGuards.concat(this.router.resolveHooks)
228
- runQueue(queue, iterator, () => {
229
- if (this.pending !== route) {
230
- return abort(createNavigationCancelledError(current, route))
231
- }
232
- this.pending = null
233
- onComplete(route)
234
- if (this.router.app) {
235
- this.router.app.$nextTick(() => {
236
- handleRouteEntered(route)
237
- })
238
- }
239
- })
240
- })
241
- }
242
-
243
- updateRoute (route: Route) {
244
- this.current = route
245
- this.cb && this.cb(route)
246
- }
247
-
248
- setupListeners () {
249
- // Default implementation is empty
250
- }
251
-
252
- teardown () {
253
- // clean up event listeners
254
- this.listeners.forEach(cleanupListener => {
255
- cleanupListener()
256
- })
257
- this.listeners = []
258
-
259
- // reset current history route
260
- this.current = START
261
- this.pending = null
262
- }
263
- }
264
-
265
- function normalizeBase (base: ?string): string {
266
- if (!base) {
267
- if (inBrowser) {
268
- // respect <base> tag
269
- const baseEl = document.querySelector('base')
270
- base = (baseEl && baseEl.getAttribute('href')) || '/'
271
- // strip full URL origin
272
- base = base.replace(/^https?:\/\/[^\/]+/, '')
273
- } else {
274
- base = '/'
275
- }
276
- }
277
- // make sure there's the starting slash
278
- if (base.charAt(0) !== '/') {
279
- base = '/' + base
280
- }
281
- // remove trailing slash
282
- return base.replace(/\/$/, '')
283
- }
284
-
285
- function resolveQueue (
286
- current: Array<RouteRecord>,
287
- next: Array<RouteRecord>
288
- ): {
289
- updated: Array<RouteRecord>,
290
- activated: Array<RouteRecord>,
291
- deactivated: Array<RouteRecord>
292
- } {
293
- let i
294
- const max = Math.max(current.length, next.length)
295
- for (i = 0; i < max; i++) {
296
- if (current[i] !== next[i]) {
297
- break
298
- }
299
- }
300
- return {
301
- updated: next.slice(0, i),
302
- activated: next.slice(i),
303
- deactivated: current.slice(i)
304
- }
305
- }
306
-
307
- function extractGuards (
308
- records: Array<RouteRecord>,
309
- name: string,
310
- bind: Function,
311
- reverse?: boolean
312
- ): Array<?Function> {
313
- const guards = flatMapComponents(records, (def, instance, match, key) => {
314
- const guard = extractGuard(def, name)
315
- if (guard) {
316
- return Array.isArray(guard)
317
- ? guard.map(guard => bind(guard, instance, match, key))
318
- : bind(guard, instance, match, key)
319
- }
320
- })
321
- return flatten(reverse ? guards.reverse() : guards)
322
- }
323
-
324
- function extractGuard (
325
- def: Object | Function,
326
- key: string
327
- ): NavigationGuard | Array<NavigationGuard> {
328
- if (typeof def !== 'function') {
329
- // extend now so that global mixins are applied.
330
- def = _Kdu.extend(def)
331
- }
332
- return def.options[key]
333
- }
334
-
335
- function extractLeaveGuards (deactivated: Array<RouteRecord>): Array<?Function> {
336
- return extractGuards(deactivated, 'beforeRouteLeave', bindGuard, true)
337
- }
338
-
339
- function extractUpdateHooks (updated: Array<RouteRecord>): Array<?Function> {
340
- return extractGuards(updated, 'beforeRouteUpdate', bindGuard)
341
- }
342
-
343
- function bindGuard (guard: NavigationGuard, instance: ?_Kdu): ?NavigationGuard {
344
- if (instance) {
345
- return function boundRouteGuard () {
346
- return guard.apply(instance, arguments)
347
- }
348
- }
349
- }
350
-
351
- function extractEnterGuards (
352
- activated: Array<RouteRecord>
353
- ): Array<?Function> {
354
- return extractGuards(
355
- activated,
356
- 'beforeRouteEnter',
357
- (guard, _, match, key) => {
358
- return bindEnterGuard(guard, match, key)
359
- }
360
- )
361
- }
362
-
363
- function bindEnterGuard (
364
- guard: NavigationGuard,
365
- match: RouteRecord,
366
- key: string
367
- ): NavigationGuard {
368
- return function routeEnterGuard (to, from, next) {
369
- return guard(to, from, cb => {
370
- if (typeof cb === 'function') {
371
- if (!match.enteredCbs[key]) {
372
- match.enteredCbs[key] = []
373
- }
374
- match.enteredCbs[key].push(cb)
375
- }
376
- next(cb)
377
- })
378
- }
379
- }