Skip to content

Commit

Permalink
Modularize import visitors (#144)
Browse files Browse the repository at this point in the history
* modularize import visitors

* bump version
  • Loading branch information
Jan Ladleif committed Sep 25, 2019
1 parent 8e9737a commit f6c0931
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 22 deletions.
4 changes: 3 additions & 1 deletion lib/core/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import DrawModule from '../draw';
import UtilsModule from '../features/utils';
import ImportModule from '../import';

export default {
__depends__: [
DrawModule,
UtilsModule
UtilsModule,
ImportModule
]
};
2 changes: 1 addition & 1 deletion lib/import/ChoreoTreeWalker.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class ChoreoTreeWalker {
* @param {*} diagram diagram interchange for the choreography (bpmndi:Plane)
*/
start(choreo, diagram) {
this._visitor.init(choreo, diagram);
this._visitor.start(choreo, diagram);
let rootShape = this._visitor.visit(choreo);
this.handleChoreography(choreo, rootShape);
this.handleDeferred();
Expand Down
7 changes: 3 additions & 4 deletions lib/import/ImportHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import ChoreoTreeWalker from './ChoreoTreeWalker';
import InitialRenderVisitor from './visitors/InitialRenderVisitor';
import RestoreFromCacheVisitor from './visitors/RestoreFromCacheVisitor';

let shapeCache = {};

Expand Down Expand Up @@ -140,9 +138,10 @@ export function displayChoreography(viewer, options) {

// restore diagram from cache if possible
if (useCache && shapeCache[choreoID]) {
visitor = new RestoreFromCacheVisitor(viewer, shapeCache[choreoID]);
visitor = viewer.get('restoreFromCacheVisitor');
visitor.init(shapeCache[choreoID]);
} else {
visitor = new InitialRenderVisitor(viewer);
visitor = viewer.get('initialRenderVisitor');
}

// start the walker
Expand Down
7 changes: 7 additions & 0 deletions lib/import/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import VisitorModule from './visitors';

export default {
__depends__: [
VisitorModule
]
};
46 changes: 34 additions & 12 deletions lib/import/visitors/InitialRenderVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,34 @@ function isPointInsideBBox(bbox, point) {
* A visitor for initially rendering a diagram.
* Creates and links shapes as the diagram is iterated.
*/
export default function InitialRenderVisitor(injector) {
export default function InitialRenderVisitor(
injector, eventBus, canvas, elementFactory, elementRegistry, translate, textRenderer, moddle
) {
this._injector = injector;
this._eventBus = injector.get('eventBus');
this._canvas = injector.get('canvas');
this._elementFactory = injector.get('elementFactory');
this._elementRegistry = injector.get('elementRegistry');
this._translate = injector.get('translate');
this._textRenderer = injector.get('textRenderer');
this._moddle = injector.get('moddle');
this._eventBus = eventBus;
this._canvas = canvas;
this._elementFactory = elementFactory;
this._elementRegistry = elementRegistry;
this._translate = translate;
this._textRenderer = textRenderer;
this._moddle = moddle;
}

InitialRenderVisitor.prototype.init = function(choreo, diagram) {
InitialRenderVisitor.$inject = [
'injector',
'eventBus',
'canvas',
'elementFactory',
'elementRegistry',
'translate',
'textRenderer',
'moddle'
];

InitialRenderVisitor.prototype.init = function(...parameters) {
};

InitialRenderVisitor.prototype.start = function(choreo, diagram) {
// load DI from selected diagram
this._registerDi(diagram.plane);
if (diagram.plane.planeElement) {
Expand Down Expand Up @@ -364,6 +380,12 @@ InitialRenderVisitor.prototype._add = function(semantic, parentShape) {
return element;
};

/**
* Returns the business object that is the host to the given boundary element.
*/
InitialRenderVisitor.prototype._getBoundaryHostSemantic = function(boundarySemantic) {
return boundarySemantic.attachedToRef;
};

/**
* Attach the boundary element to the given host
Expand All @@ -373,10 +395,10 @@ InitialRenderVisitor.prototype._add = function(semantic, parentShape) {
*/
InitialRenderVisitor.prototype._attachBoundary = function(boundarySemantic, boundaryElement) {
let translate = this._translate;
let hostSemantic = boundarySemantic.attachedToRef;
let hostSemantic = this._getBoundaryHostSemantic(boundarySemantic);

if (!hostSemantic) {
throw new Error(translate('missing {semantic}#attachedToRef', {
throw new Error(translate('missing host semantics for {semantic}', {
semantic: elementToString(boundarySemantic)
}));
}
Expand All @@ -385,7 +407,7 @@ InitialRenderVisitor.prototype._attachBoundary = function(boundarySemantic, boun
let attachers = host && host.attachers;

if (!host) {
throw notYetDrawn(translate, boundarySemantic, hostSemantic, 'attachedToRef');
throw notYetDrawn(translate, boundarySemantic, hostSemantic, '(hostSemantics)');
}

// wire element.host <> host.attachers
Expand Down
15 changes: 12 additions & 3 deletions lib/import/visitors/RestoreFromCacheVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ import {
* @param {*} injector
* @param {*} cache array of shaped to restore the diagram from
*/
export default function RestoreFromCacheVisitor(injector, cache) {
export default function RestoreFromCacheVisitor(injector, canvas, cache) {
this._injector = injector;
this._canvas = injector.get('canvas');
this._canvas = canvas;
this._cache = cache;
}

RestoreFromCacheVisitor.prototype.init = function(choreo, diagram) {
RestoreFromCacheVisitor.$inject = [
'injector',
'canvas'
];

RestoreFromCacheVisitor.prototype.init = function(...parameters) {
this._cache = parameters[0] || [];
};

RestoreFromCacheVisitor.prototype.start = function(choreo, diagram) {
};

RestoreFromCacheVisitor.prototype.visit = function(element, parentShape) {
Expand Down
11 changes: 11 additions & 0 deletions lib/import/visitors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import InitialRenderVisitor from './InitialRenderVisitor';
import RestoreFromCacheVisitor from './RestoreFromCacheVisitor';

export default {
__init__: [
'initialRenderVisitor',
'restoreFromCacheVisitor'
],
initialRenderVisitor: [ 'type', InitialRenderVisitor ],
restoreFromCacheVisitor: [ 'type', RestoreFromCacheVisitor ]
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chor-js",
"version": "0.4.0",
"version": "0.4.1",
"description": "A BPMN 2.0 choreography diagram rendering toolkit and web modeler.",
"keywords": [
"choreography model",
Expand Down

0 comments on commit f6c0931

Please sign in to comment.