This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
forked from bpmn-io/bpmn-to-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton.html
275 lines (217 loc) · 6.58 KB
/
skeleton.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Diagram Viewer</title>
<!-- bpmn-js script is injected via loadScript -->
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
html, body, #canvas {
height: 100%;
padding: 0;
margin: 0;
}
.footer {
position: absolute;
bottom: 15px;
left: 15px;
}
#title {
font-size: .85em;
color: #AAA;
font-weight: normal;
padding: 5px 0;
margin: 0;
}
</style>
</head>
<body>
<div id="canvas"></div>
<div class="footer">
<h4 id="title"></h4>
</div>
<script>
// viewer instance, lazily initialized
let bpmnViewer;
/**
* Get or create viewer instance.
*
* @return {BpmnViewer}
*/
function getViewer() {
if (bpmnViewer) {
return bpmnViewer;
}
bpmnViewer = new BpmnJS({
container: '#canvas'
});
bpmnViewer.on('import.done', function(event) {
const error = event.error;
const warnings = event.warnings;
if (error) {
return console.error('could not import BPMN 2.0 diagram', error);
}
// zoom to fit full viewport
bpmnViewer.get('canvas').zoom('fit-viewport');
});
return bpmnViewer;
}
function getBounds(element, abort = false) {
const elements = [element];
let minX = null;
let minY = null;
let maxX = null;
let maxY = null;
abort = !!abort;
elements.forEach((element) => {
let bbox = element;
if (element.waypoints && !abort) {
bbox = getBounds(element.waypoints[0], true);
}
let x = bbox.x,
y = bbox.y,
height = bbox.height || 0,
width = bbox.width || 0;
if (minX == null || x < minX) {
minX = x;
}
if (minY == null || y < minY) {
minY = y;
}
if (maxX == null || x + width > maxX) {
maxX = x + width;
}
if (maxY == null || y + height > maxY) {
maxY = y + height;
}
});
if (minX == null || maxX == null || minY == null || maxY == null) {
throw new Error('Could not get bounds');
}
return {
x: minX,
y: minY,
height: maxY - minY,
width: maxX - minX,
};
}
function zoomToElementById(elementId) {
const bpmnViewer = getViewer();
const canvas = bpmnViewer.get('canvas');
const viewbox = canvas.viewbox(false);
const elementRegistry = bpmnViewer.get('elementRegistry');
const element = elementRegistry.get(elementId);
const isDefinitionOrOtherInvisibleBpmnElement = element == null;
if (isDefinitionOrOtherInvisibleBpmnElement) {
throw new Error(`Could not zoom to Element "${elementId}", because it does not exist!`)
}
const rect = getBounds(element);
const newViewbox = {
x: rect.x + rect.width / 2 - viewbox.outer.width / 2,
y: rect.y + rect.height / 2 - viewbox.outer.height / 2,
width: viewbox.outer.width,
height: viewbox.outer.height,
};
canvas.viewbox(newViewbox);
}
function zoomDiagram(zoomFactor = 1) {
const bpmnViewer = getViewer();
const canvas = bpmnViewer.get('canvas');
canvas.zoom(zoomFactor);
}
/**
* Open diagram in our viewer instance.
*
* @param {String} bpmnXML diagram to display
* @param {Object} [options]
* @param {Dimensions} [options.minDimensions]
*
* @return {Promise<Bounds, Error>}
*/
async function openDiagram(bpmnXML, options) {
// viewer instance, lazily initialized
const bpmnViewer = getViewer();
options = options || {};
const minDimensions = options.minDimensions || {
width: 0,
height: 0
};
const title = options.title;
const footer = options.footer;
await bpmnViewer.importXML(bpmnXML);
const viewbox = bpmnViewer.get('canvas').viewbox();
// uses provided title
const titleNode = document.querySelector('#title');
if (title) {
titleNode.textContent = title;
}
titleNode.style.display = title ? 'block' : 'none';
if (options.size != null) {
if (!footer) {
document.getElementsByClassName('footer')[0].style.display = 'none';
document.getElementsByClassName('bjs-powered-by')[0].style.display = 'none';
}
return {
width: options.size.imageWidth,
height: options.size.imageHeight,
diagramHeight: options.size.imageHeight
}
}
const width = Math.max(viewbox.inner.width, minDimensions.width);
const diagramHeight = Math.max(
viewbox.inner.height + (footer ? 90 : 0),
minDimensions.height
);
return {
width,
height: diagramHeight + (footer ? 0 : 90),
diagramHeight
};
}
/**
* Resize to viewport
*/
async function resize() {
const bpmnViewer = getViewer();
const canvas = bpmnViewer.get('canvas');
canvas.resized();
canvas.zoom('fit-viewport');
}
async function toSVG() {
const bpmnViewer = getViewer();
const {
svg
} = await bpmnViewer.saveSVG();
return svg;
}
/**
* Load the script that provides the BpmnJS global
*
* @param {String} src
*
* @return {Promise<Void>}
*/
function loadScript(src) {
const head = document.head;
const script = document.createElement('script');
script.type = 'text/javascript';
script.charset = 'utf8';
script.src = src;
const promise = new Promise((resolve, reject) => {
function callback(fn) {
return (arg) => {
script.onload = script.onerror = null;
return fn(arg);
};
}
script.onload = callback(resolve);
script.onerror = callback(reject);
});
head.appendChild(script);
return promise;
}
</script>
</body>
</html>