Skip to content

Commit e06fd70

Browse files
committed
Routes not URLs
1 parent 3485a07 commit e06fd70

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

addon/components/docs-viewer/component.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export default Component.extend(EKMixin, {
2828
},
2929

3030
nextPage: Ember.on(keyDown('KeyL'), keyDown('ArrowRight'), function() {
31-
if (this.searchIsNotFocused() && this.get('docsRoutes.nextUrl')) {
32-
this.get('router').transitionTo(this.get('docsRoutes.nextUrl'));
31+
if (this.searchIsNotFocused() && this.get('docsRoutes.nextRoute')) {
32+
this.get('router').transitionTo(...this.get('docsRoutes.nextRoute'));
3333
}
3434
}),
3535

3636
previousPage: Ember.on(keyDown('KeyH'), keyDown('ArrowLeft'), function() {
37-
if (this.searchIsNotFocused() && this.get('docsRoutes.previousUrl')) {
38-
this.get('router').transitionTo(this.get('docsRoutes.previousUrl'));
37+
if (this.searchIsNotFocused() && this.get('docsRoutes.previousRoute')) {
38+
this.get('router').transitionTo(...this.get('docsRoutes.previousRoute'));
3939
}
4040
}),
4141

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{{yield}}
22

3-
{{#if docsRoutes.previousUrl}}
4-
<a href="{{docsRoutes.previousUrl}}" class='docs-viewer__page-nav'>
3+
{{#if docsRoutes.previousRoute}}
4+
{{#link-to params=docsRoutes.previousRoute class='docs-viewer__page-nav'}}
55
{{svg-jar 'left-arrow' width=32}}
6-
</a>
6+
{{/link-to}}
77
{{/if}}
88

9-
{{#if docsRoutes.nextUrl}}
10-
<a href="{{docsRoutes.nextUrl}}" class='docs-viewer__page-nav docs-viewer__page-nav--next'>
9+
{{#if docsRoutes.nextRoute}}
10+
{{#link-to params=docsRoutes.nextRoute class='docs-viewer__page-nav docs-viewer__page-nav--next'}}
1111
{{svg-jar 'right-arrow' width=32}}
12-
</a>
12+
{{/link-to}}
1313
{{/if}}

addon/services/docs-routes.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { computed } from '@ember/object';
22
import { A } from '@ember/array';
33
import Service, { inject as service } from '@ember/service';
44
import { hrefTo } from 'ember-href-to/helpers/href-to';
5+
import { assert } from '@ember/debug';
56

67
export 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

Comments
 (0)