kdu-router 3.1.3 → 3.1.7
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/LICENSE +21 -21
- package/README.md +20 -56
- package/dist/kdu-router.common.js +2917 -2881
- package/dist/kdu-router.esm.browser.js +2867 -2831
- package/dist/kdu-router.esm.browser.min.js +11 -6
- package/dist/kdu-router.esm.js +2915 -2879
- package/dist/kdu-router.js +2923 -2887
- package/dist/kdu-router.min.js +11 -6
- package/package.json +81 -107
- package/src/components/link.js +190 -0
- package/src/components/view.js +149 -0
- package/src/create-matcher.js +200 -200
- package/src/create-route-map.js +205 -205
- package/src/history/abstract.js +69 -0
- package/src/history/base.js +352 -0
- package/src/history/errors.js +22 -0
- package/src/history/hash.js +154 -0
- package/src/history/html5.js +80 -0
- package/src/index.js +261 -261
- package/src/install.js +52 -52
- package/src/util/async.js +18 -0
- package/src/util/dom.js +3 -0
- package/src/util/location.js +69 -0
- package/src/util/misc.js +6 -0
- package/src/util/params.js +37 -0
- package/src/util/path.js +74 -0
- package/src/util/push-state.js +46 -0
- package/src/util/query.js +95 -0
- package/src/util/resolve-components.js +108 -0
- package/src/util/route.js +132 -0
- package/src/util/scroll.js +156 -0
- package/src/util/state-key.js +22 -0
- package/src/util/warn.js +25 -0
- package/types/index.d.ts +16 -16
- package/types/kdu.d.ts +22 -22
- package/types/router.d.ts +145 -145
package/src/index.js
CHANGED
|
@@ -1,261 +1,261 @@
|
|
|
1
|
-
/* @flow */
|
|
2
|
-
|
|
3
|
-
import { install } from './install'
|
|
4
|
-
import { START } from './util/route'
|
|
5
|
-
import { assert } from './util/warn'
|
|
6
|
-
import { inBrowser } from './util/dom'
|
|
7
|
-
import { cleanPath } from './util/path'
|
|
8
|
-
import { createMatcher } from './create-matcher'
|
|
9
|
-
import { normalizeLocation } from './util/location'
|
|
10
|
-
import { supportsPushState } from './util/push-state'
|
|
11
|
-
|
|
12
|
-
import { HashHistory } from './history/hash'
|
|
13
|
-
import { HTML5History } from './history/html5'
|
|
14
|
-
import { AbstractHistory } from './history/abstract'
|
|
15
|
-
|
|
16
|
-
import type { Matcher } from './create-matcher'
|
|
17
|
-
|
|
18
|
-
export default class KduRouter {
|
|
19
|
-
static install: () => void;
|
|
20
|
-
static version: string;
|
|
21
|
-
|
|
22
|
-
app: any;
|
|
23
|
-
apps: Array<any>;
|
|
24
|
-
ready: boolean;
|
|
25
|
-
readyCbs: Array<Function>;
|
|
26
|
-
options: RouterOptions;
|
|
27
|
-
mode: string;
|
|
28
|
-
history: HashHistory | HTML5History | AbstractHistory;
|
|
29
|
-
matcher: Matcher;
|
|
30
|
-
fallback: boolean;
|
|
31
|
-
beforeHooks: Array<?NavigationGuard>;
|
|
32
|
-
resolveHooks: Array<?NavigationGuard>;
|
|
33
|
-
afterHooks: Array<?AfterNavigationHook>;
|
|
34
|
-
|
|
35
|
-
constructor (options: RouterOptions = {}) {
|
|
36
|
-
this.app = null
|
|
37
|
-
this.apps = []
|
|
38
|
-
this.options = options
|
|
39
|
-
this.beforeHooks = []
|
|
40
|
-
this.resolveHooks = []
|
|
41
|
-
this.afterHooks = []
|
|
42
|
-
this.matcher = createMatcher(options.routes || [], this)
|
|
43
|
-
|
|
44
|
-
let mode = options.mode || 'hash'
|
|
45
|
-
this.fallback = mode === 'history' && !supportsPushState && options.fallback !== false
|
|
46
|
-
if (this.fallback) {
|
|
47
|
-
mode = 'hash'
|
|
48
|
-
}
|
|
49
|
-
if (!inBrowser) {
|
|
50
|
-
mode = 'abstract'
|
|
51
|
-
}
|
|
52
|
-
this.mode = mode
|
|
53
|
-
|
|
54
|
-
switch (mode) {
|
|
55
|
-
case 'history':
|
|
56
|
-
this.history = new HTML5History(this, options.base)
|
|
57
|
-
break
|
|
58
|
-
case 'hash':
|
|
59
|
-
this.history = new HashHistory(this, options.base, this.fallback)
|
|
60
|
-
break
|
|
61
|
-
case 'abstract':
|
|
62
|
-
this.history = new AbstractHistory(this, options.base)
|
|
63
|
-
break
|
|
64
|
-
default:
|
|
65
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
66
|
-
assert(false, `invalid mode: ${mode}`)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
match (
|
|
72
|
-
raw: RawLocation,
|
|
73
|
-
current?: Route,
|
|
74
|
-
redirectedFrom?: Location
|
|
75
|
-
): Route {
|
|
76
|
-
return this.matcher.match(raw, current, redirectedFrom)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
get currentRoute (): ?Route {
|
|
80
|
-
return this.history && this.history.current
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
init (app: any /* Kdu component instance */) {
|
|
84
|
-
process.env.NODE_ENV !== 'production' && assert(
|
|
85
|
-
install.installed,
|
|
86
|
-
`not installed. Make sure to call \`Kdu.use(KduRouter)\` ` +
|
|
87
|
-
`before creating root instance.`
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
this.apps.push(app)
|
|
91
|
-
|
|
92
|
-
// set up app destroyed handler
|
|
93
|
-
app.$once('hook:destroyed', () => {
|
|
94
|
-
// clean out app from this.apps array once destroyed
|
|
95
|
-
const index = this.apps.indexOf(app)
|
|
96
|
-
if (index > -1) this.apps.splice(index, 1)
|
|
97
|
-
// ensure we still have a main app or null if no apps
|
|
98
|
-
// we do not release the router so it can be reused
|
|
99
|
-
if (this.app === app) this.app = this.apps[0] || null
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
// main app previously initialized
|
|
103
|
-
// return as we don't need to set up new history listener
|
|
104
|
-
if (this.app) {
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
this.app = app
|
|
109
|
-
|
|
110
|
-
const history = this.history
|
|
111
|
-
|
|
112
|
-
if (history instanceof HTML5History) {
|
|
113
|
-
history.transitionTo(history.getCurrentLocation())
|
|
114
|
-
} else if (history instanceof HashHistory) {
|
|
115
|
-
const setupHashListener = () => {
|
|
116
|
-
history.setupListeners()
|
|
117
|
-
}
|
|
118
|
-
history.transitionTo(
|
|
119
|
-
history.getCurrentLocation(),
|
|
120
|
-
setupHashListener,
|
|
121
|
-
setupHashListener
|
|
122
|
-
)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
history.listen(route => {
|
|
126
|
-
this.apps.forEach((app) => {
|
|
127
|
-
app._route = route
|
|
128
|
-
})
|
|
129
|
-
})
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
beforeEach (fn: Function): Function {
|
|
133
|
-
return registerHook(this.beforeHooks, fn)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
beforeResolve (fn: Function): Function {
|
|
137
|
-
return registerHook(this.resolveHooks, fn)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
afterEach (fn: Function): Function {
|
|
141
|
-
return registerHook(this.afterHooks, fn)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
onReady (cb: Function, errorCb?: Function) {
|
|
145
|
-
this.history.onReady(cb, errorCb)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
onError (errorCb: Function) {
|
|
149
|
-
this.history.onError(errorCb)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
|
|
153
|
-
// $flow-disable-line
|
|
154
|
-
if (!onComplete && !onAbort && typeof Promise !== 'undefined') {
|
|
155
|
-
return new Promise((resolve, reject) => {
|
|
156
|
-
this.history.push(location, resolve, reject)
|
|
157
|
-
})
|
|
158
|
-
} else {
|
|
159
|
-
this.history.push(location, onComplete, onAbort)
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
|
|
164
|
-
// $flow-disable-line
|
|
165
|
-
if (!onComplete && !onAbort && typeof Promise !== 'undefined') {
|
|
166
|
-
return new Promise((resolve, reject) => {
|
|
167
|
-
this.history.replace(location, resolve, reject)
|
|
168
|
-
})
|
|
169
|
-
} else {
|
|
170
|
-
this.history.replace(location, onComplete, onAbort)
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
go (n: number) {
|
|
175
|
-
this.history.go(n)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
back () {
|
|
179
|
-
this.go(-1)
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
forward () {
|
|
183
|
-
this.go(1)
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
getMatchedComponents (to?: RawLocation | Route): Array<any> {
|
|
187
|
-
const route: any = to
|
|
188
|
-
? to.matched
|
|
189
|
-
? to
|
|
190
|
-
: this.resolve(to).route
|
|
191
|
-
: this.currentRoute
|
|
192
|
-
if (!route) {
|
|
193
|
-
return []
|
|
194
|
-
}
|
|
195
|
-
return [].concat.apply([], route.matched.map(m => {
|
|
196
|
-
return Object.keys(m.components).map(key => {
|
|
197
|
-
return m.components[key]
|
|
198
|
-
})
|
|
199
|
-
}))
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
resolve (
|
|
203
|
-
to: RawLocation,
|
|
204
|
-
current?: Route,
|
|
205
|
-
append?: boolean
|
|
206
|
-
): {
|
|
207
|
-
location: Location,
|
|
208
|
-
route: Route,
|
|
209
|
-
href: string,
|
|
210
|
-
// for backwards compat
|
|
211
|
-
normalizedTo: Location,
|
|
212
|
-
resolved: Route
|
|
213
|
-
} {
|
|
214
|
-
current = current || this.history.current
|
|
215
|
-
const location = normalizeLocation(
|
|
216
|
-
to,
|
|
217
|
-
current,
|
|
218
|
-
append,
|
|
219
|
-
this
|
|
220
|
-
)
|
|
221
|
-
const route = this.match(location, current)
|
|
222
|
-
const fullPath = route.redirectedFrom || route.fullPath
|
|
223
|
-
const base = this.history.base
|
|
224
|
-
const href = createHref(base, fullPath, this.mode)
|
|
225
|
-
return {
|
|
226
|
-
location,
|
|
227
|
-
route,
|
|
228
|
-
href,
|
|
229
|
-
// for backwards compat
|
|
230
|
-
normalizedTo: location,
|
|
231
|
-
resolved: route
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
addRoutes (routes: Array<RouteConfig>) {
|
|
236
|
-
this.matcher.addRoutes(routes)
|
|
237
|
-
if (this.history.current !== START) {
|
|
238
|
-
this.history.transitionTo(this.history.getCurrentLocation())
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function registerHook (list: Array<any>, fn: Function): Function {
|
|
244
|
-
list.push(fn)
|
|
245
|
-
return () => {
|
|
246
|
-
const i = list.indexOf(fn)
|
|
247
|
-
if (i > -1) list.splice(i, 1)
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function createHref (base: string, fullPath: string, mode) {
|
|
252
|
-
var path = mode === 'hash' ? '#' + fullPath : fullPath
|
|
253
|
-
return base ? cleanPath(base + '/' + path) : path
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
KduRouter.install = install
|
|
257
|
-
KduRouter.version = '__VERSION__'
|
|
258
|
-
|
|
259
|
-
if (inBrowser && window.Kdu) {
|
|
260
|
-
window.Kdu.use(KduRouter)
|
|
261
|
-
}
|
|
1
|
+
/* @flow */
|
|
2
|
+
|
|
3
|
+
import { install } from './install'
|
|
4
|
+
import { START } from './util/route'
|
|
5
|
+
import { assert } from './util/warn'
|
|
6
|
+
import { inBrowser } from './util/dom'
|
|
7
|
+
import { cleanPath } from './util/path'
|
|
8
|
+
import { createMatcher } from './create-matcher'
|
|
9
|
+
import { normalizeLocation } from './util/location'
|
|
10
|
+
import { supportsPushState } from './util/push-state'
|
|
11
|
+
|
|
12
|
+
import { HashHistory } from './history/hash'
|
|
13
|
+
import { HTML5History } from './history/html5'
|
|
14
|
+
import { AbstractHistory } from './history/abstract'
|
|
15
|
+
|
|
16
|
+
import type { Matcher } from './create-matcher'
|
|
17
|
+
|
|
18
|
+
export default class KduRouter {
|
|
19
|
+
static install: () => void;
|
|
20
|
+
static version: string;
|
|
21
|
+
|
|
22
|
+
app: any;
|
|
23
|
+
apps: Array<any>;
|
|
24
|
+
ready: boolean;
|
|
25
|
+
readyCbs: Array<Function>;
|
|
26
|
+
options: RouterOptions;
|
|
27
|
+
mode: string;
|
|
28
|
+
history: HashHistory | HTML5History | AbstractHistory;
|
|
29
|
+
matcher: Matcher;
|
|
30
|
+
fallback: boolean;
|
|
31
|
+
beforeHooks: Array<?NavigationGuard>;
|
|
32
|
+
resolveHooks: Array<?NavigationGuard>;
|
|
33
|
+
afterHooks: Array<?AfterNavigationHook>;
|
|
34
|
+
|
|
35
|
+
constructor (options: RouterOptions = {}) {
|
|
36
|
+
this.app = null
|
|
37
|
+
this.apps = []
|
|
38
|
+
this.options = options
|
|
39
|
+
this.beforeHooks = []
|
|
40
|
+
this.resolveHooks = []
|
|
41
|
+
this.afterHooks = []
|
|
42
|
+
this.matcher = createMatcher(options.routes || [], this)
|
|
43
|
+
|
|
44
|
+
let mode = options.mode || 'hash'
|
|
45
|
+
this.fallback = mode === 'history' && !supportsPushState && options.fallback !== false
|
|
46
|
+
if (this.fallback) {
|
|
47
|
+
mode = 'hash'
|
|
48
|
+
}
|
|
49
|
+
if (!inBrowser) {
|
|
50
|
+
mode = 'abstract'
|
|
51
|
+
}
|
|
52
|
+
this.mode = mode
|
|
53
|
+
|
|
54
|
+
switch (mode) {
|
|
55
|
+
case 'history':
|
|
56
|
+
this.history = new HTML5History(this, options.base)
|
|
57
|
+
break
|
|
58
|
+
case 'hash':
|
|
59
|
+
this.history = new HashHistory(this, options.base, this.fallback)
|
|
60
|
+
break
|
|
61
|
+
case 'abstract':
|
|
62
|
+
this.history = new AbstractHistory(this, options.base)
|
|
63
|
+
break
|
|
64
|
+
default:
|
|
65
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
66
|
+
assert(false, `invalid mode: ${mode}`)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
match (
|
|
72
|
+
raw: RawLocation,
|
|
73
|
+
current?: Route,
|
|
74
|
+
redirectedFrom?: Location
|
|
75
|
+
): Route {
|
|
76
|
+
return this.matcher.match(raw, current, redirectedFrom)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get currentRoute (): ?Route {
|
|
80
|
+
return this.history && this.history.current
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
init (app: any /* Kdu component instance */) {
|
|
84
|
+
process.env.NODE_ENV !== 'production' && assert(
|
|
85
|
+
install.installed,
|
|
86
|
+
`not installed. Make sure to call \`Kdu.use(KduRouter)\` ` +
|
|
87
|
+
`before creating root instance.`
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
this.apps.push(app)
|
|
91
|
+
|
|
92
|
+
// set up app destroyed handler
|
|
93
|
+
app.$once('hook:destroyed', () => {
|
|
94
|
+
// clean out app from this.apps array once destroyed
|
|
95
|
+
const index = this.apps.indexOf(app)
|
|
96
|
+
if (index > -1) this.apps.splice(index, 1)
|
|
97
|
+
// ensure we still have a main app or null if no apps
|
|
98
|
+
// we do not release the router so it can be reused
|
|
99
|
+
if (this.app === app) this.app = this.apps[0] || null
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
// main app previously initialized
|
|
103
|
+
// return as we don't need to set up new history listener
|
|
104
|
+
if (this.app) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
this.app = app
|
|
109
|
+
|
|
110
|
+
const history = this.history
|
|
111
|
+
|
|
112
|
+
if (history instanceof HTML5History) {
|
|
113
|
+
history.transitionTo(history.getCurrentLocation())
|
|
114
|
+
} else if (history instanceof HashHistory) {
|
|
115
|
+
const setupHashListener = () => {
|
|
116
|
+
history.setupListeners()
|
|
117
|
+
}
|
|
118
|
+
history.transitionTo(
|
|
119
|
+
history.getCurrentLocation(),
|
|
120
|
+
setupHashListener,
|
|
121
|
+
setupHashListener
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
history.listen(route => {
|
|
126
|
+
this.apps.forEach((app) => {
|
|
127
|
+
app._route = route
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
beforeEach (fn: Function): Function {
|
|
133
|
+
return registerHook(this.beforeHooks, fn)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
beforeResolve (fn: Function): Function {
|
|
137
|
+
return registerHook(this.resolveHooks, fn)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
afterEach (fn: Function): Function {
|
|
141
|
+
return registerHook(this.afterHooks, fn)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
onReady (cb: Function, errorCb?: Function) {
|
|
145
|
+
this.history.onReady(cb, errorCb)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
onError (errorCb: Function) {
|
|
149
|
+
this.history.onError(errorCb)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
|
|
153
|
+
// $flow-disable-line
|
|
154
|
+
if (!onComplete && !onAbort && typeof Promise !== 'undefined') {
|
|
155
|
+
return new Promise((resolve, reject) => {
|
|
156
|
+
this.history.push(location, resolve, reject)
|
|
157
|
+
})
|
|
158
|
+
} else {
|
|
159
|
+
this.history.push(location, onComplete, onAbort)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
|
|
164
|
+
// $flow-disable-line
|
|
165
|
+
if (!onComplete && !onAbort && typeof Promise !== 'undefined') {
|
|
166
|
+
return new Promise((resolve, reject) => {
|
|
167
|
+
this.history.replace(location, resolve, reject)
|
|
168
|
+
})
|
|
169
|
+
} else {
|
|
170
|
+
this.history.replace(location, onComplete, onAbort)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
go (n: number) {
|
|
175
|
+
this.history.go(n)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
back () {
|
|
179
|
+
this.go(-1)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
forward () {
|
|
183
|
+
this.go(1)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
getMatchedComponents (to?: RawLocation | Route): Array<any> {
|
|
187
|
+
const route: any = to
|
|
188
|
+
? to.matched
|
|
189
|
+
? to
|
|
190
|
+
: this.resolve(to).route
|
|
191
|
+
: this.currentRoute
|
|
192
|
+
if (!route) {
|
|
193
|
+
return []
|
|
194
|
+
}
|
|
195
|
+
return [].concat.apply([], route.matched.map(m => {
|
|
196
|
+
return Object.keys(m.components).map(key => {
|
|
197
|
+
return m.components[key]
|
|
198
|
+
})
|
|
199
|
+
}))
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
resolve (
|
|
203
|
+
to: RawLocation,
|
|
204
|
+
current?: Route,
|
|
205
|
+
append?: boolean
|
|
206
|
+
): {
|
|
207
|
+
location: Location,
|
|
208
|
+
route: Route,
|
|
209
|
+
href: string,
|
|
210
|
+
// for backwards compat
|
|
211
|
+
normalizedTo: Location,
|
|
212
|
+
resolved: Route
|
|
213
|
+
} {
|
|
214
|
+
current = current || this.history.current
|
|
215
|
+
const location = normalizeLocation(
|
|
216
|
+
to,
|
|
217
|
+
current,
|
|
218
|
+
append,
|
|
219
|
+
this
|
|
220
|
+
)
|
|
221
|
+
const route = this.match(location, current)
|
|
222
|
+
const fullPath = route.redirectedFrom || route.fullPath
|
|
223
|
+
const base = this.history.base
|
|
224
|
+
const href = createHref(base, fullPath, this.mode)
|
|
225
|
+
return {
|
|
226
|
+
location,
|
|
227
|
+
route,
|
|
228
|
+
href,
|
|
229
|
+
// for backwards compat
|
|
230
|
+
normalizedTo: location,
|
|
231
|
+
resolved: route
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
addRoutes (routes: Array<RouteConfig>) {
|
|
236
|
+
this.matcher.addRoutes(routes)
|
|
237
|
+
if (this.history.current !== START) {
|
|
238
|
+
this.history.transitionTo(this.history.getCurrentLocation())
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function registerHook (list: Array<any>, fn: Function): Function {
|
|
244
|
+
list.push(fn)
|
|
245
|
+
return () => {
|
|
246
|
+
const i = list.indexOf(fn)
|
|
247
|
+
if (i > -1) list.splice(i, 1)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function createHref (base: string, fullPath: string, mode) {
|
|
252
|
+
var path = mode === 'hash' ? '#' + fullPath : fullPath
|
|
253
|
+
return base ? cleanPath(base + '/' + path) : path
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
KduRouter.install = install
|
|
257
|
+
KduRouter.version = '__VERSION__'
|
|
258
|
+
|
|
259
|
+
if (inBrowser && window.Kdu) {
|
|
260
|
+
window.Kdu.use(KduRouter)
|
|
261
|
+
}
|
package/src/install.js
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import View from './components/view'
|
|
2
|
-
import Link from './components/link'
|
|
3
|
-
|
|
4
|
-
export let _Kdu
|
|
5
|
-
|
|
6
|
-
export function install (Kdu) {
|
|
7
|
-
if (install.installed && _Kdu === Kdu) return
|
|
8
|
-
install.installed = true
|
|
9
|
-
|
|
10
|
-
_Kdu = Kdu
|
|
11
|
-
|
|
12
|
-
const isDef = v => v !== undefined
|
|
13
|
-
|
|
14
|
-
const registerInstance = (vm, callVal) => {
|
|
15
|
-
let i = vm.$options.
|
|
16
|
-
if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
|
|
17
|
-
i(vm, callVal)
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
Kdu.mixin({
|
|
22
|
-
beforeCreate () {
|
|
23
|
-
if (isDef(this.$options.router)) {
|
|
24
|
-
this._routerRoot = this
|
|
25
|
-
this._router = this.$options.router
|
|
26
|
-
this._router.init(this)
|
|
27
|
-
Kdu.util.defineReactive(this, '_route', this._router.history.current)
|
|
28
|
-
} else {
|
|
29
|
-
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
|
|
30
|
-
}
|
|
31
|
-
registerInstance(this, this)
|
|
32
|
-
},
|
|
33
|
-
destroyed () {
|
|
34
|
-
registerInstance(this)
|
|
35
|
-
}
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
Object.defineProperty(Kdu.prototype, '$router', {
|
|
39
|
-
get () { return this._routerRoot._router }
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
Object.defineProperty(Kdu.prototype, '$route', {
|
|
43
|
-
get () { return this._routerRoot._route }
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
Kdu.component('RouterView', View)
|
|
47
|
-
Kdu.component('RouterLink', Link)
|
|
48
|
-
|
|
49
|
-
const strats = Kdu.config.optionMergeStrategies
|
|
50
|
-
// use the same hook merging strategy for route hooks
|
|
51
|
-
strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created
|
|
52
|
-
}
|
|
1
|
+
import View from './components/view'
|
|
2
|
+
import Link from './components/link'
|
|
3
|
+
|
|
4
|
+
export let _Kdu
|
|
5
|
+
|
|
6
|
+
export function install (Kdu) {
|
|
7
|
+
if (install.installed && _Kdu === Kdu) return
|
|
8
|
+
install.installed = true
|
|
9
|
+
|
|
10
|
+
_Kdu = Kdu
|
|
11
|
+
|
|
12
|
+
const isDef = v => v !== undefined
|
|
13
|
+
|
|
14
|
+
const registerInstance = (vm, callVal) => {
|
|
15
|
+
let i = vm.$options._parentKnode
|
|
16
|
+
if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
|
|
17
|
+
i(vm, callVal)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
Kdu.mixin({
|
|
22
|
+
beforeCreate () {
|
|
23
|
+
if (isDef(this.$options.router)) {
|
|
24
|
+
this._routerRoot = this
|
|
25
|
+
this._router = this.$options.router
|
|
26
|
+
this._router.init(this)
|
|
27
|
+
Kdu.util.defineReactive(this, '_route', this._router.history.current)
|
|
28
|
+
} else {
|
|
29
|
+
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
|
|
30
|
+
}
|
|
31
|
+
registerInstance(this, this)
|
|
32
|
+
},
|
|
33
|
+
destroyed () {
|
|
34
|
+
registerInstance(this)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
Object.defineProperty(Kdu.prototype, '$router', {
|
|
39
|
+
get () { return this._routerRoot._router }
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
Object.defineProperty(Kdu.prototype, '$route', {
|
|
43
|
+
get () { return this._routerRoot._route }
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
Kdu.component('RouterView', View)
|
|
47
|
+
Kdu.component('RouterLink', Link)
|
|
48
|
+
|
|
49
|
+
const strats = Kdu.config.optionMergeStrategies
|
|
50
|
+
// use the same hook merging strategy for route hooks
|
|
51
|
+
strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created
|
|
52
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
|
|
3
|
+
export function runQueue (queue: Array<?NavigationGuard>, fn: Function, cb: Function) {
|
|
4
|
+
const step = index => {
|
|
5
|
+
if (index >= queue.length) {
|
|
6
|
+
cb()
|
|
7
|
+
} else {
|
|
8
|
+
if (queue[index]) {
|
|
9
|
+
fn(queue[index], () => {
|
|
10
|
+
step(index + 1)
|
|
11
|
+
})
|
|
12
|
+
} else {
|
|
13
|
+
step(index + 1)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
step(0)
|
|
18
|
+
}
|
package/src/util/dom.js
ADDED