From b2d2665adf101fa2cd61b57adcb5fae2eb19a68c Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 15 Sep 2021 16:11:38 +0200 Subject: [PATCH] feat(camunda-platform): clean up camunda:formData#values on type change Closes #46 Related to https://github.com/bpmn-io/bpmn-properties-panel/issues/2 --- .../UserTaskGeneratedFormsBehavior.js | 36 +++++ .../features/modeling/behavior/index.js | 7 +- .../UserTaskGeneratedFormsBehaviorSpec.js | 132 ++++++++++++++++++ ...nda-user-task-generated-forms-diagram.bpmn | 35 +++++ 4 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 lib/camunda-platform/features/modeling/behavior/UserTaskGeneratedFormsBehavior.js create mode 100644 test/camunda-platform/features/modeling/UserTaskGeneratedFormsBehaviorSpec.js create mode 100644 test/camunda-platform/features/modeling/camunda-user-task-generated-forms-diagram.bpmn diff --git a/lib/camunda-platform/features/modeling/behavior/UserTaskGeneratedFormsBehavior.js b/lib/camunda-platform/features/modeling/behavior/UserTaskGeneratedFormsBehavior.js new file mode 100644 index 00000000..2499dd69 --- /dev/null +++ b/lib/camunda-platform/features/modeling/behavior/UserTaskGeneratedFormsBehavior.js @@ -0,0 +1,36 @@ +import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor'; + +import { is } from 'bpmn-js/lib/util/ModelUtil'; + + +/** + * Camunda BPMN specific user task generated forms behavior removing camunda:FormField#values if camunda:FormField#type + * is changed to something other than enum. + */ +export default class UserTaskFormsBehavior extends CommandInterceptor { + constructor(eventBus) { + super(eventBus); + + this.preExecute([ + 'element.updateModdleProperties', + 'properties-panel.update-businessobject' + ], function(context) { + let { + businessObject, + moddleElement, + properties + } = context; + + businessObject = businessObject || moddleElement; + + if (is(businessObject, 'camunda:FormField') + && 'camunda:type' in properties + && properties[ 'camunda:type' ] !== 'enum') { + properties[ 'camunda:values' ] = undefined; + } + }, true); + } +} + + +UserTaskFormsBehavior.$inject = [ 'eventBus' ]; \ No newline at end of file diff --git a/lib/camunda-platform/features/modeling/behavior/index.js b/lib/camunda-platform/features/modeling/behavior/index.js index 83098445..a90b7be7 100644 --- a/lib/camunda-platform/features/modeling/behavior/index.js +++ b/lib/camunda-platform/features/modeling/behavior/index.js @@ -4,6 +4,7 @@ import UpdateCamundaExclusiveBehavior from './UpdateCamundaExclusiveBehavior'; import UpdateInputOutputBehavior from './UpdateInputOutputBehavior'; import UpdateResultVariableBehavior from './UpdateResultVariableBehavior'; import UserTaskFormsBehavior from './UserTaskFormsBehavior'; +import UserTaskGeneratedFormsBehavior from './UserTaskGeneratedFormsBehavior'; export default { __init__: [ @@ -12,12 +13,14 @@ export default { 'updateCamundaExclusiveBehavior', 'updateResultVariableBehavior', 'updateInputOutputBehavior', - 'userTaskFormsBehavior' + 'userTaskFormsBehavior', + 'userTaskGeneratedFormsBehavior' ], deleteErrorEventDefinitionBehavior: [ 'type', DeleteErrorEventDefinitionBehavior ], deleteRetryTimeCycleBehavior: [ 'type', DeleteRetryTimeCycleBehavior ], updateCamundaExclusiveBehavior: [ 'type', UpdateCamundaExclusiveBehavior ], updateResultVariableBehavior: [ 'type', UpdateResultVariableBehavior ], updateInputOutputBehavior: [ 'type', UpdateInputOutputBehavior ], - userTaskFormsBehavior: [ 'type', UserTaskFormsBehavior ] + userTaskFormsBehavior: [ 'type', UserTaskFormsBehavior ], + userTaskGeneratedFormsBehavior: [ 'type', UserTaskGeneratedFormsBehavior ] }; diff --git a/test/camunda-platform/features/modeling/UserTaskGeneratedFormsBehaviorSpec.js b/test/camunda-platform/features/modeling/UserTaskGeneratedFormsBehaviorSpec.js new file mode 100644 index 00000000..43d4c423 --- /dev/null +++ b/test/camunda-platform/features/modeling/UserTaskGeneratedFormsBehaviorSpec.js @@ -0,0 +1,132 @@ +import { + bootstrapCamundaPlatformModeler, + inject +} from 'test/TestHelper'; + +import { + getBpmnJS +} from 'bpmn-js/test/helper'; + +import coreModule from 'bpmn-js/lib/core'; + +import modelingModule from 'bpmn-js/lib/features/modeling'; + +import { + getBusinessObject, + is +} from 'bpmn-js/lib/util/ModelUtil'; + +import camundaModdleExtensions from 'camunda-bpmn-moddle/resources/camunda'; + +import camundaPlatformModelingModules from 'lib/camunda-platform/features/modeling'; + +import propertiesPanelCommandHandler from 'bpmn-js-properties-panel/lib/cmd'; + +import diagramXML from './camunda-user-task-generated-forms-diagram.bpmn'; + + +describe('camunda-platform/features/modeling - UserTaskGeneratedFormsBehavior', function() { + + const testModules = [ + camundaPlatformModelingModules, + coreModule, + modelingModule, + propertiesPanelCommandHandler + ]; + + const moddleExtensions = { + camunda: camundaModdleExtensions + }; + + beforeEach(bootstrapCamundaPlatformModeler(diagramXML, { + modules: testModules, + moddleExtensions + })); + + function updateModdleProperties(element, moddleElement, properties) { + getBpmnJS().invoke(function(modeling) { + modeling.updateModdleProperties(element, moddleElement, properties); + }); + } + + function updateBusinessObject(element, businessObject, properties) { + getBpmnJS().invoke(function(commandStack) { + commandStack.execute('properties-panel.update-businessobject', { + element, + businessObject, + properties + }); + }); + } + + [ + [ 'element.updateModdleProperties', updateModdleProperties ], + [ 'properties-panel.update-businessobject', updateBusinessObject ] + ].forEach(([ command, fn ]) => { + + describe(command, function() { + + [ + [ 'start event', 'StartEvent' ], + [ 'user task', 'UserTask' ] + ].forEach(([ type, prefix ]) => { + + describe('setting camunda:FormField#type to boolean', function() { + + describe(type, function() { + + it('should delete camunda:FormField#values', inject(function(elementRegistry) { + + // when + const element = elementRegistry.get(`${ prefix }_1`); + + const businessObject = getFormField(element); + + // when + fn(element, businessObject, { 'camunda:type': 'boolean' }); + + // then + expect(businessObject.get('camunda:values')).to.be.empty; + })); + + + it('should not delete camunda:FormField#values', inject(function(elementRegistry) { + + // when + const element = elementRegistry.get(`${ prefix }_1`); + + const businessObject = getFormField(element); + + // when + fn(element, businessObject, { 'camunda:type': 'enum' }); + + // then + expect(businessObject.get('camunda:values')).not.to.be.empty; + })); + + }); + + }); + + }); + + }); + + }); + +}); + +// helpers ////////// + +function getFormField(element, index = 0) { + const businessObject = getBusinessObject(element); + + const extensionElements = businessObject.get('extensionElements'), + values = extensionElements.get('values'); + + const formData = values.find((value) => { + return is(value, 'camunda:FormData'); + }); + + return formData.get('camunda:fields')[ index ]; +} diff --git a/test/camunda-platform/features/modeling/camunda-user-task-generated-forms-diagram.bpmn b/test/camunda-platform/features/modeling/camunda-user-task-generated-forms-diagram.bpmn new file mode 100644 index 00000000..171ed4d3 --- /dev/null +++ b/test/camunda-platform/features/modeling/camunda-user-task-generated-forms-diagram.bpmn @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +