import { Injectable , Inject , Optional } from '@angular/core' ; import { NgProgressRef } from './ng-progress-ref' ; import { NgProgressConfig , ProgressConfig , NG_PROGRESS_CONFIG } from './ng-progress.interface' ; const defaultConfig = { min : 8 , max : 100 , speed : 200 , debounceTime : 0 , trickleSpeed : 300 , fixed : true , meteor : true , thick : false , spinner : true , ease : 'linear' , color : '#1B95E0' , direction : 'ltr+' , spinnerPosition : 'right' , trickleFunc : ( n ) : number => { if ( n >= 0 && n < 20 ) return 10 ; if ( n >= 20 && n < 50 ) return 4 ; if ( n >= 50 && n < 80 ) return 2 ; if ( n >= 80 && n < 99 ) return 0.5 ; return 0 ; } } ; @ Injectable ( { providedIn : 'root' } ) export class NgProgress { private readonly _instances = new Map < string , NgProgressRef > ( ) ; config : ProgressConfig ; constructor ( @ Optional ( ) @ Inject ( NG_PROGRESS_CONFIG ) config ) { this . config = config ? { ... defaultConfig , ... config } : defaultConfig ; } /**
     * Get or Create progress bar by ID
     */ ref ( id = 'root' , config ? ) { if ( this . _instances . has ( id ) ) { const progressRef = this . _instances . get ( id ) as NgProgressRef ; if ( config ) { progressRef . setConfig ( { ... this . config , ... config } ) ; } return progressRef as NgProgressRef ; } else { const progressRef = new NgProgressRef ( { ... this . config , ... config } , this . deleteInstance ( id ) ) ; return this . _instances . set ( id , progressRef ) . get ( id ) as NgProgressRef ; } } /**
     * Destroy all progress bar instances
     */ destroyAll ( ) { this . _instances . forEach ( ( ref ) => ref . destroy ( ) ) ; } /**
     * A destroyer function for each progress bar instance
     */ private deleteInstance ( id ) { return ( ) => { this . _instances . delete ( id ) ; } ; } }