]>
Commit | Line | Data |
---|---|---|
529479f9 DG |
1 | import { Injectable } from '@angular/core' |
2 | import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router' | |
3 | import { Observable } from 'rxjs/Observable' | |
f6a043df | 4 | import { ConfirmService } from '../../core/index' |
529479f9 DG |
5 | |
6 | export interface CanComponentDeactivate { | |
f6a043df | 7 | canDeactivate: () => { text?: string, canDeactivate: Observable<boolean> | boolean } |
529479f9 DG |
8 | } |
9 | ||
10 | @Injectable() | |
11 | export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> { | |
12 | constructor (private confirmService: ConfirmService) { } | |
13 | ||
14 | canDeactivate (component: CanComponentDeactivate, | |
15 | currentRoute: ActivatedRouteSnapshot, | |
16 | currentState: RouterStateSnapshot, | |
17 | nextState: RouterStateSnapshot | |
1f30a185 | 18 | ) { |
f6a043df C |
19 | const result = component.canDeactivate() |
20 | const text = result.text || 'All unsaved data will be lost, are you sure you want to leave this page?' | |
21 | ||
22 | return result.canDeactivate || this.confirmService.confirm( | |
23 | text, | |
24 | 'Warning' | |
529479f9 DG |
25 | ) |
26 | } | |
27 | ||
28 | } |