All files / src/hierarchy artifact-layout.ts

100% Statements 122/122
100% Branches 70/70
100% Functions 18/18
100% Lines 118/118

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307            41x 6x   35x       2222x                 2181x                                       735x 735x 2x 2x 2x             735x 2x 2x             735x       185x 185x 185x   185x 735x     185x                     81x 81x   81x   37x 30x 30x 30x 9x 9x   21x 4x 4x     37x 13x   24x       81x       14x 4x   12x 12x 10x   10x 12x 12x 12x           12x 12x 12x                 7x 7x 1x 1x           1x 1x 1x                 7x 7x 7x   7x 13x 4x 9x 5x 4x 2x 2x 1x   1x       7x 7x   7x     7x   5x     7x             81x 81x 13x 13x 13x     81x 7x 7x         7x       7x         7x 3x 3x       3x     4x       7x       7x       27x 27x         27x 16x 16x 12x         4x 3x             12x 12x         12x 6x 6x 2x         4x 3x             7x               86x 83x 3x   15x 15x 15x 15x 15x 13x 13x        
import { getElementDimensions } from '../di-constants';
import { estimateTextAnnotationDimensions } from '../graph/label-layout';
import type { Bounds, ElementDimension, Point } from '../types';
import { getRefId } from './subprocess-layout';
 
export function getArtifactDimensions(artifact: any): ElementDimension {
  if (artifact.$type === 'bpmn:TextAnnotation') {
    return estimateTextAnnotationDimensions(artifact.text || artifact.name);
  }
  return getElementDimensions(artifact.$type);
}
 
export function isArtifact(el: any): boolean {
  return (
    el.$type === 'bpmn:TextAnnotation' ||
    el.$type === 'bpmn:DataObjectReference' ||
    el.$type === 'bpmn:DataStoreReference' ||
    el.$type === 'bpmn:Group'
  );
}
 
export function isAssociation(el: any): boolean {
  return el.$type === 'bpmn:Association';
}
 
export interface ConnectedArtifactInfo {
  artifact: any;
  hostId: string;
}
 
export interface ConnectedLayoutContext {
  boundsMap: Map<string, Bounds>;
  shapes: Array<{ element: any; bounds: Bounds; isExpanded?: boolean }>;
}
 
export interface RowPlacementConfig {
  hostCenterX: number;
  y: (height: number) => number;
  ctx: ConnectedLayoutContext;
}
 
export function collectTaskDataAssociations(node: any): any[] {
  const result: any[] = [];
  for (const dia of node.dataInputAssociations || []) {
    const src = Array.isArray(dia.sourceRef) ? dia.sourceRef[0] : dia.sourceRef;
    const srcId = getRefId(src);
    result.push({
      $type: 'bpmn:Association',
      id: dia.id ?? `Assoc_${srcId}_${node.id}`,
      sourceRef: src,
      targetRef: node,
    });
  }
  for (const doa of node.dataOutputAssociations || []) {
    const tgtId = getRefId(doa.targetRef);
    result.push({
      $type: 'bpmn:Association',
      id: doa.id ?? `Assoc_${node.id}_${tgtId}`,
      sourceRef: node,
      targetRef: doa.targetRef,
    });
  }
  return result;
}
 
export function collectScopeAssociations(scopeElement: any, regularNodes: any[]): any[] {
  const flowElements = scopeElement.flowElements || [];
  const artifacts = scopeElement.artifacts || [];
  const result = [...flowElements.filter(isAssociation), ...artifacts.filter(isAssociation)];
 
  for (const node of regularNodes) {
    result.push(...collectTaskDataAssociations(node));
  }
 
  return result;
}
 
export function partitionArtifacts(
  artifacts: any[],
  associations: any[],
  regularNodeIds: Set<string>
): {
  connected: ConnectedArtifactInfo[];
  unconnected: any[];
} {
  const connected: ConnectedArtifactInfo[] = [];
  const unconnected: any[] = [];
 
  for (const art of artifacts) {
    let hostId: string | undefined;
    for (const assoc of associations) {
      const srcId = getRefId(assoc.sourceRef);
      const tgtId = getRefId(assoc.targetRef);
      if (srcId === art.id && tgtId && regularNodeIds.has(tgtId)) {
        hostId = tgtId;
        break;
      }
      if (tgtId === art.id && srcId && regularNodeIds.has(srcId)) {
        hostId = srcId;
        break;
      }
    }
    if (hostId) {
      connected.push({ artifact: art, hostId });
    } else {
      unconnected.push(art);
    }
  }
 
  return { connected, unconnected };
}
 
export function placeArtifactRow(artifacts: any[], config: RowPlacementConfig): void {
  if (artifacts.length === 0) {
    return;
  }
  const dims = artifacts.map((a) => getArtifactDimensions(a));
  const totalWidth = dims.reduce((sum, d) => sum + d.width, 0) + (artifacts.length - 1) * 20;
  let curX = Math.round(config.hostCenterX - totalWidth / 2);
 
  for (let i = 0; i < artifacts.length; i++) {
    const art = artifacts[i];
    const dim = dims[i];
    const bounds: Bounds = {
      x: curX,
      y: config.y(dim.height),
      width: dim.width,
      height: dim.height,
    };
    config.ctx.boundsMap.set(art.id, bounds);
    config.ctx.shapes.push({ element: art, bounds });
    curX += dim.width + 20;
  }
}
 
