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,175 +0,0 @@
1
- /* @flow */
2
-
3
- import type Router from '../index'
4
- import { assert } from './warn'
5
- import { getStateKey, setStateKey } from './state-key'
6
- import { extend } from './misc'
7
-
8
- const positionStore = Object.create(null)
9
-
10
- export function setupScroll () {
11
- // Prevent browser scroll behavior on History popstate
12
- if ('scrollRestoration' in window.history) {
13
- window.history.scrollRestoration = 'manual'
14
- }
15
- // Fix for #1585 for Firefox
16
- // Fix for #2195 Add optional third attribute to workaround a bug in safari https://bugs.webkit.org/show_bug.cgi?id=182678
17
- // Fix for #2774 Support for apps loaded from Windows file shares not mapped to network drives: replaced location.origin with
18
- // window.location.protocol + '//' + window.location.host
19
- // location.host contains the port and location.hostname doesn't
20
- const protocolAndPath = window.location.protocol + '//' + window.location.host
21
- const absolutePath = window.location.href.replace(protocolAndPath, '')
22
- // preserve existing history state as it could be overriden by the user
23
- const stateCopy = extend({}, window.history.state)
24
- stateCopy.key = getStateKey()
25
- window.history.replaceState(stateCopy, '', absolutePath)
26
- window.addEventListener('popstate', handlePopState)
27
- return () => {
28
- window.removeEventListener('popstate', handlePopState)
29
- }
30
- }
31
-
32
- export function handleScroll (
33
- router: Router,
34
- to: Route,
35
- from: Route,
36
- isPop: boolean
37
- ) {
38
- if (!router.app) {
39
- return
40
- }
41
-
42
- const behavior = router.options.scrollBehavior
43
- if (!behavior) {
44
- return
45
- }
46
-
47
- if (process.env.NODE_ENV !== 'production') {
48
- assert(typeof behavior === 'function', `scrollBehavior must be a function`)
49
- }
50
-
51
- // wait until re-render finishes before scrolling
52
- router.app.$nextTick(() => {
53
- const position = getScrollPosition()
54
- const shouldScroll = behavior.call(
55
- router,
56
- to,
57
- from,
58
- isPop ? position : null
59
- )
60
-
61
- if (!shouldScroll) {
62
- return
63
- }
64
-
65
- if (typeof shouldScroll.then === 'function') {
66
- shouldScroll
67
- .then(shouldScroll => {
68
- scrollToPosition((shouldScroll: any), position)
69
- })
70
- .catch(err => {
71
- if (process.env.NODE_ENV !== 'production') {
72
- assert(false, err.toString())
73
- }
74
- })
75
- } else {
76
- scrollToPosition(shouldScroll, position)
77
- }
78
- })
79
- }
80
-
81
- export function saveScrollPosition () {
82
- const key = getStateKey()
83
- if (key) {
84
- positionStore[key] = {
85
- x: window.pageXOffset,
86
- y: window.pageYOffset
87
- }
88
- }
89
- }
90
-
91
- function handlePopState (e) {
92
- saveScrollPosition()
93
- if (e.state && e.state.key) {
94
- setStateKey(e.state.key)
95
- }
96
- }
97
-
98
- function getScrollPosition (): ?Object {
99
- const key = getStateKey()
100
- if (key) {
101
- return positionStore[key]
102
- }
103
- }
104
-
105
- function getElementPosition (el: Element, offset: Object): Object {
106
- const docEl: any = document.documentElement
107
- const docRect = docEl.getBoundingClientRect()
108
- const elRect = el.getBoundingClientRect()
109
- return {
110
- x: elRect.left - docRect.left - offset.x,
111
- y: elRect.top - docRect.top - offset.y
112
- }
113
- }
114
-
115
- function isValidPosition (obj: Object): boolean {
116
- return isNumber(obj.x) || isNumber(obj.y)
117
- }
118
-
119
- function normalizePosition (obj: Object): Object {
120
- return {
121
- x: isNumber(obj.x) ? obj.x : window.pageXOffset,
122
- y: isNumber(obj.y) ? obj.y : window.pageYOffset
123
- }
124
- }
125
-
126
- function normalizeOffset (obj: Object): Object {
127
- return {
128
- x: isNumber(obj.x) ? obj.x : 0,
129
- y: isNumber(obj.y) ? obj.y : 0
130
- }
131
- }
132
-
133
- function isNumber (v: any): boolean {
134
- return typeof v === 'number'
135
- }
136
-
137
- const hashStartsWithNumberRE = /^#\d/
138
-
139
- function scrollToPosition (shouldScroll, position) {
140
- const isObject = typeof shouldScroll === 'object'
141
- if (isObject && typeof shouldScroll.selector === 'string') {
142
- // getElementById would still fail if the selector contains a more complicated query like #main[data-attr]
143
- // but at the same time, it doesn't make much sense to select an element with an id and an extra selector
144
- const el = hashStartsWithNumberRE.test(shouldScroll.selector) // $flow-disable-line
145
- ? document.getElementById(shouldScroll.selector.slice(1)) // $flow-disable-line
146
- : document.querySelector(shouldScroll.selector)
147
-
148
- if (el) {
149
- let offset =
150
- shouldScroll.offset && typeof shouldScroll.offset === 'object'
151
- ? shouldScroll.offset
152
- : {}
153
- offset = normalizeOffset(offset)
154
- position = getElementPosition(el, offset)
155
- } else if (isValidPosition(shouldScroll)) {
156
- position = normalizePosition(shouldScroll)
157
- }
158
- } else if (isObject && isValidPosition(shouldScroll)) {
159
- position = normalizePosition(shouldScroll)
160
- }
161
-
162
- if (position) {
163
- // $flow-disable-line
164
- if ('scrollBehavior' in document.documentElement.style) {
165
- window.scrollTo({
166
- left: position.x,
167
- top: position.y,
168
- // $flow-disable-line
169
- behavior: shouldScroll.behavior
170
- })
171
- } else {
172
- window.scrollTo(position.x, position.y)
173
- }
174
- }
175
- }
@@ -1,22 +0,0 @@
1
- /* @flow */
2
- import { inBrowser } from './dom'
3
-
4
- // use User Timing api (if present) for more accurate key precision
5
- const Time =
6
- inBrowser && window.performance && window.performance.now
7
- ? window.performance
8
- : Date
9
-
10
- export function genStateKey (): string {
11
- return Time.now().toFixed(3)
12
- }
13
-
14
- let _key: string = genStateKey()
15
-
16
- export function getStateKey () {
17
- return _key
18
- }
19
-
20
- export function setStateKey (key: string) {
21
- return (_key = key)
22
- }
package/src/util/warn.js DELETED
@@ -1,14 +0,0 @@
1
- /* @flow */
2
-
3
- export function assert (condition: any, message: string) {
4
- if (!condition) {
5
- throw new Error(`[kdu-router] ${message}`)
6
- }
7
- }
8
-
9
- export function warn (condition: any, message: string) {
10
- if (!condition) {
11
- typeof console !== 'undefined' && console.warn(`[kdu-router] ${message}`)
12
- }
13
- }
14
-
package/types/index.d.ts DELETED
@@ -1,21 +0,0 @@
1
- import './kdu'
2
- import { KduRouter } from './router'
3
-
4
- export default KduRouter
5
-
6
- export {
7
- RouterMode,
8
- RouteMeta,
9
- RawLocation,
10
- RedirectOption,
11
- RouterOptions,
12
- RouteConfig,
13
- RouteRecord,
14
- RouteRecordPublic,
15
- Location,
16
- Route,
17
- NavigationGuard,
18
- NavigationGuardNext,
19
- NavigationFailureType,
20
- NavigationFailure
21
- } from './router'
package/types/kdu.d.ts DELETED
@@ -1,22 +0,0 @@
1
- /**
2
- * Augment the typings of Kdu.js
3
- */
4
-
5
- import Kdu from 'kdu'
6
- import KduRouter, { Route, RawLocation, NavigationGuard } from './index'
7
-
8
- declare module 'kdu/types/kdu' {
9
- interface Kdu {
10
- $router: KduRouter
11
- $route: Route
12
- }
13
- }
14
-
15
- declare module 'kdu/types/options' {
16
- interface ComponentOptions<V extends Kdu> {
17
- router?: KduRouter
18
- beforeRouteEnter?: NavigationGuard<V>
19
- beforeRouteLeave?: NavigationGuard<V>
20
- beforeRouteUpdate?: NavigationGuard<V>
21
- }
22
- }
package/types/router.d.ts DELETED
@@ -1,211 +0,0 @@
1
- import Kdu, { ComponentOptions, PluginFunction, AsyncComponent } from 'kdu'
2
-
3
- type Component = ComponentOptions<Kdu> | typeof Kdu | AsyncComponent
4
- type Dictionary<T> = { [key: string]: T }
5
- type ErrorHandler = (err: Error) => void
6
-
7
- export type RouterMode = 'hash' | 'history' | 'abstract'
8
- export type RawLocation = string | Location
9
- export type RedirectOption = RawLocation | ((to: Route) => RawLocation)
10
- export type NavigationGuardNext<V extends Kdu = Kdu> = (
11
- to?: RawLocation | false | ((vm: V) => any) | void
12
- ) => void
13
-
14
- export type NavigationGuard<V extends Kdu = Kdu> = (
15
- to: Route,
16
- from: Route,
17
- next: NavigationGuardNext<V>
18
- ) => any
19
-
20
- export declare class KduRouter {
21
- constructor(options?: RouterOptions)
22
-
23
- app: Kdu
24
- options: RouterOptions
25
- mode: RouterMode
26
- currentRoute: Route
27
-
28
- beforeEach(guard: NavigationGuard): Function
29
- beforeResolve(guard: NavigationGuard): Function
30
- afterEach(hook: (to: Route, from: Route) => any): Function
31
- push(location: RawLocation): Promise<Route>
32
- replace(location: RawLocation): Promise<Route>
33
- push(
34
- location: RawLocation,
35
- onComplete?: Function,
36
- onAbort?: ErrorHandler
37
- ): void
38
- replace(
39
- location: RawLocation,
40
- onComplete?: Function,
41
- onAbort?: ErrorHandler
42
- ): void
43
- go(n: number): void
44
- back(): void
45
- forward(): void
46
- match (raw: RawLocation, current?: Route, redirectedFrom?: Location): Route
47
- getMatchedComponents(to?: RawLocation | Route): Component[]
48
- onReady(cb: Function, errorCb?: ErrorHandler): void
49
- onError(cb: ErrorHandler): void
50
- addRoutes(routes: RouteConfig[]): void
51
-
52
- addRoute(parent: string, route: RouteConfig): void
53
- addRoute(route: RouteConfig): void
54
- getRoutes(): RouteRecordPublic[]
55
-
56
- resolve(
57
- to: RawLocation,
58
- current?: Route,
59
- append?: boolean
60
- ): {
61
- location: Location
62
- route: Route
63
- href: string
64
- // backwards compat
65
- normalizedTo: Location
66
- resolved: Route
67
- }
68
-
69
- static install: PluginFunction<never>
70
- static version: string
71
-
72
- static isNavigationFailure: (
73
- error: any,
74
- type?: number
75
- ) => error is NavigationFailure
76
- static NavigationFailureType: {
77
- [k in keyof typeof NavigationFailureType]: NavigationFailureType
78
- }
79
-
80
- static START_LOCATION: Route
81
- }
82
-
83
- export enum NavigationFailureType {
84
- redirected = 2,
85
- aborted = 4,
86
- cancelled = 8,
87
- duplicated = 16
88
- }
89
-
90
- export interface NavigationFailure extends Error {
91
- to: Route
92
- from: Route
93
- type: number
94
- }
95
-
96
- type Position = { x: number; y: number }
97
- type PositionResult = Position | { selector: string; offset?: Position, behavior?: ScrollBehavior } | void
98
-
99
- export interface RouterOptions {
100
- routes?: RouteConfig[]
101
- mode?: RouterMode
102
- fallback?: boolean
103
- base?: string
104
- linkActiveClass?: string
105
- linkExactActiveClass?: string
106
- parseQuery?: (query: string) => Object
107
- stringifyQuery?: (query: Object) => string
108
- scrollBehavior?: (
109
- to: Route,
110
- from: Route,
111
- savedPosition: Position | void
112
- ) => PositionResult | Promise<PositionResult> | undefined | null
113
- }
114
-
115
- type RoutePropsFunction = (route: Route) => Object
116
-
117
- export interface PathToRegexpOptions {
118
- sensitive?: boolean
119
- strict?: boolean
120
- end?: boolean
121
- }
122
-
123
- interface _RouteConfigBase {
124
- path: string
125
- name?: string
126
- children?: RouteConfig[]
127
- redirect?: RedirectOption
128
- alias?: string | string[]
129
- meta?: RouteMeta
130
- beforeEnter?: NavigationGuard
131
- caseSensitive?: boolean
132
- pathToRegexpOptions?: PathToRegexpOptions
133
- }
134
-
135
- interface RouteConfigSingleView extends _RouteConfigBase {
136
- component?: Component
137
- props?: boolean | Object | RoutePropsFunction
138
- }
139
-
140
- interface RouteConfigMultipleViews extends _RouteConfigBase {
141
- components?: Dictionary<Component>
142
- props?: Dictionary<boolean | Object | RoutePropsFunction>
143
- }
144
-
145
- export type RouteConfig = RouteConfigSingleView | RouteConfigMultipleViews
146
-
147
- export interface RouteRecord {
148
- path: string
149
- regex: RegExp
150
- components: Dictionary<Component>
151
- instances: Dictionary<Kdu>
152
- name?: string
153
- parent?: RouteRecord
154
- redirect?: RedirectOption
155
- matchAs?: string
156
- meta: RouteMeta
157
- beforeEnter?: (
158
- route: Route,
159
- redirect: (location: RawLocation) => void,
160
- next: () => void
161
- ) => any
162
- props:
163
- | boolean
164
- | Object
165
- | RoutePropsFunction
166
- | Dictionary<boolean | Object | RoutePropsFunction>
167
- }
168
-
169
- export interface RouteRecordPublic {
170
- path: string
171
- components: Dictionary<Component>
172
- instances: Dictionary<Kdu>
173
- name?: string
174
- redirect?: RedirectOption
175
- meta: any
176
- beforeEnter?: (
177
- route: Route,
178
- redirect: (location: RawLocation) => void,
179
- next: () => void
180
- ) => any
181
- props:
182
- | boolean
183
- | Object
184
- | RoutePropsFunction
185
- | Dictionary<boolean | Object | RoutePropsFunction>
186
- }
187
-
188
-
189
- export interface Location {
190
- name?: string
191
- path?: string
192
- hash?: string
193
- query?: Dictionary<string | (string | null)[] | null | undefined>
194
- params?: Dictionary<string>
195
- append?: boolean
196
- replace?: boolean
197
- }
198
-
199
- export interface Route {
200
- path: string
201
- name?: string | null
202
- hash: string
203
- query: Dictionary<string | (string | null)[]>
204
- params: Dictionary<string>
205
- fullPath: string
206
- matched: RouteRecord[]
207
- redirectedFrom?: string
208
- meta?: RouteMeta
209
- }
210
-
211
- export interface RouteMeta extends Record<string | number | symbol, any> {}