-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(copy/paste): allow copy/paste of camunda:ErrorEventDefinition er…
…rorRefs Closes bpmn-io/bpmn-js-properties-panel#442
- Loading branch information
Showing
7 changed files
with
2,686 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') ] | ||
}; |
Oops, something went wrong.