import { Injectable } from '@angular/core' ; import { LoaderInstance } from './loader-instance' ; @ Injectable ( ) export class NgxSmartLoaderService { private _loaderStack : LoaderInstance [ ] = [ ] ; private _actions : Array < { identifier : string ; action : string ; } > = [ ] ; param loaderInstance param force returns public addLoader ( loaderInstance , force ? ) { if ( force ) { const i = this . _loaderStack . findIndex ( ( o ) => { return o . id === loaderInstance . id ; } ) ; if ( i > - 1 ) { this . _loaderStack [ i ] . component = loaderInstance . component ; } else { this . _loaderStack . push ( loaderInstance ) ; } return ; } let loader ; if ( loader = this . _getLoader ( loaderInstance . id ) ) { throw ( new Error ( 'Loader with ' + loaderInstance . id + ' identifier already exist' ) ) ; } else { this . _loaderStack . push ( loaderInstance ) ; } } param id public removeLoader ( id ) { this . _loaderStack = this . _loaderStack . filter ( ( loader ) => loader . id !== id ) ; this . _removeAction ( id , '*' ) ; } returns public getLoaderStack ( ) { return this . _loaderStack ; } returns public getLoaderStackCount ( ) { return this . _loaderStack . length ; } returns public getOpenedLoaders ( ) { return this . _loaderStack . filter ( ( loader ) => loader . component . visible ) ; } returns public getActiveLoaders ( ) { return this . _loaderStack . filter ( ( loader ) => loader . component . loading ) ; } returns public getHigherIndex ( ) { const index = this . getOpenedLoaders ( ) . map ( ( loader ) => loader . component . layerPosition ) ; return Math . max ( ... index ) + 1 ; } param id public start ( id ) { let loader ; if ( Array . isArray ( id ) ) { id . forEach ( ( i ) => { this . start ( i ) ; } ) ; } else if ( loader = this . _getLoader ( id ) ) { loader . component . start ( ) ; this . _removeAction ( id , 'start' ) ; } else { this . _addAction ( id , 'start' ) ; } } /**
     * Enable loading state to all loaders.
     */ public startAll ( ) { this . _loaderStack . forEach ( ( loader ) => this . start ( loader . id ) ) ; } param id public stop ( id ) { let loader ; if ( Array . isArray ( id ) ) { id . forEach ( ( i ) => { this . stop ( i ) ; } ) ; } else if ( loader = this . _getLoader ( id ) ) { loader . component . stop ( ) ; this . _removeAction ( id , 'stop' ) ; } else { this . _addAction ( id , 'stop' ) ; } } /**
     * Disable loading state to all loaders.
     */ public stopAll ( ) { this . _loaderStack . forEach ( ( loader ) => this . stop ( loader . id ) ) ; } public isLoading ( id ) { let loader ; if ( Array . isArray ( id ) ) { const tmp = [ ] ; id . forEach ( ( i ) => { this . _loaderStack . forEach ( ( load ) => { if ( load . id === i ) { tmp . push ( load . component . loading ) ; } } ) ; } ) ; return tmp . indexOf ( false ) === - 1 ; } else if ( loader = this . _getLoader ( id ) ) { return loader . component . loading ; } else { return false ; } } param id param action public executeAction ( id , action ) { if ( this . _actions . find ( ( act ) => act . identifier === id && act . action === action ) ) { switch ( action ) { case 'start' : this . start ( id ) ; break ; case 'stop' : this . stop ( id ) ; break ; } } } param id private _getLoader ( id ) { return this . _loaderStack . find ( ( load ) => load . id === id ) || null ; } param id param action private _addAction ( id , action ) { if ( Array . isArray ( id ) ) { id . forEach ( ( i ) => { this . _addAction ( i , action ) ; } ) ; } else { this . _actions . push ( { identifier : id , action : action } ) ; } } param id param action private _removeAction ( id , action ) { if ( Array . isArray ( id ) ) { id . forEach ( ( i ) => { this . _removeAction ( i , action ) ; } ) ; } else { this . _actions = this . _actions . filter ( ( act ) => act . identifier !== id || ( act . action !== action && action !== '*' ) ) ; } } }