feat(viewer): allow additional modules to be passed

This enables users to instantiate Viewers/Modelers via

```
new Viewer({
additionalModules: [ ... ]
});
```

to bootstrap their own modules with the viewer without overriding the
existing ones.
This commit is contained in:
Nico Rehwaldt 2014-08-01 07:55:47 +02:00
parent e337ffe246
commit cb77832fbe
4 changed files with 71 additions and 23 deletions

View File

@ -25,11 +25,11 @@ var initialDiagram =
'</bpmn:definitions>'; '</bpmn:definitions>';
/** /**
* @class
*
* A modeler for BPMN 2.0 diagrams. * A modeler for BPMN 2.0 diagrams.
* *
* @borrows Viewer as Modeler * @class
*
* @inheritDoc djs.Viewer
*/ */
function Modeler(options) { function Modeler(options) {
Viewer.call(this, options); Viewer.call(this, options);

View File

@ -43,14 +43,16 @@ function checkValidationError(err) {
} }
/** /**
* @class
*
* A viewer for BPMN 2.0 diagrams * A viewer for BPMN 2.0 diagrams
* *
* @class
*
* @param {Object} [options] configuration options to pass to the viewer * @param {Object} [options] configuration options to pass to the viewer
* @param {DOMElement} [options.container] the container to render the viewer in, defaults to body. * @param {DOMElement} [options.container] the container to render the viewer in, defaults to body.
* @param {String|Number} [options.width] the width of the viewer * @param {String|Number} [options.width] the width of the viewer
* @param {String|Number} [options.height] the height of the viewer * @param {String|Number} [options.height] the height of the viewer
* @param {Array<didi.Module>} [options.modules] a list of modules to override the default modules
* @param {Array<didi.Module>} [options.additionalModules] a list of modules to use with the default modules
*/ */
function Viewer(options) { function Viewer(options) {
this.options = options = options || {}; this.options = options = options || {};
@ -186,7 +188,7 @@ Viewer.prototype.importDefinitions = function(definitions, done) {
this.clear(); this.clear();
} }
this.diagram = diagram = this._createDiagram(this.options.modules); this.diagram = diagram = this._createDiagram(this.options);
this.definitions = definitions; this.definitions = definitions;
this._init(diagram); this._init(diagram);
@ -201,19 +203,23 @@ Viewer.prototype._init = function(diagram) {
initListeners(diagram, this.__listeners || []); initListeners(diagram, this.__listeners || []);
}; };
Viewer.prototype._createDiagram = function(modules) { Viewer.prototype._createDiagram = function(options) {
modules = [].concat(modules || this.getModules()); var modules = [].concat(options.modules || this.getModules(), options.additionalModules || []);
// add self as an available service // add self as an available service
modules.unshift({ modules.unshift({
bpmnjs: [ 'value', this ] bpmnjs: [ 'value', this ]
}); });
return new Diagram({ options = _.omit(options, 'additionalModules');
options = _.extend(options, {
canvas: { container: this.container }, canvas: { container: this.container },
modules: modules modules: modules
}); });
return new Diagram(options);
}; };

View File

@ -4,8 +4,9 @@ var BpmnTreeWalker = require('./BpmnTreeWalker');
/** /**
* Import the definitions into the given diagram, reporting errors and warnings * Import the definitions into a diagram.
* via the specified callback. *
* Errors and warnings are reported through the specified callback.
* *
* @param {Diagram} diagram * @param {Diagram} diagram
* @param {ModdleElement} definitions * @param {ModdleElement} definitions
@ -34,14 +35,10 @@ function importBpmnDiagram(diagram, definitions, done) {
var walker = new BpmnTreeWalker(visitor); var walker = new BpmnTreeWalker(visitor);
try { // import
// import walker.handleDefinitions(definitions);
walker.handleDefinitions(definitions);
done(null, warnings); done(null, warnings);
} catch (e) {
done(e);
}
} }
module.exports.importBpmnDiagram = importBpmnDiagram; module.exports.importBpmnDiagram = importBpmnDiagram;

View File

@ -24,10 +24,10 @@ describe('viewer', function() {
function createViewer(xml, done) { function createViewer(xml, done) {
var renderer = new Viewer({ container: container }); var viewer = new Viewer({ container: container });
renderer.importXML(xml, function(err) { viewer.importXML(xml, function(err) {
done(err, renderer); done(err, viewer);
}); });
} }
@ -119,14 +119,14 @@ describe('viewer', function() {
// given // given
var xml = fs.readFileSync('test/fixtures/bpmn/empty-definitions.bpmn', 'utf8'); var xml = fs.readFileSync('test/fixtures/bpmn/empty-definitions.bpmn', 'utf8');
createViewer(xml, function(err, renderer) { createViewer(xml, function(err, viewer) {
if (err) { if (err) {
return done(err); return done(err);
} }
// when // when
renderer.saveSVG(function(err, svg) { viewer.saveSVG(function(err, svg) {
if (err) { if (err) {
return done(err); return done(err);
@ -153,4 +153,49 @@ describe('viewer', function() {
}); });
describe('configuration', function() {
var testModules = [
{ logger: [ 'type', function() { this.called = true; } ] }
];
// given
var xml = fs.readFileSync('test/fixtures/bpmn/empty-definitions.bpmn', 'utf8');
it('should override default modules', function(done) {
// given
var viewer = new Viewer({ container: container, modules: testModules });
// when
viewer.importXML(xml, function(err) {
// then
expect(err.message).toBe('No provider for "bpmnImporter"! (Resolving: bpmnImporter)');
done();
});
});
it('should add module to default modules', function(done) {
// given
var viewer = new Viewer({ container: container, additionalModules: testModules });
// when
viewer.importXML(xml, function(err) {
// then
var logger = viewer.get('logger');
expect(logger.called).toBe(true);
done(err);
});
});
});
}); });