-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathDiagramGeneration.ts
More file actions
218 lines (186 loc) · 5.36 KB
/
Copy pathDiagramGeneration.ts
File metadata and controls
218 lines (186 loc) · 5.36 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
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
import { is } from '../di/DiUtil.js';
import { finalizeLayoutConnections } from './connections/FinalizeConnections.js';
import {
integerBounds,
normalizeLayout
} from './geometry/index.js';
import { layoutExternalLabels } from './labels/LayoutLabels.js';
import type {
BpmnDiModdleElementAttributes,
BpmnDiModdleElementFor,
BpmnDiModdleFactory,
BpmnDiModdleTypeName
} from './bpmn/Types.js';
import type {
BpmnElement,
Bounds,
LayoutState,
Waypoint
} from './Types.js';
type BpmnDiagram = BpmnDiModdleElementFor<'bpmndi:BPMNDiagram'>;
type BpmnEdge = BpmnDiModdleElementFor<'bpmndi:BPMNEdge'>;
type BpmnLabel = BpmnDiModdleElementFor<'bpmndi:BPMNLabel'>;
type BpmnPlane = BpmnDiModdleElementFor<'bpmndi:BPMNPlane'>;
type BpmnShape = BpmnDiModdleElementFor<'bpmndi:BPMNShape'>;
type BoundsElement = BpmnDiModdleElementFor<'dc:Bounds'>;
type PointElement = BpmnDiModdleElementFor<'dc:Point'>;
type PlaneElement = BpmnShape | BpmnEdge;
type PreparedPlane = {
layout: LayoutState;
labels: Map<BpmnElement, Bounds>;
};
type ShapeAttributes = Pick<
BpmnDiModdleElementAttributes<'bpmndi:BPMNShape'>,
'id' | 'isExpanded' | 'isHorizontal' | 'isMarkerVisible' | 'label'
>;
type EdgeAttributes = Pick<
BpmnDiModdleElementAttributes<'bpmndi:BPMNEdge'>,
'id' | 'label'
>;
export function generateDiagrams(
moddle: BpmnDiModdleFactory,
layout: LayoutState
): BpmnDiagram[] {
const preparedPlanes = collectPlaneLayouts(layout).map(planeLayout => {
normalizeLayout(planeLayout);
finalizeLayoutConnections(planeLayout, planeLayout === layout);
return {
layout: planeLayout,
labels: layoutExternalLabels(planeLayout)
};
});
return preparedPlanes.map(plane => emitDiagram(moddle, plane));
}
function collectPlaneLayouts(layout: LayoutState): LayoutState[] {
const planes = [ layout ];
collectCollapsedLayouts(layout, planes);
return planes;
}
function collectCollapsedLayouts(
layout: LayoutState,
planes: LayoutState[]
): void {
for (const child of layout.children) {
if (!child.emitInParent) {
planes.push(child);
}
collectCollapsedLayouts(child, planes);
}
}
function emitDiagram(
moddle: BpmnDiModdleFactory,
{ layout, labels }: PreparedPlane
): BpmnDiagram {
const planeElements: PlaneElement[] = [];
const plane: BpmnPlane = createElement(moddle, 'bpmndi:BPMNPlane', {
id: `BPMNPlane_${layout.scope.id}`,
bpmnElement: layout.scope,
planeElement: planeElements
});
const diagram = createElement(moddle, 'bpmndi:BPMNDiagram', {
id: `BPMNDiagram_${layout.scope.id}`,
plane
});
emitLayout(moddle, layout, labels, planeElements);
return diagram;
}
function emitLayout(
moddle: BpmnDiModdleFactory,
layout: LayoutState,
labels: Map<BpmnElement, Bounds>,
planeElements: PlaneElement[]
): void {
for (const [ element, rect ] of layout.shapes) {
const attrs: ShapeAttributes = {};
const labelBounds = labels.get(element);
if (is(element, 'bpmn:SubProcess') && layout.children.some(child => {
return child.scope === element && child.emitInParent;
})) {
attrs.isExpanded = true;
}
if (is(element, 'bpmn:Participant') || is(element, 'bpmn:Lane')) {
attrs.isHorizontal = true;
}
if (is(element, 'bpmn:Gateway')) {
attrs.isMarkerVisible = true;
}
if (labelBounds) {
attrs.label = createLabel(moddle, integerBounds(labelBounds));
}
planeElements.push(createShape(moddle, element, integerBounds(rect), {
id: `BPMNShape_${element.id}`,
...attrs
}));
}
for (const [ element, points ] of layout.edges) {
const attrs: EdgeAttributes = {
id: `BPMNEdge_${element.id}`
};
const labelBounds = labels.get(element);
if (labelBounds) {
attrs.label = createLabel(moddle, integerBounds(labelBounds));
}
planeElements.push(createEdge(moddle, element, points, attrs));
}
for (const child of layout.children) {
if (child.emitInParent) {
emitLayout(moddle, child, labels, planeElements);
}
}
}
function createShape(
moddle: BpmnDiModdleFactory,
semantic: BpmnElement,
shapeBounds: Bounds,
attrs: ShapeAttributes
): BpmnShape {
return createElement(moddle, 'bpmndi:BPMNShape', {
bpmnElement: semantic,
bounds: createBounds(moddle, shapeBounds),
...attrs
});
}
function createEdge(
moddle: BpmnDiModdleFactory,
semantic: BpmnElement,
waypoints: Waypoint[],
attrs: EdgeAttributes
): BpmnEdge {
return createElement(moddle, 'bpmndi:BPMNEdge', {
bpmnElement: semantic,
waypoint: waypoints.map(point => {
return createPoint(moddle, point);
}),
...attrs
});
}
function createLabel(
moddle: BpmnDiModdleFactory,
labelBounds: Bounds
): BpmnLabel {
return createElement(moddle, 'bpmndi:BPMNLabel', {
bounds: createBounds(moddle, labelBounds)
});
}
function createBounds(
moddle: BpmnDiModdleFactory,
rect: Bounds
): BoundsElement {
return createElement(moddle, 'dc:Bounds', rect);
}
function createPoint(
moddle: BpmnDiModdleFactory,
point: Waypoint
): PointElement {
return createElement(moddle, 'dc:Point', {
x: point.x,
y: point.y
});
}
function createElement<Type extends BpmnDiModdleTypeName>(
moddle: BpmnDiModdleFactory,
type: Type,
attrs: BpmnDiModdleElementAttributes<Type>
): BpmnDiModdleElementFor<Type> {
return moddle.create(type, attrs);
}