aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/guards
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/guards')
-rw-r--r--client/src/app/shared/guards/can-deactivate-guard.service.ts28
1 files changed, 28 insertions, 0 deletions
diff --git a/client/src/app/shared/guards/can-deactivate-guard.service.ts b/client/src/app/shared/guards/can-deactivate-guard.service.ts
new file mode 100644
index 000000000..15618f699
--- /dev/null
+++ b/client/src/app/shared/guards/can-deactivate-guard.service.ts
@@ -0,0 +1,28 @@
1import { Injectable } from '@angular/core'
2import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'
3import { Observable } from 'rxjs/Observable'
4import { ConfirmService } from '../../core/index'
5
6export interface CanComponentDeactivate {
7 canDeactivate: () => { text?: string, canDeactivate: Observable<boolean> | boolean }
8}
9
10@Injectable()
11export 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 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'
25 )
26 }
27
28}