Fix menu link click error and charts failing after the JSON editor

- NavMenu: drop CustomToggle="true" (left over from the project
  template). It made every nav link run
  document.getElementById('navmenu-toggle').click(), but there is no
  such element, so each click threw a TypeError.
- charts.js: Monaco's AMD loader (JSON editor) leaves a global define()
  and pages switch without a full reload. Chart.js and the treemap plugin
  are UMD bundles that then register as AMD modules instead of setting
  window.Chart, so Analyse, Strom and Wasser failed with "Cannot read
  properties of undefined (reading 'defaults')" and the circuit was
  terminated. define is now hidden while they load, through one shared
  loading promise so concurrent charts can't restore it wrongly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claudio Schaad 2026-10-30 22:21:08 +01:00
parent 651586c4f9
commit c07fd2893e
3 changed files with 34 additions and 19 deletions

View file

@ -77,7 +77,7 @@ Pages live in `Schaad.Accounting.UI/Components/Pages/`. Each page typically has
- `Microsoft.FluentUI.AspNetCore.Components` — UI components (FluentDataGrid, FluentDialog, etc.) - `Microsoft.FluentUI.AspNetCore.Components` — UI components (FluentDataGrid, FluentDialog, etc.)
- `Plotly.Blazor` — charts on the older chart pages - `Plotly.Blazor` — charts on the older chart pages
- `BlazorMonaco` — Monaco editor for the JSON editor page; loaded on demand by `wwwroot/js/monaco.js` instead of script tags in `App.razor`. Pages are interactive one by one, so `NavigationManager.NavigateTo` and menu links run enhanced navigation, which syncs `<head>` (removing Monaco's styles; `monaco.js` puts them back) and re-renders the page on the server. The editor therefore switches files in place and only rewrites the URL (`wwwroot/js/url.js`) - `BlazorMonaco` — Monaco editor for the JSON editor page; loaded on demand by `wwwroot/js/monaco.js` instead of script tags in `App.razor`. Pages are interactive one by one, so `NavigationManager.NavigateTo` and menu links run enhanced navigation, which syncs `<head>` (removing Monaco's styles; `monaco.js` puts them back) and re-renders the page on the server. The editor therefore switches files in place and only rewrites the URL (`wwwroot/js/url.js`)
- Chart.js 4 + `chartjs-chart-treemap` — charts on the Analyse page; vendored in `wwwroot/lib/` (no CDN), wrapped by `wwwroot/js/charts.js` and `ChartJsChart.razor` - Chart.js 4 + `chartjs-chart-treemap` — charts on the Analyse page; vendored in `wwwroot/lib/` (no CDN), wrapped by `wwwroot/js/charts.js` and `ChartJsChart.razor`; `charts.js` hides Monaco's global AMD `define` while loading them, otherwise the UMD bundles register as AMD modules and `window.Chart` stays undefined after visiting the JSON editor
- `FreeSpire.PDF` — PDF parsing for bank statement imports - `FreeSpire.PDF` — PDF parsing for bank statement imports
- `Schaad.Finance.dll` / `Schaad.Finance.Api.dll` — proprietary DLLs for MT940/CAMT053 parsing and FX rate lookups (FixerIo API key in `appsettings.Development.json`) - `Schaad.Finance.dll` / `Schaad.Finance.Api.dll` — proprietary DLLs for MT940/CAMT053 parsing and FX rate lookups (FixerIo API key in `appsettings.Development.json`)

View file

@ -5,7 +5,7 @@
<div class="navmenu hidePrint"> <div class="navmenu hidePrint">
<nav class="sitenav" aria-labelledby="main-menu"> <nav class="sitenav" aria-labelledby="main-menu">
<FluentNavMenu Id="main-menu" Collapsible="true" Width="250" Title="Navigation menu" @bind-Expanded="expanded" CustomToggle="true"> <FluentNavMenu Id="main-menu" Collapsible="true" Width="250" Title="Navigation menu" @bind-Expanded="expanded">
<FluentNavLink Href="/" Match="NavLinkMatch.All" Icon="@(new Size20.Home())" IconColor="Color.Accent">Home</FluentNavLink> <FluentNavLink Href="/" Match="NavLinkMatch.All" Icon="@(new Size20.Home())" IconColor="Color.Accent">Home</FluentNavLink>
<FluentNavLink Href="transactions" Icon="@(new Size20.NumberSymbolSquare())" IconColor="Color.Accent">Transaktionen</FluentNavLink> <FluentNavLink Href="transactions" Icon="@(new Size20.NumberSymbolSquare())" IconColor="Color.Accent">Transaktionen</FluentNavLink>
<FluentNavLink Href="banktransactions" Icon="@(new Size20.BuildingBank())" IconColor="Color.Accent">Bank Transaktionen</FluentNavLink> <FluentNavLink Href="banktransactions" Icon="@(new Size20.BuildingBank())" IconColor="Color.Accent">Bank Transaktionen</FluentNavLink>

View file

@ -1,28 +1,43 @@
// Thin Chart.js wrapper for Blazor (see ChartJsChart.razor). Chart.js and the treemap plugin are // Thin Chart.js wrapper for Blazor (see ChartJsChart.razor). Chart.js and the treemap plugin are
// loaded on first use from wwwroot/lib, so pages without charts don't pay for them. // loaded on first use from wwwroot/lib, so pages without charts don't pay for them.
const scripts = new Map(); let chartJsLoading;
let treemapLoading;
function loadScript(src) { function loadScript(src) {
if (!scripts.has(src)) { return new Promise((resolve, reject) => {
scripts.set(src, new Promise((resolve, reject) => {
const script = document.createElement('script'); const script = document.createElement('script');
script.src = src; script.src = src;
script.onload = resolve; script.onload = resolve;
script.onerror = () => reject(new Error(`${src} could not be loaded`)); script.onerror = () => reject(new Error(`${src} could not be loaded`));
document.head.appendChild(script); document.head.appendChild(script);
})); });
}
return scripts.get(src);
} }
async function loadChartJs(kind) { // The JSON editor's Monaco loader (js/monaco.js) leaves a global AMD define() in the window, and
await loadScript('lib/chartjs/chart.umd.min.js'); // pages switch without a full reload. Chart.js and the treemap plugin are UMD bundles that prefer
if (kind === 'treemap') { // define() when it exists, registering as AMD modules instead of setting window.Chart, so hide it
// Registers itself with the global Chart. // while they load. Each script loads only once (see loadChartJs), so this never overlaps with
await loadScript('lib/chartjs-chart-treemap/chartjs-chart-treemap.min.js'); // itself and always restores Monaco's define.
async function loadUmdScript(src) {
const define = window.define;
window.define = undefined;
try {
await loadScript(src);
} finally {
window.define = define;
} }
window.Chart.defaults.font.family = theme.font; }
function loadChartJs(kind) {
chartJsLoading ??= loadUmdScript('lib/chartjs/chart.umd.min.js')
.then(() => { window.Chart.defaults.font.family = theme.font; });
if (kind !== 'treemap') {
return chartJsLoading;
}
// Registers itself with the global Chart.
treemapLoading ??= chartJsLoading.then(() => loadUmdScript('lib/chartjs-chart-treemap/chartjs-chart-treemap.min.js'));
return treemapLoading;
} }
// Same look as ChartTheme.cs. // Same look as ChartTheme.cs.