From 5cab796e79572ca9c2b970b7e6007288a4779638 Mon Sep 17 00:00:00 2001 From: Hofi Date: Wed, 22 May 2024 17:08:23 +0200 Subject: [PATCH] Added a common selection clear function Signed-off-by: Hofi --- _includes/globals.html | 5 +++++ _js/custom/navigation.js | 33 +++++++++++++++++++++++++-------- _js/lunr/lunr-en.js | 4 ++-- _js/main.min.js | 21 ++++++++++++++++----- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/_includes/globals.html b/_includes/globals.html index 2f51ee26..8abeb5b1 100644 --- a/_includes/globals.html +++ b/_includes/globals.html @@ -52,6 +52,11 @@ }; } + function clearFocus() { + if (document.activeElement instanceof HTMLElement) + document.activeElement.blur(); + } + function getElementPositionRelativeToRoot(element) { var rect = element.getBoundingClientRect(); diff --git a/_js/custom/navigation.js b/_js/custom/navigation.js index 98f8ec05..aa3a9924 100644 --- a/_js/custom/navigation.js +++ b/_js/custom/navigation.js @@ -149,7 +149,9 @@ $(function () { if (anchorId) scrollToAnchor(anchorId); // Clear any focus (e.g back navigation keeps the previously clicked link focused) - window.trigger('blur'); + clearFocus(); + // Forcibly hide the search content that can remain open in certain back and force navigation cases + hideSearch(); } function updateContentFromUrl(url) { @@ -248,8 +250,9 @@ $(function () { // Clear focus from the clicked element, as we have other visualization for the selected items event.target.blur(); } - if (false == updated) + if (false == updated) { console.debug("Different collection item or new tab/page requested, loading full page...") + } } } @@ -779,15 +782,21 @@ $(function () { input.trigger("select"); }, 100); } - else { - // set focus back via the initial content otherwise the focus will not get back to the search input once again - $(".initial-content").find("input").focus(); - } + // else { + // // set focus back via the initial content otherwise the focus will not get back to the search input once again + // $(".initial-content").find("input").focus(); + // } if (tooltipTarget) hideTooltip(true); - // NOTE: event.target is not always the toggle here, use it directly instead of the event - $("#search-button").trigger('blur'); + + clearFocus(); + } + + function hideSearch() { + $(".search-content").removeClass("is--visible"); + $(".search-content__form").removeClass("is--visible"); + $(".initial-content").removeClass("is--hidden"); } $("#search-button").on('click', toggleSearch); @@ -803,9 +812,17 @@ $(function () { // (e.g. when an inner embedded page link is opened directly in a new tab, not via the internal navigational links) finalizeContent(); + hideSearch(); + // Listen for popstate events and update content accordingly window.addEventListener('popstate', function () { updateContentFromUrl(window.location.pathname); }); + window.searchResultLinkClickHandler = function (event) { + // Clicking a link that url is shown in the active page will not trigger anything + // just show the actual pag, hide the search panel + hideSearch(); + handleNavLinkClick(event); + }; }); diff --git a/_js/lunr/lunr-en.js b/_js/lunr/lunr-en.js index 7cd47620..93b166d5 100644 --- a/_js/lunr/lunr-en.js +++ b/_js/lunr/lunr-en.js @@ -81,7 +81,7 @@ $(document).ready(function() { '
' + '
' + '

' + - '' + store[ref].title + '' + + '' + store[ref].title + '' + '

' + '
' + '' + @@ -95,7 +95,7 @@ $(document).ready(function() { '
' + '' + diff --git a/_js/main.min.js b/_js/main.min.js index bb1e4623..4f5dbf47 100644 --- a/_js/main.min.js +++ b/_js/main.min.js @@ -8539,7 +8539,8 @@ $(function() { if (ClipboardJS.isSupported()) addCodeBlocksTitle(); addContentTooltips(); if (anchorId) scrollToAnchor(anchorId); - window.trigger("blur"); + clearFocus(); + hideSearch(); } function updateContentFromUrl(url) { var currContent = document.querySelector(contentID); @@ -8593,7 +8594,9 @@ $(function() { } event.target.blur(); } - if (false == updated) console.debug("Different collection item or new tab/page requested, loading full page..."); + if (false == updated) { + console.debug("Different collection item or new tab/page requested, loading full page..."); + } } } function updateNavLinks(event) { @@ -8921,19 +8924,27 @@ $(function() { input.trigger("focus"); input.trigger("select"); }, 100); - } else { - $(".initial-content").find("input").focus(); } if (tooltipTarget) hideTooltip(true); - $("#search-button").trigger("blur"); + clearFocus(); + } + function hideSearch() { + $(".search-content").removeClass("is--visible"); + $(".search-content__form").removeClass("is--visible"); + $(".initial-content").removeClass("is--hidden"); } $("#search-button").on("click", toggleSearch); } stickySideBar(); finalizeContent(); + hideSearch(); window.addEventListener("popstate", function() { updateContentFromUrl(window.location.pathname); }); + window.searchResultLinkClickHandler = function(event) { + hideSearch(); + handleNavLinkClick(event); + }; }); $(function() {