diff options
Diffstat (limited to 'client/src/app/shared')
-rw-r--r-- | client/src/app/shared/can-deactivate-guard.service.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/client/src/app/shared/can-deactivate-guard.service.ts b/client/src/app/shared/can-deactivate-guard.service.ts new file mode 100644 index 000000000..4239df737 --- /dev/null +++ b/client/src/app/shared/can-deactivate-guard.service.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router' | ||
3 | import { Observable } from 'rxjs/Observable' | ||
4 | import { ConfirmService } from '../core' | ||
5 | |||
6 | export interface CanComponentDeactivate { | ||
7 | canDeactivate: () => Observable<boolean> | boolean | ||
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 | ||
18 | ): Observable<boolean> | boolean { | ||
19 | return component.canDeactivate() || this.confirmService.confirm( | ||
20 | 'All unsaved data will be lost, are you sure you want to leave ?', | ||
21 | 'Unsaved Data' | ||
22 | ) | ||
23 | } | ||
24 | |||
25 | } | ||