2018-04-02 19:01:53 +00:00
|
|
|
import BpmnTreeWalker from './BpmnTreeWalker';
|
2014-04-25 14:14:36 +00:00
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The importBpmnDiagram result.
|
|
|
|
*
|
|
|
|
* @typedef {Object} ImportBPMNDiagramResult
|
|
|
|
*
|
|
|
|
* @property {Array<string>} warnings
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The importBpmnDiagram error.
|
|
|
|
*
|
|
|
|
* @typedef {Error} ImportBPMNDiagramError
|
|
|
|
*
|
|
|
|
* @property {Array<string>} warnings
|
|
|
|
*/
|
2014-04-25 14:14:36 +00:00
|
|
|
|
2014-05-21 14:23:52 +00:00
|
|
|
/**
|
2014-08-01 05:55:47 +00:00
|
|
|
* Import the definitions into a diagram.
|
|
|
|
*
|
|
|
|
* Errors and warnings are reported through the specified callback.
|
2014-06-30 15:01:00 +00:00
|
|
|
*
|
2019-04-12 09:56:00 +00:00
|
|
|
* @param {djs.Diagram} diagram
|
|
|
|
* @param {ModdleElement<Definitions>} definitions
|
|
|
|
* @param {ModdleElement<BPMNDiagram>} [bpmnDiagram] the diagram to be rendered
|
2019-04-04 14:17:43 +00:00
|
|
|
* (if not provided, the first one will be rendered)
|
2020-04-29 09:02:42 +00:00
|
|
|
*
|
|
|
|
* Returns {Promise<ImportBPMNDiagramResult, ImportBPMNDiagramError>}
|
2014-05-21 14:23:52 +00:00
|
|
|
*/
|
2020-04-29 09:02:42 +00:00
|
|
|
export function importBpmnDiagram(diagram, definitions, bpmnDiagram) {
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2017-12-04 21:27:32 +00:00
|
|
|
var importer,
|
|
|
|
eventBus,
|
2021-09-17 07:44:58 +00:00
|
|
|
translate,
|
|
|
|
canvas;
|
2014-04-25 14:14:36 +00:00
|
|
|
|
2014-09-08 17:04:18 +00:00
|
|
|
var error,
|
|
|
|
warnings = [];
|
2014-04-25 14:14:36 +00:00
|
|
|
|
2016-03-17 08:39:43 +00:00
|
|
|
/**
|
|
|
|
* Walk the diagram semantically, importing (=drawing)
|
|
|
|
* all elements you encounter.
|
|
|
|
*
|
2019-04-12 09:56:00 +00:00
|
|
|
* @param {ModdleElement<Definitions>} definitions
|
|
|
|
* @param {ModdleElement<BPMNDiagram>} bpmnDiagram
|
2016-03-17 08:39:43 +00:00
|
|
|
*/
|
2019-04-12 09:56:00 +00:00
|
|
|
function render(definitions, bpmnDiagram) {
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-09-08 17:04:18 +00:00
|
|
|
var visitor = {
|
2014-07-01 09:33:28 +00:00
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
root: function(element, di) {
|
|
|
|
return importer.add(element, di);
|
2014-09-08 17:04:18 +00:00
|
|
|
},
|
2014-04-25 14:14:36 +00:00
|
|
|
|
2021-08-06 08:35:41 +00:00
|
|
|
element: function(element, di, parentShape) {
|
|
|
|
return importer.add(element, di, parentShape);
|
2014-09-08 17:04:18 +00:00
|
|
|
},
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2014-09-08 17:04:18 +00:00
|
|
|
error: function(message, context) {
|
|
|
|
warnings.push({ message: message, context: context });
|
|
|
|
}
|
|
|
|
};
|
2014-04-25 14:14:36 +00:00
|
|
|
|
2016-02-25 16:40:56 +00:00
|
|
|
var walker = new BpmnTreeWalker(visitor, translate);
|
2014-06-18 11:05:32 +00:00
|
|
|
|
2016-03-17 08:39:43 +00:00
|
|
|
// traverse BPMN 2.0 document model,
|
|
|
|
// starting at definitions
|
2021-09-17 07:44:58 +00:00
|
|
|
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];
|
2021-12-09 23:31:00 +00:00
|
|
|
|
2021-12-10 10:06:09 +00:00
|
|
|
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
|
2021-12-09 23:31:00 +00:00
|
|
|
canvas.setRootElement(
|
2021-12-10 10:06:09 +00:00
|
|
|
canvas.findRoot(rootId + '_plane') || canvas.findRoot(rootId)
|
2021-12-09 23:31:00 +00:00
|
|
|
);
|
2014-09-08 17:04:18 +00:00
|
|
|
}
|
2014-08-05 06:34:54 +00:00
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
try {
|
|
|
|
importer = diagram.get('bpmnImporter');
|
|
|
|
eventBus = diagram.get('eventBus');
|
|
|
|
translate = diagram.get('translate');
|
2021-09-17 07:44:58 +00:00
|
|
|
canvas = diagram.get('canvas');
|
2017-12-04 21:27:32 +00:00
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
eventBus.fire('import.render.start', { definitions: definitions });
|
2017-12-04 21:27:32 +00:00
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
render(definitions, bpmnDiagram);
|
2017-12-04 21:27:32 +00:00
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
eventBus.fire('import.render.complete', {
|
|
|
|
error: error,
|
|
|
|
warnings: warnings
|
|
|
|
});
|
|
|
|
|
|
|
|
return resolve({ warnings: warnings });
|
|
|
|
} catch (e) {
|
2014-09-08 17:04:18 +00:00
|
|
|
|
2020-04-29 09:02:42 +00:00
|
|
|
e.warnings = warnings;
|
|
|
|
return reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|