2018-04-02 19:01:53 +00:00
|
|
|
import {
|
2019-03-07 09:39:27 +00:00
|
|
|
assign
|
2018-04-02 19:01:53 +00:00
|
|
|
} from 'min-dash';
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import { is } from '../util/ModelUtil';
|
2017-02-13 12:47:00 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
2018-04-30 09:06:26 +00:00
|
|
|
isLabelExternal,
|
2018-04-02 19:01:53 +00:00
|
|
|
getExternalLabelBounds
|
|
|
|
} from '../util/LabelUtil';
|
2015-07-15 06:50:21 +00:00
|
|
|
|
2018-05-03 14:02:41 +00:00
|
|
|
import {
|
|
|
|
getMid
|
|
|
|
} from 'diagram-js/lib/layout/LayoutUtil';
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
isExpanded
|
|
|
|
} from '../util/DiUtil';
|
|
|
|
|
2019-05-22 06:45:12 +00:00
|
|
|
import {
|
|
|
|
getLabel
|
|
|
|
} from '../features/label-editing/LabelUtil';
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
elementToString
|
|
|
|
} from './Util';
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
function elementData(semantic, di, attrs) {
|
2015-02-02 13:46:21 +00:00
|
|
|
return assign({
|
2014-07-23 16:53:33 +00:00
|
|
|
id: semantic.id,
|
|
|
|
type: semantic.$type,
|
2021-08-06 08:35:41 +00:00
|
|
|
businessObject: semantic,
|
|
|
|
di: di
|
2014-07-23 16:53:33 +00:00
|
|
|
}, attrs);
|
|
|
|
}
|
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
function getWaypoints(di, source, target) {
|
2019-03-07 09:39:27 +00:00
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
var waypoints = di.waypoint;
|
2019-03-07 09:39:27 +00:00
|
|
|
|
|
|
|
if (!waypoints || waypoints.length < 2) {
|
|
|
|
return [ getMid(source), getMid(target) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return waypoints.map(function(p) {
|
2014-07-23 16:53:33 +00:00
|
|
|
return { x: p.x, y: p.y };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-25 16:40:56 +00:00
|
|
|
function notYetDrawn(translate, semantic, refSemantic, property) {
|
|
|
|
return new Error(translate('element {element} referenced by {referenced}#{property} not yet drawn', {
|
2016-06-07 06:46:45 +00:00
|
|
|
element: elementToString(refSemantic),
|
|
|
|
referenced: elementToString(semantic),
|
|
|
|
property: property
|
|
|
|
}));
|
2015-07-15 06:50:21 +00:00
|
|
|
}
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2017-02-13 12:47:00 +00:00
|
|
|
|
2014-06-30 15:01:00 +00:00
|
|
|
/**
|
|
|
|
* An importer that adds bpmn elements to the canvas
|
|
|
|
*
|
|
|
|
* @param {EventBus} eventBus
|
|
|
|
* @param {Canvas} canvas
|
2014-07-23 16:53:33 +00:00
|
|
|
* @param {ElementFactory} elementFactory
|
|
|
|
* @param {ElementRegistry} elementRegistry
|
2018-04-02 19:01:53 +00:00
|
|
|
* @param {Function} translate
|
2018-05-24 11:42:40 +00:00
|
|
|
* @param {TextRenderer} textRenderer
|
2014-06-30 15:01:00 +00:00
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export default function BpmnImporter(
|
|
|
|
eventBus, canvas, elementFactory,
|
2018-05-24 11:42:40 +00:00
|
|
|
elementRegistry, translate, textRenderer) {
|
2018-04-02 19:01:53 +00:00
|
|
|
|
2014-06-30 15:01:00 +00:00
|
|
|
this._eventBus = eventBus;
|
|
|
|
this._canvas = canvas;
|
2014-07-18 12:39:15 +00:00
|
|
|
this._elementFactory = elementFactory;
|
2014-07-23 16:53:33 +00:00
|
|
|
this._elementRegistry = elementRegistry;
|
2016-02-25 16:40:56 +00:00
|
|
|
this._translate = translate;
|
2018-05-24 11:42:40 +00:00
|
|
|
this._textRenderer = textRenderer;
|
2014-06-30 15:01:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
BpmnImporter.$inject = [
|
|
|
|
'eventBus',
|
|
|
|
'canvas',
|
|
|
|
'elementFactory',
|
|
|
|
'elementRegistry',
|
2018-05-24 11:42:40 +00:00
|
|
|
'translate',
|
|
|
|
'textRenderer'
|
2018-04-02 19:01:53 +00:00
|
|
|
];
|
2014-06-30 15:01:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-16 14:15:23 +00:00
|
|
|
* Add bpmn element (semantic) to the canvas onto the
|
|
|
|
* specified parent shape.
|
2014-06-30 15:01:00 +00:00
|
|
|
*/
|
2021-08-06 08:35:41 +00:00
|
|
|
BpmnImporter.prototype.add = function(semantic, di, parentElement) {
|
|
|
|
var element,
|
2016-06-08 07:34:11 +00:00
|
|
|
translate = this._translate,
|
|
|
|
hidden;
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2017-11-13 10:11:07 +00:00
|
|
|
var parentIndex;
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
// ROOT ELEMENT
|
2014-07-01 09:33:28 +00:00
|
|
|
// handle the special case that we deal with a
|
|
|
|
// invisible root element (process or collaboration)
|
2016-03-08 13:24:50 +00:00
|
|
|
if (is(di, 'bpmndi:BPMNPlane')) {
|
2014-07-01 09:33:28 +00:00
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
// add a virtual element (not being drawn)
|
2021-08-06 08:35:41 +00:00
|
|
|
element = this._elementFactory.createRoot(elementData(semantic, di));
|
2014-11-26 10:49:14 +00:00
|
|
|
|
2021-09-17 07:44:58 +00:00
|
|
|
if (is(semantic, 'bpmn:SubProcess')) {
|
|
|
|
element.id = element.id + '_plane';
|
|
|
|
}
|
|
|
|
|
|
|
|
this._canvas.createPlane(semantic.id, element);
|
2014-07-18 12:39:15 +00:00
|
|
|
}
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
// SHAPE
|
2016-03-08 13:24:50 +00:00
|
|
|
else if (is(di, 'bpmndi:BPMNShape')) {
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
var collapsed = !isExpanded(semantic, di),
|
2020-02-07 08:10:39 +00:00
|
|
|
isFrame = isFrameElement(semantic);
|
2021-08-06 08:35:41 +00:00
|
|
|
|
2016-06-08 07:34:11 +00:00
|
|
|
hidden = parentElement && (parentElement.hidden || parentElement.collapsed);
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
var bounds = di.bounds;
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
element = this._elementFactory.createShape(elementData(semantic, di, {
|
2014-07-18 12:39:15 +00:00
|
|
|
collapsed: collapsed,
|
2014-07-23 16:53:33 +00:00
|
|
|
hidden: hidden,
|
2014-12-22 10:44:44 +00:00
|
|
|
x: Math.round(bounds.x),
|
|
|
|
y: Math.round(bounds.y),
|
|
|
|
width: Math.round(bounds.width),
|
2020-02-07 08:10:39 +00:00
|
|
|
height: Math.round(bounds.height),
|
|
|
|
isFrame: isFrame
|
2014-07-23 16:53:33 +00:00
|
|
|
}));
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2015-07-15 06:50:21 +00:00
|
|
|
if (is(semantic, 'bpmn:BoundaryEvent')) {
|
|
|
|
this._attachBoundary(semantic, element);
|
|
|
|
}
|
|
|
|
|
2017-11-13 10:11:07 +00:00
|
|
|
// insert lanes behind other flow nodes (cf. #727)
|
|
|
|
if (is(semantic, 'bpmn:Lane')) {
|
|
|
|
parentIndex = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:02:41 +00:00
|
|
|
if (is(semantic, 'bpmn:DataStoreReference')) {
|
|
|
|
|
2019-09-09 10:21:33 +00:00
|
|
|
// check whether data store is inside our outside of its semantic parent
|
2018-05-03 14:02:41 +00:00
|
|
|
if (!isPointInsideBBox(parentElement, getMid(bounds))) {
|
2021-09-17 07:44:58 +00:00
|
|
|
parentElement = this._canvas.findPlane(parentElement).rootElement;
|
2018-05-03 14:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 10:11:07 +00:00
|
|
|
this._canvas.addShape(element, parentElement, parentIndex);
|
2014-07-18 12:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CONNECTION
|
2016-03-08 13:24:50 +00:00
|
|
|
else if (is(di, 'bpmndi:BPMNEdge')) {
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
var source = this._getSource(semantic),
|
|
|
|
target = this._getTarget(semantic);
|
|
|
|
|
2016-06-08 07:34:11 +00:00
|
|
|
hidden = parentElement && (parentElement.hidden || parentElement.collapsed);
|
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
element = this._elementFactory.createConnection(elementData(semantic, di, {
|
2016-06-08 07:34:11 +00:00
|
|
|
hidden: hidden,
|
2014-07-23 16:53:33 +00:00
|
|
|
source: source,
|
|
|
|
target: target,
|
2021-08-06 08:35:41 +00:00
|
|
|
waypoints: getWaypoints(di, source, target)
|
2014-07-23 16:53:33 +00:00
|
|
|
}));
|
|
|
|
|
2016-12-06 13:49:45 +00:00
|
|
|
if (is(semantic, 'bpmn:DataAssociation')) {
|
|
|
|
|
|
|
|
// render always on top; this ensures DataAssociations
|
|
|
|
// are rendered correctly across different "hacks" people
|
|
|
|
// love to model such as cross participant / sub process
|
|
|
|
// associations
|
2021-09-17 07:44:58 +00:00
|
|
|
parentElement = this._canvas.findPlane(parentElement).rootElement;
|
2016-05-09 10:38:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 10:11:07 +00:00
|
|
|
// insert sequence flows behind other flow nodes (cf. #727)
|
|
|
|
if (is(semantic, 'bpmn:SequenceFlow')) {
|
|
|
|
parentIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._canvas.addConnection(element, parentElement, parentIndex);
|
2014-07-23 16:53:33 +00:00
|
|
|
} else {
|
2016-02-25 16:40:56 +00:00
|
|
|
throw new Error(translate('unknown di {di} for element {semantic}', {
|
2016-06-07 06:46:45 +00:00
|
|
|
di: elementToString(di),
|
|
|
|
semantic: elementToString(semantic)
|
|
|
|
}));
|
2014-06-30 15:01:00 +00:00
|
|
|
}
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
// (optional) LABEL
|
2019-05-22 06:45:12 +00:00
|
|
|
if (isLabelExternal(semantic) && getLabel(element)) {
|
2021-08-06 08:35:41 +00:00
|
|
|
this.addLabel(semantic, di, element);
|
2014-07-16 14:15:23 +00:00
|
|
|
}
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-11-17 16:36:22 +00:00
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
this._eventBus.fire('bpmnElement.added', { element: element });
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
return element;
|
2014-06-30 15:01:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-07-15 06:50:21 +00:00
|
|
|
/**
|
|
|
|
* Attach the boundary element to the given host
|
|
|
|
*
|
|
|
|
* @param {ModdleElement} boundarySemantic
|
|
|
|
* @param {djs.model.Base} boundaryElement
|
|
|
|
*/
|
|
|
|
BpmnImporter.prototype._attachBoundary = function(boundarySemantic, boundaryElement) {
|
2016-02-25 16:40:56 +00:00
|
|
|
var translate = this._translate;
|
2015-07-15 06:50:21 +00:00
|
|
|
var hostSemantic = boundarySemantic.attachedToRef;
|
|
|
|
|
|
|
|
if (!hostSemantic) {
|
2016-02-25 16:40:56 +00:00
|
|
|
throw new Error(translate('missing {semantic}#attachedToRef', {
|
|
|
|
semantic: elementToString(boundarySemantic)
|
|
|
|
}));
|
2015-07-15 06:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var host = this._elementRegistry.get(hostSemantic.id),
|
|
|
|
attachers = host && host.attachers;
|
|
|
|
|
|
|
|
if (!host) {
|
2016-02-25 16:40:56 +00:00
|
|
|
throw notYetDrawn(translate, boundarySemantic, hostSemantic, 'attachedToRef');
|
2015-07-15 06:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wire element.host <> host.attachers
|
|
|
|
boundaryElement.host = host;
|
|
|
|
|
|
|
|
if (!attachers) {
|
|
|
|
host.attachers = attachers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachers.indexOf(boundaryElement) === -1) {
|
|
|
|
attachers.push(boundaryElement);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
/**
|
|
|
|
* add label for an element
|
|
|
|
*/
|
2021-08-06 08:35:41 +00:00
|
|
|
BpmnImporter.prototype.addLabel = function(semantic, di, element) {
|
2017-02-13 12:47:00 +00:00
|
|
|
var bounds,
|
|
|
|
text,
|
|
|
|
label;
|
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
bounds = getExternalLabelBounds(di, element);
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2019-05-22 06:45:12 +00:00
|
|
|
text = getLabel(element);
|
2017-02-13 12:47:00 +00:00
|
|
|
|
|
|
|
if (text) {
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2017-02-13 12:47:00 +00:00
|
|
|
// get corrected bounds from actual layouted text
|
2018-06-13 07:01:16 +00:00
|
|
|
bounds = this._textRenderer.getExternalLabelBounds(bounds, text);
|
2017-02-13 12:47:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
label = this._elementFactory.createLabel(elementData(semantic, di, {
|
2014-07-23 16:53:33 +00:00
|
|
|
id: semantic.id + '_label',
|
|
|
|
labelTarget: element,
|
|
|
|
type: 'label',
|
2019-05-22 06:45:12 +00:00
|
|
|
hidden: element.hidden || !getLabel(element),
|
2014-12-22 10:44:44 +00:00
|
|
|
x: Math.round(bounds.x),
|
|
|
|
y: Math.round(bounds.y),
|
|
|
|
width: Math.round(bounds.width),
|
|
|
|
height: Math.round(bounds.height)
|
2014-07-23 16:53:33 +00:00
|
|
|
}));
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
return this._canvas.addShape(label, element.parent);
|
|
|
|
};
|
|
|
|
|
2014-10-31 14:05:16 +00:00
|
|
|
/**
|
|
|
|
* Return the drawn connection end based on the given side.
|
|
|
|
*
|
|
|
|
* @throws {Error} if the end is not yet drawn
|
|
|
|
*/
|
|
|
|
BpmnImporter.prototype._getEnd = function(semantic, side) {
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
var element,
|
2014-10-31 14:05:16 +00:00
|
|
|
refSemantic,
|
2016-02-25 16:40:56 +00:00
|
|
|
type = semantic.$type,
|
|
|
|
translate = this._translate;
|
2014-10-31 14:05:16 +00:00
|
|
|
|
|
|
|
refSemantic = semantic[side + 'Ref'];
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2018-03-22 11:43:36 +00:00
|
|
|
// handle mysterious isMany DataAssociation#sourceRef
|
2014-10-31 14:05:16 +00:00
|
|
|
if (side === 'source' && type === 'bpmn:DataInputAssociation') {
|
|
|
|
refSemantic = refSemantic && refSemantic[0];
|
2014-07-23 16:53:33 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 14:05:16 +00:00
|
|
|
// fix source / target for DataInputAssociation / DataOutputAssociation
|
|
|
|
if (side === 'source' && type === 'bpmn:DataOutputAssociation' ||
|
|
|
|
side === 'target' && type === 'bpmn:DataInputAssociation') {
|
|
|
|
|
|
|
|
refSemantic = semantic.$parent;
|
2014-07-23 16:53:33 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 14:05:16 +00:00
|
|
|
element = refSemantic && this._getElement(refSemantic);
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
if (element) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2014-10-31 14:05:16 +00:00
|
|
|
if (refSemantic) {
|
2016-02-25 16:40:56 +00:00
|
|
|
throw notYetDrawn(translate, semantic, refSemantic, side + 'Ref');
|
2014-10-31 14:05:16 +00:00
|
|
|
} else {
|
2016-02-25 16:40:56 +00:00
|
|
|
throw new Error(translate('{semantic}#{side} Ref not specified', {
|
|
|
|
semantic: elementToString(semantic),
|
|
|
|
side: side
|
|
|
|
}));
|
2014-10-31 14:05:16 +00:00
|
|
|
}
|
2014-07-23 16:53:33 +00:00
|
|
|
};
|
|
|
|
|
2014-10-31 14:05:16 +00:00
|
|
|
BpmnImporter.prototype._getSource = function(semantic) {
|
|
|
|
return this._getEnd(semantic, 'source');
|
|
|
|
};
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
BpmnImporter.prototype._getTarget = function(semantic) {
|
2014-10-31 14:05:16 +00:00
|
|
|
return this._getEnd(semantic, 'target');
|
2014-07-23 16:53:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BpmnImporter.prototype._getElement = function(semantic) {
|
2014-11-17 16:36:22 +00:00
|
|
|
return this._elementRegistry.get(semantic.id);
|
2016-06-08 07:34:11 +00:00
|
|
|
};
|
2017-02-13 12:47:00 +00:00
|
|
|
|
|
|
|
|
2018-05-24 11:42:40 +00:00
|
|
|
// helpers ////////////////////
|
2018-05-03 14:02:41 +00:00
|
|
|
|
|
|
|
function isPointInsideBBox(bbox, point) {
|
|
|
|
var x = point.x,
|
|
|
|
y = point.y;
|
|
|
|
|
|
|
|
return x >= bbox.x &&
|
|
|
|
x <= bbox.x + bbox.width &&
|
|
|
|
y >= bbox.y &&
|
|
|
|
y <= bbox.y + bbox.height;
|
2020-02-07 08:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isFrameElement(semantic) {
|
|
|
|
return is(semantic, 'bpmn:Group');
|
2017-02-13 12:47:00 +00:00
|
|
|
}
|