diff --git a/templates/course_dashboard.html b/templates/course_dashboard.html index 093da69..99f0492 100644 --- a/templates/course_dashboard.html +++ b/templates/course_dashboard.html @@ -1131,9 +1131,17 @@ function loadLibrary() { const libraryCard = document.getElementById('library-card'); if (!libraryCard) return; - libraryTrail = []; - fetchLibraryLevel(null); - history.replaceState({ libraryTrail: [], path: null }, ''); + // If we landed here via popstate/reload from a course view + // that got unloaded (see the popstate listener below), the + // current history entry may already carry the folder trail + // we were browsing before the course was loaded - restore it + // instead of always resetting to the library root. + const restored = history.state && history.state.libraryTrail; + libraryTrail = restored ? history.state.libraryTrail : []; + fetchLibraryLevel(libraryTrail.length ? libraryTrail[libraryTrail.length - 1].path : null); + if (!restored) { + history.replaceState({ libraryTrail: [], path: null }, ''); + } } // Drilling into a folder only ever swapped #library-groups's @@ -1148,6 +1156,17 @@ } window.addEventListener('popstate', function(event) { + {% if course %} + // The course view is a distinct server-side render tied to + // current_course, not something this page can patch back to + // client-side - unload the course and reload so back actually + // returns to the library/dashboard instead of doing nothing + // (which would otherwise leave a stale course view on screen + // while quietly burning through history entries until there's + // nowhere left to go but out of the app). + fetch('/reset_course').then(function() { location.reload(); }); + return; + {% endif %} const state = event.state; if (!state || !document.getElementById('library-card')) return; libraryTrail = state.libraryTrail || []; @@ -1385,6 +1404,11 @@ .then(r => r.json()) .then(data => { if (data.success) { + // A plain reload doesn't touch history, so without this + // push there'd be nothing for back to land on once the + // course view replaces this page - see the popstate + // listener above for how landing back here unloads it. + history.pushState({ courseLoaded: true }, ''); location.reload(); } else { alert('Error: ' + data.error);