All files / src di-generator.ts

100% Statements 34/34
100% Branches 14/14
100% Functions 5/5
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                    73x       68x 65x     68x 68x 65x         65x       65x     68x 1x             68x 1x     68x 68x       353x             353x           353x 10x     353x 138x           138x         353x 353x 353x       271x 271x 652x           271x           271x 1x           1x         271x 271x 271x      
import type { BPMNModdle } from 'bpmn-moddle';
import type { Bounds, Point } from './types';
 
export interface ShapeConfig extends Bounds {
  isExpanded?: boolean;
  labelBounds?: Bounds;
}
 
export interface EdgeConfig {
  bpmnElement: any;
  waypoints: Point[];
  labelBounds?: Bounds;
}
 
export class DiGenerator {
  private moddle: BPMNModdle;
 
  constructor(moddle: BPMNModdle) {
    this.moddle = moddle;
  }
 
  public ensureDiagram(definitions: any, targetElement: any): any {
    if (!definitions.diagrams) {
      definitions.diagrams = [];
    }
 
    let diagram = definitions.diagrams[0];
    if (!diagram) {
      const plane = this.moddle.create('bpmndi:BPMNPlane', {
        id: `BPMNPlane_1`,
        bpmnElement: targetElement,
        planeElement: [],
      });
      diagram = this.moddle.create('bpmndi:BPMNDiagram', {
        id: `BPMNDiagram_1`,
        plane,
      });
      definitions.diagrams.push(diagram);
    }
 
    if (!diagram.plane) {
      diagram.plane = this.moddle.create('bpmndi:BPMNPlane', {
        id: `BPMNPlane_1`,
        bpmnElement: targetElement,
        planeElement: [],
      });
    }
 
    if (!diagram.plane.planeElement) {
      diagram.plane.planeElement = [];
    }
 
    diagram.plane.bpmnElement = targetElement;
    return diagram;
  }
 
  public addShape(plane: any, bpmnElement: any, config: ShapeConfig): any {
    const dcBounds = this.moddle.create('dc:Bounds', {
      x: config.x,
      y: config.y,
      width: config.width,
      height: config.height,
    });
 
    const shapeProps: Record<string, any> = {
      id: `${bpmnElement.id}_di`,
      bpmnElement,
      bounds: dcBounds,
    };
 
    if (typeof config.isExpanded === 'boolean') {
      shapeProps.isExpanded = config.isExpanded;
    }
 
    if (config.labelBounds) {
      const dcLabelBounds = this.moddle.create('dc:Bounds', {
        x: config.labelBounds.x,
        y: config.labelBounds.y,
        width: config.labelBounds.width,
        height: config.labelBounds.height,
      });
      shapeProps.label = this.moddle.create('bpmndi:BPMNLabel', {
        bounds: dcLabelBounds,
      });
    }
 
    const shape = this.moddle.create('bpmndi:BPMNShape', shapeProps);
    plane.planeElement.push(shape);
    return shape;
  }
 
  public addEdge(plane: any, config: EdgeConfig): any {
    const { bpmnElement, waypoints, labelBounds } = config;
    const diWaypoints = waypoints.map((pt) =>
      this.moddle.create('dc:Point', {
        x: pt.x,
        y: pt.y,
      })
    );
 
    const edgeProps: Record<string, any> = {
      id: `${bpmnElement.id}_di`,
      bpmnElement,
      waypoint: diWaypoints,
    };
 
    if (labelBounds) {
      const dcLabelBounds = this.moddle.create('dc:Bounds', {
        x: labelBounds.x,
        y: labelBounds.y,
        width: labelBounds.width,
        height: labelBounds.height,
      });
      edgeProps.label = this.moddle.create('bpmndi:BPMNLabel', {
        bounds: dcLabelBounds,
      });
    }
 
    const edge = this.moddle.create('bpmndi:BPMNEdge', edgeProps);
    plane.planeElement.push(edge);
    return edge;
  }
}