import { Injectable , TemplateRef } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { ColorService } from '../../services/color/index' ; @ Injectable ( { providedIn : 'root' } ) export class NotificationService { /**
     *  Sets the order in which notifications are displayed:
        `above` - newer notifications will appear above older ones.
        `below` - newer notifications will appear below older ones.
     */ direction : NotificationListDirection = 'above' ; /**
     * The list of notifications including notifications that have been dismissed
     */ notifications$ : BehaviorSubject < NotificationRef [ ] > = new BehaviorSubject < NotificationRef [ ] > ( [ ] ) ; /**
     * Access the list of notifications as an array
     */ get notifications ( ) : NotificationRef [ ] { return this . notifications$ . value ; } /**
     * Define the default set of notification options
     */ options : NotificationOptions = { duration : 4 , backgroundColor : this . _colorService . getColor ( 'accent' ) . toHex ( ) , iconColor : this . _colorService . getColor ( 'accent' ) . toHex ( ) } ; constructor ( private readonly _colorService ) { } param templateRef param options param context show ( templateRef , options = this . options , context = { } ) { options = { ... this . options , ... options } ; const notificationRef = { templateRef , duration : options . duration , date : new Date ( ) , visible : true , height : options . height , spacing : options . spacing , backgroundColor : options . backgroundColor , iconColor : options . iconColor , data : context } ; this . direction === 'above' ? this . notifications . unshift ( notificationRef ) : this . notifications . push ( notificationRef ) ; this . notifications$ . next ( this . notifications ) ; if ( options . duration !== 0 ) { setTimeout ( ( ) => this . dismiss ( notificationRef ) , options . duration * 1000 ) ; } return notificationRef ; } /**
     * This function will return a list of all the notifications that have been shown.
     */ getHistory ( ) { return this . notifications ; } param notificationRef dismiss ( notificationRef ) { notificationRef . visible = false ; this . notifications$ . next ( this . notifications ) ; } /**
     * This function will dismiss any currently visible notifications.
     */ dismissAll ( ) { this . notifications . forEach ( notificationRef => notificationRef . visible = false ) ; this . notifications$ . next ( this . notifications ) ; } /** Remove the notification from the screen and from the notification history */ remove ( notificationRef ) { this . notifications$ . next ( this . notifications . filter ( _notificationRef => _notificationRef !== notificationRef ) ) ; } /** Remove all notifications from the screen and from the notification history */ removeAll ( ) { this . notifications$ . next ( [ ] ) ; } } export interface NotificationRef extends NotificationOptions { /** The content to display in the notification */ templateRef : TemplateRef < any > ; /** The datestamp to display in the notification */ date : Date ; /** Indicated whether or not the notification has been dismissed or not */ visible ? : boolean ; /** Additional data passed as template context to the notification */ data : { [ key ] : any ; } ; } export interface NotificationOptions { /** The duration the notification should display for before it is automatically dismissed */ duration : number ; /** The height of the notification */ height ? : number ; /** The spacing between each notification */ spacing ? : number ; /** The background color of the notification */ backgroundColor ? : string ; /** The color of the notification icon */ iconColor ? : string ; } export type NotificationListDirection = 'above' | 'below' ;