1 import {Edge} from "cwlts/models";
2 import {PluginBase} from "../plugin-base";
3 import { Workflow } from "~/lib/cwl-svg";
5 export class SVGValidatePlugin extends PluginBase {
7 private modelDisposers: any[] = [];
9 /** Map of CSS classes attached by this plugin */
11 plugin: "__plugin-validate",
12 invalid: "__validate-invalid"
15 registerWorkflow(workflow: Workflow): void {
16 super.registerWorkflow(workflow);
18 // add plugin specific class to the svgRoot for scoping
19 this.workflow.svgRoot.classList.add(this.css.plugin);
22 afterModelChange(): void {
24 this.disposeModelListeners();
26 // add listener for all subsequent edge validation
27 const update = this.workflow.model.on("connections.updated", this.renderEdgeValidation.bind(this));
28 const create = this.workflow.model.on("connection.create", this.renderEdgeValidation.bind(this));
30 this.modelDisposers.concat([update.dispose, create.dispose]);
34 this.disposeModelListeners();
38 // do initial validation rendering for edges
39 this.renderEdgeValidation();
42 onEditableStateChange(enabled: boolean): void {
45 // only show validation if workflow is editable
46 this.renderEdgeValidation();
48 this.removeClasses(this.workflow.workflow.querySelectorAll(".edge"))
52 private disposeModelListeners(): void {
53 for (let disposeListener of this.modelDisposers) {
56 this.modelDisposers = [];
59 private removeClasses(edges: NodeListOf<Element>): void {
60 // remove validity class on all edges
61 for (const e of (edges as any)) {
62 e.classList.remove(this.css.invalid);
66 private renderEdgeValidation(): void {
67 const graphEdges: any = this.workflow.workflow.querySelectorAll(".edge") as NodeListOf<Element>;
69 this.removeClasses(graphEdges);
71 // iterate through all modal connections
72 this.workflow.model.connections.forEach((e: Edge) => {
73 // if the connection isn't valid (should be colored on graph)
76 // iterate through edges on the svg
77 for (const ge of graphEdges) {
78 const sourceNodeID = ge.getAttribute("data-source-connection");
79 const destinationNodeID = ge.getAttribute("data-destination-connection");
81 // compare invalid edge source/destination with svg edge
82 if (e.source.id === sourceNodeID && e.destination.id === destinationNodeID) {
83 // if its a match, tag it with the appropriate class and break from the loop
84 ge.classList.add(this.css.invalid);