Skip to content

Commit

Permalink
feat(copy/paste): allow copy/paste of camunda:ErrorEventDefinition er…
Browse files Browse the repository at this point in the history
  • Loading branch information
azeghers committed Mar 23, 2021
1 parent 4313e55 commit 06c14d7
Show file tree
Hide file tree
Showing 7 changed files with 2,686 additions and 5 deletions.
64 changes: 64 additions & 0 deletions karma.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var path = require('path');

var basePath = '.';

var absoluteBasePath = path.resolve(path.join(__dirname, basePath));

var suite = 'test/browser/*Spec.js';

// configures browsers to run test against
// any of [ 'ChromeHeadless', 'Chrome', 'Firefox', 'Safari' ]
var browsers = (process.env.TEST_BROWSERS || 'ChromeHeadless').split(',');

// use puppeteer provided Chrome for testing
process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(karma) {
var config = {

basePath,

frameworks: [
'mocha',
'sinon-chai'
],

files: [
suite
],

preprocessors: {
[ suite ]: [ 'webpack', 'env' ]
},

reporters: [ 'progress' ],

browsers,

browserNoActivityTimeout: 30000,

singleRun: true,
autoWatch: false,

webpack: {
mode: 'development',
module: {
rules: [
{
test: /\.bpmn$/,
use: 'raw-loader'
}
]
},
resolve: {
modules: [
'node_modules',
absoluteBasePath
]
},
devtool: 'eval-source-map'
}
};

karma.set(config);
};
174 changes: 174 additions & 0 deletions lib/RootElementReferenceBehavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
'use strict';

var inherits = require('inherits');

var find = require('min-dash').find,
matchPattern = require('min-dash').matchPattern;

var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor').default;

var collectionAdd = require('diagram-js/lib/util/Collections').add,
collectionRemove = require('diagram-js/lib/util/Collections').remove;

var getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject,
is = require('bpmn-js/lib/util/ModelUtil').is;

var LOW_PRIORITY = 500;


/**
* Add referenced root elements (bpmn:Error) if they don't exist.
* Copy referenced root elements on copy & paste.
*/
function RootElementReferenceBehavior(
bpmnjs, eventBus, injector, moddleCopy, bpmnFactory
) {

injector.invoke(CommandInterceptor, this);

function hasRootElement(rootElement) {
var definitions = bpmnjs.getDefinitions(),
rootElements = definitions.get('rootElements');

return !!find(rootElements, matchPattern({ id: rootElement.id }));
}

function getRootElements(bo, type) {
var extensionElements = bo.get('extensionElements');
var filteredExtensionElements = [];

if (extensionElements) {
filteredExtensionElements = extensionElements.values.filter(function(element) {
return is(element, type);
});
}

var rootElements = filteredExtensionElements.map(function(element) {
return element[getRootElementReferencePropertyName(element)];
});

return rootElements;
}

function setRootElement(bo, rootElement, index) {
var extensionElement = bo.get('extensionElements').values[index];

extensionElement.set(getRootElementReferencePropertyName(extensionElement), rootElement);
}

// create shape
this.executed('shape.create', function(context) {

var shape = context.shape,
businessObject = getBusinessObject(shape);

if (!canHaveNestedRootElementReference(businessObject)) {
return;
}

var referencedRootElements = getRootElements(businessObject, getReferencingElement(shape)),
rootElements = bpmnjs.getDefinitions().get('rootElements');

context.addedRootElements = [];

referencedRootElements.forEach(function(element) {
if (!hasRootElement(element)) {

// add root element
collectionAdd(rootElements, element);

context.addedRootElements.push(element);
}
});
}, true);

this.reverted('shape.create', function(context) {
var addedRootElements = context.addedRootElements;

if (!addedRootElements) {
return;
}

var rootElements = bpmnjs.getDefinitions().get('rootElements');

// remove root elements
addedRootElements.forEach(function(addedRootElement) {
collectionRemove(rootElements, addedRootElement);
});
}, true);

eventBus.on('copyPaste.copyElement', function(context) {
var descriptor = context.descriptor,
element = context.element,
businessObject = getBusinessObject(element);

if (!canHaveNestedRootElementReference(businessObject)) {
return;
}

var rootElements = getRootElements(businessObject, getReferencingElement(element));

if (rootElements) {
descriptor.referencedRootElements = rootElements;
}
});


eventBus.on('copyPaste.pasteElement', LOW_PRIORITY, function(context) {
var descriptor = context.descriptor,
businessObject = descriptor.businessObject;

if (!canHaveNestedRootElementReference(businessObject)) {
return;
}

var referencedRootElements = descriptor.referencedRootElements;

if (referencedRootElements && referencedRootElements.length) {

referencedRootElements.forEach(function(element, index) {
if (!hasRootElement(element)) {
element = moddleCopy.copyElement(
element,
bpmnFactory.create(element.$type)
);
}
setRootElement(businessObject, element, index);
});
}

delete descriptor.referencedRootElements;
});
}

// Helpers

function getReferencingElement(element) {
if (is(element, 'bpmn:ServiceTask')) {
return 'camunda:ErrorEventDefinition';
}
}

function getRootElementReferencePropertyName(bo) {
if (is(bo, 'camunda:ErrorEventDefinition')) {
return 'errorRef';
}
}

function canHaveNestedRootElementReference(bo) {
var isExternalServiceTask = is(bo, 'bpmn:ServiceTask') && bo.type === 'external';
return isExternalServiceTask;
}


RootElementReferenceBehavior.$inject = [
'bpmnjs',
'eventBus',
'injector',
'moddleCopy',
'bpmnFactory'
];

inherits(RootElementReferenceBehavior, CommandInterceptor);

module.exports = RootElementReferenceBehavior;
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

module.exports = {
__init__: [ 'camundaModdleExtension' ],
camundaModdleExtension: [ 'type', require('./extension') ]
};
__init__: [ 'camundaModdleExtension', 'rootElementReferenceBehavior' ],
camundaModdleExtension: [ 'type', require('./extension') ],
rootElementReferenceBehavior: [ 'type', require('./RootElementReferenceBehavior') ]
};
Loading

0 comments on commit 06c14d7

Please sign in to comment.