bpmn-js/lib/import/Importer.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

'use strict';
var BpmnTreeWalker = require('./BpmnTreeWalker');
/**
* Import the definitions into a diagram.
*
* Errors and warnings are reported through the specified callback.
*
* @param {Diagram} diagram
* @param {ModdleElement} definitions
* @param {Function} done the callback, invoked with (err, [ warning ]) once the import is done
*/
function importBpmnDiagram(diagram, definitions, done) {
2014-08-05 06:34:54 +00:00
var importer = diagram.get('bpmnImporter'),
eventBus = diagram.get('eventBus');
2014-09-08 17:04:18 +00:00
var error,
warnings = [];
2014-09-08 17:04:18 +00:00
function parse(definitions) {
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 });
}
};
2014-09-08 17:04:18 +00:00
var walker = new BpmnTreeWalker(visitor);
2014-08-05 06:34:54 +00:00
// import
walker.handleDefinitions(definitions);
2014-09-08 17:04:18 +00:00
}
2014-08-05 06:34:54 +00:00
2014-09-08 17:04:18 +00:00
eventBus.fire('import.start');
2014-08-05 06:34:54 +00:00
2014-09-08 17:04:18 +00:00
try {
parse(definitions);
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
eventBus.fire(error ? 'import.error' : 'import.success', { error: error, warnings: warnings });
done(error, warnings);
}
module.exports.importBpmnDiagram = importBpmnDiagram;