43 lines
1.4 KiB
TypeScript
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',
|
|
);
|
|
}
|