All files / src/web/webview elementActions.ts

25.64% Statements 30/117
23.8% Branches 10/42
15.25% Functions 9/59
26.54% Lines 30/113

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374                                                                                                                                                                                  1x 1x       1x 1x 1x       1x 1x     1x       1x 1x 1x                                 1x 1x       1x 1x 1x       1x 1x 1x   1x 1x 1x                                       1x 1x     1x         1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                               2x                                                          
import type {
	FlowableAttributeKey,
	FlowableElementState,
	FlowableFieldExtension,
	FlowableFormProperty,
	FlowableIOParameter,
	FlowableListener,
	FlowableMultiInstance,
	FlowableTimerDefinition,
} from '../flowable/types';
import { getElementId, getElementType, type BpmnElement } from './bpmnTypeGuards';
import { createFieldExtension, createFormProperty, createIOParameter, createListener } from './elementState';
 
interface ActionCounters {
	field: number;
	listener: number;
	formProperty: number;
	ioParameter: number;
}
 
interface ModelingService {
	updateProperties(element: BpmnElement, properties: Record<string, unknown>): void;
}
 
interface ElementActionDeps {
	getSelectedElement: () => BpmnElement | null;
	ensureElementState: (element: BpmnElement) => FlowableElementState;
	renameElementState: (oldId: string, newId: string, type: string) => void;
	modeling: ModelingService;
	queueMetadataSave: () => void;
	setStatus: (message: string, state?: 'idle' | 'error') => void;
	renderProperties: () => void;
	counters: ActionCounters;
}
 
type ListenerCollectionKey = 'taskListeners' | 'executionListeners';
type ParameterCollectionKey = 'inputParameters' | 'outputParameters';
type SavedStringPropertyKey =
	| 'conditionExpression'
	| 'script'
	| 'errorRef'
	| 'signalRef'
	| 'messageRef'
	| 'terminateAll'
	| 'compensateActivityRef'
	| 'isForCompensation'
	| 'failedJobRetryTimeCycle'
	| 'documentation';
 
function getListenerKey(kind: 'task' | 'execution'): ListenerCollectionKey {
	return kind === 'task' ? 'taskListeners' : 'executionListeners';
}
 
function getParameterKey(kind: 'input' | 'output'): ParameterCollectionKey {
	return kind === 'input' ? 'inputParameters' : 'outputParameters';
}
 
export interface ElementActions {
	updateGeneralProperty: (property: 'id' | 'name', value: string) => void;
	updateFlowableAttribute: (attribute: FlowableAttributeKey, value: string | boolean) => void;
	updateFieldExtension: (index: number, patch: Partial<FlowableFieldExtension>) => void;
	removeFieldExtension: (index: number) => void;
	addFieldExtension: () => void;
	updateListener: (kind: 'task' | 'execution', index: number, patch: Partial<FlowableListener>) => void;
	addListener: (kind: 'task' | 'execution') => void;
	removeListener: (kind: 'task' | 'execution', index: number) => void;
	updateFormProperty: (index: number, patch: Partial<FlowableFormProperty>) => void;
	addFormProperty: () => void;
	removeFormProperty: (index: number) => void;
	updateConditionExpression: (value: string) => void;
	updateScript: (value: string) => void;
	updateMultiInstance: (patch: Partial<FlowableMultiInstance>) => void;
	removeMultiInstance: () => void;
	updateIOParameter: (kind: 'input' | 'output', index: number, patch: Partial<FlowableIOParameter>) => void;
	addIOParameter: (kind: 'input' | 'output') => void;
	removeIOParameter: (kind: 'input' | 'output', index: number) => void;
	updateTimerDefinition: (patch: Partial<FlowableTimerDefinition>) => void;
	updateErrorRef: (value: string) => void;
	updateSignalRef: (value: string) => void;
	updateMessageRef: (value: string) => void;
	updateTerminateAll: (checked: boolean) => void;
	updateCompensateActivityRef: (value: string) => void;
	updateIsForCompensation: (checked: boolean) => void;
	updateFailedJobRetryTimeCycle: (value: string) => void;
	updateDocumentation: (value: string) => void;
}
 
