import { Injectable } from "@angular/core" ; import { MatSnackBar } from "@angular/material/snack-bar" ; import { Alert } from "./alert" ; import { AlertComponent } from "./alerts/alert.component" ; import { AlertDisplay } from "./alert-display" ; /**
 * Display alerts to the user as a hovering message at the bottom of the view.
 * (Angular Material "SnackBar")
 *
 * Inject this service in your classes to easily trigger alerts in the app consistent style.
 *
 * If you want to log technical details and problems, use  LoggingService instead!
 * This service is for user facing messages.
  @ Injectable ( ) export class AlertService { /** All alerts currently to be displayed */ alerts : Alert [ ] = [ ] ; constructor ( public snackBar ) { } param alert addAlert ( alert ) { this . alerts . push ( alert ) ; this . displayAlert ( alert ) ; } private displayAlert ( alert ) { const snackConfig = { data : alert , duration : 10000 , panelClass : "alerts-snackbar" , } ; switch ( alert . display ) { case AlertDisplay . NONE : return ; case AlertDisplay . TEMPORARY : snackConfig . duration = 5000 ; break ; case AlertDisplay . PERSISTENT : snackConfig . duration = 3600000 ; break ; } alert . notificationRef = this . snackBar . openFromComponent ( AlertComponent , snackConfig ) ; } param alert removeAlert ( alert ) { const index = this . alerts . indexOf ( alert , 0 ) ; if ( index > - 1 ) { this . alerts . splice ( index , 1 ) ; } } param message param display public addInfo ( message , display = AlertDisplay . TEMPORARY ) { this . addAlert ( new Alert ( message , Alert . INFO , display ) ) ; } param message param display public addWarning ( message , display = AlertDisplay . PERSISTENT ) { this . addAlert ( new Alert ( message , Alert . WARNING , display ) ) ; } param message param display public addDanger ( message , display = AlertDisplay . PERSISTENT ) { this . addAlert ( new Alert ( message , Alert . DANGER , display ) ) ; } }