import { ChangeDetectorRef , Component , ComponentFactoryResolver , EventEmitter , HostBinding , Input , OnDestroy , OnInit , Output , ViewChild , ViewContainerRef } from '@angular/core' ; import { AbstractEvent } from '../../event-registry/abstract-event' ; import { animate , state , style , transition , trigger } from '@angular/animations' ; import { HostDirective } from '../../../../host.directive' ; import { AttributeValue } from '../../../../phaser/entities/cc-entity' ; import { AbstractWidget } from '../../../abstract-widget' ; import { WidgetRegistryService } from '../../../widget-registry.service' ; import { JsonWidgetComponent } from '../../../json-widget/json-widget.component' ; import { EventHelperService } from '../event-helper.service' ; import { Subscription } from 'rxjs' ; export type RefreshType = 'Node' | 'Full' ; @ Component ( { selector : 'app-event-detail' , templateUrl : './event-detail.component.html' , styleUrls : [ './event-detail.component.scss' ] } ) export class EventDetailComponent implements OnDestroy { @ ViewChild ( HostDirective , { static : true } ) appHost ! : HostDirective ; @ Input ( ) event ! : AbstractEvent < any > ; @ Output ( ) close = new EventEmitter < void > ( ) ; @ Output ( ) refresh = new EventEmitter < RefreshType > ( ) ; newData : any ; unknownObj ? : { data : any ; } ; warning = false ; private changeSubscriptions : Subscription [ ] = [ ] ; constructor ( private componentFactoryResolver , private widgetRegistry , private helper , private ref ) { } ngOnDestroy ( ) { this . clearSubscriptions ( ) ; } closeDetails ( ) { this . close . emit ( ) ; } public loadEvent ( event ) { this . event = event ; this . loadSettings ( ) ; } private clearSubscriptions ( ) { for ( const sub of this . changeSubscriptions ) { sub . unsubscribe ( ) ; } this . changeSubscriptions = [ ] ; } private loadSettings ( ) { this . clearSubscriptions ( ) ; const ref = this . appHost . viewContainerRef ; ref . clear ( ) ; const exported = this . event . export ( ) ; this . newData = this . helper . getEventFromType ( exported , this . event . actionStep ) . data ; if ( ! this . event . getAttributes ) { console . log ( 'wtf' , this ) ; } const attributes = this . event . getAttributes ( ) ; if ( attributes ) { this . warning = false ; Object . entries ( attributes ) . forEach ( ( [ key , val ] ) => { this . generateWidget ( this . newData , key , val , ref ) ; } ) ; } else { this . warning = true ; this . unknownObj = { data : this . newData } ; const instance = this . generateWidget ( this . unknownObj , 'data' , { type : '' , description : '' } , ref ) as JsonWidgetComponent ; instance . noPropName = true ; } this . ref . detectChanges ( ) ; } private generateWidget ( data , key , val , ref ) { const componentFactory = this . componentFactoryResolver . resolveComponentFactory ( this . widgetRegistry . getWidget ( val . type ) ) ; const componentRef = ref . createComponent ( componentFactory ) ; const instance = < AbstractWidget > componentRef . instance ; instance . custom = data ; instance . key = key ; instance . attribute = val ; const sub = instance . onChange . subscribe ( ( ) => this . update ( ) ) ; this . changeSubscriptions . push ( sub ) ; return instance ; } private update ( ) { this . event . data = this . unknownObj ? this . unknownObj . data : this . newData ; const previousChildCount = this . event . children . length ; this . event . update ( ) ; const childCount = this . event . children . length ; this . refresh . emit ( ( childCount > 0 || previousChildCount !== childCount ) ? 'Full' : 'Node' ) ; } }