114 lines
2.8 KiB
JavaScript
114 lines
2.8 KiB
JavaScript
import BpmnTreeWalker from './BpmnTreeWalker';
|
|
|
|
|
|
/**
|
|
* The importBpmnDiagram result.
|
|
*
|
|
* @typedef {Object} ImportBPMNDiagramResult
|
|
*
|
|
* @property {Array<string>} warnings
|
|
*/
|
|
|
|
/**
|
|
* The importBpmnDiagram error.
|
|
*
|
|
* @typedef {Error} ImportBPMNDiagramError
|
|
*
|
|
* @property {Array<string>} warnings
|
|
*/
|
|
|
|
/**
|
|
* Import the definitions into a diagram.
|
|
*
|
|
* Errors and warnings are reported through the specified callback.
|
|
*
|
|
* @param {djs.Diagram} diagram
|
|
* @param {ModdleElement<Definitions>} definitions
|
|
* @param {ModdleElement<BPMNDiagram>} [bpmnDiagram] the diagram to be rendered
|
|
* (if not provided, the first one will be rendered)
|
|
*
|
|
* Returns {Promise<ImportBPMNDiagramResult, ImportBPMNDiagramError>}
|
|
*/
|
|
export function importBpmnDiagram(diagram, definitions, bpmnDiagram) {
|
|
|
|
var importer,
|
|
eventBus,
|
|
translate,
|
|
canvas;
|
|
|
|
var error,
|
|
warnings = [];
|
|
|
|
/**
|
|
* Walk the diagram semantically, importing (=drawing)
|
|
* all elements you encounter.
|
|
*
|
|
* @param {ModdleElement<Definitions>} definitions
|
|
* @param {ModdleElement<BPMNDiagram>} bpmnDiagram
|
|
*/
|
|
function render(definitions, bpmnDiagram) {
|
|
|
|
var visitor = {
|
|
|
|
root: function(element, di) {
|
|
return importer.add(element, di);
|
|
},
|
|
|
|
element: function(element, di, parentShape) {
|
|
return importer.add(element, di, parentShape);
|
|
},
|
|
|
|
error: function(message, context) {
|
|
warnings.push({ message: message, context: context });
|
|
}
|
|
};
|
|
|
|
var walker = new BpmnTreeWalker(visitor, translate);
|
|
|
|
// traverse BPMN 2.0 document model,
|
|
// starting at definitions
|
|
if (!bpmnDiagram && definitions.diagrams) {
|
|
for (var i = 0; i < definitions.diagrams.length; i++) {
|
|
walker.handleDefinitions(definitions, definitions.diagrams[i]);
|
|
}
|
|
} else {
|
|
walker.handleDefinitions(definitions, bpmnDiagram);
|
|
}
|
|
|
|
var mainDiagram = bpmnDiagram || definitions.diagrams[0];
|
|
|
|
var rootId = mainDiagram.plane.bpmnElement.id;
|
|
|
|
// we do need to account for different ways we create root elements
|
|
// each nested imported <root> do have the `_plane` suffix, while
|
|
// the root <root> is found under the business object ID
|
|
canvas.setRootElement(
|
|
canvas.findRoot(rootId + '_plane') || canvas.findRoot(rootId)
|
|
);
|
|
}
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
try {
|
|
importer = diagram.get('bpmnImporter');
|
|
eventBus = diagram.get('eventBus');
|
|
translate = diagram.get('translate');
|
|
canvas = diagram.get('canvas');
|
|
|
|
eventBus.fire('import.render.start', { definitions: definitions });
|
|
|
|
render(definitions, bpmnDiagram);
|
|
|
|
eventBus.fire('import.render.complete', {
|
|
error: error,
|
|
warnings: warnings
|
|
});
|
|
|
|
return resolve({ warnings: warnings });
|
|
} catch (e) {
|
|
|
|
e.warnings = warnings;
|
|
return reject(e);
|
|
}
|
|
});
|
|
}
|