bpmn-js/lib/import/Importer.js

81 lines
1.9 KiB
JavaScript
Raw Normal View History

import BpmnTreeWalker from './BpmnTreeWalker';
import {
isFunction
} from 'min-dash';
/**
* Import the definitions into a diagram.
*
* Errors and warnings are reported through the specified callback.
*
* @param {Diagram} diagram
* @param {ModdleElement} definitions
* @param {ModdleElement} [selectedDiagram] the BPMNDiagram element selected to be rendered
* (if not provided, the first one will be rendered)
* @param {Function} done the callback, invoked with (err, [ warning ]) once the import is done
*/
export function importBpmnDiagram(diagram, definitions, selectedDiagram, done) {
if (isFunction(selectedDiagram)) {
done = selectedDiagram;
selectedDiagram = null;
}
var importer,
eventBus,
translate;
2014-09-08 17:04:18 +00:00
var error,
warnings = [];
/**
* Walk the diagram semantically, importing (=drawing)
* all elements you encounter.
*
* @param {ModdleElement} definitions
* @param {ModdleElement} selectedDiagram
*/
function render(definitions, selectedDiagram) {
2014-09-08 17:04:18 +00:00
var visitor = {
2014-09-08 17:04:18 +00:00
root: function(element) {
return importer.add(element);
},
2014-09-08 17:04:18 +00:00
element: function(element, parentShape) {
return importer.add(element, parentShape);
},
2014-09-08 17:04:18 +00:00
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
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 {
importer = diagram.get('bpmnImporter');
eventBus = diagram.get('eventBus');
translate = diagram.get('translate');
eventBus.fire('import.render.start', { definitions: definitions });
render(definitions, selectedDiagram);
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);
}