chore(modeler/viewer): clean up module loading + api

This commit is contained in:
Nico Rehwaldt 2014-06-17 11:19:31 +02:00
parent d4e55f0ba3
commit e947cb8bbe
4 changed files with 72 additions and 25 deletions

View File

@ -18,17 +18,13 @@ function Modeler(options) {
Modeler.prototype = Object.create(Viewer.prototype); Modeler.prototype = Object.create(Viewer.prototype);
Modeler.prototype.createDiagram = function(modules) { // modules that comprise the bpmn modeler
return Viewer.prototype.createDiagram.call(this, modules || Modeler.modules); Modeler.prototype._modules = Modeler.prototype._modules.concat([
};
Modeler.modules = [
// TODO (nre): buggy in conjunction with zoomscroll / move canvas // TODO (nre): buggy in conjunction with zoomscroll / move canvas
// require('diagram-js/lib/features/move'), // require('diagram-js/lib/features/move'),
require('./features/label-editing'), require('./features/label-editing'),
require('./core'),
require('./features/zoomscroll'), require('./features/zoomscroll'),
require('./features/movecanvas') require('./features/movecanvas')
]; ]);
module.exports = Modeler; module.exports = Modeler;

View File

@ -141,12 +141,21 @@ Viewer.prototype.saveSVG = function(options, done) {
Viewer.prototype.get = function(name) { Viewer.prototype.get = function(name) {
if (!this.diagram) { if (!this.diagram) {
return null; throw new Error('no diagram loaded');
} }
return this.diagram.get(name); return this.diagram.get(name);
}; };
Viewer.prototype.invoke = function(fn) {
if (!this.diagram) {
throw new Error('no diagram loaded');
}
return this.diagram.invoke(fn);
};
Viewer.prototype.importDefinitions = util.failSafeAsync(function(definitions, done) { Viewer.prototype.importDefinitions = util.failSafeAsync(function(definitions, done) {
var diagram = this.diagram; var diagram = this.diagram;
@ -172,12 +181,23 @@ Viewer.prototype.initDiagram = function(diagram) {
Viewer.prototype.createDiagram = function(modules) { Viewer.prototype.createDiagram = function(modules) {
modules = [].concat(modules || this.getModules());
// add self as an available service
modules.unshift({
bpmnjs: [ 'value', this ]
});
return new Diagram({ return new Diagram({
canvas: { container: this.container }, canvas: { container: this.container },
modules: modules || Viewer.modules modules: modules
}); });
}; };
Viewer.prototype.getModules = function() {
return this._modules;
};
Viewer.prototype.clear = function() { Viewer.prototype.clear = function() {
var diagram = this.diagram; var diagram = this.diagram;
@ -198,7 +218,8 @@ Viewer.prototype.on = function(event, handler) {
} }
}; };
Viewer.modules = [ // modules that comprise the bpmn viewer
Viewer.prototype._modules = [
require('./core') require('./core')
]; ];

View File

@ -17,28 +17,23 @@ describe('Modeler', function() {
}); });
it('should import simple process', function(done) { function createModeler(xml, done) {
var modeler = new Modeler({ container: container });
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8'); modeler.importXML(xml, function(err) {
done(err, modeler);
var renderer = new Modeler(container);
renderer.importXML(xml, function(err) {
done(err);
}); });
}
it('should import simple process', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8');
createModeler(xml, done);
}); });
it('should import empty definitions', function(done) { it('should import empty definitions', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/empty-definitions.bpmn', 'utf8'); var xml = fs.readFileSync('test/fixtures/bpmn/empty-definitions.bpmn', 'utf8');
createModeler(xml, done);
var renderer = new Modeler(container);
renderer.importXML(xml, function(err) {
done(err);
});
}); });
@ -56,4 +51,22 @@ describe('Modeler', function() {
}); });
}); });
describe('dependency injection', function() {
it('should be available via di as <bpmnjs>', function(done) {
debugger;
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8');
createModeler(xml, function(err, modeler) {
expect(modeler.get('bpmnjs')).toBe(modeler);
done(err);
});
});
});
}); });

View File

@ -73,6 +73,23 @@ describe('Viewer', function() {
}); });
describe('dependency injection', function() {
it('should be available via di as <bpmnjs>', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8');
createViewer(xml, function(err, viewer) {
expect(viewer.get('bpmnjs')).toBe(viewer);
done(err);
});
});
});
describe('export', function() { describe('export', function() {
it('should export svg', function(done) { it('should export svg', function(done) {