1
1
import { matchPattern } from './PatternUtils'
2
2
3
3
/**
4
- * Returns true if a route and params that match the given
5
- * pathname are currently active.
4
+ * Returns true if the given pathname matches the active pathname.
6
5
*/
7
- function pathnameIsActive ( pathname , activePathname , activeRoutes , activeParams ) {
8
- if ( pathname === activePathname || activePathname . indexOf ( pathname + '/' ) === 0 )
9
- return true
6
+ function pathnameIsActive ( pathname , activePathname ) {
7
+ return pathname === activePathname || activePathname . indexOf ( pathname + '/' ) === 0
8
+ }
9
+
10
+ function paramsAreActive ( paramNames , paramValues , activeParams ) {
11
+ return paramNames . every ( function ( paramName , index ) {
12
+ return String ( paramValues [ index ] ) === String ( activeParams [ paramName ] )
13
+ } )
14
+ }
10
15
16
+ function getMatchingRoute ( pathname , activeRoutes , activeParams ) {
11
17
let route , pattern , basename = ''
12
18
for ( let i = 0 , len = activeRoutes . length ; i < len ; ++ i ) {
13
19
route = activeRoutes [ i ]
14
20
15
- if ( ! route . path )
16
- return false
21
+ // if (!route.path)
22
+ // return false
17
23
18
24
pattern = route . path || ''
19
25
@@ -22,16 +28,29 @@ function pathnameIsActive(pathname, activePathname, activeRoutes, activeParams)
22
28
23
29
let { remainingPathname, paramNames, paramValues } = matchPattern ( pattern , pathname )
24
30
25
- if ( remainingPathname === '' ) {
26
- return paramNames . every ( function ( paramName , index ) {
27
- return String ( paramValues [ index ] ) === String ( activeParams [ paramName ] )
28
- } )
29
- }
31
+ if ( remainingPathname === '' && paramsAreActive ( paramNames , paramValues , activeParams ) )
32
+ return route
30
33
31
34
basename = pattern
32
35
}
33
36
34
- return false
37
+ return null
38
+ }
39
+
40
+ /**
41
+ * Returns true if the given pathname matches the active routes
42
+ * and params.
43
+ */
44
+ function routeIsActive ( pathname , activeRoutes , activeParams , indexOnly ) {
45
+ let route = getMatchingRoute ( pathname , activeRoutes , activeParams )
46
+
47
+ if ( route == null )
48
+ return false
49
+
50
+ if ( indexOnly )
51
+ return activeRoutes . length > 1 && activeRoutes [ activeRoutes . length - 2 ] . indexRoute === route
52
+
53
+ return true
35
54
}
36
55
37
56
/**
@@ -60,11 +79,10 @@ function isActive(pathname, query, indexOnly, location, routes, params) {
60
79
if ( location == null )
61
80
return false
62
81
63
- if ( indexOnly && ( routes . length < 2 || routes [ routes . length - 2 ] . indexRoute !== routes [ routes . length - 1 ] ) )
82
+ if ( ! pathnameIsActive ( pathname , location . pathname ) && ! routeIsActive ( pathname , routes , params , indexOnly ) )
64
83
return false
65
84
66
- return pathnameIsActive ( pathname , location . pathname , routes , params ) &&
67
- queryIsActive ( query , location . query )
85
+ return queryIsActive ( query , location . query )
68
86
}
69
87
70
88
export default isActive
0 commit comments