import { Component , Injector , Input , OnDestroy , OnInit , Optional , SkipSelf , ViewChild , ViewContainerRef } from '@angular/core' ; import * as dagre from 'dagre' ; import { AbstractEmptyDirective , AbstractObject3D } from '../../../object' ; import { RendererService } from '../../../renderer' ; import { provideParent } from '../../../util' ; import { DagreLayoutComponent } from './dagre-layout.component' ; import { Subscription } from 'rxjs' ; @ Component ( { selector : 'atft-dagre-node' , providers : [ provideParent ( DagreNodeComponent ) ] , template : '<template #container></template><ng-content></ng-content>' } ) export class DagreNodeComponent extends AbstractEmptyDirective implements OnInit , OnDestroy { @ Input ( ) composition : string ; @ Input ( ) translateZ = 0 ; @ ViewChild ( 'container' , { read : ViewContainerRef , static : true } ) container ; protected dagreLayout : DagreLayoutComponent ; protected graphUpdated : Subscription ; constructor ( protected rendererService , @ SkipSelf ( ) @ Optional ( ) protected parent , protected injector ) { super ( rendererService , parent ) ; this . dagreLayout = this . injector . get < DagreLayoutComponent > ( DagreLayoutComponent ) ; if ( ! this . dagreLayout ) { console . warn ( 'DagreNodeComponent.constructor: atft-dagre-layout not found!' ) ; } this . syncGraph = this . syncGraph . bind ( this ) ; this . graphUpdated = this . dagreLayout . updated . subscribe ( this . syncGraph ) ; } ngOnInit ( ) { super . ngOnInit ( ) ; this . addNode ( ) ; } protected addNode ( ) { if ( this . dagreLayout && this . dagreLayout . getGraphModel ( ) ) { this . dagreLayout . getChildren ( ) . push ( this ) ; this . dagreLayout . getGraphModel ( ) . nodes . push ( { name : this . name , label : this . name , composition : this . composition } ) ; this . dagreLayout . refreshLayout ( ) ; } } ngOnDestroy ( ) { super . ngOnDestroy ( ) ; this . removeNode ( ) ; } protected removeNode ( ) { if ( this . dagreLayout && this . dagreLayout . getGraphModel ( ) ) { this . graphUpdated ?. unsubscribe ( ) ; this . dagreLayout . removeChildByName ( this . name ) ; this . dagreLayout . getGraphModel ( ) . nodes = this . dagreLayout . getGraphModel ( ) . nodes . filter ( i => i . name !== this . name ) ; this . dagreLayout . refreshLayout ( ) ; } } protected syncGraphNodes ( g ) { g . nodes ( ) . forEach ( ( name ) => { if ( name === this . name ) { const node = g . node ( name ) ; this . translateX = node . x ; this . translateY = node . y ; this . applyTranslation ( ) ; } } ) ; } protected syncGraph ( ) { if ( this . object ) { this . syncGraphNodes ( this . dagreLayout . getGraph ( ) ) ; } } }