/
CreateOnFlowBehavior.js
95 lines (65 loc) · 2.45 KB
/
CreateOnFlowBehavior.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
'use strict';
var inherits = require('inherits');
var assign = require('lodash/object/assign');
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
var getApproxIntersection = require('diagram-js/lib/util/LineIntersection').getApproxIntersection;
function copy(obj) {
return assign({}, obj);
}
function CreateOnFlowBehavior(eventBus, bpmnRules, modeling) {
CommandInterceptor.call(this, eventBus);
/**
* Reconnect start / end of a connection after
* dropping an element on a flow.
*/
this.preExecute('shape.create', function(context) {
var parent = context.parent,
shape = context.shape;
if (bpmnRules.canInsert(shape, parent)) {
context.insertTarget = parent;
context.parent = parent.parent;
}
}, true);
this.postExecute('shape.create', function(context) {
var shape = context.shape,
insertTarget = context.insertTarget,
position = context.position,
source,
target,
reconnected,
intersection,
waypoints,
waypointsBefore,
waypointsAfter,
dockingPoint;
if (insertTarget) {
waypoints = insertTarget.waypoints;
intersection = getApproxIntersection(waypoints, position);
if (intersection) {
waypointsBefore = waypoints.slice(0, intersection.index);
waypointsAfter = waypoints.slice(intersection.index + (intersection.bendpoint ? 1 : 0));
dockingPoint = intersection.bendpoint ? waypoints[intersection.index] : position;
waypointsBefore.push(copy(dockingPoint));
waypointsAfter.unshift(copy(dockingPoint));
}
source = insertTarget.source;
target = insertTarget.target;
if (bpmnRules.canConnect(source, shape, insertTarget)) {
// reconnect source -> inserted shape
modeling.reconnectEnd(insertTarget, shape, waypointsBefore || copy(position));
reconnected = true;
}
if (bpmnRules.canConnect(shape, target, insertTarget)) {
if (!reconnected) {
// reconnect inserted shape -> end
modeling.reconnectStart(insertTarget, shape, waypointsAfter || copy(position));
} else {
modeling.connect(shape, target, { type: insertTarget.type, waypoints: waypointsAfter });
}
}
}
}, true);
}
inherits(CreateOnFlowBehavior, CommandInterceptor);
CreateOnFlowBehavior.$inject = [ 'eventBus', 'bpmnRules', 'modeling' ];
module.exports = CreateOnFlowBehavior;