/
InitialRenderVisitor.js
516 lines (442 loc) · 16.2 KB
/
InitialRenderVisitor.js
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import {
is
} from 'bpmn-js/lib/util/ModelUtil';
import Refs from 'object-refs';
import {
elementToString
} from 'bpmn-js/lib/import/Util';
import {
assign,
omit
} from 'min-dash';
import {
isLabelExternal,
getExternalLabelBounds
} from 'bpmn-js/lib/util/LabelUtil';
import {
getMid
} from 'diagram-js/lib/layout/LayoutUtil';
import {
resetAllBands
} from '../../util/BandUtil';
import {
createMessageShape,
createMessageFlowSemantics,
linkMessageFlowSemantics
} from '../../util/MessageUtil';
let diRefs = new Refs(
{ name: 'bpmnElement', enumerable: true },
{ name: 'di', configurable: true }
);
function elementData(semantic, attrs) {
return assign({
id: semantic.id,
type: semantic.$type,
businessObject: semantic
}, attrs);
}
function notYetDrawn(translate, semantic, refSemantic, property) {
return new Error(translate('element {element} referenced by {referenced}#{property} not yet drawn', {
element: elementToString(refSemantic),
referenced: elementToString(semantic),
property: property
}));
}
function isPointInsideBBox(bbox, point) {
return point.x >= bbox.x &&
point.x <= bbox.x + bbox.width &&
point.y >= bbox.y &&
point.y <= bbox.y + bbox.height;
}
/**
* A visitor for initially rendering a diagram.
* Creates and links shapes as the diagram is iterated.
* @constructor
* @param {Injector} injector
* @param {EventBus} eventBus
* @param {Canvas} canvas
* @param {ElementFactory} elementFactory
* @param {ElementRegistry} elementRegistry
* @param {*} translate
* @param {TextRenderer} textRenderer
* @param {BpmnModdle} moddle
*/
export default function InitialRenderVisitor(
injector, eventBus, canvas, elementFactory, elementRegistry, translate, textRenderer, moddle
) {
this._injector = injector;
this._eventBus = eventBus;
this._canvas = canvas;
this._elementFactory = elementFactory;
this._elementRegistry = elementRegistry;
this._translate = translate;
this._textRenderer = textRenderer;
this._moddle = moddle;
}
InitialRenderVisitor.$inject = [
'injector',
'eventBus',
'canvas',
'elementFactory',
'elementRegistry',
'translate',
'textRenderer',
'moddle'
];
InitialRenderVisitor.prototype.init = function(...parameters) {
};
InitialRenderVisitor.prototype.start = function(choreo, diagram) {
// load DI from selected diagram
this._registerDi(diagram.plane);
if (diagram.plane.planeElement) {
// if there are no elements in the diagram there will not be any planeElements
diagram.plane.planeElement.forEach(InitialRenderVisitor.prototype._registerDi.bind(this));
}
// If there are participants in the diagram which are not participating in any choreo their DI would never be set.
// Even though their DI is not important in choreo diagrams due to diBands, it needs to be set because bpmn-js
// refers to it in some places. Therefore, we set the di for all participants to a dummy DI.
if (choreo.participants) {
choreo.participants.forEach(participant => {
let newDi = this._moddle.create('bpmndi:BPMNShape', {
id: participant.id + choreo.id + '_dummy_di',
bpmnElement: participant,
bounds: this._moddle.create('dc:Bounds', {
x: 0, y: 0, width: 0, height: 0
})
});
participant.di = newDi;
});
}
};
InitialRenderVisitor.prototype.visit = function(element, parentShape) {
// avoid multiple rendering of elements
if (element.gfx) {
throw new Error('already rendered ' + elementToString(element));
}
return this._add(element, parentShape);
};
/**
* Iterate through all DI elements and link their business objects to them.
*/
InitialRenderVisitor.prototype._registerDi = function(di) {
let bpmnElement = di.bpmnElement;
if (bpmnElement) {
// do not link DIs for participants because they have more than one
if (!is(bpmnElement, 'bpmn:Participant')) {
if (bpmnElement.di) {
throw new Error('multiple DI elements defined for ' + elementToString(bpmnElement));
} else {
diRefs.bind(bpmnElement, 'di');
bpmnElement.di = di;
}
}
} else {
throw new Error('no bpmnElement referenced in ' + elementToString(di));
}
};
/**
* Add bpmn element (semantic) to the canvas onto the specified parent shape.
*/
InitialRenderVisitor.prototype._add = function(semantic, parentShape) {
let element;
let isParticipantBand = is(semantic, 'bpmn:Participant') && is(parentShape, 'bpmn:ChoreographyActivity');
let isMessageFlow = is(semantic, 'bpmn:MessageFlow');
let isChoreoActivity = is(semantic, 'bpmn:ChoreographyActivity');
let isChoreoTask = is(semantic, 'bpmn:ChoreographyTask');
let hasDI = !isMessageFlow;
/*
* Most semantic elements have an attached DI semantic object (diagram interchange) that
* bounds, widths, heights, etc. However, some elements like messages do not but
* we still want to display them as standalone shapes.
*/
if (hasDI) {
// get the DI object corresponding to this element
let di;
if (isParticipantBand) {
/*
* For participant bands, the DI object is not as easy to get as there can
* be multiple bands for the same semantic object (i.e., a bpmn:Participant).
* For that reason, we have to iterate through all band DIs and find the right one.
*/
di = parentShape.businessObject.di.$parent.planeElement.find(
diBand => diBand.choreographyActivityShape === parentShape.businessObject.di && diBand.bpmnElement === semantic
);
} else {
di = semantic.di;
}
/*
* For choreography activities, we order the participants according
* to the y position of their band. We then reposition and resize the
* bands to fit the look and feel of our modeler.
*
* On a side note, we set the properties of the di elements ourselves
* and largely ignore the values in the model. Most external modelers do
* not properly maintain these values and they can not really be trusted.
*/
if (isChoreoActivity) {
let participants = semantic.participantRef;
// temporarily link all participant business objects to the di band
// for this specific choreography activity
participants.forEach(participant => {
participant.diBand = semantic.di.$parent.planeElement.find(
diBand => diBand.choreographyActivityShape === di && diBand.bpmnElement === participant
);
});
// sort the participants by their y coordinate and get all the di bands
participants.sort((left, right) => left.diBand.bounds.y - right.diBand.bounds.y);
let diBands = participants.map(participant => participant.diBand);
// remove the temporary reference to the di band we stored in participants
participants.forEach(participant => {
delete participant.diBand;
});
// reset all the underlying di attributes
resetAllBands(semantic, diBands, di.bounds);
}
/*
* Handle attached message flows. A task must have 1 or 2 message flows attached
* and one of them must be an initiating message flow, i.e., one whose source is
* the initiating participant.
*/
if (isChoreoTask) {
let needsAdditionalMessageFlow = false;
if (semantic.get('messageFlowRef').length == 0) {
needsAdditionalMessageFlow = true;
} else if (semantic.get('messageFlowRef').length == 1) {
needsAdditionalMessageFlow = semantic.get('messageFlowRef')[0].sourceRef != semantic.initiatingParticipantRef;
}
// TODO add validation that at least one message flow has the
// initiating participant as a source
if (needsAdditionalMessageFlow) {
let messageFlow = createMessageFlowSemantics(
this._injector,
semantic,
semantic.initiatingParticipantRef
);
linkMessageFlowSemantics(this._injector, semantic, messageFlow);
}
}
/*
* Depending on the type of the element's DI objects we have to create
* different shapes.
*/
if (is(di, 'bpmndi:BPMNPlane')) {
// handle the special case that we deal with an invisible root element (process or collaboration)
element = this._elementFactory.createRoot(elementData(semantic));
this._canvas.setRootElement(element, true);
} else if (is(di, 'bpmndi:BPMNShape')) {
// elements that are displayed as shapes
let collapsed = (
is(semantic, 'bpmn:SubChoreography') || is(semantic, 'bpmn:CallChoreography')
) ? (!semantic.di.isExpanded) : false;
let hidden = parentShape && (parentShape.hidden || parentShape.collapsed);
// the bands of a collapsed choreo activity should still be visible
if (isParticipantBand && !parentShape.hidden && parentShape.collapsed) {
hidden = false;
}
let data = elementData(semantic, {
collapsed: collapsed,
hidden: hidden,
x: Math.round(di.bounds.x),
y: Math.round(di.bounds.y),
width: Math.round(di.bounds.width),
height: Math.round(di.bounds.height)
});
// choreography activity shapes need references to the band shapes
if (isChoreoActivity) {
assign(data, {
bandShapes: []
});
}
// participant bands refer the same participant, so the IDs need to be
// made unique here based on the choreography activity as well
if (isParticipantBand) {
assign(data, {
id: semantic.id + '_' + parentShape.businessObject.id,
activityShape: parentShape,
diBand: di
});
}
element = this._elementFactory.createShape(data);
// add participant band shapes to choreo shape registry
if (isParticipantBand) {
parentShape.bandShapes.push(element);
}
if (is(semantic, 'bpmn:BoundaryEvent')) {
this._attachBoundary(semantic, element);
}
if (is(semantic, 'bpmn:DataStoreReference')) {
// check wether data store is inside our outside of its semantic parent
if (!isPointInsideBBox(parentShape, getMid(di.bounds))) {
parentShape = this._canvas.getRootElement();
}
}
this._canvas.addShape(element, parentShape);
} else if (is(di, 'bpmndi:BPMNEdge')) {
// elements that are displayed as connections, sequence flows, etc.
let source = this._getSource(semantic);
let target = this._getTarget(semantic);
let hidden = parentShape && (parentShape.hidden || parentShape.collapsed);
element = this._elementFactory.createConnection(elementData(semantic, {
hidden: hidden,
source: source,
target: target,
waypoints: di.waypoint.map(waypoint => ({ x: waypoint.x, y: waypoint.y }))
}));
if (is(semantic, 'bpmn:DataAssociation')) {
// render always on top; this ensures DataAssociations
// are rendered correctly across different "hacks" people
// love to model such as cross participant / sub process
// associations
parentShape = null;
}
// insert sequence flows behind other flow nodes (cf. #727)
let parentIndex;
if (is(semantic, 'bpmn:SequenceFlow')) {
parentIndex = 0;
}
this._canvas.addConnection(element, parentShape, parentIndex);
} else {
throw new Error(this._translate('unknown di {di} for element {semantic}', {
di: elementToString(di),
semantic: elementToString(semantic)
}));
}
// (optional) external LABEL
if (isLabelExternal(semantic) && semantic.name) {
this._addLabel(semantic, element);
}
} else {
if (isMessageFlow) {
/*
* Messages are attached to a participant band. They are separate shapes
* but move with the band. They do not have an underlying DI element.
* If no message is attached to the message flow, we first have to create one.
*/
const choreo = this._canvas.getRootElement().businessObject;
const definitions = choreo.$parent;
let message = semantic.messageRef;
if (!message) {
message = this._moddle.create('bpmn:Message');
message.id = this._moddle.ids.nextPrefixed('Message_', message);
definitions.rootElements.unshift(message);
semantic.messageRef = message;
}
if (this._elementRegistry.get(message.id)) {
/*
* A shape for this message has already been created. In that case, we
* copy the message so that we do not have to deal with that when deleting
* or unlinking them later.
*/
let newMessage = this._moddle.create('bpmn:Message', omit(message, ['id','$type','di']));
newMessage.id = this._moddle.ids.nextPrefixed('Message_', newMessage);
definitions.rootElements.unshift(newMessage);
semantic.messageRef = newMessage;
}
element = createMessageShape(this._injector, parentShape, semantic);
this._canvas.addShape(element, parentShape);
}
}
this._eventBus.fire('bpmnElement.added', { element: element });
return element;
};
/**
* Returns the business object that is the host to the given boundary element.
*/
InitialRenderVisitor.prototype._getBoundaryHostSemantic = function(boundarySemantic) {
return boundarySemantic.attachedToRef;
};
/**
* Attach the boundary element to the given host
*
* @param {ModdleElement} boundarySemantic
* @param {djs.model.Base} boundaryElement
*/
InitialRenderVisitor.prototype._attachBoundary = function(boundarySemantic, boundaryElement) {
let translate = this._translate;
let hostSemantic = this._getBoundaryHostSemantic(boundarySemantic);
if (!hostSemantic) {
throw new Error(translate('missing host semantics for {semantic}', {
semantic: elementToString(boundarySemantic)
}));
}
let host = this._elementRegistry.get(hostSemantic.id);
let attachers = host && host.attachers;
if (!host) {
throw notYetDrawn(translate, boundarySemantic, hostSemantic, '(hostSemantics)');
}
// wire element.host <> host.attachers
boundaryElement.host = host;
if (!attachers) {
host.attachers = attachers = [];
}
if (attachers.indexOf(boundaryElement) === -1) {
attachers.push(boundaryElement);
}
};
/**
* add label for an element
*/
InitialRenderVisitor.prototype._addLabel = function(semantic, element) {
let bounds;
let text;
let label;
bounds = getExternalLabelBounds(semantic, element);
text = semantic.name;
if (text) {
// get corrected bounds from actual layouted text
bounds = this._textRenderer.getExternalLabelBounds(bounds, text);
}
label = this._elementFactory.createLabel(elementData(semantic, {
id: semantic.id + '_label',
labelTarget: element,
type: 'label',
hidden: element.hidden || !semantic.name,
x: Math.round(bounds.x),
y: Math.round(bounds.y),
width: Math.round(bounds.width),
height: Math.round(bounds.height)
}));
return this._canvas.addShape(label, element.parent);
};
/**
* Return the drawn connection end based on the given side.
*
* @throws {Error} if the end is not yet drawn
*/
InitialRenderVisitor.prototype._getEnd = function(semantic, side) {
let element;
let refSemantic;
let type = semantic.$type;
let translate = this._translate;
refSemantic = semantic[side + 'Ref'];
// handle mysterious isMany DataAssociation#sourceRef
if (side === 'source' && type === 'bpmn:DataInputAssociation') {
refSemantic = refSemantic && refSemantic[0];
}
// fix source / target for DataInputAssociation / DataOutputAssociation
if (side === 'source' && type === 'bpmn:DataOutputAssociation' ||
side === 'target' && type === 'bpmn:DataInputAssociation') {
refSemantic = semantic.$parent;
}
element = refSemantic && this._getElement(refSemantic);
if (element) {
return element;
}
if (refSemantic) {
throw notYetDrawn(translate, semantic, refSemantic, side + 'Ref');
} else {
throw new Error(translate('{semantic}#{side} Ref not specified', {
semantic: elementToString(semantic),
side: side
}));
}
};
InitialRenderVisitor.prototype._getSource = function(semantic) {
return this._getEnd(semantic, 'source');
};
InitialRenderVisitor.prototype._getTarget = function(semantic) {
return this._getEnd(semantic, 'target');
};
InitialRenderVisitor.prototype._getElement = function(semantic) {
return this._elementRegistry.get(semantic.id);
};