PlotDirector/PlotLine/wwwroot/js/application-shell.js

79 lines
4.4 KiB
JavaScript

(() => {
const shell = document.querySelector('[data-ribbon-shell]');
if (!shell) return;
const appearance = window.plotdirectorAppearance || { theme: 'plotdirector', migrate: false };
const form = shell.querySelector('[data-appearance-form]');
const status = shell.querySelector('[data-appearance-status]');
const tabs = [...shell.querySelectorAll('[data-ribbon-tab]')];
const surface = shell.querySelector('#ribbon-surface');
const collapse = shell.querySelector('[data-ribbon-collapse]');
const choices = [...shell.querySelectorAll('[data-app-theme-choice]')];
let theme = appearance.theme, busy = false;
function applyTheme(value) {
theme = value;
// Non-sensitive device preference keeps installed reauthentication visually continuous.
try { localStorage.setItem("plotdirector.applicationTheme", value); } catch { }
document.documentElement.dataset.appTheme = value;
document.documentElement.dataset.theme = value.endsWith('-dark') ? 'dark' : 'light';
document.documentElement.dataset.bsTheme = document.documentElement.dataset.theme;
choices.forEach(button => button.setAttribute('aria-pressed', String(button.dataset.appThemeChoice === value)));
window.dispatchEvent(new CustomEvent('plotdirector:theme-changed', { detail: { theme: value } }));
}
async function save(url, values) {
const data = new FormData(form);
Object.entries(values).forEach(([key, value]) => data.append(key, String(value)));
const response = await fetch(url, { method: 'POST', body: data, headers: { 'X-Requested-With': 'XMLHttpRequest' } });
if (!response.ok || response.redirected) throw new Error('Could not save appearance. Please try again.');
return response;
}
async function choose(value, migrate = false) {
if (busy) return;
busy = true;
const previous = theme;
applyTheme(value);
choices.forEach(button => button.disabled = true);
status.textContent = '';
try {
const result = await (await save(form.dataset.themeUrl, { themeKey: value, onlyIfUnset: migrate })).json();
applyTheme(result.themeKey);
status.textContent = migrate ? '' : 'Theme saved';
} catch (error) { applyTheme(previous); status.textContent = error.message; }
finally { busy = false; choices.forEach(button => button.disabled = false); }
}
applyTheme(theme);
choices.forEach(button => button.addEventListener('click', () => choose(button.dataset.appThemeChoice)));
if (appearance.migrate) choose(theme, true);
function setCollapsed(value) {
shell.dataset.collapsed = String(value);
surface.hidden = value;
collapse.setAttribute('aria-expanded', String(!value));
window.dispatchEvent(new CustomEvent('plotdirector:ribbon-layout-changed'));
collapse.innerHTML = value ? 'Expand ribbon <span aria-hidden="true">⌄</span>' : 'Collapse ribbon <span aria-hidden="true">⌃</span>';
}
function select(tab) {
tabs.forEach(item => { const selected = item === tab; item.setAttribute('aria-selected', String(selected)); item.tabIndex = selected ? 0 : -1; });
shell.querySelectorAll('[role="tabpanel"]').forEach(panel => panel.hidden = panel.id !== tab.getAttribute('aria-controls'));
// A collapsed preference stays collapsed on the next page; selecting a category opens it temporarily.
setCollapsed(false);
}
tabs.forEach((tab, index) => {
tab.addEventListener('click', () => select(tab));
tab.addEventListener('keydown', event => {
let next;
if (event.key === 'ArrowRight') next = (index + 1) % tabs.length;
if (event.key === 'ArrowLeft') next = (index + tabs.length - 1) % tabs.length;
if (event.key === 'Home') next = 0;
if (event.key === 'End') next = tabs.length - 1;
if (next === undefined) return;
event.preventDefault(); tabs[next].focus(); select(tabs[next]);
});
});
collapse.addEventListener('click', async () => {
const previous = surface.hidden;
setCollapsed(!previous); collapse.disabled = true;
try { await save(form.dataset.ribbonUrl, { collapsed: !previous }); }
catch (error) { setCollapsed(previous); status.textContent = error.message; collapse.title = error.message; }
finally { collapse.disabled = false; }
});
})();