import { EventEmitter , Inject , Injectable } from '@angular/core' ; import { Subject } from 'rxjs' ; import { DEFAULT_ICONS } from '../consts/default-icons.const' ; import { NotificationType } from '../enums/notification-type.enum' ; import { Icons } from '../interfaces/icons' ; import { NotificationEvent } from '../interfaces/notification-event.type' ; import { Notification } from '../interfaces/notification.type' ; @ Injectable ( ) export class NotificationsService { constructor ( @ Inject ( 'options' ) public globalOptions ) { } emitter = new Subject < NotificationEvent > ( ) ; icons : Icons = DEFAULT_ICONS ; set ( notification , to ) { notification . id = notification . override && notification . override . id ? notification . override . id : Math . random ( ) . toString ( 36 ) . substring ( 3 ) ; notification . click = new EventEmitter < { } > ( ) ; notification . clickIcon = new EventEmitter < { } > ( ) ; notification . timeoutEnd = new EventEmitter < { } > ( ) ; this . emitter . next ( { command : 'set' , notification , add : to } ) ; return notification ; } success ( title = '' , content = '' , override ? , context ? ) { return this . set ( { title , content : content || '' , type : NotificationType . Success , icon : this . icons . success , override , context } , true ) ; } error ( title = '' , content = '' , override ? , context ? ) { return this . set ( { title , content : content || '' , type : NotificationType . Error , icon : this . icons . error , override , context } , true ) ; } alert ( title = '' , content = '' , override ? , context ? ) { return this . set ( { title , content : content || '' , type : NotificationType . Alert , icon : this . icons . alert , override , context } , true ) ; } info ( title = '' , content = '' , override ? , context ? ) { return this . set ( { title , content : content || '' , type : NotificationType . Info , icon : this . icons . info , override , context } , true ) ; } warn ( title = '' , content = '' , override ? , context ? ) { return this . set ( { title , content : content || '' , type : NotificationType . Warn , icon : this . icons . warn , override , context } , true ) ; } bare ( title = '' , content = '' , override ? , context ? ) { return this . set ( { title , content : content || '' , type : NotificationType . Bare , icon : 'bare' , override , context } , true ) ; } create ( title = '' , content = '' , type = NotificationType . Success , override ? , context ? ) { return this . set ( { title , content , type , icon : ( this . icons as any ) [ type ] , override , context } , true ) ; } html ( html , type = NotificationType . Success , override ? , icon = 'bare' , context ? ) { return this . set ( { html , type , icon : ( this . icons as any ) [ icon ] , override , context } , true ) ; } remove ( id ? ) { if ( id ) { this . emitter . next ( { command : 'clean' , id } ) ; } else { this . emitter . next ( { command : 'cleanAll' } ) ; } } }