mida
Version:
Muiltiple Instance and Data Animator for BPMN models.
142 lines (108 loc) • 3.23 kB
JavaScript
import inherits from 'inherits';
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, 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));
}
inherits(CustomUpdater, CommandInterceptor);
CustomUpdater.$inject = [ 'eventBus', 'bpmnjs' ];
/////// helpers ///////////////////////////////////
function copyWaypoints(connection) {
return connection.waypoints.map(function(p) {
return { x: p.x, y: p.y };
});
}
function isCustom(element) {
return element && (element.type === 'bpmn:DataInput' || element.type === 'bpmn:DataOutput');
}
function ifCustomElement(fn) {
return function(event) {
var context = event.context,
element = context.shape || context.connection;
if (isCustom(element)) {
fn(event);
}
};
}