diff --git a/lib/camunda-cloud/FormDefinitionBehavior.js b/lib/camunda-cloud/FormsBehavior.js similarity index 79% rename from lib/camunda-cloud/FormDefinitionBehavior.js rename to lib/camunda-cloud/FormsBehavior.js index ce15768..d3f1cd3 100644 --- a/lib/camunda-cloud/FormDefinitionBehavior.js +++ b/lib/camunda-cloud/FormsBehavior.js @@ -1,4 +1,3 @@ - import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor'; import { createElement } from '../util/ElementUtil'; @@ -9,19 +8,18 @@ import { } from 'bpmn-js/lib/util/ModelUtil'; import { - createFormDefinition, - createFormId, - createFormKey, - createUserTaskForm, + createUserTaskFormId, getFormDefinition, - getUserTaskForm + getRootElement, + getUserTaskForm, + userTaskFormIdToFormKey } from './util/FormsUtil'; /** - * Zeebe BPMN specific form definition behavior. + * Zeebe BPMN specific forms behavior. */ -export default class FormDefinitionBehavior extends CommandInterceptor { +export default class FormsBehavior extends CommandInterceptor { constructor(bpmnFactory, eventBus, modeling) { super(eventBus); @@ -87,9 +85,11 @@ export default class FormDefinitionBehavior extends CommandInterceptor { }); // (3) create new form definition - const formId = createFormId(); + const userTaskFormId = createUserTaskFormId(); - const newFormDefinition = createFormDefinition({ formKey: createFormKey(formId) }, extensionElements, bpmnFactory); + const newFormDefinition = createElement('zeebe:FormDefinition', { + formKey: userTaskFormIdToFormKey(userTaskFormId) + }, extensionElements, bpmnFactory); values = [ ...values, @@ -101,8 +101,8 @@ export default class FormDefinitionBehavior extends CommandInterceptor { }); // (4) create new user task form - const userTaskForm = createUserTaskForm({ - id: formId, + const userTaskForm = createElement('zeebe:UserTaskForm', { + id: userTaskFormId, body: oldUserTaskForm ? oldUserTaskForm.get('body') : '' }, rootExtensionElements, bpmnFactory); @@ -117,22 +117,8 @@ export default class FormDefinitionBehavior extends CommandInterceptor { } } -FormDefinitionBehavior.$inject = [ +FormsBehavior.$inject = [ 'bpmnFactory', 'eventBus', 'modeling' -]; - - -// helpers ////////////// - -function getRootElement(element) { - var businessObject = getBusinessObject(element), - parent = businessObject; - - while (parent.$parent && !is(parent, 'bpmn:Process')) { - parent = parent.$parent; - } - - return parent; -} \ No newline at end of file +]; \ No newline at end of file diff --git a/lib/camunda-cloud/index.js b/lib/camunda-cloud/index.js index 4fc052f..e46d206 100644 --- a/lib/camunda-cloud/index.js +++ b/lib/camunda-cloud/index.js @@ -4,7 +4,7 @@ import CleanUpTimerExpressionBehavior from './CleanUpTimerExpressionBehavior'; import CopyPasteBehavior from './CopyPasteBehavior'; import CreateZeebeCallActivityBehavior from './CreateZeebeCallActivityBehavior'; import DeleteParticipantBehaviour from '../shared/DeleteParticipantBehaviour'; -import FormDefinitionBehavior from './FormDefinitionBehavior'; +import FormsBehavior from './FormsBehavior'; import RemoveAssignmentDefinitionBehavior from './RemoveAssignmentDefinitionBehavior'; import RemoveTaskScheduleBehavior from './RemoveTaskScheduleBehavior'; import UpdatePropagateAllChildVariablesBehavior from './UpdatePropagateAllChildVariablesBehavior'; @@ -17,7 +17,7 @@ export default { 'copyPasteBehavior', 'createZeebeCallActivityBehavior', 'deleteParticipantBehaviour', - 'formDefinitionBehavior', + 'formsBehavior', 'removeAssignmentDefinitionBehavior', 'removeTaskScheduleBehavior', 'updatePropagateAllChildVariablesBehavior' @@ -28,7 +28,7 @@ export default { copyPasteBehavior: [ 'type', CopyPasteBehavior ], createZeebeCallActivityBehavior: [ 'type', CreateZeebeCallActivityBehavior ], deleteParticipantBehaviour: [ 'type', DeleteParticipantBehaviour ], - formDefinitionBehavior: [ 'type', FormDefinitionBehavior ], + formsBehavior: [ 'type', FormsBehavior ], removeAssignmentDefinitionBehavior: [ 'type', RemoveAssignmentDefinitionBehavior ], removeTaskScheduleBehavior: [ 'type', RemoveTaskScheduleBehavior ], updatePropagateAllChildVariablesBehavior: [ 'type', UpdatePropagateAllChildVariablesBehavior ] diff --git a/lib/camunda-cloud/util/FormsUtil.js b/lib/camunda-cloud/util/FormsUtil.js index ae7c8dd..6565da2 100644 --- a/lib/camunda-cloud/util/FormsUtil.js +++ b/lib/camunda-cloud/util/FormsUtil.js @@ -1,7 +1,3 @@ -import { getExtensionElementsList } from '../../util/ExtensionElementsUtil'; - -import { createElement } from '../../util/ElementUtil'; - import { getBusinessObject, is @@ -9,76 +5,61 @@ import { import { getPrefixedId } from '../../util/IdsUtil'; -import { find } from 'min-dash'; +import { getExtensionElementsList } from '../../util/ExtensionElementsUtil'; -const USER_TASK_FORM_PREFIX = 'UserTaskForm_'; +const FORM_KEY_PREFIX = 'camunda-forms:bpmn:', + USER_TASK_FORM_ID_PREFIX = 'UserTaskForm_'; +export function getFormDefinition(element) { + const businessObject = getBusinessObject(element); -export function createFormDefinition(properties, extensionElements, bpmnFactory) { - return createElement( - 'zeebe:FormDefinition', - properties, - extensionElements, - bpmnFactory - ); -} + const formDefinitions = getExtensionElementsList(businessObject, 'zeebe:FormDefinition'); -export function createFormId() { - return getPrefixedId(USER_TASK_FORM_PREFIX); + return formDefinitions[ 0 ]; } -export function createFormKey(formId) { - return `camunda-forms:bpmn:${ formId }`; -} +export function getUserTaskForm(element, rootElement) { + rootElement = rootElement || getRootElement(element); -export function createUserTaskForm(properties, extensionElements, bpmnFactory) { - return createElement( - 'zeebe:UserTaskForm', - properties, - extensionElements, - bpmnFactory - ); -} + const formDefinition = getFormDefinition(element); -function findUserTaskForm(formKey, rootElement) { - const userTaskForms = getExtensionElementsList(rootElement, 'zeebe:UserTaskForm'); + if (!formDefinition) { + return; + } + + const formKey = formDefinition.get('formKey'); - return find(userTaskForms, (userTaskForm) => { - const id = userTaskForm.get('zeebe:id'); + const userTaskForms = getExtensionElementsList(rootElement, 'zeebe:UserTaskForm'); - return createFormKey(id) === formKey; + return userTaskForms.find(userTaskForm => { + return userTaskFormIdToFormKey(userTaskForm.get('id')) === formKey; }); } -export function getFormDefinition(element) { - const businessObject = getBusinessObject(element); - - const formDefinitions = getExtensionElementsList(businessObject, 'zeebe:FormDefinition'); - - return formDefinitions[ 0 ]; +export function userTaskFormIdToFormKey(userTaskFormId) { + return `${ FORM_KEY_PREFIX }${ userTaskFormId }`; } -function getRootElement(element) { - var businessObject = getBusinessObject(element), - parent = businessObject; +export function formKeyToUserTaskFormId(formKey) { + return formKey.replace(FORM_KEY_PREFIX, ''); +} - while (parent.$parent && !is(parent, 'bpmn:Process')) { - parent = parent.$parent; - } +export function isUserTaskFormKey(formKey) { + return formKey.startsWith(FORM_KEY_PREFIX); +} - return parent; +export function createUserTaskFormId() { + return getPrefixedId(USER_TASK_FORM_ID_PREFIX); } -export function getUserTaskForm(element, parent) { - const rootElement = parent || getRootElement(element); +export function getRootElement(element) { + const businessObject = getBusinessObject(element); - const formDefinition = getFormDefinition(element); + let parent = businessObject; - if (!formDefinition) { - return; + while (parent.$parent && !is(parent, 'bpmn:Process')) { + parent = parent.$parent; } - const formKey = formDefinition.get('zeebe:formKey'); - - return findUserTaskForm(formKey, rootElement); + return parent; } \ No newline at end of file diff --git a/test/camunda-cloud/FormDefinitionBehaviorSpec.js b/test/camunda-cloud/FormDefinitionBehaviorSpec.js index 6d77212..cc214a0 100644 --- a/test/camunda-cloud/FormDefinitionBehaviorSpec.js +++ b/test/camunda-cloud/FormDefinitionBehaviorSpec.js @@ -15,7 +15,7 @@ import { import diagramXML from './process-user-tasks.bpmn'; -describe('camunda-cloud/features/modeling - FormDefinitionBehavior', function() { +describe('camunda-cloud/features/modeling - FormsBehavior', function() { beforeEach(bootstrapCamundaCloudModeler(diagramXML));