Skip to content

Commit f197887

Browse files
committed
chore: simplify
1 parent a280c08 commit f197887

File tree

4 files changed

+175
-59
lines changed

4 files changed

+175
-59
lines changed

lib/features/copy-paste/ModdleCopy.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import {
1010
sortBy
1111
} from 'min-dash';
1212

13-
import { is, isAny } from '../../util/ModelUtil';
14-
15-
import { hasAnyEventDefinition } from '../../util/ModelUtil';
13+
import { is } from '../../util/ModelUtil';
1614

1715
var DISALLOWED_PROPERTIES = [
1816
'artifacts',
@@ -26,6 +24,13 @@ var DISALLOWED_PROPERTIES = [
2624
'categoryValue'
2725
];
2826

27+
var ALLOWED_REFERENCES = [
28+
'errorRef',
29+
'escalationRef',
30+
'messageRef',
31+
'signalRef'
32+
];
33+
2934
/**
3035
* @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
3136
* @typedef {import('../modeling/BpmnFactory').default} BpmnFactory
@@ -66,13 +71,13 @@ export default function ModdleCopy(eventBus, bpmnFactory, moddle) {
6671
propertyName = context.propertyName,
6772
property = context.property;
6873

69-
// allow specific referenced elements to be copied
70-
// check whether the property is a reference and belongs to event definition
71-
if (isRefPropertyAllowed(property, parent)) {
74+
if (propertyName && ALLOWED_REFERENCES.includes(propertyName)) {
75+
76+
// allow copying reference
7277
return property;
7378
}
7479

75-
if (propertyName && DISALLOWED_PROPERTIES.indexOf(propertyName) !== -1) {
80+
if (propertyName && DISALLOWED_PROPERTIES.includes(propertyName)) {
7681

7782
// disallow copying property
7883
return false;
@@ -279,23 +284,4 @@ export function getPropertyNames(descriptor, keepDefaultProperties) {
279284

280285
return properties.concat(property.name);
281286
}, []);
282-
}
283-
284-
function isRefPropertyAllowed(property, parent) {
285-
const allowedEventDefTypes = [
286-
'bpmn:ErrorEventDefinition',
287-
'bpmn:EscalationEventDefinition',
288-
'bpmn:MessageEventDefinition',
289-
'bpmn:SignalEventDefinition'
290-
];
291-
292-
const allowedPropertyTypes = [
293-
'bpmn:Error',
294-
'bpmn:Message',
295-
'bpmn:Signal',
296-
'bpmn:Escalation'
297-
];
298-
299-
return hasAnyEventDefinition(parent, allowedEventDefTypes) &&
300-
isAny(property, allowedPropertyTypes);
301-
}
287+
}

lib/util/ModelUtil.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
2-
some,
3-
isArray
2+
some
43
} from 'min-dash';
54

65
/**
@@ -57,20 +56,4 @@ export function getBusinessObject(element) {
5756
*/
5857
export function getDi(element) {
5958
return element && element.di;
60-
}
61-
62-
/**
63-
* @param {ModdleElement} moddleElement
64-
* @param {string[]} types
65-
*
66-
* @return {boolean}
67-
*/
68-
export function hasAnyEventDefinition(moddleElement, types) {
69-
if (!isArray(types)) {
70-
types = [ types ];
71-
}
72-
73-
return some(types, function(type) {
74-
return is(moddleElement, type);
75-
});
7659
}

test/spec/features/copy-paste/ModdleCopySpec.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,162 @@ describe('features/copy-paste/ModdleCopy', function() {
823823
expect(newGroup.categoryValueRef.$parent).to.equal(newCategory);
824824
}));
825825

