@@ -2,6 +2,7 @@ import { computed } from '@ember/object';
22import { A } from '@ember/array' ;
33import Service , { inject as service } from '@ember/service' ;
44import { hrefTo } from 'ember-href-to/helpers/href-to' ;
5+ import { assert } from '@ember/debug' ;
56
67export default Service . extend ( {
78
@@ -16,40 +17,47 @@ export default Service.extend({
1617 this . set ( 'items' , A ( ) ) ;
1718 } ,
1819
19- allUrls : computed ( 'items.[]' , function ( ) {
20+ // Each routeParam is [ routeName, model ] where model is optional
21+ routes : computed ( 'items.[]' , function ( ) {
2022 return this . get ( 'items' ) . map ( item => {
21- let hrefToArgs = [ this , item . route ] ;
23+ let routeParams = [ item . route ] ;
2224 if ( item . model ) {
23- hrefToArgs . push ( item . model ) ;
25+ routeParams . push ( item . model ) ;
2426 }
2527
26- return hrefTo . apply ( null , hrefToArgs ) ;
28+ return routeParams ;
2729 } ) ;
2830 } ) ,
2931
30- currentUrl : computed ( 'router.router.currentURL' , function ( ) {
31- let router = this . get ( 'router.router' ) ;
32- let currentUrl = router . get ( 'rootURL' ) + router . get ( 'currentURL' ) ;
32+ routeUrls : computed ( 'routes.[]' , function ( ) {
33+ return this . get ( 'routes' ) . map ( route => {
34+ return hrefTo . apply ( null , [ this , ...route ] ) ;
35+ } ) ;
36+ } ) ,
3337
34- return currentUrl
35- . replace ( "//" , "/" ) // dedup slashes
36- . replace ( / \/ $ / , "" ) ; // remove trailing slash
38+ currentRouteIndex : computed ( 'router.router.currentURL' , 'routeUrls.[]' , function ( ) {
39+ if ( this . get ( 'routeUrls.length' ) ) {
40+ let currentURL = this . get ( 'router.router.currentURL' ) . replace ( / \/ $ / , "" ) ;
41+ let index = this . get ( 'routeUrls' ) . indexOf ( currentURL ) ;
42+ assert ( `DocsRoutes wasn't able to correctly detect the current route.` , index > - 1 ) ;
43+ return index ;
44+ }
3745 } ) ,
3846
39- previousUrl : computed ( 'allUrls.[] ' , 'currentUrl ' , function ( ) {
40- let currentIndex = this . get ( 'allUrls' ) . indexOf ( this . get ( 'currentUrl' ) ) ;
47+ nextRoute : computed ( 'currentRouteIndex ' , 'routes.[] ' , function ( ) {
48+ let currentIndex = this . get ( 'currentRouteIndex' ) ;
4149
42- if ( currentIndex > 0 ) {
43- return this . get ( 'allUrls ' ) [ ( currentIndex - 1 ) ] ;
50+ if ( currentIndex < this . get ( 'routes.length' ) ) {
51+ return this . get ( 'routes ' ) [ ( currentIndex + 1 ) ] ;
4452 }
4553 } ) ,
4654
47- nextUrl : computed ( 'allUrls.[] ' , 'currentUrl ' , function ( ) {
48- let currentIndex = this . get ( 'allUrls' ) . indexOf ( this . get ( 'currentUrl' ) ) ;
55+ previousRoute : computed ( 'currentRouteIndex ' , 'routes.[] ' , function ( ) {
56+ let currentIndex = this . get ( 'currentRouteIndex' ) ;
4957
50- if ( currentIndex < this . get ( 'allUrls.length' ) ) {
51- return this . get ( 'allUrls ' ) [ ( currentIndex + 1 ) ] ;
58+ if ( currentIndex > 0 ) {
59+ return this . get ( 'routes ' ) [ ( currentIndex - 1 ) ] ;
5260 }
5361 } )
54-
62+
5563} ) ;
0 commit comments