From 675ca028188ef828be3f540c6aea0f236fd59a81 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Wed, 11 Mar 2026 22:57:54 +1100 Subject: [PATCH] Add diy replacement for turbo --- app/static/js/diy-turbo.js | 68 +++++++++++++++++++++++++ app/templates/_layout.html | 99 +++++++++++++++++++++++------------- app/templates/dashboard.html | 43 ++++++++-------- 3 files changed, 155 insertions(+), 55 deletions(-) create mode 100644 app/static/js/diy-turbo.js diff --git a/app/static/js/diy-turbo.js b/app/static/js/diy-turbo.js new file mode 100644 index 0000000..baf520b --- /dev/null +++ b/app/static/js/diy-turbo.js @@ -0,0 +1,68 @@ +document.addEventListener('click', async (e) => { + const link = e.target.closest('a'); + if (!link) return; + + const url = link.getAttribute('href'); + if (!url || url.startsWith('#') || url.startsWith('javascript:')) return; + + // Only intercept same-origin links + if (link.origin !== window.location.origin) return; + + // Ignore links that open in a new tab, download, or modifier keys + if (link.target === '_blank' || link.hasAttribute('download')) return; + if (e.ctrlKey || e.shiftKey || e.metaKey || e.altKey) return; + + // Optional: add a "data-turbo='false'" attribute check to disable it on specific links + if (link.getAttribute('data-turbo') === 'false') return; + + e.preventDefault(); + + document.body.style.cursor = 'wait'; + + try { + const response = await fetch(url); + if (!response.ok) throw new Error('Fetch failed'); + + const html = await response.text(); + const parser = new DOMParser(); + const doc = parser.parseFromString(html, 'text/html'); + + document.title = doc.title; + document.body.innerHTML = doc.body.innerHTML; + + // Carry over classes on the body if they changed + document.body.className = doc.body.className; + + document.body.style.cursor = 'default'; + window.history.pushState({}, '', url); + window.scrollTo(0, 0); + + // Dispatch a custom event so other scripts can re-initialize if necessary + document.dispatchEvent(new Event('diy-turbo:load')); + } catch (error) { + console.error('DIY Turbo navigation error:', error); + window.location.href = url; // fallback + } +}); + +window.addEventListener('popstate', async () => { + document.body.style.cursor = 'wait'; + try { + const response = await fetch(window.location.href); + if (!response.ok) throw new Error('Fetch failed'); + + const html = await response.text(); + const parser = new DOMParser(); + const doc = parser.parseFromString(html, 'text/html'); + + document.title = doc.title; + document.body.innerHTML = doc.body.innerHTML; + document.body.className = doc.body.className; + document.body.style.cursor = 'default'; + + document.dispatchEvent(new Event('diy-turbo:load')); + } catch (error) { + console.error('DIY Turbo popstate error:', error); + window.location.reload(); // fallback + } +}); diff --git a/app/templates/_layout.html b/app/templates/_layout.html index 1ffebca..53f9b11 100644 --- a/app/templates/_layout.html +++ b/app/templates/_layout.html @@ -7,6 +7,7 @@ {% block title %}BP Tracker{% endblock %} + @@ -128,26 +129,25 @@ diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html index d0ad875..2888004 100644 --- a/app/templates/dashboard.html +++ b/app/templates/dashboard.html @@ -114,38 +114,41 @@ {% endblock %} \ No newline at end of file