/
ChoreoCopyPaste.js
286 lines (244 loc) · 9.86 KB
/
ChoreoCopyPaste.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
import ModelCloneHelper from 'bpmn-js/lib/util/model/ModelCloneHelper';
import { getBusinessObject, is } from 'bpmn-js/lib/util/ModelUtil';
import { assign, filter, forEach } from 'min-dash';
import { getProperties, IGNORED_PROPERTIES } from 'bpmn-js/lib/util/model/ModelCloneUtils';
/**
* This is copied from BpmnCopyPaste.js because it is not exported and could thus not be reused
* @param descriptor
* @param data
* @param properties
*/
function setProperties(descriptor, data, properties) {
forEach(properties, function(property) {
if (data[property] !== undefined) {
descriptor[property] = data[property];
}
});
}
/**
* This is copied from BpmnCopyPaste.js because it is not exported and could thus not be reused
* @param element
* @param properties
*/
function removeProperties(element, properties) {
forEach(properties, function(prop) {
if (element[prop]) {
delete element[prop];
}
});
}
/**
* Creates a descriptor for copying bpmn elements. A descriptor is an intermediary object that is later turned into a
* Shape object. (Potentially also Label object).
* This is copied from BpmnCopyPaste.js because it is not exported and could thus not be reused.
* This function is called on copy
* @param element
* @param descriptor an intermediary object that is later converted to a shape
* @returns {*}
*/
function bpmnDescriptor(element, descriptor) {
// Unfortunately the business object is not copied at this point. It will be cloned on paste
// this makes it necessary to copy some of the attributes already here and save them in the descriptor.
let businessObject = getBusinessObject(element);
descriptor.oldBusinessObject = businessObject;
var colors = {};
descriptor.type = element.type;
setProperties(descriptor, businessObject.di, ['isExpanded']);
setProperties(descriptor, element, ['collapsed', 'hidden']);
setProperties(colors, businessObject.di, ['fill', 'stroke']);
descriptor.colors = colors;
if (element.type === 'label') {
// Apparently we need to return early if element is a label.
return descriptor;
}
// This is concerned with processes and moste likely not relevant for choreos. It is copied from bpmn.js
setProperties(descriptor, businessObject, [
'processRef',
'triggeredByEvent'
]);
// This is copied from bpmn.js and has something to do with sequence flows
if (businessObject.default) {
descriptor.default = businessObject.default.id;
}
if (is(element, 'bpmn:ChoreographyActivity')) {
// We need to save the participantRef array as it might be mutated by the user before pasting and cause nasty bugs.
// This array will also be used to reconstruct the band shape order
// ATTENTION: This assumes that Participant Objects cannot be deleted.
// Same for initiating participant
descriptor.copyParticipantRef = businessObject.participantRef.slice();
descriptor.copyInitParticpant = businessObject.initiatingParticipantRef;
if (is(element, 'bpmn:CallChoreography')) {
descriptor.copyParticipantAssociations = businessObject.participantAssociations;
descriptor.copyCalledChoreoRef = businessObject.calledChoreographyRef;
}
if (businessObject.messageFlowRef) {
// We need to copy the message flow and later set the new messages, unfortunately assign() doesn't work
// because the keys of a MessageFlow are not enumerable
descriptor.copyMessageFlowRef = businessObject.messageFlowRef.map(flow => {
const newFlow = assign({}, flow);
newFlow.messageRef = flow.messageRef;
newFlow.targetRef = flow.targetRef;
newFlow.sourceRef = flow.sourceRef;
newFlow.$type = flow.$type;
newFlow.$parent = flow.$parent;
return newFlow;
});
}
}
if (element.diBand) {
// shallow copy of di properties to preserve e.g. participantBandKind
descriptor.oldDiBand = assign({}, element.diBand);
}
return descriptor;
}
/**
* Creates a new Band for a copied band, however, does not set any bounds.
* @returns di
* @param descriptor
* @param bpmnFactory
* @param newElement
*/
function copyBandDi(descriptor, bpmnFactory, newElement) {
const tempBounds = { x: 0, y: 0, width: 0, height: 0 };
// Todo: this leads to 0 bounds di in xml.
// The problem is that the final position of the bands is not known yet
const bandDI = bpmnFactory.createDiShape('bpmndi:BPMNShape', tempBounds, {
choreographyActivityShape: newElement.businessObject.di,
bpmnElement: descriptor.businessObject,
participantBandKind: descriptor.oldDiBand.participantBandKind,
isMessageVisible: descriptor.oldDiBand.isMessageVisible,
isMarkerVisible: descriptor.oldDiBand.isMarkerVisible
});
return bandDI;
}
/**
* Handles the paste of a Choreo element
* This is mostly copied from BpmnCopyPaste.js because it is not exported and could, therefore,
* not be reused because we need to add special behavior for choreo elements.
* @param canvas
* @param bpmnFactory
* @param bpmnRules
* @param helper
* @param context
* @param elementFactory
* @param moddle
* @param injector
*/
function pasteElement(canvas, bpmnFactory, bpmnRules, helper, context, elementFactory, moddle, injector) {
// the descriptor is an intermediary object whose attributes will be assigned to a
// shape object which is created later thus some things cannot be set yet. Especially shape relations.
let descriptor = context.descriptor;
const createdElements = context.createdElements;
const parent = descriptor.parent;
const rootElement = canvas.getRootElement();
const oldBusinessObject = descriptor.oldBusinessObject;
let newBusinessObject;
let source;
let target;
let canConnect;
newBusinessObject = bpmnFactory.create(oldBusinessObject.$type);
var properties = getProperties(oldBusinessObject.$descriptor);
properties = filter(properties, function(property) {
return IGNORED_PROPERTIES.indexOf(property.replace(/bpmn:/, '')) === -1;
});
// copies business object
descriptor.businessObject = helper.clone(oldBusinessObject, newBusinessObject, properties);
// clone additional stuff which is specific to choreos and not part of bpmn.js
if (is(descriptor.oldBusinessObject, 'bpmn:ChoreographyActivity')) {
descriptor.businessObject.participantRef = descriptor.copyParticipantRef;
descriptor.businessObject.initiatingParticipantRef = descriptor.copyInitParticpant;
descriptor.bandShapes = [];
if (is(descriptor.oldBusinessObject, 'bpmn:CallChoreography')) {
descriptor.businessObject.participantAssociations = descriptor.copyParticipantAssociations;
descriptor.businessObject.calledChoreographyRef = descriptor.copyCalledChoreoRef;
}
}
if (descriptor.parent && descriptor.parent.oldBusinessObject &&
createdElements[descriptor.parent.oldBusinessObject.id]) {
const newElement = createdElements[descriptor.parent.oldBusinessObject.id].element;
if (is(newElement.oldBusinessObject, 'bpmn:ChoreographyActivity')
&& is(descriptor.businessObject, 'bpmn:Participant')) {
// we need to overwrite the created business object because bands do not need a new one
descriptor.businessObject = oldBusinessObject;
// The id will be overwritten by ElementFactory with the business objects id. So we have to set it again later.
descriptor.id = moddle.ids.nextPrefixed('ParticipantBand_', descriptor.businessObject);
descriptor.diBand = copyBandDi(descriptor, bpmnFactory, newElement);
descriptor.activityShape = newElement;
}
}
// copy element end
if (descriptor.type === 'label') {
return;
}
if (is(parent, 'bpmn:Process')) {
descriptor.parent = is(rootElement, 'bpmn:Collaboration') ? rootElement : parent;
}
if (descriptor.type === 'bpmn:DataOutputAssociation' ||
descriptor.type === 'bpmn:DataInputAssociation' ||
descriptor.type === 'bpmn:MessageFlow') {
descriptor.parent = rootElement;
}
if (is(parent, 'bpmn:Lane')) {
descriptor.parent = parent.parent;
}
// make sure that the correct type of connection is created
if (descriptor.waypoints) {
source = createdElements[descriptor.source];
target = createdElements[descriptor.target];
if (source && target) {
source = source.element;
target = target.element;
}
canConnect = bpmnRules.canConnect(source, target);
if (canConnect) {
descriptor.type = canConnect.type;
}
}
// remove the id or else we cannot paste multiple times
delete newBusinessObject.id;
// assign an ID
bpmnFactory._ensureId(newBusinessObject);
// Does not fire for Choreo participants because processRef is undefined, could be removed?
if (descriptor.type === 'bpmn:Participant' && descriptor.processRef) {
descriptor.processRef = newBusinessObject.processRef = bpmnFactory.create('bpmn:Process');
}
setProperties(newBusinessObject, descriptor, [
'isExpanded',
'triggeredByEvent'
]);
removeProperties(descriptor, [
'triggeredByEvent'
]);
}
/**
* This Module is responsible for pasting single elements. It completely replaces the BpmnCopyPaste module as
* that has incompatible behaviour that cannot be overwritten. Some functionality is copied and used when applicable.
* @param bpmnFactory
* @param eventBus
* @param copyPaste
* @param clipboard
* @param canvas
* @param bpmnRules
* @param elementFactory
* @param moddle
* @param injector
* @constructor
*/
export default function ChoreoCopyPaste(bpmnFactory, eventBus, copyPaste, clipboard, canvas, bpmnRules, elementFactory, moddle, injector) {
var helper = new ModelCloneHelper(eventBus, bpmnFactory);
copyPaste.registerDescriptor(bpmnDescriptor);
eventBus.on('element.paste', function(context) {
pasteElement(canvas, bpmnFactory, bpmnRules, helper, context, elementFactory, moddle, injector);
});
}
ChoreoCopyPaste.$inject = [
'bpmnFactory',
'eventBus',
'copyPaste',
'clipboard',
'canvas',
'bpmnRules',
'elementFactory',
'moddle',
'injector'
];