40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
|
import { Injectable } from '@angular/core';
|
||
|
import { MatDialog } from '@angular/material/dialog';
|
||
|
import { CanDeactivate } from '@angular/router';
|
||
|
import { Observable } from 'rxjs';
|
||
|
import { TranslateService } from '@ngx-translate/core';
|
||
|
import { map } from 'rxjs/operators';
|
||
|
import { BaseComponent } from '../../core/common/base/base.component';
|
||
|
import { CheckDeactivateBaseComponent } from './deactivate.component';
|
||
|
import { ConfirmationDialogComponent } from '../confirmation-dialog/confirmation-dialog.component';
|
||
|
|
||
|
@Injectable()
|
||
|
export class CanDeactivateGuard extends BaseComponent implements CanDeactivate<CheckDeactivateBaseComponent> {
|
||
|
|
||
|
constructor(
|
||
|
private dialog: MatDialog,
|
||
|
public language: TranslateService
|
||
|
) {
|
||
|
super();
|
||
|
}
|
||
|
|
||
|
canDeactivate(component: CheckDeactivateBaseComponent): boolean | Observable<boolean> {
|
||
|
|
||
|
if (component.canDeactivate()) {
|
||
|
return true;
|
||
|
} else {
|
||
|
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
|
||
|
maxWidth: '700px',
|
||
|
data: {
|
||
|
message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.LEAVE-PAGE'),
|
||
|
warning: this.language.instant('GENERAL.CONFIRMATION-DIALOG.LEAVE-WARNING'),
|
||
|
cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL'),
|
||
|
confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.LEAVE'),
|
||
|
icon: 'error_outline'
|
||
|
}
|
||
|
});
|
||
|
return dialogRef.afterClosed().pipe(map(x => x ? true : false));
|
||
|
}
|
||
|
}
|
||
|
}
|