From c18c46efb4c3b6d8cd35ce373127586fc5272972 Mon Sep 17 00:00:00 2001 From: Splines Date: Mon, 27 May 2024 19:07:08 +0200 Subject: [PATCH] Fix broken browser navigation --- app/assets/javascripts/lectures_admin.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/lectures_admin.js b/app/assets/javascripts/lectures_admin.js index 6231f2e38..9839ab2c4 100644 --- a/app/assets/javascripts/lectures_admin.js +++ b/app/assets/javascripts/lectures_admin.js @@ -4,7 +4,8 @@ * * This is necessary to be able to share the URL with a specific tab open. * It also allows to stay on the same tab after a page reload - * (which is done when an edit action is canceled). + * (which is done when an edit action is saved/canceled, + * also see the lectures controller update action). * * Find out more details in this guide: * https://webdesign.tutsplus.com/how-to-add-deep-linking-to-the-bootstrap-4-tabs-component--cms-31180t @@ -14,9 +15,11 @@ function configureUrlHashesForBootstrapTabs() { const hash = $(this).attr("href"); const urlWithoutHash = location.href.split("#")[0]; const newUrl = `${urlWithoutHash}${hash}`; - history.replaceState(null, null, newUrl); + history.pushState(null, "", newUrl); }); +} +function navigateToActiveNavTab() { if (location.hash) { const hrefXPathIdentifier = `button[href="${location.hash}"]`; $(`#lecture-nav-pills ${hrefXPathIdentifier}`).tab("show"); @@ -29,9 +32,14 @@ function configureUrlHashesForBootstrapTabs() { $(document).on("ready turbolinks:load", function () { initBootstrapPopovers(); configureUrlHashesForBootstrapTabs(); + navigateToActiveNavTab(); // Reinitialize the masonry grid system when the lecture content is shown $("#lecture-nav-content").on("focus click", () => { initMasonryGridSystem(); }); }); + +$(window).on("hashchange", function () { + navigateToActiveNavTab(); +});