import { Inject , Injectable } from '@angular/core' ; import { Observable , Subject , Subscription , from } from 'rxjs' ; import { SnotifyToastConfig } from '../interfaces/snotify-toast-config.interface' ; import { Snotify } from '../interfaces/snotify.interface' ; import { SnotifyTypeType } from '../types/snotify-type.type' ; import { SafeHtml } from '@angular/platform-browser' ; import { TransformArgument } from '../decorators/transform-argument.decorator' ; import { mergeDeep , uuid } from '../utils' ; import { SetToastType } from '../decorators/set-toast-type.decorator' ; import { SnotifyDefaults } from '../interfaces/snotify-defaults.interface' ; import { SnotifyToast } from '../models/snotify-toast.model' ; import { SnotifyStyle } from '../enums/snotify-style.enum' ; /**
 * SnotifyService - create, remove, config toasts
 */ @ Injectable ( ) export class SnotifyService { readonly emitter = new Subject < SnotifyToast [ ] > ( ) ; readonly toastChanged = new Subject < SnotifyToast > ( ) ; readonly toastDeleted = new Subject < number > ( ) ; private notifications : SnotifyToast [ ] = [ ] ; constructor ( @ Inject ( 'SnotifyToastConfig' ) public config ) { } /**
     * emit changes in notifications array
     */ private emit ( ) { this . emitter . next ( this . notifications . slice ( ) ) ; } param id return get ( id ) { return this . notifications . find ( toast => toast . id === id ) ; } param toast private add ( toast ) { if ( this . config . global . filterDuplicates && this . containsToast ( toast ) ) { return ; } if ( this . config . global . newOnTop ) { this . notifications . unshift ( toast ) ; } else { this . notifications . push ( toast ) ; } this . emit ( ) ; } param inToast returns private containsToast ( inToast ) { return this . notifications . some ( toast => toast . equals ( inToast ) ) ; } param id param remove remove ( id ? , remove ? ) { if ( ! id ) { return this . clear ( ) ; } else if ( remove ) { this . notifications = this . notifications . filter ( toast => toast . id !== id ) ; return this . emit ( ) ; } this . toastDeleted . next ( id ) ; } /**
     * Clear notifications array
     */ clear ( ) { this . notifications = [ ] ; this . emit ( ) ; } param snotify return create ( snotify ) { const config = mergeDeep ( this . config . toast , this . config . type [ snotify . config . type ] , snotify . config ) ; const toast = new SnotifyToast ( uuid ( ) , snotify . title , snotify . body , config ) ; this . add ( toast ) ; return toast ; } setDefaults ( defaults ) { return ( this . config = mergeDeep ( this . config , defaults ) as SnotifyDefaults ) ; } param body returns simple ( body ) ; param body param title returns simple ( body , title ) ; param body param config returns simple ( body , config ) ; param body param title param config returns simple ( body , title , config ) ; /**
     * Transform toast arguments into Snotify object
     */ @ TransformArgument @ SetToastType simple ( args ) { return this . create ( args ) ; } param body returns success ( body ) ; param body param title returns success ( body , title ) ; param body param config returns success ( body , config ) ; param body param title param config returns success ( body , title , config ) ; /**
     * Transform toast arguments into Snotify object
     */ @ TransformArgument @ SetToastType success ( args ) { return this . create ( args ) ; } param body returns error ( body ) ; param body param title returns error ( body , title ) ; param body param config returns error ( body , config ) ; param body param title param config returns error ( body , title , config ) ; /**
     * Transform toast arguments into Snotify object
     */ @ TransformArgument @ SetToastType error ( args ) { return this . create ( args ) ; } param body returns info ( body ) ; param body param title returns info ( body , title ) ; param body param config returns info ( body , config ) ; param body param title param config returns info ( body , title , config ) ; /**
     * Transform toast arguments into Snotify object
     */ @ TransformArgument @ SetToastType info ( args ) { return this . create ( args ) ; } param body returns warning ( body ) ; param body param title returns warning ( body , title ) ; param body param config returns warning ( body , config ) ; param body param title param config returns warning ( body , title , config ) ; /**
     * Transform toast arguments into Snotify object
     */ @ TransformArgument @ SetToastType warning ( args ) { return this . create ( args ) ; } param body returns confirm ( body ) ; param body param title returns confirm ( body , title ) ; param body param config returns confirm ( body , config ) ; param body param title param config returns confirm ( body , title , config ) ; /**
     * Transform toast arguments into Snotify object
     */ @ TransformArgument @ SetToastType confirm ( args ) { return this . create ( args ) ; } param body returns prompt ( body ) ; param body param title returns prompt ( body , title ) ; param body param config returns prompt ( body , config ) ; param body param title param config returns prompt ( body , title , config ) ; /**
     * Transform toast arguments into Snotify object
     */ @ TransformArgument @ SetToastType prompt ( args ) { return this . create ( args ) ; } param body param action returns async ( body , action ) ; param body param title param action returns async ( body , title , action ) ; param body param action param config returns async ( body , action , config ) ; param body param title param action param config returns async ( body , title , action , config ) ; /**
     * Transform toast arguments into Snotify object
     */ @ TransformArgument @ SetToastType async ( args ) { let async ; if ( args . action instanceof Promise ) { async = from ( args . action ) ; } else { async = args . action ; } const toast = this . create ( args ) ; toast . on ( 'mounted' , ( ) => { const subscription = async . subscribe ( ( next ? ) => { this . mergeToast ( toast , next ) ; } , ( error ? ) => { this . mergeToast ( toast , error , SnotifyStyle . error ) ; subscription . unsubscribe ( ) ; } , ( ) => { this . mergeToast ( toast , { } , SnotifyStyle . success ) ; subscription . unsubscribe ( ) ; } ) ; } ) ; return toast ; } private mergeToast ( toast , next , type ? ) { if ( next . body ) { toast . body = next . body ; } if ( next . title ) { toast . title = next . title ; } if ( type ) { toast . config = mergeDeep ( toast . config , this . config . global , this . config . toast [ type ] , { type } , next . config ) ; } else { toast . config = mergeDeep ( toast . config , next . config ) ; } if ( next . html ) { toast . config . html = next . html ; } this . emit ( ) ; this . toastChanged . next ( toast ) ; } param html param config returns html ( html , config ? ) { return this . create ( { title : null , body : null , config : { ... config , ... { html } } } ) ; } }