| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- /* Return state is per tab and user; only URLs and invoice keys are stored. */
- (() => {
- const listPath = '/app/forderungen/liste';
- let memory;
- const processed = new WeakSet();
- const storageKey = () => `gc-list-return:${document.body.dataset.gcUser || ''}`;
- const canonical = value => {
- const url = new URL(value, location.origin);
- url.searchParams.delete('focus');
- url.searchParams.delete('page');
- url.searchParams.delete('limit');
- url.searchParams.sort();
- return url.pathname + url.search;
- };
- const read = () => {
- try { return JSON.parse(sessionStorage.getItem(storageKey())) || memory; }
- catch { return memory; }
- };
- const clear = () => {
- memory = undefined;
- try { sessionStorage.removeItem(storageKey()); } catch { /* Storage can be disabled. */ }
- };
- function remember(value) {
- if (!value) return;
- const url = new URL(value, location.origin);
- if (url.origin !== location.origin || url.pathname !== listPath) return;
- memory = { returnUrl: url.pathname + url.search, listUrl: canonical(url), user: document.body.dataset.gcUser || '' };
- try { sessionStorage.setItem(storageKey(), JSON.stringify(memory)); } catch { /* Use document memory. */ }
- }
- function refreshReturn(event) {
- if (location.pathname !== listPath) return false;
- const state = read();
- if (!state || state.user !== (document.body.dataset.gcUser || '') || state.listUrl !== canonical(location.href)) return false;
- // Do not let HTMX paint an old history snapshot before the fresh GET.
- event?.stopImmediatePropagation();
- const url = new URL(state.returnUrl, location.origin);
- if (url.origin !== location.origin || url.pathname !== listPath) { clear(); return false; }
- location.replace(url.pathname + url.search);
- return true;
- }
- async function initialize() {
- const back = document.querySelector('[data-gc-return-url]');
- if (back) remember(back.dataset.gcReturnUrl);
- const state = document.getElementById('gc-list-state');
- if (!state || processed.has(state)) return;
- processed.add(state);
- const focus = state.dataset.focus;
- if (!focus) return;
- clear();
- const row = [...document.querySelectorAll('.gc-invoice-row[data-primary-key]')]
- .find(node => node.dataset.primaryKey === focus);
- if (!row) return;
- await Promise.race([document.fonts?.ready, new Promise(resolve => setTimeout(resolve, 500))]);
- await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
- if (!row.isConnected) return;
- const top = (document.querySelector('.gc-app-header')?.offsetHeight || 0)
- + (document.querySelector('#invoice-list thead')?.offsetHeight || 0) + 16;
- const bottom = (document.querySelector('.gc-list-summary')?.offsetHeight || 0) + 16;
- const rect = row.getBoundingClientRect();
- row.focus({ preventScroll: true });
- window.scrollTo({ top: Math.max(0, scrollY + rect.top - top - Math.max(0, (innerHeight - top - bottom - rect.height) / 2)), behavior: 'instant' });
- row.classList.add('gc-return-highlight');
- setTimeout(() => row.classList.remove('gc-return-highlight'), 3000);
- }
- document.addEventListener('click', event => {
- const link = event.target.closest('[data-gc-detail-link]');
- // New tabs receive their own return_to URL; leave this tab's state alone.
- if (link && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey && link.target !== '_blank') remember(link.dataset.returnUrl);
- });
- window.addEventListener('popstate', refreshReturn, { capture: true });
- window.addEventListener('pageshow', event => {
- if (!event.persisted || !refreshReturn(event)) initialize();
- });
- document.addEventListener('DOMContentLoaded', initialize);
- document.addEventListener('htmx:load', initialize);
- document.addEventListener('htmx:historyRestore', event => {
- if (!refreshReturn(event)) initialize();
- }, { capture: true });
- })();
|