diff --git a/templates/course_dashboard.html b/templates/course_dashboard.html index 247c63c..093da69 100644 --- a/templates/course_dashboard.html +++ b/templates/course_dashboard.html @@ -1133,8 +1133,27 @@ if (!libraryCard) return; libraryTrail = []; fetchLibraryLevel(null); + history.replaceState({ libraryTrail: [], path: null }, ''); } + // Drilling into a folder only ever swapped #library-groups's + // contents - no history entry, no URL change - so the back + // button/gesture had nothing of ours to step back through once + // you were a few folders deep, and fell straight through to + // exiting the app instead of going up one level. Pushing a state + // per level (and restoring it on popstate, below) makes back + // step back up the library trail one folder at a time instead. + function pushLibraryState() { + history.pushState({ libraryTrail: libraryTrail.slice(), path: libraryTrail.length ? libraryTrail[libraryTrail.length - 1].path : null }, ''); + } + + window.addEventListener('popstate', function(event) { + const state = event.state; + if (!state || !document.getElementById('library-card')) return; + libraryTrail = state.libraryTrail || []; + fetchLibraryLevel(state.path || null); + }); + function fetchLibraryLevel(path) { searchActive = false; const searchInput = document.getElementById('library-search-input'); @@ -1176,11 +1195,13 @@ if (index < 0) { libraryTrail = []; fetchLibraryLevel(null); + pushLibraryState(); return; } const target = libraryTrail[index]; libraryTrail = libraryTrail.slice(0, index + 1); fetchLibraryLevel(target.path); + pushLibraryState(); } function renderLibraryLevel(data) { @@ -1202,6 +1223,7 @@ function enterLibraryDir(path, name) { libraryTrail.push({ name: name, path: path }); fetchLibraryLevel(path); + pushLibraryState(); } // A few shimmering placeholder rows for in-flight fetches, instead