2014-06-30 15:01:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
var LabelUtil = require('../util/Label');
|
|
|
|
|
|
|
|
var hasExternalLabel = LabelUtil.hasExternalLabel,
|
|
|
|
getExternalLabelBounds = LabelUtil.getExternalLabelBounds,
|
2014-07-18 12:39:15 +00:00
|
|
|
isExpanded = require('../util/Di').isExpanded;
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
function elementData(semantic, attrs) {
|
|
|
|
return _.extend({
|
|
|
|
id: semantic.id,
|
|
|
|
type: semantic.$type,
|
|
|
|
businessObject: semantic
|
|
|
|
}, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
function collectWaypoints(waypoints) {
|
|
|
|
return _.collect(waypoints, function(p) {
|
|
|
|
return { x: p.x, y: p.y };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2014-07-23 16:53:33 +00:00
|
|
|
function BpmnImporter(eventBus, canvas, elementFactory, elementRegistry) {
|
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;
|
2014-06-30 15:01:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
BpmnImporter.$inject = [ 'eventBus', 'canvas', 'elementFactory', 'elementRegistry' ];
|
|
|
|
|
|
|
|
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,
|
|
|
|
element;
|
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)
|
|
|
|
if (di.$instanceOf('bpmndi:BPMNPlane')) {
|
|
|
|
|
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-07-18 12:39:15 +00:00
|
|
|
}
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
// SHAPE
|
|
|
|
else if (di.$instanceOf('bpmndi:BPMNShape')) {
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
var collapsed = !isExpanded(semantic);
|
|
|
|
var 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,
|
|
|
|
x: bounds.x,
|
|
|
|
y: bounds.y,
|
|
|
|
width: bounds.width,
|
|
|
|
height: bounds.height
|
|
|
|
}));
|
2014-06-30 15:01:00 +00:00
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
this._canvas.addShape(element, parentElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CONNECTION
|
2014-07-23 16:53:33 +00:00
|
|
|
else if (di.$instanceOf('bpmndi:BPMNEdge')) {
|
|
|
|
|
|
|
|
var source = this._getSource(semantic),
|
|
|
|
target = this._getTarget(semantic);
|
|
|
|
|
|
|
|
if (!source || !target) {
|
|
|
|
throw new Error('source or target not rendered for element <' + semantic.id + '>');
|
|
|
|
}
|
|
|
|
|
|
|
|
element = this._elementFactory.createConnection(elementData(semantic, {
|
|
|
|
source: source,
|
|
|
|
target: target,
|
|
|
|
waypoints: collectWaypoints(semantic.di.waypoint)
|
|
|
|
}));
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
this._canvas.addConnection(element, parentElement);
|
2014-07-23 16:53:33 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('unknown di <' + di.$type + '> for element <' + semantic.id + '>');
|
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-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
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
/**
|
|
|
|
* add label for an element
|
|
|
|
*/
|
|
|
|
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',
|
2014-09-09 13:20:30 +00:00
|
|
|
hidden: element.hidden,
|
2014-07-23 16:53:33 +00:00
|
|
|
x: bounds.x,
|
|
|
|
y: bounds.y,
|
|
|
|
width: bounds.width,
|
|
|
|
height: bounds.height
|
|
|
|
}));
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
return this._canvas.addShape(label, element.parent);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
BpmnImporter.prototype._getSource = function(semantic) {
|
|
|
|
|
|
|
|
var element,
|
|
|
|
elementSemantic = semantic.sourceRef;
|
|
|
|
|
|
|
|
// handle mysterious isMany DataAssociation#sourceRef
|
|
|
|
if (_.isArray(elementSemantic)) {
|
|
|
|
elementSemantic = elementSemantic[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elementSemantic && elementSemantic.$instanceOf('bpmn:DataOutput')) {
|
|
|
|
elementSemantic = elementSemantic.$parent.$parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
element = elementSemantic && this._getElement(elementSemantic);
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('element <' + elementSemantic.id + '> referenced by <' + semantic.id + '> not yet drawn');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BpmnImporter.prototype._getTarget = function(semantic) {
|
|
|
|
|
|
|
|
var element,
|
|
|
|
elementSemantic = semantic.targetRef;
|
|
|
|
|
|
|
|
if (elementSemantic && elementSemantic.$instanceOf('bpmn:DataInput')) {
|
|
|
|
elementSemantic = elementSemantic.$parent.$parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
element = elementSemantic && this._getElement(elementSemantic);
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('element <' + elementSemantic.id + '> referenced by <' + semantic.id + '> not yet drawn');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BpmnImporter.prototype._getElement = function(semantic) {
|
|
|
|
return this._elementRegistry.getById(semantic.id);
|
|
|
|
};
|