-
Notifications
You must be signed in to change notification settings - Fork 63
enabled arrow icons to jump to previous and next lessons #1041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 22 commits
6b8794c
7798eb3
d3758f2
0667163
e3be024
bfc44b7
89831ea
e84f72b
caeb0bc
60f6141
5fe9484
52958ef
feb7744
ef0f7d5
e91ae03
d9ad958
f524ec9
28ddb49
4126829
65c6403
f5acd63
c9c3956
5bde57b
154b746
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { MenuRouteService } from './menu-route.service'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { MENU_ROUTES, MenuRoutes } from '../../common'; | ||
|
||
describe('MenuRouteService', () => { | ||
const menuRoutes: MenuRoutes = [ | ||
{ | ||
path: 'previouslesson', | ||
prod: true | ||
}, | ||
{ | ||
path: 'currentlesson', | ||
prod: true | ||
}, | ||
{ | ||
path: 'nextlesson', | ||
prod: true | ||
} | ||
]; | ||
|
||
const activatedRouteStub = { | ||
snapshot: { | ||
pathFromRoot: [ | ||
{ | ||
routeConfig: menuRoutes[1] | ||
} | ||
] | ||
} | ||
} as ActivatedRoute; | ||
|
||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
providers: [{ provide: MENU_ROUTES, useValue: menuRoutes }] | ||
}) | ||
); | ||
|
||
it('should be created', () => { | ||
const service: MenuRouteService = TestBed.inject(MenuRouteService); | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('getPreviousLink should return previouslesson', () => { | ||
const service: MenuRouteService = TestBed.inject(MenuRouteService); | ||
const previousLink = service.getPreviousLink(activatedRouteStub); | ||
expect(previousLink).toEqual('../../' + menuRoutes[0].path); | ||
}); | ||
|
||
it('getNextLink should return nextlesson', () => { | ||
const service: MenuRouteService = TestBed.inject(MenuRouteService); | ||
const nextLink = service.getNextLink(activatedRouteStub); | ||
expect(nextLink).toEqual('../../' + menuRoutes[2].path); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Injectable, Inject } from '@angular/core'; | ||
import { ActivatedRoute, Router } from '@angular/router'; | ||
import { MENU_ROUTES, MenuRoute } from '../../common'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class MenuRouteService { | ||
constructor(@Inject(MENU_ROUTES) private readonly menuRoutes) {} | ||
|
||
getPreviousLink(activeRoute: ActivatedRoute): string { | ||
const index = this.getCurrentIndex(activeRoute); | ||
if (index > 0) { | ||
return this.getMenuRoutePathByIndex(index - 1); | ||
} | ||
return ''; | ||
} | ||
|
||
getNextLink(activeRoute: ActivatedRoute): string { | ||
const index = this.getCurrentIndex(activeRoute); | ||
if (index < this.menuRoutes.length - 1) { | ||
NothingEverHappens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this.getMenuRoutePathByIndex(index + 1); | ||
} | ||
return ''; | ||
} | ||
|
||
private getCurrentIndex(activeRoute: ActivatedRoute): number { | ||
// TODO: figure out a way to inject the ActivatedRoute instead of parameter | ||
// This method gets the index of the current menuRoute. Ideally we should be able | ||
// to inject in the ActivatedRoute in the constructor. However we noticed that | ||
// probably because this is a service, activatedRoute has the value when the | ||
// service is constructed and not the current activated route. We are using a | ||
// workaround now which expects the calling method to pass the current activated | ||
// route. Fix this to use DI. | ||
const config = activeRoute.snapshot.pathFromRoot | ||
.map(a => a.routeConfig) | ||
.find(r => r && (r as MenuRoute).prod); | ||
if (config == null) { | ||
return -1; | ||
} | ||
const index = this.menuRoutes.findIndex(c => c.path === config.path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for an intermediate var, just return? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ☝️ |
||
return index; | ||
} | ||
|
||
private getMenuRouteByIndex(index: number) { | ||
if (index >= 0 && index < this.menuRoutes.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need these checks? Can it be just |
||
return this.menuRoutes[index]; | ||
} | ||
return null; | ||
} | ||
|
||
private getMenuRoutePathByIndex(index: number): string { | ||
const indexRoute = this.getMenuRouteByIndex(index); | ||
if (indexRoute != null) { | ||
let path = indexRoute.path; | ||
if (path) { | ||
path = '../../' + path; | ||
} | ||
return path; | ||
} | ||
return ''; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { Component, Input, OnInit } from '@angular/core'; | ||
import { SlidesDeckComponent } from '@codelab/slides/src/lib/deck/deck.component'; | ||
import { MenuRouteService } from '../../../codelabs/angular/menu-route.service'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
|
||
@Component({ | ||
selector: 'codelab-closing-slide', | ||
|
@@ -10,7 +13,16 @@ export class CodelabClosingSlideComponent implements OnInit { | |
@Input() body: String; | ||
@Input() footer: String; | ||
|
||
constructor() {} | ||
constructor( | ||
private readonly activeRoute: ActivatedRoute, | ||
private readonly menuRouteService: MenuRouteService, | ||
private readonly presentation: SlidesDeckComponent | ||
) { | ||
if (this.presentation != null) { | ||
const nextLink = this.menuRouteService.getNextLink(this.activeRoute); | ||
this.presentation.setNext(nextLink); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drop empty ngOnInit while you're here? |
||
} | ||
} | ||
|
||
ngOnInit() {} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.