2014-03-13 19:21:42 +00:00
|
|
|
var _ = require('lodash');
|
|
|
|
|
2014-04-03 09:47:26 +00:00
|
|
|
var BpmnTreeWalker = require('./BpmnTreeWalker'),
|
|
|
|
Util = require('../Util');
|
2014-03-13 15:06:30 +00:00
|
|
|
|
|
|
|
function importBpmnDiagram(diagram, definitions, done) {
|
|
|
|
|
2014-03-20 15:18:23 +00:00
|
|
|
var canvas = diagram.get('canvas');
|
2014-04-03 09:47:26 +00:00
|
|
|
var events = diagram.get('eventBus');
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
var visitor = {
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
element: function(element, di, parent) {
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
var shape;
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-04-03 09:47:26 +00:00
|
|
|
function fire(type, shape) {
|
|
|
|
events.fire('bpmn.element.' + type, {
|
|
|
|
semantic: element, di: di, diagramElement: shape
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
if (di.$type === 'bpmndi:BPMNShape') {
|
|
|
|
var bounds = di.bounds;
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-04-01 12:16:18 +00:00
|
|
|
shape = {
|
|
|
|
id: element.id, type: element.$type,
|
|
|
|
x: bounds.x, y: bounds.y,
|
|
|
|
width: bounds.width, height: bounds.height,
|
|
|
|
parent: parent
|
|
|
|
};
|
2014-03-13 19:21:42 +00:00
|
|
|
|
2014-04-03 09:47:26 +00:00
|
|
|
fire('add', shape);
|
2014-03-21 15:46:56 +00:00
|
|
|
canvas.addShape(shape);
|
|
|
|
} else {
|
2014-03-13 19:21:42 +00:00
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
var waypoints = _.collect(di.waypoint, function(p) {
|
|
|
|
return { x: p.x, y: p.y };
|
|
|
|
});
|
|
|
|
|
|
|
|
shape = { id: element.id, type: element.$type, waypoints: waypoints };
|
2014-04-03 09:47:26 +00:00
|
|
|
|
|
|
|
fire('add', shape);
|
2014-03-21 15:46:56 +00:00
|
|
|
canvas.addConnection(shape);
|
|
|
|
}
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-04-03 09:47:26 +00:00
|
|
|
fire('added', shape);
|
2014-03-18 16:01:24 +00:00
|
|
|
|
2014-03-21 15:46:56 +00:00
|
|
|
return shape;
|
|
|
|
},
|
|
|
|
|
|
|
|
error: function(message, context) {
|
|
|
|
console.warn('[import]', message, context);
|
|
|
|
}
|
2014-03-13 15:06:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var walker = new BpmnTreeWalker(visitor);
|
|
|
|
walker.handleDefinitions(definitions);
|
2014-04-03 09:47:26 +00:00
|
|
|
|
2014-03-13 15:06:30 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2014-04-03 09:47:26 +00:00
|
|
|
module.exports.importBpmnDiagram = Util.failSafeAsync(importBpmnDiagram);
|