-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathBpmnRenderer.ts
More file actions
151 lines (128 loc) · 6.46 KB
/
BpmnRenderer.ts
File metadata and controls
151 lines (128 loc) · 6.46 KB
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
/*
Copyright 2020 Bonitasoft S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import type { Edge, Waypoint } from '../../model/bpmn/internal/edge/edge';
import { MessageFlow } from '../../model/bpmn/internal/edge/flows';
import type Shape from '../../model/bpmn/internal/shape/Shape';
import type ShapeBpmnElement from '../../model/bpmn/internal/shape/ShapeBpmnElement';
import type Bounds from '../../model/bpmn/internal/Bounds';
import { MessageVisibleKind, ShapeUtil } from '../../model/bpmn/internal';
import CoordinatesTranslator from './renderer/CoordinatesTranslator';
import StyleComputer from './renderer/StyleComputer';
import type { BpmnGraph } from './BpmnGraph';
import type { FitOptions, RendererOptions } from '../options';
import type { RenderedModel } from '../registry/bpmn-model-registry';
import { mxgraph } from './initializer';
import type { mxCell } from 'mxgraph';
/**
* @internal
*/
export class BpmnRenderer {
constructor(readonly graph: BpmnGraph, readonly coordinatesTranslator: CoordinatesTranslator, readonly styleComputer: StyleComputer) {}
render(renderedModel: RenderedModel, fitOptions?: FitOptions): void {
this.insertShapesAndEdges(renderedModel);
this.graph.customFit(fitOptions);
}
private insertShapesAndEdges({ pools, lanes, subprocesses, otherFlowNodes, boundaryEvents, edges }: RenderedModel): void {
this.graph.batchUpdate(() => {
this.graph.getModel().clear(); // ensure to remove manual changes or already loaded graphs
this.insertShapes(pools);
this.insertShapes(lanes);
this.insertShapes(subprocesses);
this.insertShapes(otherFlowNodes);
// last shape as the boundary event parent must be in the model (subprocess or activity)
this.insertShapes(boundaryEvents);
// at last as edge source and target must be present in the model prior insertion, otherwise they are not rendered
this.insertEdges(edges);
});
}
private insertShapes(shapes: Shape[]): void {
shapes.forEach(shape => this.insertShape(shape));
}
private getParent(bpmnElement: ShapeBpmnElement): mxCell {
const bpmnElementParent = this.getCell(bpmnElement.parentId);
return bpmnElementParent ?? this.graph.getDefaultParent();
}
private insertShape(shape: Shape): void {
const bpmnElement = shape.bpmnElement;
const parent = this.getParent(bpmnElement);
const bounds = shape.bounds;
let labelBounds = shape.label?.bounds;
// pool/lane label bounds are not managed for now (use hard coded values)
labelBounds = ShapeUtil.isPoolOrLane(bpmnElement.kind) ? undefined : labelBounds;
const style = this.styleComputer.computeStyle(shape, labelBounds);
this.insertVertex(parent, bpmnElement.id, bpmnElement.name, bounds, labelBounds, style);
}
private insertEdges(edges: Edge[]): void {
edges.forEach(edge => {
const bpmnElement = edge.bpmnElement;
const parent = this.graph.getDefaultParent();
const source = this.getCell(bpmnElement.sourceRefId);
const target = this.getCell(bpmnElement.targetRefId);
const labelBounds = edge.label?.bounds;
const style = this.styleComputer.computeStyle(edge, labelBounds);
const mxEdge = this.graph.insertEdge(parent, bpmnElement.id, bpmnElement.name, source, target, style);
this.insertWaypoints(edge.waypoints, mxEdge);
if (labelBounds) {
mxEdge.geometry.width = labelBounds.width;
mxEdge.geometry.height = labelBounds.height;
const edgeCenterCoordinate = this.coordinatesTranslator.computeEdgeCenter(mxEdge);
if (edgeCenterCoordinate) {
mxEdge.geometry.relative = false;
const labelBoundsRelativeCoordinateFromParent = this.coordinatesTranslator.computeRelativeCoordinates(mxEdge.parent, new mxgraph.mxPoint(labelBounds.x, labelBounds.y));
const relativeLabelX = labelBoundsRelativeCoordinateFromParent.x + labelBounds.width / 2 - edgeCenterCoordinate.x;
const relativeLabelY = labelBoundsRelativeCoordinateFromParent.y - edgeCenterCoordinate.y;
mxEdge.geometry.offset = new mxgraph.mxPoint(relativeLabelX, relativeLabelY);
}
}
this.insertMessageFlowIconIfNeeded(edge, mxEdge);
});
}
private insertMessageFlowIconIfNeeded(edge: Edge, mxEdge: mxCell): void {
if (edge.bpmnElement instanceof MessageFlow && edge.messageVisibleKind !== MessageVisibleKind.NONE) {
const cell = this.graph.insertVertex(mxEdge, messageFowIconId(mxEdge.id), undefined, 0, 0, 20, 14, this.styleComputer.computeMessageFlowIconStyle(edge));
cell.geometry.relative = true;
cell.geometry.offset = new mxgraph.mxPoint(-10, -7);
}
}
private insertWaypoints(waypoints: Waypoint[], mxEdge: mxCell): void {
if (waypoints) {
mxEdge.geometry.points = waypoints.map(waypoint => this.coordinatesTranslator.computeRelativeCoordinates(mxEdge.parent, new mxgraph.mxPoint(waypoint.x, waypoint.y)));
}
}
private getCell(id: string): mxCell {
return this.graph.getModel().getCell(id);
}
private insertVertex(parent: mxCell, id: string | null, value: string, bounds: Bounds, labelBounds: Bounds, style?: string): mxCell {
const vertexCoordinates = this.coordinatesTranslator.computeRelativeCoordinates(parent, new mxgraph.mxPoint(bounds.x, bounds.y));
const cell = this.graph.insertVertex(parent, id, value, vertexCoordinates.x, vertexCoordinates.y, bounds.width, bounds.height, style);
if (labelBounds) {
// label coordinates are relative in the cell referential coordinates
const relativeLabelX = labelBounds.x - bounds.x;
const relativeLabelY = labelBounds.y - bounds.y;
cell.geometry.offset = new mxgraph.mxPoint(relativeLabelX, relativeLabelY);
}
return cell;
}
}
/**
* @internal
*/
export function newBpmnRenderer(graph: BpmnGraph, options: RendererOptions): BpmnRenderer {
return new BpmnRenderer(graph, new CoordinatesTranslator(graph), new StyleComputer(options));
}
/**
* @internal
*/
export function messageFowIconId(messageFlowId: string): string {
return `messageFlowIcon_of_${messageFlowId}`;
}