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