826+
827+
describe.only('default events', function() {
828+
829+
describe('allowed references', function() {
830+
831+
it('should copy error reference', inject(function(moddle, moddleCopy) {
832+
833+
// given
834+
var boundaryEvent = moddle.create('bpmn:BoundaryEvent'),
835+
errorEventDefinition = moddle.create('bpmn:ErrorEventDefinition');
836+
837+
errorEventDefinition.$parent = boundaryEvent;
838+
839+
boundaryEvent.eventDefinitions = [ errorEventDefinition ];
840+
841+
var error = moddle.create('bpmn:Error');
842+
843+
errorEventDefinition.errorRef = error;
844+
845+
// when
846+
var boundaryEventCopy = moddleCopy.copyElement(boundaryEvent, moddle.create('bpmn:BoundaryEvent'));
847+
848+
// then
849+
expect(boundaryEventCopy.eventDefinitions).to.have.length(1);
850+
expect(boundaryEventCopy.eventDefinitions[0]).not.to.equal(errorEventDefinition);
851+
expect(boundaryEventCopy.eventDefinitions[0].$type).to.equal('bpmn:ErrorEventDefinition');
852+
expect(boundaryEventCopy.eventDefinitions[0].errorRef).to.exist;
853+
expect(boundaryEventCopy.eventDefinitions[0].errorRef).to.equal(error);
854+
}));
855+
856+
857+
it('should copy escalation reference', inject(function(moddle, moddleCopy) {
858+
859+
// given
860+
var boundaryEvent = moddle.create('bpmn:BoundaryEvent'),
861+
escalationEventDefinition = moddle.create('bpmn:EscalationEventDefinition');
862+
863+
escalationEventDefinition.$parent = boundaryEvent;
864+
865+
boundaryEvent.eventDefinitions = [ escalationEventDefinition ];
866+
867+
var error = moddle.create('bpmn:Escalation');
868+
869+
escalationEventDefinition.escalationRef = error;
870+
871+
// when
872+
var boundaryEventCopy = moddleCopy.copyElement(boundaryEvent, moddle.create('bpmn:BoundaryEvent'));
873+
874+
// then
875+
expect(boundaryEventCopy.eventDefinitions).to.have.length(1);
876+
expect(boundaryEventCopy.eventDefinitions[0]).not.to.equal(escalationEventDefinition);
877+
expect(boundaryEventCopy.eventDefinitions[0].$type).to.equal('bpmn:EscalationEventDefinition');
878+
expect(boundaryEventCopy.eventDefinitions[0].escalationRef).to.exist;
879+
expect(boundaryEventCopy.eventDefinitions[0].escalationRef).to.equal(error);
880+
}));
881+
882+
883+
it('should copy message reference (event)', inject(function(moddle, moddleCopy) {
884+
885+
// given
886+
var boundaryEvent = moddle.create('bpmn:BoundaryEvent'),
887+
messageEventDefinition = moddle.create('bpmn:MessageEventDefinition');
888+
889+
messageEventDefinition.$parent = boundaryEvent;
890+
891+
boundaryEvent.eventDefinitions = [ messageEventDefinition ];
892+
893+
var message = moddle.create('bpmn:Message');
894+
895+
messageEventDefinition.messageRef = message;
896+
897+
// when
898+
var boundaryEventCopy = moddleCopy.copyElement(boundaryEvent, moddle.create('bpmn:BoundaryEvent'));
899+
900+
// then
901+
expect(boundaryEventCopy.eventDefinitions).to.have.length(1);
902+
expect(boundaryEventCopy.eventDefinitions[0]).not.to.equal(messageEventDefinition);
903+
expect(boundaryEventCopy.eventDefinitions[0].$type).to.equal('bpmn:MessageEventDefinition');
904+
expect(boundaryEventCopy.eventDefinitions[0].messageRef).to.exist;
905+
expect(boundaryEventCopy.eventDefinitions[0].messageRef).to.equal(message);
906+
}));
907+
908+
909+
it('should copy message reference (receive task)', inject(function(moddle, moddleCopy) {
910+
911+
// given
912+
var receiveTask = moddle.create('bpmn:ReceiveTask');
913+
914+
var message = moddle.create('bpmn:Message');
915+
916+
receiveTask.messageRef = message;
917+
918+
// when
919+
var receiveTaskCopy = moddleCopy.copyElement(receiveTask, moddle.create('bpmn:ReceiveTask'));
920+
921+
// then
922+
expect(receiveTaskCopy.messageRef).to.exist;
923+
expect(receiveTaskCopy.messageRef).to.equal(message);
924+
}));
925+
926+
927+
it('should copy signal reference', inject(function(moddle, moddleCopy) {
928+
929+
// given
930+
var boundaryEvent = moddle.create('bpmn:BoundaryEvent'),
931+
signalEventDefinition = moddle.create('bpmn:SignalEventDefinition');
932+
933+
signalEventDefinition.$parent = boundaryEvent;
934+
935+
boundaryEvent.eventDefinitions = [ signalEventDefinition ];
936+
937+
var signal = moddle.create('bpmn:Signal');
938+
939+
signalEventDefinition.signalRef = signal;
940+
941+
// when
942+
var boundaryEventCopy = moddleCopy.copyElement(boundaryEvent, moddle.create('bpmn:BoundaryEvent'));
943+
944+
// then
945+
expect(boundaryEventCopy.eventDefinitions).to.have.length(1);
946+
expect(boundaryEventCopy.eventDefinitions[0]).not.to.equal(signalEventDefinition);
947+
expect(boundaryEventCopy.eventDefinitions[0].$type).to.equal('bpmn:SignalEventDefinition');
948+
expect(boundaryEventCopy.eventDefinitions[0].signalRef).to.exist;
949+
expect(boundaryEventCopy.eventDefinitions[0].signalRef).to.equal(signal);
950+
}));
951+
952+
});
953+
954+
955+
describe('disallowed properties', function() {
956+
957+
it('should NOT copy incoming and outgoing', inject(function(moddle, moddleCopy) {
958+
959+
// given
960+
var incoming = moddle.create('bpmn:SequenceFlow'),
961+
outgoing = moddle.create('bpmn:SequenceFlow'),
962+
task = moddle.create('bpmn:Task', {
963+
incoming: [ incoming ],
964+
outgoing: [ outgoing ]
965+
});
966+
967+
expect(task.get('incoming')).to.have.length(1);
968+
expect(task.get('outgoing')).to.have.length(1);
969+
970+
// when
971+
var taskCopy = moddleCopy.copyElement(task, moddle.create('bpmn:Task'));
972+
973+
// then
974+
expect(taskCopy.get('incoming')).to.have.length(0);
975+
expect(taskCopy.get('outgoing')).to.have.length(0);
976+
}));
977+
978+
});
979+
980+
});
981+
826982
});
827983

