import { Component , ComponentFactoryResolver , Input , OnDestroy , OnInit , ViewChild , } from '@angular/core' ; import { ReplaySubject , Subject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { HsPanelComponent } from './panel-component.interface' ; import { HsPanelContainerServiceInterface } from './panel-container.service.interface' ; import { HsPanelHostDirective } from './panel-host.directive' ; import { HsPanelItem } from './panel-item' ; @ Component ( { selector : 'hs-panel-container' , templateUrl : './panel-container.html' , } ) export class HsPanelContainerComponent implements OnInit , OnDestroy { @ ViewChild ( HsPanelHostDirective , { static : true } ) panelHost : HsPanelHostDirective ; /** Service which manages the list of panels */ @ Input ( ) service : HsPanelContainerServiceInterface ; /** Miscellaneous data object to set to each of the panels inside this container.
     * This is used if undefined value is passed to the create functions data parameter. */ @ Input ( ) data : any ; @ Input ( ) panelObserver ? : ReplaySubject < HsPanelItem > ; interval : any ; private ngUnsubscribe = new Subject < void > ( ) ; constructor ( private componentFactoryResolver ) { } ngOnDestroy ( ) { this . ngUnsubscribe . next ( ) ; this . ngUnsubscribe . complete ( ) ; } ngOnInit ( ) { ( this . panelObserver ?? this . service . panelObserver ) . pipe ( takeUntil ( this . ngUnsubscribe ) ) . subscribe ( ( item ) => { this . loadPanel ( item ) ; } ) ; this . service . panelDestroyObserver . pipe ( takeUntil ( this . ngUnsubscribe ) ) . subscribe ( ( item ) => { this . destroyPanel ( item ) ; } ) ; } destroyPanel ( panel ) { const viewContainerRef = this . panelHost . viewContainerRef ; const index = viewContainerRef . indexOf ( panel . viewRef ) ; if ( index > - 1 ) { viewContainerRef . remove ( index ) ; } } loadPanel ( panelItem ) { const componentFactory = this . componentFactoryResolver . resolveComponentFactory ( panelItem . component ) ; const viewContainerRef = this . panelHost . viewContainerRef ; const componentRef = viewContainerRef . createComponent ( componentFactory ) ; const componentRefInstance = < HsPanelComponent > componentRef . instance ; componentRefInstance . viewRef = componentRef . hostView ; if ( componentRefInstance . data == undefined ) { componentRefInstance . data = panelItem . data || this . data ; } } }