import { AfterViewInit, Component, ComponentRef, ElementRef, ViewChild, ViewContainerRef } from '@angular/core'; import { Store } from '@ngrx/store'; import { TranslateService } from '@ngx-translate/core'; import { BaseNode, PipelineComponentFactoryService, SubGraphNode, SubscriptionnerDirective } from '@onepoint-yap/dashboard'; import { LGraph, LGraphCanvas, LGraphNode, LiteGraph } from 'litegraph.js'; import { Observable, withLatestFrom } from 'rxjs'; import { map } from 'rxjs/operators'; import { PipelineActions, PipelineError, PipelineResponse } from '../../store/pipeline.actions'; import { PipelineSelectors } from '../../store/pipeline.selectors'; import { GraphSerializerUtils } from './graph-serializer.utils'; export const MIME_TYPE_PIPELINE_COMPONENT = 'pipeline/component'; @Component({ selector: 'yap-pipeline-editor', templateUrl: './pipeline-editor.component.html' }) export class YapPipelineEditorComponent extends SubscriptionnerDirective implements AfterViewInit { @ViewChild('canvas') canvas!: ElementRef; @ViewChild('canvasParent') canvasParent!: ElementRef; lGraphs: LGraph[] = []; private lGraphCanvas!: LGraphCanvas; types$: Observable; selectedNode$: Observable; response$: Observable; @ViewChild('specific', { read: ViewContainerRef, static: false }) componentRef!: ViewContainerRef; instanciatedComponent!: ComponentRef; constructor( private store: Store, private pipelineComponentFactoryService: PipelineComponentFactoryService, private translateService: TranslateService ) { super(); BaseNode.store = store; BaseNode.check = () => this.check(); this.types$ = pipelineComponentFactoryService.types$; this.selectedNode$ = this.store.select(PipelineSelectors.selectedNodeId).pipe(map((id) => this.findNodeById(id))); this.response$ = this.store.select(PipelineSelectors.checkResponse); this.subscriptions.add( this.response$.subscribe((response) => { this.allNodes.forEach((errorNode) => { // @ts-ignore delete errorNode?.bgcolor; }); response?.errors ?.filter((error) => !!error.nodeId) .map((error) => this.findNodeById(error.nodeId!)) .filter((errorNode) => !!errorNode) .forEach((errorNode) => (errorNode!.bgcolor = '#d93535')); this.refresh(); }) ); SubGraphNode.trigger = (graph: LGraph) => { this.displayGraph(graph); }; this.selectedNode$.pipe(withLatestFrom(this.response$)).subscribe(([node, response]) => { if (this.instanciatedComponent) { this.componentRef.clear(); } if (node) { const module = this.pipelineComponentFactoryService.get(node?.type ?? ''); if (module) { this.instanciatedComponent = this.componentRef.createComponent(module.component); this.instanciatedComponent.instance.node = node; this.instanciatedComponent.instance.checkFn = () => {}; setTimeout(() => { this.instanciatedComponent.instance.checkFn = () => this.check(); }, 100); if (response) { const matchingStatus = response.status.find((status) => status.nodeId === node.properties['id']); if (matchingStatus) { this.instanciatedComponent.instance.model = matchingStatus.out; } } } } }); } get currentGraph() { return this.lGraphs[this.lGraphs.length - 1]; } get allNodes(): (SubGraphNode | BaseNode)[] { // @ts-ignore return this.currentGraph?._nodes ?? []; } findNodeById(id: string) { return this.allNodes.find((node) => node.properties['id'] === id); } ngAfterViewInit(): void { this.lGraphs.push(new LGraph()); this.lGraphCanvas = new LGraphCanvas(this.canvas.nativeElement, this.currentGraph); this.lGraphCanvas.allow_searchbox = false; this.currentGraph.start(); const rect = this.canvasParent.nativeElement.getBoundingClientRect(); this.canvas.nativeElement.width = rect.width; this.canvas.nativeElement.height = rect.height; } displayGraph(graph: LGraph) { this.currentGraph.stop(); this.lGraphs.push(graph); this.lGraphCanvas.setGraph(graph); graph.start(); } popGraph() { this.currentGraph.stop(); this.lGraphs.length = this.lGraphs.length - 1; this.currentGraph.start(); this.lGraphCanvas.setGraph(this.currentGraph); } pupGraph(node: SubGraphNode | BaseNode) { if (node instanceof SubGraphNode) { this.displayGraph(node.subGraph); this.store.dispatch(PipelineActions.select({ id: '' })); } } check() { setTimeout(() => { this.store.dispatch(PipelineActions.check({ graph: GraphSerializerUtils.serialize(this.lGraphs[0]) })); }, 100); } refresh() { this.currentGraph?.change(); } createNode(type: string, position: [number, number]) { const node_const = LiteGraph.createNode(type, type, {}); node_const.title = type; node_const.pos = position; this.currentGraph.add(node_const); } onDragStart($event: DragEvent, type: string) { if (!!$event && !!$event.dataTransfer && !$event.dataTransfer.types.includes(MIME_TYPE_PIPELINE_COMPONENT)) { $event.dataTransfer.dropEffect = 'copy'; $event.dataTransfer.effectAllowed = 'copy'; $event.dataTransfer.setData(MIME_TYPE_PIPELINE_COMPONENT, type); } } onDrop($event: DragEvent) { $event.preventDefault(); if (!!$event && !!$event.dataTransfer) { if ($event.dataTransfer.types.includes(MIME_TYPE_PIPELINE_COMPONENT)) { const componentName = $event.dataTransfer.getData(MIME_TYPE_PIPELINE_COMPONENT); this.createNode(componentName, [ $event.clientX - this.canvas.nativeElement.offsetLeft, $event.clientY - this.canvas.nativeElement.offsetTop ]); this.check(); $event.stopPropagation(); } } } changeTitle(node: LGraphNode, $event: Event) { // @ts-ignore node.title = $event.target?.value; this.refresh(); } errorTranslated(error: PipelineError) { return this.translateService.instant('@yap.pipeline.codes.' + error.code, { ...error.params }); } }