import { Injectable } from '@angular/core' ; import { BlockUIActions } from '../constants/block-ui-actions.constant' ; import { BlockUIInstanceService } from './block-ui-instance.service' ; import { NgBlockUI } from '../models/block-ui.model' ; @ Injectable ( ) export class BlockUIService { globalDispatch : NgBlockUI = this . blockUIInstance . decorate ( ) ; constructor ( private blockUIInstance ) { } /**
    * Starts blocking for given BlockUI instance or instances
    */ start ( target , message ? ) { this . dispatch ( target , BlockUIActions . START , message ) ; } /**
    * Stops blocking for given BlockUI instance or instances
    */ stop ( target ) { this . dispatch ( target , BlockUIActions . STOP ) ; } /**
    * Reset blocking for given BlockUI instance or instances
    */ reset ( target ) { this . dispatch ( target , BlockUIActions . RESET ) ; } /**
    * Reset blocking for all BlockUI instances
    */ resetGlobal ( ) { this . globalDispatch . resetGlobal ( ) ; } /**
    * Updates message for given BlockUI instance or instances
    */ update ( target , message ) { this . dispatch ( target , BlockUIActions . UPDATE , message ) ; } /**
    * Unsubscribes for given BlockUI instance or instances
    */ unsubscribe ( target ) { this . dispatch ( target , BlockUIActions . UNSUBSCRIBE ) ; } /**
    * Checks if BlockUI is actively blocking
    */ isActive ( target = null ) { const targets = target ? this . toArray ( target ) : null ; const instances = this . blockUIInstance . blockUIInstances ; return Object . keys ( instances ) . some ( ( key ) => { if ( ! targets ) { return instances [ key ] . isActive ; } return targets . indexOf ( instances [ key ] . name ) >= 0 && instances [ key ] . isActive ; } ) ; } private dispatch ( target = [ ] , type , message ? ) { const instances = this . toArray ( target ) ; instances . forEach ( i => this . blockUIInstance . decorate ( i ) [ type ] ( message ) ) ; } private toArray ( target = [ ] ) { return typeof target === 'string' ? [ target ] : target ; } }