|
| 1 | +import { |
| 2 | + is, |
| 3 | + getBusinessObject |
| 4 | +} from 'bpmn-js/lib/util/ModelUtil'; |
| 5 | + |
| 6 | +import { |
| 7 | + isFeelEntryEdited, |
| 8 | + isTextFieldEntryEdited, |
| 9 | + TextFieldEntry |
| 10 | +} from '@bpmn-io/properties-panel'; |
| 11 | + |
| 12 | +import { useService } from '../../../hooks'; |
| 13 | + |
| 14 | +import { BpmnFeelEntry } from '../../../entries/BpmnFeelEntry'; |
| 15 | + |
| 16 | +import { |
| 17 | + getExtensionElementsList, |
| 18 | + addExtensionElements |
| 19 | +} from '../../../utils/ExtensionElementsUtil'; |
| 20 | + |
| 21 | +import { createElement } from '../../../utils/ElementUtil'; |
| 22 | + |
| 23 | +export function OutputCollectionProps(props) { |
| 24 | + const { |
| 25 | + element |
| 26 | + } = props; |
| 27 | + |
| 28 | + if (!is(element, 'bpmn:AdHocSubProcess')) { |
| 29 | + return []; |
| 30 | + } |
| 31 | + |
| 32 | + const entries = [ |
| 33 | + { |
| 34 | + id: 'adHocOutputCollection', |
| 35 | + component: OutputCollection, |
| 36 | + isEdited: isTextFieldEntryEdited |
| 37 | + }, |
| 38 | + { |
| 39 | + id: 'adHocOutputElement', |
| 40 | + component: OutputElement, |
| 41 | + isEdited: isFeelEntryEdited |
| 42 | + } |
| 43 | + ]; |
| 44 | + |
| 45 | + return entries; |
| 46 | +} |
| 47 | + |
| 48 | +function OutputCollection(props) { |
| 49 | + const { |
| 50 | + element, |
| 51 | + id |
| 52 | + } = props; |
| 53 | + |
| 54 | + const commandStack = useService('commandStack'); |
| 55 | + const bpmnFactory = useService('bpmnFactory'); |
| 56 | + const translate = useService('translate'); |
| 57 | + const debounce = useService('debounceInput'); |
| 58 | + |
| 59 | + const getValue = () => { |
| 60 | + return getOutputCollectionProperty(element); |
| 61 | + }; |
| 62 | + |
| 63 | + const setValue = (value) => { |
| 64 | + return setOutputCollectionProperty(element, value, commandStack, bpmnFactory); |
| 65 | + }; |
| 66 | + |
| 67 | + return TextFieldEntry({ |
| 68 | + element, |
| 69 | + id, |
| 70 | + label: translate('Output collection'), |
| 71 | + getValue, |
| 72 | + setValue, |
| 73 | + debounce |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +function OutputElement(props) { |
| 78 | + const { |
| 79 | + element, |
| 80 | + id |
| 81 | + } = props; |
| 82 | + |
| 83 | + const commandStack = useService('commandStack'); |
| 84 | + const bpmnFactory = useService('bpmnFactory'); |
| 85 | + const translate = useService('translate'); |
| 86 | + const debounce = useService('debounceInput'); |
| 87 | + |
| 88 | + const getValue = () => { |
| 89 | + return getOutputElementProperty(element); |
| 90 | + }; |
| 91 | + |
| 92 | + const setValue = (value) => { |
| 93 | + return setOutputElementProperty(element, value, commandStack, bpmnFactory); |
| 94 | + }; |
| 95 | + |
| 96 | + return BpmnFeelEntry({ |
| 97 | + element, |
| 98 | + id, |
| 99 | + label: translate('Output element'), |
| 100 | + feel: 'required', |
| 101 | + getValue, |
| 102 | + setValue, |
| 103 | + debounce |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +function getOutputCollectionProperty(element) { |
| 108 | + const extensionElement = getExtensionElement(element); |
| 109 | + return extensionElement && extensionElement.get('outputCollection'); |
| 110 | +} |
| 111 | + |
| 112 | +function setOutputCollectionProperty(element, value, commandStack, bpmnFactory) { |
| 113 | + |
| 114 | + const extensionElement = getExtensionElement(element); |
| 115 | + |
| 116 | + if (!extensionElement) { |
| 117 | + |
| 118 | + // (1) create extension element |
| 119 | + const adHoc = createElement( |
| 120 | + 'zeebe:AdHoc', |
| 121 | + { |
| 122 | + outputCollection: value |
| 123 | + }, |
| 124 | + undefined, |
| 125 | + bpmnFactory |
| 126 | + ); |
| 127 | + |
| 128 | + const businessObject = getBusinessObject(element); |
| 129 | + addExtensionElements(element, businessObject, adHoc, bpmnFactory, commandStack); |
| 130 | + |
| 131 | + } else { |
| 132 | + |
| 133 | + // (2) update extension element's property |
| 134 | + commandStack.execute('element.updateModdleProperties', { |
| 135 | + element, |
| 136 | + moddleElement: extensionElement, |
| 137 | + properties: { |
| 138 | + outputCollection: value |
| 139 | + } |
| 140 | + }); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +function getOutputElementProperty(element) { |
| 145 | + const extensionElement = getExtensionElement(element); |
| 146 | + return extensionElement && extensionElement.get('outputElement'); |
| 147 | +} |
| 148 | + |
| 149 | +function setOutputElementProperty(element, value, commandStack, bpmnFactory) { |
| 150 | + |
| 151 | + const extensionElement = getExtensionElement(element); |
| 152 | + |
| 153 | + if (!extensionElement) { |
| 154 | + |
| 155 | + // (1) create extension element |
| 156 | + const adHoc = createElement( |
| 157 | + 'zeebe:AdHoc', |
| 158 | + { |
| 159 | + outputElement: value |
| 160 | + }, |
| 161 | + undefined, |
| 162 | + bpmnFactory |
| 163 | + ); |
| 164 | + |
| 165 | + const businessObject = getBusinessObject(element); |
| 166 | + addExtensionElements(element, businessObject, adHoc, bpmnFactory, commandStack); |
| 167 | + |
| 168 | + } else { |
| 169 | + |
| 170 | + // (2) update extension element's property |
| 171 | + commandStack.execute('element.updateModdleProperties', { |
| 172 | + element, |
| 173 | + moddleElement: extensionElement, |
| 174 | + properties: { |
| 175 | + outputElement: value |
| 176 | + } |
| 177 | + }); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +function getExtensionElement(element) { |
| 182 | + const businessObject = getBusinessObject(element); |
| 183 | + const extensions = getExtensionElementsList(businessObject, 'zeebe:AdHoc'); |
| 184 | + return extensions[0]; |
| 185 | +} |
0 commit comments