Files
compras/resources/js/lib/preparation-forms.ts
T
nala 448948a487
tests / ci (push) Waiting to run
commit compras1 2911
2026-11-29 05:28:30 +02:00

43 lines
1.4 KiB
TypeScript

export function markPreparationDirty(
form: HTMLFormElement,
dirty: boolean,
managed = false,
): void {
if (managed) form.setAttribute('data-preparation-managed', 'true');
// Explicit query forms change consultation criteria, not a business preparation.
form.setAttribute(
'data-preparation-dirty',
dirty && form.getAttribute('data-query-form') !== 'true'
? 'true'
: 'false',
);
}
/** A form that publishes its own dirty state owns clearing and restoring it. */
export function usesGenericPreparationTracking(form: HTMLFormElement): boolean {
return (
form.getAttribute('data-query-form') !== 'true' &&
form.getAttribute('data-preparation-managed') !== 'true'
);
}
/** Recover restored preparations even when their effect ran before the guard subscribed. */
export function connectedDirtyForms(
tracked: Iterable<HTMLFormElement>,
root: Pick<Document, 'querySelectorAll'> = document,
): HTMLFormElement[] {
return [
...new Set([
...tracked,
...root.querySelectorAll<HTMLFormElement>(
'form[data-preparation-dirty="true"]',
),
]),
].filter(
(form) =>
form.isConnected &&
form.getAttribute('data-query-form') !== 'true' &&
form.getAttribute('data-preparation-dirty') !== 'false',
);
}