Skip to content

Commit

Permalink
Added a common selection clear function
Browse files Browse the repository at this point in the history
Signed-off-by: Hofi <hofione@gmail.com>
  • Loading branch information
HofiOne committed May 22, 2024
1 parent a309968 commit 5cab796
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
5 changes: 5 additions & 0 deletions _includes/globals.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
};
}

function clearFocus() {
if (document.activeElement instanceof HTMLElement)
document.activeElement.blur();
}

function getElementPositionRelativeToRoot(element) {
var rect = element.getBoundingClientRect();

Expand Down
33 changes: 25 additions & 8 deletions _js/custom/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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...")
}
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
};
});
4 changes: 2 additions & 2 deletions _js/lunr/lunr-en.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ $(document).ready(function() {
'<div class="list__item">' +
'<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">' +
'<h2 class="archive__item-title" itemprop="headline">' +
'<a href="' + removeExtension(tore[ref].url) + '" rel="permalink">' + store[ref].title + '</a>' +
'<a href="' + removeExtension(tore[ref].url) + '" onclick="searchResultLinkClickHandler(event)" rel="permalink">' + store[ref].title + '</a>' +
'</h2>' +
'<div class="archive__item-teaser">' +
'<img src="' + store[ref].teaser + '" alt="">' +
Expand All @@ -95,7 +95,7 @@ $(document).ready(function() {
'<div class="list__item">' +
'<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">' +
'<h2 class="archive__item-title" itemprop="headline">' +
'<a href="' + removeExtension(store[ref].url) + '" rel="permalink">' + store[ref].title + '</a>' +
'<a href="' + removeExtension(store[ref].url) + '" onclick="searchResultLinkClickHandler(event)" rel="permalink">' + store[ref].title + '</a>' +
'</h2>' +
'<p class="archive__item-excerpt" itemprop="description">' + store[ref].excerpt.split(" ").splice(0, 20).join(" ") + '...</p>' +
'</article>' +
Expand Down
21 changes: 16 additions & 5 deletions _js/main.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 5cab796

Please sign in to comment.