Skip to content

Commit

Permalink
chore: refactor utility
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Sep 15, 2023
1 parent b5ff290 commit 03f958e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';

import { createElement } from '../util/ElementUtil';
Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -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);

Expand All @@ -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;
}
];
6 changes: 3 additions & 3 deletions lib/camunda-cloud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -17,7 +17,7 @@ export default {
'copyPasteBehavior',
'createZeebeCallActivityBehavior',
'deleteParticipantBehaviour',
'formDefinitionBehavior',
'formsBehavior',
'removeAssignmentDefinitionBehavior',
'removeTaskScheduleBehavior',
'updatePropagateAllChildVariablesBehavior'
Expand All @@ -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 ]
Expand Down
87 changes: 34 additions & 53 deletions lib/camunda-cloud/util/FormsUtil.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,65 @@
import { getExtensionElementsList } from '../../util/ExtensionElementsUtil';

import { createElement } from '../../util/ElementUtil';

import {
getBusinessObject,
is
} from 'bpmn-js/lib/util/ModelUtil';

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;
}
2 changes: 1 addition & 1 deletion test/camunda-cloud/FormDefinitionBehaviorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 03f958e

Please sign in to comment.