kdu-router 2.7.0 → 3.1.3

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/types/kdu.d.ts CHANGED
@@ -1,23 +1,22 @@
1
- /**
2
- * Augment the typings of Kdu.js
3
- */
4
-
5
- import Kdu = require("types/kdu");
6
- import KduRouter = require("./index");
7
- import { Route, RawLocation, NavigationGuard } from "./index";
8
-
9
- declare module "kdu/types/kdu" {
10
- interface Kdu {
11
- $router: KduRouter;
12
- $route: Route;
13
- }
14
- }
15
-
16
- declare module "kdu/types/options" {
17
- interface ComponentOptions<V extends Kdu> {
18
- router?: KduRouter;
19
- beforeRouteEnter?: NavigationGuard;
20
- beforeRouteLeave?: NavigationGuard;
21
- beforeRouteUpdate?: NavigationGuard;
22
- }
23
- }
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 CHANGED
@@ -1,126 +1,145 @@
1
- import Kdu = require("types/kdu");
2
- import { ComponentOptions, PluginFunction } from "types/kdu";
3
-
4
- type Component = ComponentOptions<Kdu> | typeof Kdu;
5
- type Dictionary<T> = { [key: string]: T };
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 NavigationGuard = (
11
- to: Route,
12
- from: Route,
13
- next: (to?: RawLocation | false | ((vm: Kdu) => any) | void) => void
14
- ) => any
15
-
16
- declare class KduRouter {
17
- constructor (options?: RouterOptions);
18
-
19
- app: Kdu;
20
- mode: RouterMode;
21
- currentRoute: Route;
22
-
23
- beforeEach (guard: NavigationGuard): Function;
24
- beforeResolve (guard: NavigationGuard): Function;
25
- afterEach (hook: (to: Route, from: Route) => any): Function;
26
- push (location: RawLocation, onComplete?: Function, onAbort?: Function): void;
27
- replace (location: RawLocation, onComplete?: Function, onAbort?: Function): void;
28
- go (n: number): void;
29
- back (): void;
30
- forward (): void;
31
- getMatchedComponents (to?: RawLocation | Route): Component[];
32
- onReady (cb: Function, errorCb?: Function): void;
33
- onError (cb: Function): void;
34
- addRoutes (routes: RouteConfig[]): void;
35
- resolve (to: RawLocation, current?: Route, append?: boolean): {
36
- location: Location;
37
- route: Route;
38
- href: string;
39
- // backwards compat
40
- normalizedTo: Location;
41
- resolved: Route;
42
- };
43
-
44
- static install: PluginFunction<never>;
45
- }
46
-
47
- type Position = { x: number, y: number };
48
-
49
- export interface RouterOptions {
50
- routes?: RouteConfig[];
51
- mode?: RouterMode;
52
- fallback?: boolean;
53
- base?: string;
54
- linkActiveClass?: string;
55
- linkExactActiveClass?: string;
56
- parseQuery?: (query: string) => Object;
57
- stringifyQuery?: (query: Object) => string;
58
- scrollBehavior?: (
59
- to: Route,
60
- from: Route,
61
- savedPosition: Position | void
62
- ) => Position | { selector: string, offset?: Position } | void;
63
- }
64
-
65
- type RoutePropsFunction = (route: Route) => Object;
66
-
67
- export interface PathToRegexpOptions {
68
- sensitive?: boolean;
69
- strict?: boolean;
70
- end?: boolean;
71
- }
72
-
73
- export interface RouteConfig {
74
- path: string;
75
- name?: string;
76
- component?: Component;
77
- components?: Dictionary<Component>;
78
- redirect?: RedirectOption;
79
- alias?: string | string[];
80
- children?: RouteConfig[];
81
- meta?: any;
82
- beforeEnter?: NavigationGuard;
83
- props?: boolean | Object | RoutePropsFunction;
84
- caseSensitive?: boolean;
85
- pathToRegexpOptions?: PathToRegexpOptions;
86
- }
87
-
88
- export interface RouteRecord {
89
- path: string;
90
- regex: RegExp;
91
- components: Dictionary<Component>;
92
- instances: Dictionary<Kdu>;
93
- name?: string;
94
- parent?: RouteRecord;
95
- redirect?: RedirectOption;
96
- matchAs?: string;
97
- meta: any;
98
- beforeEnter?: (
99
- route: Route,
100
- redirect: (location: RawLocation) => void,
101
- next: () => void
102
- ) => any;
103
- props: boolean | Object | RoutePropsFunction | Dictionary<boolean | Object | RoutePropsFunction>;
104
- }
105
-
106
- export interface Location {
107
- name?: string;
108
- path?: string;
109
- hash?: string;
110
- query?: Dictionary<string>;
111
- params?: Dictionary<string>;
112
- append?: boolean;
113
- replace?: boolean;
114
- }
115
-
116
- export interface Route {
117
- path: string;
118
- name?: string;
119
- hash: string;
120
- query: Dictionary<string>;
121
- params: Dictionary<string>;
122
- fullPath: string;
123
- matched: RouteRecord[];
124
- redirectedFrom?: string;
125
- meta?: any;
126
- }
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 NavigationGuard < V extends Kdu = Kdu > = (
11
+ to: Route,
12
+ from: Route,
13
+ next: (to?: RawLocation | false | ((vm: V) => any) | void) => void
14
+ ) => any
15
+
16
+ export declare class KduRouter {
17
+ constructor(options?: RouterOptions)
18
+
19
+ app: Kdu
20
+ mode: RouterMode
21
+ currentRoute: Route
22
+
23
+ beforeEach(guard: NavigationGuard): Function
24
+ beforeResolve(guard: NavigationGuard): Function
25
+ afterEach(hook: (to: Route, from: Route) => any): Function
26
+ push(location: RawLocation): Promise<Route>
27
+ replace(location: RawLocation): Promise<Route>
28
+ push(
29
+ location: RawLocation,
30
+ onComplete?: Function,
31
+ onAbort?: ErrorHandler
32
+ ): void
33
+ replace(
34
+ location: RawLocation,
35
+ onComplete?: Function,
36
+ onAbort?: ErrorHandler
37
+ ): void
38
+ go(n: number): void
39
+ back(): void
40
+ forward(): void
41
+ getMatchedComponents(to?: RawLocation | Route): Component[]
42
+ onReady(cb: Function, errorCb?: ErrorHandler): void
43
+ onError(cb: ErrorHandler): void
44
+ addRoutes(routes: RouteConfig[]): void
45
+ resolve(
46
+ to: RawLocation,
47
+ current?: Route,
48
+ append?: boolean
49
+ ): {
50
+ location: Location
51
+ route: Route
52
+ href: string
53
+ // backwards compat
54
+ normalizedTo: Location
55
+ resolved: Route
56
+ }
57
+
58
+ static install: PluginFunction<never>
59
+ }
60
+
61
+ type Position = { x: number; y: number }
62
+ type PositionResult = Position | { selector: string; offset?: Position } | void
63
+
64
+ export interface RouterOptions {
65
+ routes?: RouteConfig[]
66
+ mode?: RouterMode
67
+ fallback?: boolean
68
+ base?: string
69
+ linkActiveClass?: string
70
+ linkExactActiveClass?: string
71
+ parseQuery?: (query: string) => Object
72
+ stringifyQuery?: (query: Object) => string
73
+ scrollBehavior?: (
74
+ to: Route,
75
+ from: Route,
76
+ savedPosition: Position | void
77
+ ) => PositionResult | Promise<PositionResult>
78
+ }
79
+
80
+ type RoutePropsFunction = (route: Route) => Object
81
+
82
+ export interface PathToRegexpOptions {
83
+ sensitive?: boolean
84
+ strict?: boolean
85
+ end?: boolean
86
+ }
87
+
88
+ export interface RouteConfig {
89
+ path: string
90
+ name?: string
91
+ component?: Component
92
+ components?: Dictionary<Component>
93
+ redirect?: RedirectOption
94
+ alias?: string | string[]
95
+ children?: RouteConfig[]
96
+ meta?: any
97
+ beforeEnter?: NavigationGuard
98
+ props?: boolean | Object | RoutePropsFunction
99
+ caseSensitive?: boolean
100
+ pathToRegexpOptions?: PathToRegexpOptions
101
+ }
102
+
103
+ export interface RouteRecord {
104
+ path: string
105
+ regex: RegExp
106
+ components: Dictionary<Component>
107
+ instances: Dictionary<Kdu>
108
+ name?: string
109
+ parent?: RouteRecord
110
+ redirect?: RedirectOption
111
+ matchAs?: string
112
+ meta: any
113
+ beforeEnter?: (
114
+ route: Route,
115
+ redirect: (location: RawLocation) => void,
116
+ next: () => void
117
+ ) => any
118
+ props:
119
+ | boolean
120
+ | Object
121
+ | RoutePropsFunction
122
+ | Dictionary<boolean | Object | RoutePropsFunction>
123
+ }
124
+
125
+ export interface Location {
126
+ name?: string
127
+ path?: string
128
+ hash?: string
129
+ query?: Dictionary<string | (string | null)[] | null | undefined>
130
+ params?: Dictionary<string>
131
+ append?: boolean
132
+ replace?: boolean
133
+ }
134
+
135
+ export interface Route {
136
+ path: string
137
+ name?: string
138
+ hash: string
139
+ query: Dictionary<string | (string | null)[]>
140
+ params: Dictionary<string>
141
+ fullPath: string
142
+ matched: RouteRecord[]
143
+ redirectedFrom?: string
144
+ meta?: any
145
+ }
@@ -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
- }
@@ -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
- }
@@ -1,51 +0,0 @@
1
- /* @flow */
2
-
3
- import type Router from '../index'
4
- import { History } from './base'
5
-
6
- export class AbstractHistory extends History {
7
- index: number;
8
- stack: Array<Route>;
9
-
10
- constructor (router: Router, base: ?string) {
11
- super(router, base)
12
- this.stack = []
13
- this.index = -1
14
- }
15
-
16
- push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
17
- this.transitionTo(location, route => {
18
- this.stack = this.stack.slice(0, this.index + 1).concat(route)
19
- this.index++
20
- onComplete && onComplete(route)
21
- }, onAbort)
22
- }
23
-
24
- replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
25
- this.transitionTo(location, route => {
26
- this.stack = this.stack.slice(0, this.index).concat(route)
27
- onComplete && onComplete(route)
28
- }, onAbort)
29
- }
30
-
31
- go (n: number) {
32
- const targetIndex = this.index + n
33
- if (targetIndex < 0 || targetIndex >= this.stack.length) {
34
- return
35
- }
36
- const route = this.stack[targetIndex]
37
- this.confirmTransition(route, () => {
38
- this.index = targetIndex
39
- this.updateRoute(route)
40
- })
41
- }
42
-
43
- getCurrentLocation () {
44
- const current = this.stack[this.stack.length - 1]
45
- return current ? current.fullPath : '/'
46
- }
47
-
48
- ensureURL () {
49
- // noop
50
- }
51
- }