From 06008f5a8ffc5b86ead27a63990953bf5874a753 Mon Sep 17 00:00:00 2001 From: Niko Date: Wed, 23 Feb 2022 10:34:52 +0100 Subject: [PATCH] Preserve last URL hash in /app to make going back easier --- web/src/app/js/app.js | 8 +++++++- web/src/app/js/base.js | 7 +++++++ web/src/app/js/pages/home.js | 4 ++-- web/src/app/js/pages/meet.js | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/web/src/app/js/app.js b/web/src/app/js/app.js index 9cea948..d8cfec8 100644 --- a/web/src/app/js/app.js +++ b/web/src/app/js/app.js @@ -418,12 +418,18 @@ window.onload = function(e) { window.onhashchange(); } +var prevHash = null; window.onhashchange = function(e) { - var path = window.location.hash.substring(1).trim(); + try { + window.prevHash = new URL(e.oldURL) + .hash.substr(1).trim(); + } catch (e) { window.prevHash = null } + var path = window.location.hash.substr(1).trim(); var args = path.split('/'); if (args.length > 0 && args[0].trim() === '') args = []; + window.lastHash = path; for (var i = 0; i < app.hashHandlers.length; i++) { const cfg = app.hashHandlers[i]; if (cfg.exact !== undefined && diff --git a/web/src/app/js/base.js b/web/src/app/js/base.js index 768c4b5..fe75228 100644 --- a/web/src/app/js/base.js +++ b/web/src/app/js/base.js @@ -142,6 +142,13 @@ function loadScript(id, file, cback) { document.body.appendChild(script); } +function hashredir(tohash, fallback) { + fallback = fallback || ''; + if (tohash === undefined || tohash === null) + tohash = fallback; + window.location.href = `#${tohash}`; +} + function printstack() { console.error(new Error().stack) } function uuidv4() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { diff --git a/web/src/app/js/pages/home.js b/web/src/app/js/pages/home.js index 8bb9f03..a07d69a 100644 --- a/web/src/app/js/pages/home.js +++ b/web/src/app/js/pages/home.js @@ -18,10 +18,10 @@ app.pages.home = { setTimeout(function() { document.getElementById('app-container').innerHTML = ''; if (app.template.isLoaded('quiz.index')) - window.location.href = `#quiz/${id}` + hashredir(`quiz/${id}`); }, 80); if (!app.template.isLoaded('quiz.index')) - window.location.href = `#quiz/${id}`; + hashredir(`quiz/${id}`); }, paint: function(section, json) { json = json || app.pages.home.data[section]; diff --git a/web/src/app/js/pages/meet.js b/web/src/app/js/pages/meet.js index e863295..3d1abef 100644 --- a/web/src/app/js/pages/meet.js +++ b/web/src/app/js/pages/meet.js @@ -204,7 +204,7 @@ app.pages.meet = { }, }, exit: function() { - window.location.href = '#'; + hashredir(window.prevHash); app.overlay.hideAll(); }, carousel: {