export function placeArtifactSide(
  artifacts: any[],
  hostBounds: Bounds,
  ctx: ConnectedLayoutContext
): void {
  let curX = hostBounds.x + hostBounds.width + 30;
  for (const art of artifacts) {
    const dim = getArtifactDimensions(art);
    const bounds: Bounds = {
      x: curX,
      y: Math.round(hostBounds.y + (hostBounds.height - dim.height) / 2),
      width: dim.width,
      height: dim.height,
    };
    ctx.boundsMap.set(art.id, bounds);
    ctx.shapes.push({ element: art, bounds });
    curX += dim.width + 20;
  }
}
 
export function placeHostArtifacts(
  artifacts: any[],
  hostBounds: Bounds,
  ctx: ConnectedLayoutContext
): void {
  const aboveList: any[] = [];
  const belowList: any[] = [];
  const sideList: any[] = [];
 
  for (const art of artifacts) {
    if (art.$type === 'bpmn:DataStoreReference') {
      belowList.push(art);
    } else if (art.$type === 'bpmn:DataObjectReference') {
      aboveList.push(art);
    } else if (aboveList.length === 0) {
      aboveList.push(art);
    } else if (belowList.length === 0) {
      belowList.push(art);
    } else {
      sideList.push(art);
    }
  }
 
  const hostCenterX = Math.round(hostBounds.x + hostBounds.width / 2);
  placeArtifactRow(aboveList, {
    hostCenterX,
    y: (h) => hostBounds.y - 30 - h,
    ctx,
  });
  placeArtifactRow(belowList, {
    hostCenterX,
    y: () => hostBounds.y + hostBounds.height + 30,
    ctx,
  });
  placeArtifactSide(sideList, hostBounds, ctx);
}
 
export function layoutConnectedArtifacts(
  connectedList: ConnectedArtifactInfo[],
  ctx: ConnectedLayoutContext
): void {
  const byHost = new Map<string, any[]>();
  for (const item of connectedList) {
    const list = byHost.get(item.hostId) || [];
    list.push(item.artifact);
    byHost.set(item.hostId, list);
  }
 
  for (const [hostId, artifacts] of byHost.entries()) {
    const hostBounds = ctx.boundsMap.get(hostId)!;
    placeHostArtifacts(artifacts, hostBounds, ctx);
  }
}
 
function routeDiagonalAssociation(src: Bounds, tgt: Bounds): Point[] {
  const srcCenter: Point = {
    x: Math.round(src.x + src.width / 2),
    y: Math.round(src.y + src.height / 2),
  };
  const tgtCenter: Point = {
    x: Math.round(tgt.x + tgt.width / 2),
    y: Math.round(tgt.y + tgt.height / 2),
  };
 
  if (src.x + src.width <= tgt.x) {
    const p1: Point = { x: src.x + src.width, y: srcCenter.y };
    const p3: Point = {
      x: tgtCenter.x,
      y: srcCenter.y < tgtCenter.y ? tgt.y : tgt.y + tgt.height,
    };
    return [p1, { x: p3.x, y: p1.y }, p3];
  }
 
  const p1: Point = {
    x: srcCenter.x,
    y: srcCenter.y < tgtCenter.y ? src.y + src.height : src.y,
  };
  const p3: Point = {
    x: tgt.x + tgt.width,
    y: tgtCenter.y,
  };
  return [p1, { x: p1.x, y: p3.y }, p3];
}
 
export function routeAssociationEdge(sourceBounds: Bounds, targetBounds: Bounds): Point[] {
  const overlapMinX = Math.max(sourceBounds.x, targetBounds.x);
  const overlapMaxX = Math.min(
    sourceBounds.x + sourceBounds.width,
    targetBounds.x + targetBounds.width
  );
 
  if (overlapMinX < overlapMaxX) {
    const midX = Math.round((overlapMinX + overlapMaxX) / 2);
    if (sourceBounds.y + sourceBounds.height <= targetBounds.y) {
      return [
        { x: midX, y: sourceBounds.y + sourceBounds.height },
        { x: midX, y: targetBounds.y },
      ];
    }
    if (targetBounds.y + targetBounds.height <= sourceBounds.y) {
      return [
        { x: midX, y: sourceBounds.y },
        { x: midX, y: targetBounds.y + targetBounds.height },
      ];
    }
  }
 
  const overlapMinY = Math.max(sourceBounds.y, targetBounds.y);
  const overlapMaxY = Math.min(
    sourceBounds.y + sourceBounds.height,
    targetBounds.y + targetBounds.height
  );
 
  if (overlapMinY < overlapMaxY) {
    const midY = Math.round((overlapMinY + overlapMaxY) / 2);
    if (sourceBounds.x + sourceBounds.width <= targetBounds.x) {
      return [
        { x: sourceBounds.x + sourceBounds.width, y: midY },
        { x: targetBounds.x, y: midY },
      ];
    }
    if (targetBounds.x + targetBounds.width <= sourceBounds.x) {
      return [
        { x: sourceBounds.x, y: midY },
        { x: targetBounds.x + targetBounds.width, y: midY },
      ];
    }
  }
 
  return routeDiagonalAssociation(sourceBounds, targetBounds);
}
 
export function routeAssociations(
  associations: any[],
  boundsMap: Map<string, Bounds>,
  edges: Array<{ element: any; waypoints: Point[]; isFeedback?: boolean }>
): void {
  for (const assoc of associations) {
    if (edges.some((e) => e.element.id === assoc.id)) {
      continue;
    }
    const srcId = getRefId(assoc.sourceRef);
    const tgtId = getRefId(assoc.targetRef);
    const srcBounds = boundsMap.get(srcId as string);
    const tgtBounds = boundsMap.get(tgtId as string);
    if (srcBounds && tgtBounds) {
      const waypoints = routeAssociationEdge(srcBounds, tgtBounds);
      edges.push({ element: assoc, waypoints });
    }
  }
}