1 import inherits from 'inherits';
3 import { pick, assign } from 'min-dash';
5 import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
9 remove as collectionRemove
10 } from 'diagram-js/lib/util/Collections';
13 * A handler responsible for updating the custom element's businessObject
14 * once changes on the diagram happen.
16 export default function CustomUpdater(eventBus, bpmnjs) {
17 CommandInterceptor.call(this, eventBus);
19 function updateCustomElement(e) {
20 var context = e.context,
21 shape = context.shape,
22 businessObject = shape.businessObject;
24 if (!isCustom(shape)) {
28 var parent = shape.parent;
30 var customElements = bpmnjs._customElements;
32 // make sure element is added / removed from bpmnjs.customElements
34 collectionRemove(customElements, businessObject);
36 collectionAdd(customElements, businessObject);
39 // save custom element position
40 assign(businessObject, pick(shape, ['x', 'y']));
43 function updateCustomConnection(e) {
44 var context = e.context,
45 connection = context.connection,
46 source = connection.source,
47 target = connection.target,
48 businessObject = connection.businessObject;
50 var parent = connection.parent;
52 var customElements = bpmnjs._customElements;
54 // make sure element is added / removed from bpmnjs.customElements
56 collectionRemove(customElements, businessObject);
58 collectionAdd(customElements, businessObject);
62 assign(businessObject, {
63 waypoints: copyWaypoints(connection)
66 if (source && target) {
67 assign(businessObject, {
75 ['shape.create', 'shape.move', 'shape.delete'],
76 ifCustomElement(updateCustomElement)
80 ['shape.create', 'shape.move', 'shape.delete'],
81 ifCustomElement(updateCustomElement)
87 'connection.reconnectStart',
88 'connection.reconnectEnd',
89 'connection.updateWaypoints',
94 ifCustomElement(updateCustomConnection)
100 'connection.reconnectStart',
101 'connection.reconnectEnd',
102 'connection.updateWaypoints',
107 ifCustomElement(updateCustomConnection)
111 inherits(CustomUpdater, CommandInterceptor);
113 CustomUpdater.$inject = ['eventBus', 'bpmnjs'];
115 /////// helpers ///////////////////////////////////
117 function copyWaypoints(connection) {
118 return connection.waypoints.map(function(p) {
119 return { x: p.x, y: p.y };
123 function isCustom(element) {
124 return element && /custom:/.test(element.type);
127 function ifCustomElement(fn) {
128 return function(event) {
129 var context = event.context,
130 element = context.shape || context.connection;
132 if (isCustom(element)) {