export function createElementActions(deps: ElementActionDeps): ElementActions {
	function getSelectedState(): FlowableElementState | undefined {
		const selectedElement = deps.getSelectedElement();
		return selectedElement ? deps.ensureElementState(selectedElement) : undefined;
	}
 
	function rerender(message: string): void {
		deps.queueMetadataSave();
		deps.setStatus(message);
		deps.renderProperties();
	}
 
	function withSelectedState(update: (elementState: FlowableElementState) => void): void {
		const elementState = getSelectedState();
		Iif (!elementState) {
			return;
		}
		update(elementState);
	}
 
	function updateAndRerender(update: (elementState: FlowableElementState) => void, message: string): void {
		withSelectedState((elementState) => {
			update(elementState);
			rerender(message);
		});
	}
 
	function setSavedStringProperty(
		key: SavedStringPropertyKey,
		value: string,
		message: string,
	): void {
		withSelectedState((elementState) => {
			elementState[key] = value;
			deps.queueMetadataSave();
			deps.setStatus(message);
		});
	}
 
	function updateGeneralProperty(property: 'id' | 'name', value: string): void {
		const selectedElement = deps.getSelectedElement();
		Iif (!selectedElement) {
			return;
		}
 
		Eif (property === 'id') {
			const nextId = value.trim();
			Iif (!nextId) {
				return;
			}
 
			const previousId = getElementId(selectedElement);
			try {
				deps.modeling.updateProperties(selectedElement, { id: nextId });
			} catch (error) {
				deps.setStatus(error instanceof Error ? error.message : 'Unable to update element ID', 'error');
				deps.renderProperties();
				return;
			}
 
			const appliedId = getElementId(selectedElement);
			if (appliedId !== nextId) {
				deps.setStatus('Element ID was not applied', 'error');
				deps.renderProperties();
				return;
			}
 
			deps.renameElementState(previousId, appliedId, getElementType(selectedElement));
			deps.queueMetadataSave();
			return;
		}
 
		deps.modeling.updateProperties(selectedElement, { name: value });
	}
 
	function updateFlowableAttribute(attribute: FlowableAttributeKey, value: string | boolean): void {
		let stringValue: string;
		if (typeof value === 'boolean') {
			Iif (value) {
				stringValue = 'true';
			} else {
				stringValue = attribute === 'exclusive' ? 'false' : '';
			}
		} else E{
			stringValue = value.trim();
		}
		updateAndRerender((elementState) => {
			Eif (stringValue) {
				elementState.activitiAttributes[attribute] = stringValue;
				return;
			}
			delete elementState.activitiAttributes[attribute];
		}, 'Flowable properties updated');
	}
 
	function updateFieldExtension(index: number, patch: Partial<FlowableFieldExtension>): void {
		updateAndRerender((elementState) => {
			elementState.fieldExtensions = elementState.fieldExtensions.map((fieldExtension, fieldIndex) => {
				return fieldIndex === index ? { ...fieldExtension, ...patch } : fieldExtension;
			});
		}, 'Field extensions updated');
	}
 
	function removeFieldExtension(index: number): void {
		updateAndRerender((elementState) => {
			elementState.fieldExtensions = elementState.fieldExtensions.filter((_, fieldIndex) => fieldIndex !== index);
		}, 'Field extension removed');
	}
 
	function addFieldExtension(): void {
		updateAndRerender((elementState) => {
			deps.counters.field += 1;
			elementState.fieldExtensions = [...elementState.fieldExtensions, createFieldExtension(deps.counters.field)];
		}, 'Field extension added');
	}
 
	function updateListener(kind: 'task' | 'execution', index: number, patch: Partial<FlowableListener>): void {
		const key = getListenerKey(kind);
		updateAndRerender((elementState) => {
			elementState[key] = elementState[key].map((listener, listenerIndex) => {
				return listenerIndex === index ? { ...listener, ...patch } : listener;
			});
		}, 'Listener updated');
	}
 
	function addListener(kind: 'task' | 'execution'): void {
		const key = getListenerKey(kind);
		updateAndRerender((elementState) => {
			deps.counters.listener += 1;
			elementState[key] = [...elementState[key], createListener(kind, deps.counters.listener)];
		}, 'Listener added');
	}
 
	function removeListener(kind: 'task' | 'execution', index: number): void {
		const key = getListenerKey(kind);
		updateAndRerender((elementState) => {
			elementState[key] = elementState[key].filter((_, listenerIndex) => listenerIndex !== index);
		}, 'Listener removed');
	}
 
	function updateFormProperty(index: number, patch: Partial<FlowableFormProperty>): void {
		updateAndRerender((elementState) => {
			elementState.formProperties = elementState.formProperties.map((formProperty, formPropertyIndex) => {
				return formPropertyIndex === index ? { ...formProperty, ...patch } : formProperty;
			});
		}, 'Form property updated');
	}
 
	function addFormProperty(): void {
		updateAndRerender((elementState) => {
			deps.counters.formProperty += 1;
			elementState.formProperties = [...elementState.formProperties, createFormProperty(deps.counters.formProperty)];
		}, 'Form property added');
	}
 
	function removeFormProperty(index: number): void {
		updateAndRerender((elementState) => {
			elementState.formProperties = elementState.formProperties.filter((_, formPropertyIndex) => formPropertyIndex !== index);
		}, 'Form property removed');
	}
 
	function updateConditionExpression(value: string): void {
		setSavedStringProperty('conditionExpression', value, 'Condition expression updated');
	}
 
	function updateScript(value: string): void {
		setSavedStringProperty('script', value, 'Script updated');
	}
 
	function updateMultiInstance(patch: Partial<FlowableMultiInstance>): void {
		const elementState = getSelectedState();
		if (!elementState) {
			return;
		}
		elementState.multiInstance ??= {
			sequential: false,
			loopCardinality: '',
			collection: '',
			elementVariable: '',
			completionCondition: '',
		};
		Object.assign(elementState.multiInstance, patch);
		rerender('Multi-instance updated');
	}
 
	function removeMultiInstance(): void {
		updateAndRerender((elementState) => {
			elementState.multiInstance = null;
		}, 'Multi-instance removed');
	}
 
	function updateIOParameter(kind: 'input' | 'output', index: number, patch: Partial<FlowableIOParameter>): void {
		const key = getParameterKey(kind);
		updateAndRerender((elementState) => {
			elementState[key] = elementState[key].map((parameter, parameterIndex) => {
				return parameterIndex === index ? { ...parameter, ...patch } : parameter;
			});
		}, 'IO parameter updated');
	}
 
	function addIOParameter(kind: 'input' | 'output'): void {
		const key = getParameterKey(kind);
		updateAndRerender((elementState) => {
			deps.counters.ioParameter += 1;
			elementState[key] = [...elementState[key], createIOParameter(deps.counters.ioParameter)];
		}, 'IO parameter added');
	}
 
	function removeIOParameter(kind: 'input' | 'output', index: number): void {
		const key = getParameterKey(kind);
		updateAndRerender((elementState) => {
			elementState[key] = elementState[key].filter((_, parameterIndex) => parameterIndex !== index);
		}, 'IO parameter removed');
	}
 
	function updateTimerDefinition(patch: Partial<FlowableTimerDefinition>): void {
		const elementState = getSelectedState();
		if (!elementState) {
			return;
		}
		elementState.timerDefinition ??= { type: 'timeDuration', value: '' };
		Object.assign(elementState.timerDefinition, patch);
		rerender('Timer definition updated');
	}
 
	function updateErrorRef(value: string): void {
		setSavedStringProperty('errorRef', value, 'Error reference updated');
	}
 
	function updateSignalRef(value: string): void {
		setSavedStringProperty('signalRef', value, 'Signal reference updated');
	}
 
	function updateMessageRef(value: string): void {
		setSavedStringProperty('messageRef', value, 'Message reference updated');
	}
 
	function updateTerminateAll(checked: boolean): void {
		setSavedStringProperty('terminateAll', checked ? 'true' : '', 'Terminate all updated');
	}
 
	function updateCompensateActivityRef(value: string): void {
		setSavedStringProperty('compensateActivityRef', value, 'Compensate activity reference updated');
	}
 
	function updateIsForCompensation(checked: boolean): void {
		setSavedStringProperty('isForCompensation', checked ? 'true' : '', 'Is for compensation updated');
	}
 
	function updateFailedJobRetryTimeCycle(value: string): void {
		setSavedStringProperty('failedJobRetryTimeCycle', value, 'Failed job retry time cycle updated');
	}
 
	function updateDocumentation(value: string): void {
		setSavedStringProperty('documentation', value, 'Documentation updated');
	}
 
	return {
		updateGeneralProperty,
		updateFlowableAttribute,
		updateFieldExtension,
		removeFieldExtension,
		addFieldExtension,
		updateListener,
		addListener,
		removeListener,
		updateFormProperty,
		addFormProperty,
		removeFormProperty,
		updateConditionExpression,
		updateScript,
		updateMultiInstance,
		removeMultiInstance,
		updateIOParameter,
		addIOParameter,
		removeIOParameter,
		updateTimerDefinition,
		updateErrorRef,
		updateSignalRef,
		updateMessageRef,
		updateTerminateAll,
		updateCompensateActivityRef,
		updateIsForCompensation,
		updateFailedJobRetryTimeCycle,
		updateDocumentation,
	};
}