-
Notifications
You must be signed in to change notification settings - Fork 8
/
CustomUpdater.js
160 lines (123 loc) · 3.7 KB
/
CustomUpdater.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
import inherits from 'inherits-browser';
import {
pick,
assign
} from 'min-dash';
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
import {
add as collectionAdd,
remove as collectionRemove
} from 'diagram-js/lib/util/Collections';
/**
* A handler responsible for updating the custom element's businessObject
* once changes on the diagram happen.
*/
export default function CustomUpdater(eventBus, modeling, bpmnjs) {
CommandInterceptor.call(this, eventBus);
function updateCustomElement(e) {
var context = e.context,
shape = context.shape,
businessObject = shape.businessObject;
if (!isCustom(shape)) {
return;
}
var parent = shape.parent;
var customElements = bpmnjs._customElements;
// make sure element is added / removed from bpmnjs.customElements
if (!parent) {
collectionRemove(customElements, businessObject);
} else {
collectionAdd(customElements, businessObject);
}
// save custom element position
assign(businessObject, pick(shape, [ 'x', 'y' ]));
}
function updateCustomConnection(e) {
var context = e.context,
connection = context.connection,
source = connection.source,
target = connection.target,
businessObject = connection.businessObject;
var parent = connection.parent;
var customElements = bpmnjs._customElements;
// make sure element is added / removed from bpmnjs.customElements
if (!parent) {
collectionRemove(customElements, businessObject);
} else {
collectionAdd(customElements, businessObject);
}
// update waypoints
assign(businessObject, {
waypoints: copyWaypoints(connection)
});
if (source && target) {
assign(businessObject, {
source: source.id,
target: target.id
});
}
}
this.executed([
'shape.create',
'shape.move',
'shape.delete'
], ifCustomElement(updateCustomElement));
this.reverted([
'shape.create',
'shape.move',
'shape.delete'
], ifCustomElement(updateCustomElement));
this.executed([
'connection.create',
'connection.reconnectStart',
'connection.reconnectEnd',
'connection.updateWaypoints',
'connection.delete',
'connection.layout',
'connection.move'
], ifCustomElement(updateCustomConnection));
this.reverted([
'connection.create',
'connection.reconnectStart',
'connection.reconnectEnd',
'connection.updateWaypoints',
'connection.delete',
'connection.layout',
'connection.move'
], ifCustomElement(updateCustomConnection));
/**
* When morphing a Process into a Collaboration or vice-versa,
* make sure that the existing custom elements get their parents updated.
*/
function updateCustomElementsRoot(event) {
var context = event.context,
oldRoot = context.oldRoot,
newRoot = context.newRoot,
children = oldRoot.children;
var customChildren = children.filter(isCustom);
if (customChildren.length) {
modeling.moveElements(customChildren, { x: 0, y: 0 }, newRoot);
}
}
this.postExecute('canvas.updateRoot', updateCustomElementsRoot);
}
inherits(CustomUpdater, CommandInterceptor);
CustomUpdater.$inject = [ 'eventBus', 'modeling', 'bpmnjs' ];
// helpers ///////////////////////////////////
function copyWaypoints(connection) {
return connection.waypoints.map(function(p) {
return { x: p.x, y: p.y };
});
}
function isCustom(element) {
return element && /custom:/.test(element.type);
}
function ifCustomElement(fn) {
return function(event) {
var context = event.context,
element = context.shape || context.connection;
if (isCustom(element)) {
fn(event);
}
};
}