2018-04-02 19:01:53 +00:00
|
|
|
import BpmnTreeWalker from './BpmnTreeWalker';
|
2014-04-25 14:14:36 +00:00
|
|
|
|
2019-04-04 14:17:43 +00:00
|
|
|
import {
|
|
|
|
isFunction
|
|
|
|
} from 'min-dash';
|
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
|
|
|
*
|
|
|
|
* @param {Diagram} diagram
|
|
|
|
* @param {ModdleElement} definitions
|
2019-04-04 14:17:43 +00:00
|
|
|
* @param {ModdleElement} [selectedDiagram] the BPMNDiagram element selected to be rendered
|
|
|
|
* (if not provided, the first one will be rendered)
|
2014-06-30 15:01:00 +00:00
|
|
|
* @param {Function} done the callback, invoked with (err, [ warning ]) once the import is done
|
2014-05-21 14:23:52 +00:00
|
|
|
*/
|
2019-04-04 14:17:43 +00:00
|
|
|
export function importBpmnDiagram(diagram, definitions, selectedDiagram, done) {
|
|
|
|
|
|
|
|
if (isFunction(selectedDiagram)) {
|
|
|
|
done = selectedDiagram;
|
|
|
|
selectedDiagram = null;
|
|
|
|
}
|
2014-03-13 15:06:30 +00:00
|
|
|
|
2017-12-04 21:27:32 +00:00
|
|
|
var importer,
|
|
|
|
eventBus,
|
|
|
|
translate;
|
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.
|
|
|
|
*
|
|
|
|
* @param {ModdleElement} definitions
|
2019-04-04 14:17:43 +00:00
|
|
|
* @param {ModdleElement} selectedDiagram
|
2016-03-17 08:39:43 +00:00
|
|
|
*/
|
2019-04-04 14:17:43 +00:00
|
|
|
function render(definitions, selectedDiagram) {
|
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
|
|
|
|
2014-09-08 17:04:18 +00:00
|
|
|
root: function(element) {
|
|
|
|
return importer.add(element);
|
|
|
|
},
|
2014-04-25 14:14:36 +00:00
|
|
|
|
2014-09-08 17:04:18 +00:00
|
|
|
element: function(element, parentShape) {
|
|
|
|
return importer.add(element, parentShape);
|
|
|
|
},
|
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
|
2019-04-04 14:17:43 +00:00
|
|
|
walker.handleDefinitions(definitions, selectedDiagram);
|
2014-09-08 17:04:18 +00:00
|
|
|
}
|
2014-08-05 06:34:54 +00:00
|
|
|
|
2014-09-08 17:04:18 +00:00
|
|
|
try {
|
2017-12-04 21:27:32 +00:00
|
|
|
importer = diagram.get('bpmnImporter');
|
|
|
|
eventBus = diagram.get('eventBus');
|
|
|
|
translate = diagram.get('translate');
|
|
|
|
|
|
|
|
eventBus.fire('import.render.start', { definitions: definitions });
|
|
|
|
|
2019-04-04 14:17:43 +00:00
|
|
|
render(definitions, selectedDiagram);
|
2017-12-04 21:27:32 +00:00
|
|
|
|
|
|
|
eventBus.fire('import.render.complete', {
|
|
|
|
error: error,
|
|
|
|
warnings: warnings
|
|
|
|
});
|
2014-08-05 06:34:54 +00:00
|
|
|
} catch (e) {
|
2014-09-08 17:04:18 +00:00
|
|
|
error = e;
|
2014-08-05 06:34:54 +00:00
|
|
|
}
|
2014-09-08 17:04:18 +00:00
|
|
|
|
|
|
|
done(error, warnings);
|
2018-04-02 19:01:53 +00:00
|
|
|
}
|