2018-11-09 13:44:39 +01:00
|
|
|
import { OnDestroy } from '@angular/core';
|
2018-11-02 11:41:03 +01:00
|
|
|
import { Title } from '@angular/platform-browser';
|
|
|
|
import { MatSnackBar, MatSnackBarRef, SimpleSnackBar } from '@angular/material';
|
2018-11-09 13:44:39 +01:00
|
|
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
|
|
|
import { BaseComponent } from '../../base.component';
|
2019-02-22 17:17:29 +01:00
|
|
|
import { Subscription } from 'rxjs';
|
2018-11-02 11:41:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A base class for all views. Implements a generic error handling by raising a snack bar
|
|
|
|
* with the error. The error is dismissed, if the component is destroyed, so if the
|
|
|
|
* view is leaved.
|
|
|
|
*/
|
|
|
|
export abstract class BaseViewComponent extends BaseComponent implements OnDestroy {
|
|
|
|
/**
|
|
|
|
* A reference to the current error snack bar.
|
|
|
|
*/
|
2019-02-21 12:18:10 +01:00
|
|
|
private messageSnackBar: MatSnackBarRef<SimpleSnackBar>;
|
2018-11-02 11:41:03 +01:00
|
|
|
|
2019-02-22 17:17:29 +01:00
|
|
|
/**
|
|
|
|
* Subscriptions added to this list will be cleared 'on destroy'
|
|
|
|
*/
|
|
|
|
protected subscriptions: Subscription[];
|
|
|
|
|
2018-11-02 11:41:03 +01:00
|
|
|
/**
|
|
|
|
* Constructor for bas elist views
|
|
|
|
* @param titleService the title serivce, passed to the base component
|
|
|
|
* @param translate the translate service, passed to the base component
|
|
|
|
* @param matSnackBar the snack bar service. Needed for showing errors.
|
|
|
|
*/
|
|
|
|
public constructor(titleService: Title, translate: TranslateService, private matSnackBar: MatSnackBar) {
|
|
|
|
super(titleService, translate);
|
2019-02-22 17:17:29 +01:00
|
|
|
this.subscriptions = [];
|
2018-11-02 11:41:03 +01:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:16:06 +01:00
|
|
|
/**
|
|
|
|
* automatically dismisses the error snack bar and clears subscriptions
|
|
|
|
* if the component is destroyed.
|
|
|
|
*/
|
|
|
|
public ngOnDestroy(): void {
|
|
|
|
if (this.messageSnackBar) {
|
|
|
|
this.messageSnackBar.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cleanSubjects();
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:18:10 +01:00
|
|
|
/**
|
|
|
|
* Opens the snack bar with the given message.
|
|
|
|
* This snack bar will only dismiss if the user clicks the 'OK'-button.
|
|
|
|
*/
|
|
|
|
protected raiseWarning = (message: string): void => {
|
|
|
|
this.messageSnackBar = this.matSnackBar.open(message, this.translate.instant('OK'));
|
|
|
|
};
|
|
|
|
|
2018-11-02 11:41:03 +01:00
|
|
|
/**
|
|
|
|
* Opens an error snack bar with the given error message.
|
|
|
|
* This is implemented as an arrow function to capture the called `this`. You can use this function
|
|
|
|
* as callback (`.then(..., this.raiseError)`) instead of doing `this.raiseError.bind(this)`.
|
|
|
|
* @param message The message to show.
|
|
|
|
*/
|
|
|
|
protected raiseError = (message: string): void => {
|
2019-02-21 12:18:10 +01:00
|
|
|
this.messageSnackBar = this.matSnackBar.open(message, this.translate.instant('OK'), {
|
2018-11-02 11:41:03 +01:00
|
|
|
duration: 0
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-02-21 12:18:10 +01:00
|
|
|
/**
|
|
|
|
* Function to manually close the snack bar if it will not automatically close
|
|
|
|
* or it should close in a previous step.
|
|
|
|
*/
|
|
|
|
protected closeSnackBar(): void {
|
|
|
|
if (this.matSnackBar) {
|
|
|
|
this.matSnackBar.dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:30:16 +01:00
|
|
|
/**
|
2019-03-13 15:16:06 +01:00
|
|
|
* Manually clears all stored subscriptions.
|
|
|
|
* Necessary for manual routing control, since the Angular
|
|
|
|
* life cycle does not accept that navigation to the same URL
|
|
|
|
* executes the life cycle again
|
2018-11-02 11:41:03 +01:00
|
|
|
*/
|
2019-03-13 15:16:06 +01:00
|
|
|
protected cleanSubjects(): void {
|
2019-02-22 17:17:29 +01:00
|
|
|
if (this.subscriptions.length > 0) {
|
|
|
|
for (const sub of this.subscriptions) {
|
|
|
|
sub.unsubscribe();
|
|
|
|
}
|
2019-03-13 15:16:06 +01:00
|
|
|
this.subscriptions = [];
|
2019-02-22 17:17:29 +01:00
|
|
|
}
|
2018-11-02 11:41:03 +01:00
|
|
|
}
|
2019-03-13 15:16:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* To catch swipe gestures.
|
|
|
|
* Should be overwritten by children which need swipe gestures
|
|
|
|
*/
|
|
|
|
protected swipe(e: TouchEvent, when: string): void {}
|
2018-11-02 11:41:03 +01:00
|
|
|
}
|