All files / src/web/webview processScoped.ts

100% Statements 36/36
90.9% Branches 20/22
100% Functions 10/10
100% Lines 32/32

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      1x 1x 1x               35x               2x                 14x 14x 27x 7x   20x 8x   12x   6x                   2x 2x 2x 6x 1x   5x 5x     2x 1x     1x 1x 1x   2x 3x     2x       1x 3x 1x     2x          
import type { FlowableDocumentState } from '../flowable/types';
 
export function getKnownProcessIds(flowableState: Pick<FlowableDocumentState, 'elements'>): string[] {
	return Object.values(flowableState.elements)
		.filter((elementState) => elementState.type === 'bpmn:Process' || elementState.type === 'process')
		.map((elementState) => elementState.id);
}
 
export function isProcessScopedItemMatch(
	processId: string | undefined,
	selectedProcessId: string,
	knownProcessIds: string[],
): boolean {
	return processId === selectedProcessId || (!processId && knownProcessIds.length <= 1);
}
 
export function getProcessScopedItems<T extends { processId?: string }>(
	items: T[],
	processId: string,
	knownProcessIds: string[],
): T[] {
	return items.filter((item) => isProcessScopedItemMatch(item.processId, processId, knownProcessIds));
}
 
export function getProcessScopedGlobalIndex<T extends { processId?: string }>(
	items: T[],
	processId: string,
	scopedIndex: number,
	knownProcessIds: string[],
): number {
	let currentScopedIndex = 0;
	for (let index = 0; index < items.length; index += 1) {
		if (!isProcessScopedItemMatch(items[index].processId, processId, knownProcessIds)) {
			continue;
		}
		if (currentScopedIndex === scopedIndex) {
			return index;
		}
		currentScopedIndex += 1;
	}
	return -1;
}
 
export function reorderProcessScopedItems<T extends { processId?: string }>(
	items: T[],
	processId: string,
	fromIndex: number,
	toIndex: number,
	knownProcessIds: string[],
): boolean {
	const scopedIndexes: number[] = [];
	const scopedItems: T[] = [];
	for (let index = 0; index < items.length; index += 1) {
		if (!isProcessScopedItemMatch(items[index].processId, processId, knownProcessIds)) {
			continue;
		}
		scopedIndexes.push(index);
		scopedItems.push(items[index]);
	}
 
	if (fromIndex < 0 || toIndex < 0 || fromIndex >= scopedItems.length || toIndex >= scopedItems.length || fromIndex === toIndex) {
		return false;
	}
 
	const reorderedItems = [...scopedItems];
	const [movedItem] = reorderedItems.splice(fromIndex, 1);
	reorderedItems.splice(fromIndex < toIndex ? toIndex - 1 : toIndex, 0, movedItem);
 
	scopedIndexes.forEach((globalIndex, scopedItemIndex) => {
		items[globalIndex] = reorderedItems[scopedItemIndex];
	});
 
	return true;
}
 
export function remapProcessScopedIds<T extends { processId?: string }>(items: T[], oldId: string, newId: string): void {
	for (let index = 0; index < items.length; index += 1) {
		if (items[index].processId !== oldId) {
			continue;
		}
 
		items[index] = {
			...items[index],
			processId: newId,
		};
	}
}