|
| 1 | +import { is, getBusinessObject } from 'bpmn-js/lib/util/ModelUtil'; |
| 2 | + |
| 3 | +import { |
| 4 | + CheckboxEntry, |
| 5 | + isTextFieldEntryEdited, |
| 6 | + TextFieldEntry, |
| 7 | +} from '@bpmn-io/properties-panel'; |
| 8 | + |
| 9 | +import { createElement } from '../../../utils/ElementUtil'; |
| 10 | + |
| 11 | +import { useService } from '../../../hooks'; |
| 12 | + |
| 13 | +/** |
| 14 | + * @typedef { import('@bpmn-io/properties-panel').EntryDefinition } Entry |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @returns {Array<Entry>} entries |
| 19 | + */ |
| 20 | +export function AdHocCompletionProps(props) { |
| 21 | + const { element } = props; |
| 22 | + |
| 23 | + if (!is(element, 'bpmn:AdHocSubProcess')) { |
| 24 | + return []; |
| 25 | + } |
| 26 | + |
| 27 | + return [ |
| 28 | + { |
| 29 | + id: 'completionCondition', |
| 30 | + component: CompletionCondition, |
| 31 | + isEdited: isTextFieldEntryEdited, |
| 32 | + }, |
| 33 | + { |
| 34 | + id: 'cancelRemainingInstances', |
| 35 | + component: CancelRemainingInstances, |
| 36 | + isEdited: (node) => node && !node.checked // the default value is true |
| 37 | + }, |
| 38 | + ]; |
| 39 | +} |
| 40 | + |
| 41 | +function CompletionCondition(props) { |
| 42 | + const { element } = props; |
| 43 | + |
| 44 | + const bpmnFactory = useService('bpmnFactory'); |
| 45 | + const debounce = useService('debounceInput'); |
| 46 | + const commandStack = useService('commandStack'); |
| 47 | + const translate = useService('translate'); |
| 48 | + |
| 49 | + const getValue = () => { |
| 50 | + const expression = getBusinessObject(element).get('completionCondition'); |
| 51 | + return expression && expression.get('body'); |
| 52 | + }; |
| 53 | + |
| 54 | + const setValue = (value) => { |
| 55 | + return commandStack.execute( |
| 56 | + 'element.updateModdleProperties', |
| 57 | + updateFormalExpression(element, 'completionCondition', value, bpmnFactory) |
| 58 | + ); |
| 59 | + }; |
| 60 | + |
| 61 | + return TextFieldEntry({ |
| 62 | + element, |
| 63 | + id: 'completionCondition', |
| 64 | + label: translate('Completion condition'), |
| 65 | + getValue, |
| 66 | + setValue, |
| 67 | + debounce |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +function CancelRemainingInstances(props) { |
| 72 | + const { element } = props; |
| 73 | + |
| 74 | + const commandStack = useService('commandStack'); |
| 75 | + const translate = useService('translate'); |
| 76 | + |
| 77 | + const businessObject = getBusinessObject(element); |
| 78 | + |
| 79 | + const getValue = () => { |
| 80 | + return businessObject.get('cancelRemainingInstances'); |
| 81 | + }; |
| 82 | + |
| 83 | + const setValue = (value) => { |
| 84 | + commandStack.execute('element.updateModdleProperties', { |
| 85 | + element, |
| 86 | + moddleElement: businessObject, |
| 87 | + properties: { |
| 88 | + cancelRemainingInstances: value, |
| 89 | + }, |
| 90 | + }); |
| 91 | + }; |
| 92 | + |
| 93 | + return CheckboxEntry({ |
| 94 | + element, |
| 95 | + id: 'cancelRemainingInstances', |
| 96 | + label: translate('Cancel remaining instances'), |
| 97 | + getValue, |
| 98 | + setValue, |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +// helper //////////////////////////// |
| 103 | + |
| 104 | +// formal expression ///////////////// |
| 105 | + |
| 106 | +/** |
| 107 | + * updateFormalExpression - updates a specific formal expression |
| 108 | + * |
| 109 | + * @param {djs.model.Base} element |
| 110 | + * @param {string} propertyName |
| 111 | + * @param {string} newValue |
| 112 | + * @param {BpmnFactory} bpmnFactory |
| 113 | + */ |
| 114 | +function updateFormalExpression(element, propertyName, newValue, bpmnFactory) { |
| 115 | + const businessObject = getBusinessObject(element); |
| 116 | + const expressionProps = {}; |
| 117 | + |
| 118 | + if (!newValue) { |
| 119 | + |
| 120 | + // remove formal expression |
| 121 | + expressionProps[propertyName] = undefined; |
| 122 | + |
| 123 | + return { |
| 124 | + element, |
| 125 | + moddleElement: businessObject, |
| 126 | + properties: expressionProps, |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + const existingExpression = businessObject.get(propertyName); |
| 131 | + if (!existingExpression) { |
| 132 | + |
| 133 | + // add formal expression |
| 134 | + expressionProps[propertyName] = createElement( |
| 135 | + 'bpmn:FormalExpression', |
| 136 | + { body: newValue }, |
| 137 | + businessObject, |
| 138 | + bpmnFactory |
| 139 | + ); |
| 140 | + |
| 141 | + return { |
| 142 | + element, |
| 143 | + moddleElement: businessObject, |
| 144 | + properties: expressionProps, |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + // edit existing formal expression |
| 149 | + return { |
| 150 | + element, |
| 151 | + moddleElement: existingExpression, |
| 152 | + properties: { |
| 153 | + body: newValue, |
| 154 | + }, |
| 155 | + }; |
| 156 | +} |
0 commit comments