PlotDirector/PlotLine/wwwroot/js/application-pwa.js
Nick Beckley e48889ac05 Complete desktop workspaces, context menus and plot health
Commit the pending application, help and browser regression work. Pin the patched PKCS dependency for the production release.
2026-10-31 09:27:04 +00:00

89 lines
4.0 KiB
JavaScript

(() => {
const root = document.documentElement;
const installItem = document.querySelector('[data-pwa-install-item]');
const installButton = document.querySelector('[data-pwa-install]');
const status = document.querySelector('[data-pwa-status]');
const standalone = window.matchMedia('(display-mode: standalone)');
const isStandalone = () => window.PlotDirectorPwa.standalone;
const header = document.querySelector('.pd-application-header');
function updateStickyOffset() {
if (header && isStandalone()) root.style.setProperty('--pd-sticky-height', `${header.getBoundingClientRect().height}px`);
else root.style.removeProperty('--pd-sticky-height');
}
function prepareStickyChrome() {
if (header && isStandalone()) {
// Escape the sticky stacking context so dialogs stay above their backdrops.
header.querySelectorAll('.modal').forEach(dialog => {
if (dialog.closest('.pd-shell')) dialog.classList.add('pd-shell-dialog');
document.body.appendChild(dialog);
});
}
updateStickyOffset();
}
if (header) new ResizeObserver(updateStickyOffset).observe(header);
standalone.addEventListener('change', prepareStickyChrome);
prepareStickyChrome();
let installPrompt = null;
let installedThisSession = false;
function updateMode() {
const installed = isStandalone();
if (installItem) installItem.hidden = installed || installedThisSession;
if (installButton) {
installButton.textContent = 'PlotDirector Desktop';
if (installPrompt) installButton.removeAttribute('aria-haspopup');
else installButton.setAttribute('aria-haspopup', 'dialog');
}
}
function updateTheme() {
// The shell's tokens remain the only palette; theme changes include save rollback.
const color = getComputedStyle(root).getPropertyValue('--pd-chrome').trim();
if (color) document.querySelector('meta[name="theme-color"]')?.setAttribute('content', color);
}
window.addEventListener('plotdirector:theme-changed', updateTheme);
standalone.addEventListener('change', updateMode);
window.addEventListener('beforeinstallprompt', event => {
if (!installButton) return;
event.preventDefault();
installPrompt = event;
updateMode();
});
window.addEventListener('appinstalled', () => {
installedThisSession = true;
installPrompt = null;
updateMode();
if (status) status.textContent = 'PlotDirector installed';
});
window.PlotDirectorInstall = {
get installed() { return installedThisSession; },
async request() {
if (!installPrompt) return 'unavailable';
const prompt = installPrompt;
installPrompt = null;
updateMode();
try {
await prompt.prompt();
return (await prompt.userChoice).outcome;
} catch { return 'unavailable'; }
}
};
installButton?.addEventListener('click', () => window.PlotDirectorDesktop?.open(true));
updateMode();
updateTheme();
if ('serviceWorker' in navigator && window.isSecureContext) {
navigator.serviceWorker.register('/service-worker.js', { scope: '/', updateViaCache: 'none' })
.then(registration => {
// Each full navigation checks for updates; long-lived editors check on return.
// No controllerchange reload: unsaved work must remain undisturbed.
let checkedAt = Date.now();
registration.update().catch(() => {});
document.addEventListener('visibilitychange', () => {
if (!document.hidden && Date.now() - checkedAt > 60 * 60 * 1000) {
checkedAt = Date.now();
registration.update().catch(() => {});
}
});
})
.catch(() => { /* Installation enhancement must never block authoring. */ });
}
})();