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:
parent
e337ffe246
commit
cb77832fbe
|
@ -25,11 +25,11 @@ var initialDiagram =
|
|||
'</bpmn:definitions>';
|
||||
|
||||
/**
|
||||
* @class
|
||||
*
|
||||
* A modeler for BPMN 2.0 diagrams.
|
||||
*
|
||||
* @borrows Viewer as Modeler
|
||||
* @class
|
||||
*
|
||||
* @inheritDoc djs.Viewer
|
||||
*/
|
||||
function Modeler(options) {
|
||||
Viewer.call(this, options);
|
||||
|
|
|
@ -43,14 +43,16 @@ function checkValidationError(err) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @class
|
||||
*
|
||||
* A viewer for BPMN 2.0 diagrams
|
||||
*
|
||||
* @class
|
||||
*
|
||||
* @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 {String|Number} [options.width] the width 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) {
|
||||
this.options = options = options || {};
|
||||
|
@ -186,7 +188,7 @@ Viewer.prototype.importDefinitions = function(definitions, done) {
|
|||
this.clear();
|
||||
}
|
||||
|
||||
this.diagram = diagram = this._createDiagram(this.options.modules);
|
||||
this.diagram = diagram = this._createDiagram(this.options);
|
||||
this.definitions = definitions;
|
||||
|
||||
this._init(diagram);
|
||||
|
@ -201,19 +203,23 @@ Viewer.prototype._init = function(diagram) {
|
|||
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
|
||||
modules.unshift({
|
||||
bpmnjs: [ 'value', this ]
|
||||
});
|
||||
|
||||
return new Diagram({
|
||||
options = _.omit(options, 'additionalModules');
|
||||
|
||||
options = _.extend(options, {
|
||||
canvas: { container: this.container },
|
||||
modules: modules
|
||||
});
|
||||
|
||||
return new Diagram(options);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -4,8 +4,9 @@ var BpmnTreeWalker = require('./BpmnTreeWalker');
|
|||
|
||||
|
||||
/**
|
||||
* Import the definitions into the given diagram, reporting errors and warnings
|
||||
* via the specified callback.
|
||||
* Import the definitions into a diagram.
|
||||
*
|
||||
* Errors and warnings are reported through the specified callback.
|
||||
*
|
||||
* @param {Diagram} diagram
|
||||
* @param {ModdleElement} definitions
|
||||
|
@ -34,14 +35,10 @@ function importBpmnDiagram(diagram, definitions, done) {
|
|||
|
||||
var walker = new BpmnTreeWalker(visitor);
|
||||
|
||||
try {
|
||||
// import
|
||||
walker.handleDefinitions(definitions);
|
||||
|
||||
done(null, warnings);
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.importBpmnDiagram = importBpmnDiagram;
|
|
@ -24,10 +24,10 @@ describe('viewer', function() {
|
|||
|
||||
|
||||
function createViewer(xml, done) {
|
||||
var renderer = new Viewer({ container: container });
|
||||
var viewer = new Viewer({ container: container });
|
||||
|
||||
renderer.importXML(xml, function(err) {
|
||||
done(err, renderer);
|
||||
viewer.importXML(xml, function(err) {
|
||||
done(err, viewer);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -119,14 +119,14 @@ describe('viewer', function() {
|
|||
// given
|
||||
var xml = fs.readFileSync('test/fixtures/bpmn/empty-definitions.bpmn', 'utf8');
|
||||
|
||||
createViewer(xml, function(err, renderer) {
|
||||
createViewer(xml, function(err, viewer) {
|
||||
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
// when
|
||||
renderer.saveSVG(function(err, svg) {
|
||||
viewer.saveSVG(function(err, svg) {
|
||||
|
||||
if (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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue