2014-07-23 16:53:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
var assign = require('lodash/object/assign'),
|
2015-05-04 12:59:26 +00:00
|
|
|
forEach = require('lodash/collection/forEach'),
|
2015-04-27 14:50:09 +00:00
|
|
|
inherits = require('inherits');
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
var Collections = require('diagram-js/lib/util/Collections'),
|
|
|
|
Model = require('diagram-js/lib/model');
|
2014-11-27 10:55:38 +00:00
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A handler responsible for updating the underlying BPMN 2.0 XML + DI
|
|
|
|
* once changes on the diagram happen
|
|
|
|
*/
|
2014-07-30 14:06:32 +00:00
|
|
|
function BpmnUpdater(eventBus, bpmnFactory, connectionDocking) {
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
this._bpmnFactory = bpmnFactory;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
////// connection cropping /////////////////////////
|
|
|
|
|
|
|
|
// crop connection ends during create/update
|
|
|
|
function cropConnection(e) {
|
|
|
|
var context = e.context,
|
|
|
|
connection;
|
|
|
|
|
|
|
|
if (!context.cropped) {
|
|
|
|
connection = context.connection;
|
|
|
|
connection.waypoints = connectionDocking.getCroppedWaypoints(connection);
|
|
|
|
context.cropped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 17:48:06 +00:00
|
|
|
this.executed([
|
|
|
|
'connection.layout',
|
|
|
|
'connection.create',
|
|
|
|
'connection.reconnectEnd',
|
|
|
|
'connection.reconnectStart'
|
|
|
|
], cropConnection);
|
2014-07-30 14:06:32 +00:00
|
|
|
|
|
|
|
this.reverted([ 'connection.layout' ], function(e) {
|
|
|
|
delete e.context.cropped;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////// BPMN + DI update /////////////////////////
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
// update parent
|
2014-11-21 08:19:35 +00:00
|
|
|
function updateParent(e) {
|
|
|
|
self.updateParent(e.context.shape || e.context.connection);
|
2014-07-23 16:53:33 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 13:20:48 +00:00
|
|
|
this.executed([ 'shape.move',
|
|
|
|
'shape.create',
|
|
|
|
'shape.delete',
|
|
|
|
'connection.create',
|
2014-11-21 08:19:35 +00:00
|
|
|
'connection.move',
|
|
|
|
'connection.delete' ], updateParent);
|
2014-08-22 13:20:48 +00:00
|
|
|
this.reverted([ 'shape.move',
|
|
|
|
'shape.create',
|
|
|
|
'shape.delete',
|
|
|
|
'connection.create',
|
2014-11-21 08:19:35 +00:00
|
|
|
'connection.move',
|
|
|
|
'connection.delete' ], updateParent);
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2015-05-04 12:59:26 +00:00
|
|
|
/*
|
|
|
|
* ## Updating Parent
|
|
|
|
*
|
|
|
|
* When morphing a Process into a Collaboration or vice-versa,
|
|
|
|
* make sure that both the *semantic* and *di* parent of each element
|
|
|
|
* is updated.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function updateRoot(event) {
|
|
|
|
var context = event.context,
|
|
|
|
oldRoot = context.oldRoot,
|
|
|
|
children = oldRoot.children;
|
|
|
|
|
|
|
|
forEach(children, function(child) {
|
|
|
|
self.updateParent(child);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.executed([ 'canvas.updateRoot' ], updateRoot);
|
|
|
|
this.reverted([ 'canvas.updateRoot' ], updateRoot);
|
|
|
|
|
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
// update bounds
|
2014-07-30 14:07:43 +00:00
|
|
|
function updateBounds(e) {
|
|
|
|
self.updateBounds(e.context.shape);
|
|
|
|
}
|
|
|
|
|
2014-10-10 07:06:21 +00:00
|
|
|
this.executed([ 'shape.move', 'shape.create', 'shape.resize' ], updateBounds);
|
|
|
|
this.reverted([ 'shape.move', 'shape.create', 'shape.resize' ], updateBounds);
|
2014-07-30 14:07:43 +00:00
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
|
|
|
|
// attach / detach connection
|
2014-07-23 16:53:33 +00:00
|
|
|
function updateConnection(e) {
|
|
|
|
self.updateConnection(e.context.connection);
|
|
|
|
}
|
|
|
|
|
2015-01-14 17:48:06 +00:00
|
|
|
this.executed([
|
|
|
|
'connection.create',
|
|
|
|
'connection.move',
|
|
|
|
'connection.delete',
|
|
|
|
'connection.reconnectEnd',
|
|
|
|
'connection.reconnectStart'
|
|
|
|
], updateConnection);
|
|
|
|
|
|
|
|
this.reverted([
|
|
|
|
'connection.create',
|
|
|
|
'connection.move',
|
|
|
|
'connection.delete',
|
|
|
|
'connection.reconnectEnd',
|
|
|
|
'connection.reconnectStart'
|
|
|
|
], updateConnection);
|
2014-07-30 14:06:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
// update waypoints
|
|
|
|
function updateConnectionWaypoints(e) {
|
|
|
|
self.updateConnectionWaypoints(e.context.connection);
|
|
|
|
}
|
|
|
|
|
2015-01-14 17:48:06 +00:00
|
|
|
this.executed([
|
|
|
|
'connection.layout',
|
|
|
|
'connection.move',
|
|
|
|
'connection.updateWaypoints',
|
|
|
|
'connection.reconnectEnd',
|
|
|
|
'connection.reconnectStart'
|
|
|
|
], updateConnectionWaypoints);
|
|
|
|
|
|
|
|
this.reverted([
|
|
|
|
'connection.layout',
|
|
|
|
'connection.move',
|
|
|
|
'connection.updateWaypoints',
|
|
|
|
'connection.reconnectEnd',
|
|
|
|
'connection.reconnectStart'
|
|
|
|
], updateConnectionWaypoints);
|
2014-07-23 16:53:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
inherits(BpmnUpdater, CommandInterceptor);
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
module.exports = BpmnUpdater;
|
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
BpmnUpdater.$inject = [ 'eventBus', 'bpmnFactory', 'connectionDocking'];
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/////// implementation //////////////////////////////////
|
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
|
2014-11-21 08:19:35 +00:00
|
|
|
BpmnUpdater.prototype.updateParent = function(element) {
|
2014-10-27 11:07:57 +00:00
|
|
|
|
2014-11-27 10:55:38 +00:00
|
|
|
// do not update BPMN 2.0 label parent
|
|
|
|
if (element instanceof Model.Label) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-21 08:19:35 +00:00
|
|
|
var parentShape = element.parent;
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2014-11-21 08:19:35 +00:00
|
|
|
var businessObject = element.businessObject,
|
2014-07-23 16:53:33 +00:00
|
|
|
parentBusinessObject = parentShape && parentShape.businessObject,
|
|
|
|
parentDi = parentBusinessObject && parentBusinessObject.di;
|
|
|
|
|
|
|
|
this.updateSemanticParent(businessObject, parentBusinessObject);
|
|
|
|
|
|
|
|
this.updateDiParent(businessObject.di, parentDi);
|
|
|
|
};
|
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
|
2014-07-30 14:07:43 +00:00
|
|
|
BpmnUpdater.prototype.updateBounds = function(shape) {
|
|
|
|
|
|
|
|
var di = shape.businessObject.di;
|
|
|
|
|
2014-11-27 10:55:38 +00:00
|
|
|
var bounds = (shape instanceof Model.Label) ? this._getLabel(di).bounds : di.bounds;
|
2014-07-30 14:07:43 +00:00
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
assign(bounds, {
|
2014-07-30 14:07:43 +00:00
|
|
|
x: shape.x,
|
2014-08-02 14:38:25 +00:00
|
|
|
y: shape.y,
|
|
|
|
width: shape.width,
|
|
|
|
height: shape.height
|
2014-07-30 14:07:43 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
BpmnUpdater.prototype.updateDiParent = function(di, parentDi) {
|
|
|
|
|
|
|
|
if (parentDi && !parentDi.$instanceOf('bpmndi:BPMNPlane')) {
|
|
|
|
parentDi = parentDi.$parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (di.$parent === parentDi) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var planeElements = (parentDi || di.$parent).get('planeElement');
|
|
|
|
|
|
|
|
if (parentDi) {
|
|
|
|
planeElements.push(di);
|
|
|
|
di.$parent = parentDi;
|
|
|
|
} else {
|
|
|
|
Collections.remove(planeElements, di);
|
|
|
|
di.$parent = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-31 13:02:04 +00:00
|
|
|
function getDefinitions(element) {
|
|
|
|
while (element && !element.$instanceOf('bpmn:Definitions')) {
|
|
|
|
element = element.$parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
BpmnUpdater.prototype.updateSemanticParent = function(businessObject, newParent) {
|
|
|
|
|
2014-12-02 07:36:15 +00:00
|
|
|
var containment;
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
if (businessObject.$parent === newParent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-02 07:36:15 +00:00
|
|
|
if (businessObject.$instanceOf('bpmn:FlowElement')) {
|
|
|
|
|
|
|
|
if (newParent && newParent.$instanceOf('bpmn:Participant')) {
|
|
|
|
newParent = newParent.processRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
containment = 'flowElements';
|
2015-04-16 07:11:04 +00:00
|
|
|
|
2014-12-02 07:36:15 +00:00
|
|
|
} else
|
|
|
|
|
|
|
|
if (businessObject.$instanceOf('bpmn:Artifact')) {
|
|
|
|
|
|
|
|
while (newParent &&
|
|
|
|
!newParent.$instanceOf('bpmn:Process') &&
|
|
|
|
!newParent.$instanceOf('bpmn:SubProcess') &&
|
|
|
|
!newParent.$instanceOf('bpmn:Collaboration')) {
|
|
|
|
|
|
|
|
if (newParent.$instanceOf('bpmn:Participant')) {
|
|
|
|
newParent = newParent.processRef;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
newParent = newParent.$parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
containment = 'artifacts';
|
2015-04-16 07:11:04 +00:00
|
|
|
} else
|
|
|
|
|
|
|
|
if (businessObject.$instanceOf('bpmn:MessageFlow')) {
|
|
|
|
containment = 'messageFlows';
|
|
|
|
|
|
|
|
} else
|
2014-12-02 07:36:15 +00:00
|
|
|
|
2015-03-31 13:02:04 +00:00
|
|
|
if (businessObject.$instanceOf('bpmn:Participant')) {
|
|
|
|
containment = 'participants';
|
|
|
|
|
|
|
|
// make sure the participants process is properly attached / detached
|
|
|
|
// from the XML document
|
|
|
|
|
|
|
|
var process = businessObject.processRef,
|
|
|
|
definitions;
|
|
|
|
|
|
|
|
if (process) {
|
|
|
|
definitions = getDefinitions(businessObject.$parent || newParent);
|
|
|
|
|
|
|
|
if (businessObject.$parent) {
|
|
|
|
Collections.remove(definitions.get('rootElements'), process);
|
|
|
|
process.$parent = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newParent) {
|
|
|
|
Collections.add(definitions.get('rootElements'), process);
|
|
|
|
process.$parent = definitions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-02 07:36:15 +00:00
|
|
|
if (!containment) {
|
|
|
|
throw new Error('no parent for ', businessObject, newParent);
|
|
|
|
}
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
var children;
|
|
|
|
|
|
|
|
if (businessObject.$parent) {
|
|
|
|
// remove from old parent
|
2014-12-02 07:36:15 +00:00
|
|
|
children = businessObject.$parent.get(containment);
|
2014-07-23 16:53:33 +00:00
|
|
|
Collections.remove(children, businessObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newParent) {
|
|
|
|
businessObject.$parent = null;
|
|
|
|
} else {
|
|
|
|
// add to new parent
|
2014-12-02 07:36:15 +00:00
|
|
|
children = newParent.get(containment);
|
2014-07-23 16:53:33 +00:00
|
|
|
children.push(businessObject);
|
|
|
|
businessObject.$parent = newParent;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
BpmnUpdater.prototype.updateConnectionWaypoints = function(connection) {
|
2014-10-27 11:07:57 +00:00
|
|
|
|
2014-07-30 14:06:32 +00:00
|
|
|
connection.businessObject.di.set('waypoint', this._bpmnFactory.createDiWaypoints(connection.waypoints));
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
BpmnUpdater.prototype.updateConnection = function(connection) {
|
|
|
|
|
|
|
|
var businessObject = connection.businessObject,
|
|
|
|
newSource = connection.source && connection.source.businessObject,
|
|
|
|
newTarget = connection.target && connection.target.businessObject;
|
|
|
|
|
2014-08-05 15:57:19 +00:00
|
|
|
var inverseSet = businessObject.$instanceOf('bpmn:SequenceFlow');
|
2014-08-05 11:57:32 +00:00
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
if (businessObject.sourceRef !== newSource) {
|
2014-08-05 15:57:19 +00:00
|
|
|
if (inverseSet) {
|
|
|
|
Collections.remove(businessObject.sourceRef && businessObject.sourceRef.get('outgoing'), businessObject);
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2014-08-05 15:57:19 +00:00
|
|
|
if (newSource) {
|
|
|
|
newSource.get('outgoing').push(businessObject);
|
|
|
|
}
|
2014-07-23 16:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
businessObject.sourceRef = newSource;
|
|
|
|
}
|
|
|
|
if (businessObject.targetRef !== newTarget) {
|
2014-08-05 15:57:19 +00:00
|
|
|
if (inverseSet) {
|
|
|
|
Collections.remove(businessObject.targetRef && businessObject.targetRef.get('incoming'), businessObject);
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2014-08-05 15:57:19 +00:00
|
|
|
if (newTarget) {
|
|
|
|
newTarget.get('incoming').push(businessObject);
|
|
|
|
}
|
2014-07-23 16:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
businessObject.targetRef = newTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
businessObject.di.set('waypoint', this._bpmnFactory.createDiWaypoints(connection.waypoints));
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/////// helpers /////////////////////////////////////////
|
|
|
|
|
2014-07-30 14:07:43 +00:00
|
|
|
BpmnUpdater.prototype._getLabel = function(di) {
|
|
|
|
if (!di.label) {
|
|
|
|
di.label = this._bpmnFactory.createDiLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return di.label;
|
2015-04-27 14:50:09 +00:00
|
|
|
};
|