AccountingNext/Schaad.Accounting.UI/wwwroot/js/unsaved-guard.js
Claudio Schaad 0c6e78b0d8 Add JSON editor for the data files
New page Stammdaten -> JSON-Editor edits the data files of the selected year
and mandator (default Transactions.json) as plain JSON in Monaco (BlazorMonaco
3.5.0, loaded on demand): highlighting, folding, live syntax errors, search
and replace with regex, format.

RawDataRepository saves only validated text: strict JSON (unknown properties
rejected with line/column), unique non-empty Ids, transactions referencing
existing accounts, accounts still used by transactions not removed. It refuses
if the file changed since loading, backs up the previous version to
backup/{mandator}/{year}/json-{timestamp}/, and saves through BaseRepository
so caches are refreshed.

- Hub MaximumReceiveMessageSize raised to 8 MB (files are several hundred KB)
- unsaved-guard.js asks before leaving with unsaved changes; NavigationLock
  doesn't apply because routing between per-page interactive pages is static

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 19:52:20 +02:00

44 lines
1.5 KiB
JavaScript

// Asks before leaving a page with unsaved changes. Blazor's NavigationLock can't do this here:
// pages are interactive one by one, so navigation between them runs through the static router
// (enhanced navigation), which doesn't consult interactive components.
let message = null;
function onClick(event) {
if (message === null || event.defaultPrevented || event.button !== 0
|| event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) {
return;
}
const link = event.target.closest?.('a[href]');
if (!link || link.target === '_blank' || link.hasAttribute('download')) {
return;
}
const url = new URL(link.href, document.baseURI);
if (url.origin !== location.origin || (url.pathname === location.pathname && url.search === location.search)) {
return;
}
if (confirm(message)) {
// Leaving for good; don't ask a second time on unload.
message = null;
} else {
// Runs in the capture phase, before Blazor's own link handling.
event.preventDefault();
event.stopImmediatePropagation();
}
}
function onBeforeUnload(event) {
if (message !== null) {
// Browsers show their own text; the call just has to ask.
event.preventDefault();
event.returnValue = '';
}
}
document.addEventListener('click', onClick, true);
window.addEventListener('beforeunload', onBeforeUnload);
/** Ask with this message before leaving the page; null stops asking. */
export function set(newMessage) {
message = newMessage ?? null;
}