import { Component , ComponentFactoryResolver , EventEmitter , Input , OnDestroy , OnInit , Type , ViewChild } from '@angular/core' ; import { Animations } from '@shared/animations/animations' ; import { ITaskBar , TaskBarOptions , TaskBarService } from '@shared/services/task-bar-service' ; import { TaskBarItemHostDirective } from '@shared/components/task-bar/task-bar-item-host-directive' ; import { BehaviorSubject } from 'rxjs' ; @ Component ( { selector : 'app-task-bar' , templateUrl : './task-bar.component.html' , styleUrls : [ './task-bar.component.scss' ] , animations : Animations . openClose } ) export class TaskBarComponent implements ITaskBar , OnInit , OnDestroy { @ Input ( ) opened : boolean ; @ ViewChild ( TaskBarItemHostDirective , { static : false } ) host : TaskBarItemHostDirective ; public options : BehaviorSubject < TaskBarOptions > = new BehaviorSubject < TaskBarOptions > ( { } ) ; private closedComplete = new EventEmitter < boolean > ( ) ; constructor ( private componentFactoryResolver , private taskBarService ) { taskBarService . registerTaskBar ( this ) ; } public open < T > ( component , data ? , taskBarOptions ? ) { this . options . next ( taskBarOptions || { } ) ; const componentFactory = this . componentFactoryResolver . resolveComponentFactory ( component ) ; const viewContainerRef = this . host . viewContainerRef ; viewContainerRef . clear ( ) ; try { const componentRef = viewContainerRef . createComponent ( componentFactory ) ; if ( data ) { Object . keys ( data ) . forEach ( key => { componentRef . instance [ key ] = data [ key ] ; } ) ; } this . opened = true ; return componentRef . instance ; } catch ( e ) { console . log ( e ) ; } return null ; } public animationComplete ( $event ) { if ( $event . toState === 'closed' ) { this . closedComplete . emit ( true ) ; } } public close ( ) { this . opened = false ; return new Promise < any > ( resolve => { const sub = this . closedComplete . subscribe ( ( ) => { this . host . viewContainerRef . clear ( ) ; this . taskBarService . markAsClosed ( ) ; sub . unsubscribe ( ) ; resolve ( ) ; } ) ; } ) ; } public ngOnInit ( ) { } public ngOnDestroy ( ) { this . taskBarService . clearReference ( ) ; } }