Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get module context #1835

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/BaseModeler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import Ids from 'ids';
import BaseViewer from './BaseViewer';


/**
* @typedef { import('didi').ModuleDeclaration } Module
*/

/**
* A base modeler for BPMN 2.0 diagrams.
*
Expand All @@ -15,8 +19,8 @@ import BaseViewer from './BaseViewer';
* @param {string|number} [options.width] the width of the viewer
* @param {string|number} [options.height] the height of the viewer
* @param {Object} [options.moddleExtensions] extension packages to provide
* @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
* @param {Module[]} [options.modules] a list of modules to override the default modules
* @param {Module[]} [options.additionalModules] a list of modules to use with the default modules
*/
export default function BaseModeler(options) {
BaseViewer.call(this, options);
Expand Down
20 changes: 16 additions & 4 deletions lib/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ import {
wrapForCompatibility
} from './util/CompatibilityUtil';


/**
* @typedef { import('didi').ModuleDeclaration } Module
*/

/**
* A base viewer for BPMN 2.0 diagrams.
*
Expand All @@ -46,8 +51,8 @@ import {
* @param {string|number} [options.width] the width of the viewer
* @param {string|number} [options.height] the height of the viewer
* @param {Object} [options.moddleExtensions] extension packages to provide
* @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
* @param {Module[]} [options.modules] a list of modules to override the default modules
* @param {Module[]} [options.additionalModules] a list of modules to use with the default modules
*/
export default function BaseViewer(options) {

Expand Down Expand Up @@ -476,7 +481,14 @@ BaseViewer.prototype._setDefinitions = function(definitions) {
this._definitions = definitions;
};

BaseViewer.prototype.getModules = function() {
/**
* Return modules to instantiate with.
*
* @param {any} options the instance got created with
*
* @return {Module[]}
*/
BaseViewer.prototype.getModules = function(options) {
return this._modules;
};

Expand Down Expand Up @@ -582,7 +594,7 @@ BaseViewer.prototype.detach = function() {

BaseViewer.prototype._init = function(container, moddle, options) {

const baseModules = options.modules || this.getModules(),
const baseModules = options.modules || this.getModules(options),
additionalModules = options.additionalModules || [],
staticModules = [
{
Expand Down
8 changes: 6 additions & 2 deletions lib/Modeler.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ var initialDiagram =
'</bpmn:definitions>';


/**
* @typedef { import('didi').ModuleDeclaration } Module
*/

/**
* A modeler for BPMN 2.0 diagrams.
*
Expand Down Expand Up @@ -130,8 +134,8 @@ var initialDiagram =
* @param {string|number} [options.width] the width of the viewer
* @param {string|number} [options.height] the height of the viewer
* @param {Object} [options.moddleExtensions] extension packages to provide
* @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
* @param {Module[]} [options.modules] a list of modules to override the default modules
* @param {Module[]} [options.additionalModules] a list of modules to use with the default modules
*/
export default function Modeler(options) {
BaseModeler.call(this, options);
Expand Down
8 changes: 6 additions & 2 deletions lib/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import DrilldownModdule from './features/drilldown';
import BaseViewer from './BaseViewer';


/**
* @typedef { import('didi').ModuleDeclaration } Module
*/

/**
* A viewer for BPMN 2.0 diagrams.
*
Expand Down Expand Up @@ -53,8 +57,8 @@ import BaseViewer from './BaseViewer';
* @param {string|number} [options.width] the width of the viewer
* @param {string|number} [options.height] the height of the viewer
* @param {Object} [options.moddleExtensions] extension packages to provide
* @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
* @param {Module[]} [options.modules] a list of modules to override the default modules
* @param {Module[]} [options.additionalModules] a list of modules to use with the default modules
*/
export default function Viewer(options) {
BaseViewer.call(this, options);
Expand Down
47 changes: 47 additions & 0 deletions test/spec/BaseModelerSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import BaseModeler from 'lib/BaseModeler';
import BaseViewer from 'lib/BaseViewer';

import inherits from 'inherits-browser';


const spy = sinon.spy;


describe('BaseModeler', function() {

Expand All @@ -17,4 +22,46 @@ describe('BaseModeler', function() {
expect(instance instanceof BaseViewer).to.be.true;
});


describe('#getModule', function() {

it('should allow override with context', function() {

// given
const options = {
__foo: 1,
some: {
other: {
thing: 'yes'
}
}
};

function SpecialModeler(options) {
this.getModules = spy(function(localOptions) {
expect(localOptions, 'options are passed').to.exist;

expect(localOptions).to.include(options);

return BaseModeler.prototype.getModules.call(this, localOptions);
});

BaseModeler.call(this, options);
}

inherits(SpecialModeler, BaseModeler);

// when
var instance = new SpecialModeler(options);

// then
expect(instance.getModules).to.have.been.calledOnce;

expect(instance instanceof SpecialModeler).to.be.true;
expect(instance instanceof BaseModeler).to.be.true;
expect(instance instanceof BaseViewer).to.be.true;
});

});

});
46 changes: 46 additions & 0 deletions test/spec/BaseViewerSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import BaseViewer from 'lib/BaseViewer';

import inherits from 'inherits-browser';


const spy = sinon.spy;


describe('BaseViewer', function() {

Expand All @@ -15,4 +20,45 @@ describe('BaseViewer', function() {
expect(instance instanceof BaseViewer).to.be.true;
});


describe('#getModule', function() {

it('should allow override with context', function() {

// given
const options = {
__foo: 1,
some: {
other: {
thing: 'yes'
}
}
};

function SpecialViewer(options) {
this.getModules = spy(function(localOptions) {
expect(localOptions, 'options are passed').to.exist;

expect(localOptions).to.include(options);

return BaseViewer.prototype.getModules.call(this, localOptions);
});

BaseViewer.call(this, options);
}

inherits(SpecialViewer, BaseViewer);

// when
var instance = new SpecialViewer(options);

// then
expect(instance.getModules).to.have.been.calledOnce;

expect(instance instanceof SpecialViewer).to.be.true;
expect(instance instanceof BaseViewer).to.be.true;
});

});

});