1
1
/*!
2
- * vue-router v3.5.4
2
+ * vue-router v3.6.0
3
3
* (c) 2022 Evan You
4
4
* @license MIT
5
5
*/
@@ -9,25 +9,32 @@ Object.defineProperty(exports, '__esModule', { value: true });
9
9
10
10
var vue = require ( 'vue' ) ;
11
11
12
+ // dev only warn if no current instance
13
+
14
+ function throwNoCurrentInstance ( method ) {
15
+ if ( ! vue . getCurrentInstance ( ) ) {
16
+ throw new Error (
17
+ ( "[vue-router]: Missing current instance. " + method + "() must be called inside <script setup> or setup()." )
18
+ )
19
+ }
20
+ }
21
+
12
22
function useRouter ( ) {
13
- var i = vue . getCurrentInstance ( ) ;
14
- if ( process . env . NODE_ENV !== 'production' && ! i ) {
23
+ if ( process . env . NODE_ENV !== 'production' ) {
15
24
throwNoCurrentInstance ( 'useRouter' ) ;
16
25
}
17
26
18
- return i . proxy . $root . $router
27
+ return vue . getCurrentInstance ( ) . proxy . $root . $router
19
28
}
20
29
21
30
function useRoute ( ) {
22
- var i = vue . getCurrentInstance ( ) ;
23
- if ( process . env . NODE_ENV !== 'production' && ! i ) {
31
+ if ( process . env . NODE_ENV !== 'production' ) {
24
32
throwNoCurrentInstance ( 'useRoute' ) ;
25
33
}
26
34
27
- var root = i . proxy . $root ;
35
+ var root = vue . getCurrentInstance ( ) . proxy . $root ;
28
36
if ( ! root . _$route ) {
29
- var route = vue . effectScope ( true ) . run (
30
- function ( ) { return vue . shallowReactive ( Object . assign ( { } , root . $router . currentRoute ) ) ; }
37
+ var route = vue . effectScope ( true ) . run ( function ( ) { return vue . shallowReactive ( Object . assign ( { } , root . $router . currentRoute ) ) ; }
31
38
) ;
32
39
root . _$route = route ;
33
40
@@ -39,49 +46,208 @@ function useRoute () {
39
46
return root . _$route
40
47
}
41
48
42
- // TODO:
43
- // export function useLink () {}
44
-
45
49
function onBeforeRouteUpdate ( guard ) {
46
- var i = vue . getCurrentInstance ( ) ;
47
- if ( process . env . NODE_ENV !== 'production' && ! i ) {
50
+ if ( process . env . NODE_ENV !== 'production' ) {
48
51
throwNoCurrentInstance ( 'onBeforeRouteUpdate' ) ;
49
52
}
50
53
54
+ return useFilteredGuard ( guard , isUpdateNavigation )
55
+ }
56
+ function isUpdateNavigation ( to , from , depth ) {
57
+ var toMatched = to . matched ;
58
+ var fromMatched = from . matched ;
59
+ return (
60
+ toMatched . length >= depth &&
61
+ toMatched
62
+ . slice ( 0 , depth + 1 )
63
+ . every ( function ( record , i ) { return record === fromMatched [ i ] ; } )
64
+ )
65
+ }
66
+
67
+ function isLeaveNavigation ( to , from , depth ) {
68
+ var toMatched = to . matched ;
69
+ var fromMatched = from . matched ;
70
+ return toMatched . length < depth || toMatched [ depth ] !== fromMatched [ depth ]
71
+ }
72
+
73
+ function onBeforeRouteLeave ( guard ) {
74
+ if ( process . env . NODE_ENV !== 'production' ) {
75
+ throwNoCurrentInstance ( 'onBeforeRouteLeave' ) ;
76
+ }
77
+
78
+ return useFilteredGuard ( guard , isLeaveNavigation )
79
+ }
80
+
81
+ var noop = function ( ) { } ;
82
+ function useFilteredGuard ( guard , fn ) {
83
+ var instance = vue . getCurrentInstance ( ) ;
51
84
var router = useRouter ( ) ;
52
85
53
- var target = i . proxy ;
54
- // find the nearest routerview to know the depth
55
- while ( target && target . $vnode && target . $vnode . data && target . $vnode . data . routerViewDepth == null ) {
86
+ var target = instance . proxy ;
87
+ // find the nearest RouterView to know the depth
88
+ while (
89
+ target &&
90
+ target . $vnode &&
91
+ target . $vnode . data &&
92
+ target . $vnode . data . routerViewDepth == null
93
+ ) {
56
94
target = target . $parent ;
57
95
}
58
96
59
- var depth = target && target . $vnode && target . $vnode . data ? target . $vnode . data . routerViewDepth : null ;
97
+ var depth =
98
+ target && target . $vnode && target . $vnode . data
99
+ ? target . $vnode . data . routerViewDepth
100
+ : null ;
60
101
61
- console . log ( 'found depth' , depth ) ;
102
+ if ( depth != null ) {
103
+ var removeGuard = router . beforeEach ( function ( to , from , next ) {
104
+ return fn ( to , from , depth ) ? guard ( to , from , next ) : next ( )
105
+ } ) ;
62
106
63
- // TODO: allow multiple guards?
64
- i . proxy . $options . beforeRouteUpdate = guard ;
107
+ vue . onUnmounted ( removeGuard ) ;
108
+ return removeGuard
109
+ }
65
110
66
- var removeGuard = router . beforeEach ( function ( to , from , next ) {
67
- // TODO: check it's an update
68
- return guard ( to , from , next )
69
- } ) ;
111
+ return noop
112
+ }
113
+
114
+ /* */
115
+
116
+ function guardEvent ( e ) {
117
+ // don't redirect with control keys
118
+ if ( e . metaKey || e . altKey || e . ctrlKey || e . shiftKey ) { return }
119
+ // don't redirect when preventDefault called
120
+ if ( e . defaultPrevented ) { return }
121
+ // don't redirect on right click
122
+ if ( e . button !== undefined && e . button !== 0 ) { return }
123
+ // don't redirect if `target="_blank"`
124
+ if ( e . currentTarget && e . currentTarget . getAttribute ) {
125
+ var target = e . currentTarget . getAttribute ( 'target' ) ;
126
+ if ( / \b _ b l a n k \b / i. test ( target ) ) { return }
127
+ }
128
+ // this may be a Weex event which doesn't have this method
129
+ if ( e . preventDefault ) {
130
+ e . preventDefault ( ) ;
131
+ }
132
+ return true
133
+ }
70
134
71
- vue . onUnmounted ( removeGuard ) ;
135
+ function includesParams ( outer , inner ) {
136
+ var loop = function ( key ) {
137
+ var innerValue = inner [ key ] ;
138
+ var outerValue = outer [ key ] ;
139
+ if ( typeof innerValue === 'string' ) {
140
+ if ( innerValue !== outerValue ) { return { v : false } }
141
+ } else {
142
+ if (
143
+ ! Array . isArray ( outerValue ) ||
144
+ outerValue . length !== innerValue . length ||
145
+ innerValue . some ( function ( value , i ) { return value !== outerValue [ i ] ; } )
146
+ ) {
147
+ return { v : false }
148
+ }
149
+ }
150
+ } ;
151
+
152
+ for ( var key in inner ) {
153
+ var returned = loop ( key ) ;
154
+
155
+ if ( returned ) return returned . v ;
156
+ }
72
157
73
- return removeGuard
158
+ return true
74
159
}
75
160
76
- // TODO:
77
- // export function onBeforeRouteLeave () {}
161
+ // helpers from vue router 4
78
162
79
- function throwNoCurrentInstance ( method ) {
80
- throw new Error (
81
- ( "[vue-router]: Missing current instance. " + method + "() must be called inside <script setup> or setup()." )
82
- )
163
+ function isSameRouteLocationParamsValue ( a , b ) {
164
+ return Array . isArray ( a )
165
+ ? isEquivalentArray ( a , b )
166
+ : Array . isArray ( b )
167
+ ? isEquivalentArray ( b , a )
168
+ : a === b
169
+ }
170
+
171
+ function isEquivalentArray ( a , b ) {
172
+ return Array . isArray ( b )
173
+ ? a . length === b . length && a . every ( function ( value , i ) { return value === b [ i ] ; } )
174
+ : a . length === 1 && a [ 0 ] === b
175
+ }
176
+
177
+ function isSameRouteLocationParams ( a , b ) {
178
+ if ( Object . keys ( a ) . length !== Object . keys ( b ) . length ) { return false }
179
+
180
+ for ( var key in a ) {
181
+ if ( ! isSameRouteLocationParamsValue ( a [ key ] , b [ key ] ) ) { return false }
182
+ }
183
+
184
+ return true
185
+ }
186
+
187
+ function useLink ( props ) {
188
+ if ( process . env . NODE_ENV !== 'production' ) {
189
+ throwNoCurrentInstance ( 'useLink' ) ;
190
+ }
191
+
192
+ var router = useRouter ( ) ;
193
+ var currentRoute = useRoute ( ) ;
194
+
195
+ var resolvedRoute = vue . computed ( function ( ) { return router . resolve ( vue . unref ( props . to ) , currentRoute ) ; } ) ;
196
+
197
+ var activeRecordIndex = vue . computed ( function ( ) {
198
+ var route = resolvedRoute . value . route ;
199
+ var matched = route . matched ;
200
+ var length = matched . length ;
201
+ var routeMatched = matched [ length - 1 ] ;
202
+ var currentMatched = currentRoute . matched ;
203
+ if ( ! routeMatched || ! currentMatched . length ) { return - 1 }
204
+ var index = currentMatched . indexOf ( routeMatched ) ;
205
+ if ( index > - 1 ) { return index }
206
+ // possible parent record
207
+ var parentRecord = currentMatched [ currentMatched . length - 2 ] ;
208
+
209
+ return (
210
+ // we are dealing with nested routes
211
+ length > 1 &&
212
+ // if the parent and matched route have the same path, this link is
213
+ // referring to the empty child. Or we currently are on a different
214
+ // child of the same parent
215
+ parentRecord && parentRecord === routeMatched . parent
216
+ )
217
+ } ) ;
218
+
219
+ var isActive = vue . computed (
220
+ function ( ) { return activeRecordIndex . value > - 1 &&
221
+ includesParams ( currentRoute . params , resolvedRoute . value . route . params ) ; }
222
+ ) ;
223
+ var isExactActive = vue . computed (
224
+ function ( ) { return activeRecordIndex . value > - 1 &&
225
+ activeRecordIndex . value === currentRoute . matched . length - 1 &&
226
+ isSameRouteLocationParams ( currentRoute . params , resolvedRoute . value . route . params ) ; }
227
+ ) ;
228
+
229
+ var navigate = function ( e ) {
230
+ var href = resolvedRoute . value . route ;
231
+ if ( guardEvent ( e ) ) {
232
+ return props . replace
233
+ ? router . replace ( href )
234
+ : router . push ( href )
235
+ }
236
+ return Promise . resolve ( )
237
+ } ;
238
+
239
+ return {
240
+ href : vue . computed ( function ( ) { return resolvedRoute . value . href ; } ) ,
241
+ route : vue . computed ( function ( ) { return resolvedRoute . value . route ; } ) ,
242
+ isExactActive : isExactActive ,
243
+ isActive : isActive ,
244
+ navigate : navigate
245
+ }
83
246
}
84
247
248
+ exports . isSameRouteLocationParams = isSameRouteLocationParams ;
249
+ exports . onBeforeRouteLeave = onBeforeRouteLeave ;
85
250
exports . onBeforeRouteUpdate = onBeforeRouteUpdate ;
251
+ exports . useLink = useLink ;
86
252
exports . useRoute = useRoute ;
87
253
exports . useRouter = useRouter ;
0 commit comments