feat(Viewer): fire <import.*> events

This commit is contained in:
Nico Rehwaldt 2014-08-05 08:34:54 +02:00
parent 911e991bc8
commit c21b295ec2
3 changed files with 57 additions and 10 deletions

View File

@ -185,21 +185,19 @@ Viewer.prototype.invoke = function(fn) {
Viewer.prototype.importDefinitions = function(definitions, done) {
var diagram = this.diagram;
// use try/catch to not swallow synchronous exceptions
// that may be raised during model parsing
try {
if (diagram) {
if (this.diagram) {
this.clear();
}
this.diagram = diagram = this._createDiagram(this.options);
this.definitions = definitions;
this.diagram = this._createDiagram(this.options);
this._init(diagram);
this._init(this.diagram);
Importer.importBpmnDiagram(diagram, definitions, done);
Importer.importBpmnDiagram(this.diagram, definitions, done);
} catch (e) {
done(e);
}

View File

@ -14,7 +14,8 @@ var BpmnTreeWalker = require('./BpmnTreeWalker');
*/
function importBpmnDiagram(diagram, definitions, done) {
var importer = diagram.get('bpmnImporter');
var importer = diagram.get('bpmnImporter'),
eventBus = diagram.get('eventBus');
var warnings = [];
@ -35,10 +36,19 @@ function importBpmnDiagram(diagram, definitions, done) {
var walker = new BpmnTreeWalker(visitor);
// import
walker.handleDefinitions(definitions);
try {
eventBus.fire('import.start');
done(null, warnings);
// import
walker.handleDefinitions(definitions);
eventBus.fire('import.success', warnings);
done(null, warnings);
} catch (e) {
eventBus.fire('import.error', e);
done(e);
}
}
module.exports.importBpmnDiagram = importBpmnDiagram;

View File

@ -48,6 +48,45 @@ describe('viewer', function() {
});
describe('import events', function() {
iit('should fire <import.*> events', function(done) {
// given
var viewer = new Viewer({ container: container });
var xml = fs.readFileSync('test/fixtures/bpmn/empty-definitions.bpmn', 'utf8');
var events = [];
viewer.on('import.start', function() {
events.push('import.start');
});
viewer.on('import.success', function() {
events.push('import.success');
});
viewer.on('import.error', function() {
events.push('import.error');
});
// when
viewer.importXML(xml, function(err) {
// then
expect(events).toEqual([
'import.start',
'import.success'
]);
done(err);
});
});
});
describe('error handling', function() {
it('should handle non-bpmn input', function(done) {