828984

test/spec/features/modeling/behavior/BoundaryEventBehaviorSpec.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('features/modeling/behavior - boundary event', function() {
8585
});
8686

8787
// then
88-
expectRefEquals(interruptingBoundaryEvent, nonInterruptingBoundaryEvent, 'messageRef');
88+
expect(getReferencedRootElement(nonInterruptingBoundaryEvent, 'messageRef')).to.equal(message);
8989
}));
9090

9191

@@ -106,7 +106,7 @@ describe('features/modeling/behavior - boundary event', function() {
106106
});
107107

108108
// then
109-
expectRefEquals(interruptingBoundaryEvent, nonInterruptingBoundaryEvent, 'escalationRef');
109+
expect(getReferencedRootElement(nonInterruptingBoundaryEvent, 'escalationRef')).to.equal(escalation);
110110
}));
111111

112112

@@ -127,7 +127,7 @@ describe('features/modeling/behavior - boundary event', function() {
127127
});
128128

129129
// then
130-
expectRefEquals(interruptingBoundaryEvent, nonInterruptingBoundaryEvent, 'errorRef');
130+
expect(getReferencedRootElement(nonInterruptingBoundaryEvent, 'errorRef')).to.equal(error);
131131
}));
132132

133133

@@ -148,7 +148,7 @@ describe('features/modeling/behavior - boundary event', function() {
148148
});
149149

150150
// then
151-
expectRefEquals(interruptingBoundaryEvent, nonInterruptingBoundaryEvent, 'signalRef');
151+
expect(getReferencedRootElement(nonInterruptingBoundaryEvent, 'signalRef')).to.equal(signal);
152152
}));
153153

154154
});
@@ -173,7 +173,7 @@ describe('features/modeling/behavior - boundary event', function() {
173173
});
174174

175175
// then
176-
expectRefEquals(interruptingBoundaryEvent, nonInterruptingBoundaryEvent, 'messageRef');
176+
expect(getReferencedRootElement(nonInterruptingBoundaryEvent, 'messageRef')).to.equal(message);
177177
}));
178178

179179
});
@@ -190,13 +190,4 @@ function getReferencedRootElement(element, propertyName) {
190190
eventDefinition = businessObject.eventDefinitions[ 0 ];
191191

192192
return eventDefinition.get(propertyName);
193-
}
194-
195-
function expectRefEquals(element, newElement, propertyName) {
196-
const previousRefElement = getReferencedRootElement(element, propertyName);
197-
const newRefElement = getReferencedRootElement(newElement, propertyName);
198-
199-
// expect name to be same
200-
expect(previousRefElement).to.equal(newRefElement);
201-
202193
}

0 commit comments

Comments
 (0)