import Vue from 'vue' ; import { SnotifyToast } from './components/toast.model' ; import { ToastDefaults } from './toastDefaults' ; import { SnotifyToastConfig , Snotify , SnotifyDefaults } from './interfaces' ; import { SnotifyStyle } from './enums' ; import { SnotifyType } from './types' ; import { TransformArgument } from './decorators/transform-argument.decorator' ; import { mergeDeep , uuid } from './utils' ; import { SetToastType } from './decorators/set-toast-type.decorator' ; /**
 * this - create, remove, config toasts
 */ export class SnotifyService { readonly emitter = new Vue ( ) ; notifications : SnotifyToast [ ] = [ ] ; config : SnotifyDefaults = ToastDefaults ; /**
     * emit changes in notifications array
     */ emit ( ) { this . emitter . $emit ( 'snotify' , this . notifications . slice ( ) ) ; } param id { Number } return { SnotifyToast | undefined } get ( id ) { return this . notifications . find ( toast => toast . id === id ) ; } param toast { SnotifyToast } add ( toast ) { if ( this . config . global . newOnTop ) { this . notifications . unshift ( toast ) ; } else { this . notifications . push ( toast ) ; } this . emit ( ) ; } param id { number } param remove { boolean } remove ( id ? , remove ? ) { if ( ! id ) { return this . clear ( ) ; } else if ( remove ) { this . notifications = this . notifications . filter ( toast => toast . id !== id ) ; return this . emit ( ) ; } this . emitter . $emit ( 'remove' , id ) ; } /**
     * Clear notifications array
     */ clear ( ) { this . notifications = [ ] ; this . emit ( ) ; } button ( text , closeOnClick = true , action = null , bold = false ) { return { text , action : closeOnClick ? ( toast ) => { action ( toast ) ; this . remove ( toast . id ) ; } : action , bold } ; } param snotify { Snotify } return { number } create ( snotify ) { if ( this . config . global . oneAtTime && this . notifications . length !== 0 ) return ; if ( this . config . global . preventDuplicates && this . notifications . filter ( t => t . config . type === snotify . config . type ) . length === 1 ) return ; const config = mergeDeep ( this . config . toast , this . config . type [ snotify . config . type ] , snotify . config ) as SnotifyToastConfig ; const toast = new SnotifyToast ( config . id ? config . id : uuid ( ) , snotify . title , snotify . body , config ) ; this . add ( toast ) ; return toast ; } setDefaults ( defaults ) { return this . config = mergeDeep ( this . config , defaults ) as SnotifyDefaults ; } param body { String } returns { number } simple ( body ) ; param body { String } param title { String } returns { number } simple ( body , title ) ; param body { String } param config { SnotifyToastConfig } returns { number } simple ( body , config ) ; param body { String } param title { String } param config { SnotifyToastConfig } returns { number } simple ( body , title , config ) ; /**
     * Transform toast arguments into {Snotify} object
     */ @ TransformArgument @ SetToastType simple ( args ) { return this . create ( args ) ; } param body { String } returns { number } success ( body ) ; param body { String } param title { String } returns { number } success ( body , title ) ; param body { String } param config { SnotifyToastConfig } returns { number } success ( body , config ) ; param body { String } param title { String } param config { SnotifyToastConfig } returns { number } success ( body , title , config ) ; /**
     * Transform toast arguments into {Snotify} object
     */ @ TransformArgument @ SetToastType success ( args ) { return this . create ( args ) ; } param body { String } returns { number } error ( body ) ; param body { String } param title { String } returns { number } error ( body , title ) ; param body { String } param config { SnotifyToastConfig } returns { number } error ( body , config ) ; param body { String } param title { String } param config { SnotifyToastConfig } returns { number } error ( body , title , config ) ; /**
     * Transform toast arguments into {Snotify} object
     */ @ TransformArgument @ SetToastType error ( args ) { return this . create ( args ) ; } param body { String } returns { number } info ( body ) ; param body { String } param title { String } returns { number } info ( body , title ) ; param body { String } param config { SnotifyToastConfig } returns { number } info ( body , config ) ; param body { String } param title { String } param config { SnotifyToastConfig } returns { number } info ( body , title , config ) ; /**
     * Transform toast arguments into {Snotify} object
     */ @ TransformArgument @ SetToastType info ( args ) { return this . create ( args ) ; } param body { String } returns { number } warning ( body ) ; param body { String } param title { String } returns { number } warning ( body , title ) ; param body { String } param config { SnotifyToastConfig } returns { number } warning ( body , config ) ; param body { String } param title { String } param config { SnotifyToastConfig } returns { number } warning ( body , title , config ) ; /**
     * Transform toast arguments into {Snotify} object
     */ @ TransformArgument @ SetToastType warning ( args ) { return this . create ( args ) ; } param body { String } returns { number } confirm ( body ) ; param body { String } param title { String } returns { number } confirm ( body , title ) ; param body { String } param config { SnotifyToastConfig } returns { number } confirm ( body , config ) ; param body { String } param title { String } param config { SnotifyToastConfig } returns { number } confirm ( body , title , config ) ; /**
     * Transform toast arguments into {Snotify} object
     */ @ TransformArgument @ SetToastType confirm ( args ) { return this . create ( args ) ; } param body { String } returns { number } prompt ( body ) ; param body { String } param title { String } returns { number } prompt ( body , title ) ; param body { String } param config { SnotifyToastConfig } returns { number } prompt ( body , config ) ; param body { String } param title { String } param config { SnotifyToastConfig } returns { number } prompt ( body , title , config ) ; /**
     * Transform toast arguments into {Snotify} object
     */ @ TransformArgument @ SetToastType prompt ( args ) { return this . create ( args ) ; } param body { String } param action { ( ) => Promise < Snotify > } returns { number } async ( body , action ) ; param body { String } param title { String } param action { ( ) => Promise < Snotify > } returns { number } async ( body , title , action ) ; param body { String } param action { ( ) => Promise < Snotify > } param config { SnotifyToastConfig } returns { number } async ( body , action , config ) ; param body { String } param title { String } param action { ( ) => Promise < Snotify > } param config { SnotifyToastConfig } returns { number } async ( body , title , action , config ) ; /**
     * Transform toast arguments into {Snotify} object
     */ @ TransformArgument @ SetToastType async ( args ) { let async = args . action ; const toast = this . create ( args ) ; toast . on ( 'mounted' , ( ) => { async ( ) . then ( ( next ) => this . mergeToast ( toast , next , SnotifyStyle . success ) ) . catch ( ( error ? ) => this . mergeToast ( toast , error , SnotifyStyle . error ) ) ; } ) ; return toast ; } 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 . emitter . $emit ( 'toastChanged' , toast ) ; } param { string } html param { SnotifyToastConfig } config returns { number } html ( html , config ? ) { return this . create ( { title : null , body : null , config : { ... config , ... { html } } } ) ; } }