2014-06-30 15:01:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
var assign = require('lodash/object/assign'),
|
|
|
|
map = require('lodash/collection/map');
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
var LabelUtil = require('../util/LabelUtil');
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2015-07-15 06:50:21 +00:00
|
|
|
var is = require('../util/ModelUtil').is;
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
var hasExternalLabel = LabelUtil.hasExternalLabel,
|
|
|
|
getExternalLabelBounds = LabelUtil.getExternalLabelBounds,
|
2015-04-27 14:50:09 +00:00
|
|
|
isExpanded = require('../util/DiUtil').isExpanded,
|
2014-10-31 14:05:16 +00:00
|
|
|
elementToString = require('./Util').elementToString;
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
function elementData(semantic, 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,
|
|
|
|
businessObject: semantic
|
|
|
|
}, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
function collectWaypoints(waypoints) {
|
2015-02-02 13:46:21 +00:00
|
|
|
return map(waypoints, 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
|
|
|
|
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
|
2014-06-30 15:01:00 +00:00
|
|
|
*/
|
2016-02-25 16:40:56 +00:00
|
|
|
function BpmnImporter(eventBus, canvas, elementFactory, elementRegistry, translate) {
|
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;
|
2014-06-30 15:01:00 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 16:40:56 +00:00
|
|
|
BpmnImporter.$inject = [ 'eventBus', 'canvas', 'elementFactory', 'elementRegistry', 'translate' ];
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
module.exports = BpmnImporter;
|
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
|
|
|
*/
|
2014-07-18 12:39:15 +00:00
|
|
|
BpmnImporter.prototype.add = function(semantic, parentElement) {
|
|
|
|
|
|
|
|
var di = semantic.di,
|
2016-02-25 16:40:56 +00:00
|
|
|
element,
|
2016-06-08 07:34:11 +00:00
|
|
|
translate = this._translate,
|
|
|
|
hidden;
|
2014-06-30 15:01:00 +00:00
|
|
|
|
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)
|
2014-07-23 16:53:33 +00:00
|
|
|
element = this._elementFactory.createRoot(elementData(semantic));
|
2014-11-26 10:49:14 +00:00
|
|
|
|
|
|
|
this._canvas.setRootElement(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
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
var collapsed = !isExpanded(semantic);
|
2016-06-08 07:34:11 +00:00
|
|
|
hidden = parentElement && (parentElement.hidden || parentElement.collapsed);
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
var bounds = semantic.di.bounds;
|
|
|
|
|
|
|
|
element = this._elementFactory.createShape(elementData(semantic, {
|
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),
|
|
|
|
height: Math.round(bounds.height)
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
this._canvas.addShape(element, parentElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
element = this._elementFactory.createConnection(elementData(semantic, {
|
2016-06-08 07:34:11 +00:00
|
|
|
hidden: hidden,
|
2014-07-23 16:53:33 +00:00
|
|
|
source: source,
|
|
|
|
target: target,
|
|
|
|
waypoints: collectWaypoints(semantic.di.waypoint)
|
|
|
|
}));
|
|
|
|
|
2016-05-09 10:38:01 +00:00
|
|
|
if (is(semantic, 'bpmn:DataInputAssociation') || is(semantic, 'bpmn:DataOutputAssociation')) {
|
|
|
|
// implicit root element
|
|
|
|
parentElement = null;
|
|
|
|
}
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
this._canvas.addConnection(element, parentElement);
|
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
|
|
|
}
|
2014-07-18 12:39:15 +00:00
|
|
|
// (optional) LABEL
|
|
|
|
if (hasExternalLabel(semantic)) {
|
|
|
|
this.addLabel(semantic, 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
|
|
|
|
*/
|
2014-10-31 14:05:16 +00:00
|
|
|
BpmnImporter.prototype.addLabel = function(semantic, element) {
|
2014-07-23 16:53:33 +00:00
|
|
|
var bounds = getExternalLabelBounds(semantic, element);
|
|
|
|
|
|
|
|
var label = this._elementFactory.createLabel(elementData(semantic, {
|
|
|
|
id: semantic.id + '_label',
|
|
|
|
labelTarget: element,
|
|
|
|
type: 'label',
|
2015-09-14 14:03:46 +00:00
|
|
|
hidden: element.hidden || !semantic.name,
|
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
|
|
|
|
|
|
|
// 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
|
|
|
};
|