diff --git a/src/lib/decompositions.ts b/src/lib/decompositions.ts index 135ba7232e8fbd3c3c7fe831a81b9608a2254c84..b5dcef1415b0c37041215b995e5f676309ac6b7b 100644 --- a/src/lib/decompositions.ts +++ b/src/lib/decompositions.ts @@ -1417,6 +1417,112 @@ function buildVisibleDecompositionsWithoutValues1( return visibleDecompositionIndex } +export function buildWaterfallDecompositions( + decompositionByName: DecompositionByName, + variableSummaryByName: VariableByName, + waterfall: Waterfall, + year: number, +): VisibleDecomposition[] { + const visibleDecompositions: VisibleDecomposition[] = [] + buildWaterfallDecompositions1( + decompositionByName, + variableSummaryByName, + waterfall.name, + waterfall.root, + 0, + false, + true, + visibleDecompositions, + year, + ) + return visibleDecompositions +} + +function buildWaterfallDecompositions1( + decompositionByName: DecompositionByName, + variableSummaryByName: VariableByName, + waterfallName: string, + name: string, + depth: number, + negate: boolean, + trunk = true, + visibleDecompositions: VisibleDecomposition[], + year: number, +): number { + const decomposition = decompositionByName[name] + if (decomposition === undefined) { + return -1 + } + + let visibleDecompositionIndex = -1 + // If we need to show less variables, then we should pass + // a situation and handle it here + const visibleDecomposition = { + decomposition, + depth, + trunk, + } as VisibleDecomposition + + let visibleChildren = decomposition.children + for (const options of decomposition.options ?? []) { + if (options.waterfall !== undefined) { + if ( + (options as WaterfallOptions).then?.children !== undefined && + (options as WaterfallOptions).waterfall.includes(waterfallName) + ) { + visibleChildren = + (options as WaterfallOptions).then.children ?? undefined + } else if ( + (options as WaterfallOptions).else?.children !== undefined && + !(options as WaterfallOptions).waterfall.includes(waterfallName) + ) { + visibleChildren = + (options as WaterfallOptions).else.children ?? undefined + } + } + } + if (visibleChildren !== undefined) { + visibleDecomposition.visibleChildren = visibleChildren + } + let childrenDepth = depth + if (!trunk) { + visibleDecompositionIndex = visibleDecompositions.length + visibleDecompositions.push(visibleDecomposition) + childrenDepth = depth + 1 + } + if (visibleChildren !== undefined) { + const beforeChildrenVisibleDecompositionLength = + visibleDecompositions.length + for (const childReference of visibleChildren) { + buildWaterfallDecompositions1( + decompositionByName, + variableSummaryByName, + waterfallName, + childReference.name, + childrenDepth, + Boolean(negate ? !childReference.negate : childReference.negate), + trunk && + visibleDecompositions.length === + beforeChildrenVisibleDecompositionLength, + visibleDecompositions, + year, + ) + } + } + if (trunk) { + visibleDecompositionIndex = visibleDecompositions.length + visibleDecompositions.push(visibleDecomposition) + } + + const variable = variableSummaryByName[name] + if (variable !== undefined) { + visibleDecomposition.variable = variable + } + + + return visibleDecompositionIndex +} + function extractLinkedVariablesName( linkedVariablesName: Set<string>, name: string, @@ -1592,6 +1698,23 @@ export function getDecompositionParentName( )?.name } +export function getNonTrunkParentInVisibleDecompositions( + visibleDecompositions: VisibleDecomposition[], + variableName: string, +): string | undefined { + for (const decomposition of visibleDecompositions) { + if (decomposition.visibleChildren !== undefined) { + for (const child of decomposition.visibleChildren) { + if (child.name === variableName && !decomposition.trunk) { + return decomposition.variable!.name + } + } + } + } + return undefined +} + + function patchDecompositionCoreByName( decompositionCoreByName: DecompositionCoreByName, patch: { [name: string]: DecompositionCore | null }, diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index af48d771610a27cec08126a4608fec3cbcfe3afc..e5a64ea6643f65d8a95dcbd41d840270cab79750 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -50,9 +50,12 @@ import WaterfallPlainView from "$lib/components/WaterfallPlainView.svelte" import WithoutBudgetCard from "$lib/components/budget/WithoutBudgetCard.svelte" import { + buildWaterfallDecompositions, getDecompositionParentName, getLatestCalculation, + getNonTrunkParentInVisibleDecompositions, waterfalls, + type VisibleDecomposition, } from "$lib/decompositions" import type { DisplayMode } from "$lib/displays" import { entityByKey } from "$lib/entities" @@ -202,6 +205,27 @@ ], }, ] + + const allWaterfallsDecompositions = waterfalls.reduce( + (acc, waterfall) => [ + ...acc, + ...buildWaterfallDecompositions( + shared.decompositionByName, + variableSummaryByName, + waterfall, + year, + ), + ], + [] as VisibleDecomposition[], // Typage du tableau résultant + ) + + let parameterParentName = $derived( + getNonTrunkParentInVisibleDecompositions( + allWaterfallsDecompositions, + displayMode.parametersVariableName!, + ), + ) + const formatLongOrdinalSup = (n: number) => { const rule = ordinalPluralRules.select(n) const suffix = longOrdinalSuffixes.get(rule) @@ -241,7 +265,6 @@ let testCaseSharingModal: { open: boolean; token?: string } = $state({ open: false, }) - let variablesHistory: string[] = $state([]) let windowInnerWidth: number | undefined = $state(undefined) let saveButtonClicked: boolean = $state(false) @@ -662,18 +685,6 @@ } } - function updateHistory(displayMode: DisplayMode) { - if ( - displayMode.parametersVariableName !== undefined && - variablesHistory[variablesHistory.length - 1] !== - displayMode.parametersVariableName - ) { - variablesHistory = [ - ...variablesHistory, - displayMode.parametersVariableName, - ] - } - } let { user } = $derived(data) if ($page.url.searchParams || $page.url.hash) { ensureValidDisplayMode($page.url.searchParams, $page.url.hash) @@ -1086,16 +1097,20 @@ <!-- Vue modification de la loi --> {:else} <div class="mb-4 flex justify-between"> - {#if variablesHistory.length >= 1} + {#if displayMode.parametersVariableName !== undefined && parameterParentName !== undefined} + {@const parentShortLabel = parameterParentName + ? (shared.decompositionByName[parameterParentName] + .short_label ?? + shared.decompositionByName[parameterParentName].label) + : undefined} + <button class="flex items-center gap-2 rounded-lg bg-white px-3 py-1.5 text-sm uppercase tracking-wider text-neutral-600 transition-all duration-200 ease-out-back hover:bg-neutral-100 hover:text-black active:bg-neutral-200" onclick={() => { - variablesHistory.pop() - const previous = variablesHistory.pop() goto( newSimulationUrl({ ...displayMode, - parametersVariableName: previous, + parametersVariableName: parameterParentName, tab: "fiche_de_paie", }), ) @@ -1104,7 +1119,7 @@ <iconify-icon class="align-[-0.23rem] text-xl" icon="ri-arrow-left-line" - ></iconify-icon>Retour + ></iconify-icon>{parentShortLabel} </button> {/if}