navigation.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Return state is per tab and user; only URLs and invoice keys are stored. */
  2. (() => {
  3. const listPath = '/app/forderungen/liste';
  4. let memory;
  5. const processed = new WeakSet();
  6. const storageKey = () => `gc-list-return:${document.body.dataset.gcUser || ''}`;
  7. const canonical = value => {
  8. const url = new URL(value, location.origin);
  9. url.searchParams.delete('focus');
  10. url.searchParams.delete('page');
  11. url.searchParams.delete('limit');
  12. url.searchParams.sort();
  13. return url.pathname + url.search;
  14. };
  15. const read = () => {
  16. try { return JSON.parse(sessionStorage.getItem(storageKey())) || memory; }
  17. catch { return memory; }
  18. };
  19. const clear = () => {
  20. memory = undefined;
  21. try { sessionStorage.removeItem(storageKey()); } catch { /* Storage can be disabled. */ }
  22. };
  23. function remember(value) {
  24. if (!value) return;
  25. const url = new URL(value, location.origin);
  26. if (url.origin !== location.origin || url.pathname !== listPath) return;
  27. memory = { returnUrl: url.pathname + url.search, listUrl: canonical(url), user: document.body.dataset.gcUser || '' };
  28. try { sessionStorage.setItem(storageKey(), JSON.stringify(memory)); } catch { /* Use document memory. */ }
  29. }
  30. function refreshReturn(event) {
  31. if (location.pathname !== listPath) return false;
  32. const state = read();
  33. if (!state || state.user !== (document.body.dataset.gcUser || '') || state.listUrl !== canonical(location.href)) return false;
  34. // Do not let HTMX paint an old history snapshot before the fresh GET.
  35. event?.stopImmediatePropagation();
  36. const url = new URL(state.returnUrl, location.origin);
  37. if (url.origin !== location.origin || url.pathname !== listPath) { clear(); return false; }
  38. location.replace(url.pathname + url.search);
  39. return true;
  40. }
  41. async function initialize() {
  42. const back = document.querySelector('[data-gc-return-url]');
  43. if (back) remember(back.dataset.gcReturnUrl);
  44. const state = document.getElementById('gc-list-state');
  45. if (!state || processed.has(state)) return;
  46. processed.add(state);
  47. const focus = state.dataset.focus;
  48. if (!focus) return;
  49. clear();
  50. const row = [...document.querySelectorAll('.gc-invoice-row[data-primary-key]')]
  51. .find(node => node.dataset.primaryKey === focus);
  52. if (!row) return;
  53. await Promise.race([document.fonts?.ready, new Promise(resolve => setTimeout(resolve, 500))]);
  54. await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
  55. if (!row.isConnected) return;
  56. const top = (document.querySelector('.gc-app-header')?.offsetHeight || 0)
  57. + (document.querySelector('#invoice-list thead')?.offsetHeight || 0) + 16;
  58. const bottom = (document.querySelector('.gc-list-summary')?.offsetHeight || 0) + 16;
  59. const rect = row.getBoundingClientRect();
  60. row.focus({ preventScroll: true });
  61. window.scrollTo({ top: Math.max(0, scrollY + rect.top - top - Math.max(0, (innerHeight - top - bottom - rect.height) / 2)), behavior: 'instant' });
  62. row.classList.add('gc-return-highlight');
  63. setTimeout(() => row.classList.remove('gc-return-highlight'), 3000);
  64. }
  65. document.addEventListener('click', event => {
  66. const link = event.target.closest('[data-gc-detail-link]');
  67. // New tabs receive their own return_to URL; leave this tab's state alone.
  68. if (link && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey && link.target !== '_blank') remember(link.dataset.returnUrl);
  69. });
  70. window.addEventListener('popstate', refreshReturn, { capture: true });
  71. window.addEventListener('pageshow', event => {
  72. if (!event.persisted || !refreshReturn(event)) initialize();
  73. });
  74. document.addEventListener('DOMContentLoaded', initialize);
  75. document.addEventListener('htmx:load', initialize);
  76. document.addEventListener('htmx:historyRestore', event => {
  77. if (!refreshReturn(event)) initialize();
  78. }, { capture: true });
  79. })();