11import { matchPattern } from './PatternUtils'
22
33/**
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.
65 */
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+ }
1015
16+ function getMatchingRoute ( pathname , activeRoutes , activeParams ) {
1117 let route , pattern , basename = ''
1218 for ( let i = 0 , len = activeRoutes . length ; i < len ; ++ i ) {
1319 route = activeRoutes [ i ]
1420
15- if ( ! route . path )
16- return false
21+ // if (!route.path)
22+ // return false
1723
1824 pattern = route . path || ''
1925
@@ -22,16 +28,29 @@ function pathnameIsActive(pathname, activePathname, activeRoutes, activeParams)
2228
2329 let { remainingPathname, paramNames, paramValues } = matchPattern ( pattern , pathname )
2430
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
3033
3134 basename = pattern
3235 }
3336
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
3554}
3655
3756/**
@@ -60,11 +79,10 @@ function isActive(pathname, query, indexOnly, location, routes, params) {
6079 if ( location == null )
6180 return false
6281
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 ) )
6483 return false
6584
66- return pathnameIsActive ( pathname , location . pathname , routes , params ) &&
67- queryIsActive ( query , location . query )
85+ return queryIsActive ( query , location . query )
6886}
6987
7088export default isActive
0 commit comments