1
Fork 0

When a URL hash refers to a hidden element, it makes the element visible

This commit is contained in:
Guillaume Gomez 2019-11-05 17:41:40 +01:00
parent e4931eaaa3
commit f66a331335

View file

@ -161,17 +161,18 @@ function getSearchElement() {
return window.history && typeof window.history.pushState === "function"; return window.history && typeof window.history.pushState === "function";
} }
var main = document.getElementById("main"); function isHidden(elem) {
return elem.offsetHeight === 0;
function onHashChange(ev) {
// If we're in mobile mode, we should hide the sidebar in any case.
hideSidebar();
var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
if (match) {
return highlightSourceLines(match, ev);
} }
var main = document.getElementById("main");
var savedHash = "";
function handleHashes(ev) {
var search = getSearchElement(); var search = getSearchElement();
if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) { if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
// This block occurs when clicking on an element in the navbar while
// in a search.
addClass(search, "hidden"); addClass(search, "hidden");
removeClass(main, "hidden"); removeClass(main, "hidden");
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1); var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
@ -183,6 +184,35 @@ function getSearchElement() {
elem.scrollIntoView(); elem.scrollIntoView();
} }
} }
// This part is used in case an element is not visible.
if (savedHash !== window.location.hash) {
savedHash = window.location.hash;
if (savedHash.length === 0) {
return;
}
var elem = document.getElementById(savedHash.slice(1)); // we remove the '#'
if (!elem || !isHidden(elem)) {
return;
}
var parent = elem.parentNode;
if (parent && hasClass(parent, "impl-items")) {
// In case this is a trait implementation item, we first need to toggle
// the "Show hidden undocumented items".
onEachLazy(parent.getElementsByClassName("collapsed"), function(e) {
if (e.parentNode === parent) {
// Only click on the toggle we're looking for.
e.click();
return true;
}
});
if (isHidden(elem)) {
// The whole parent is collapsed. We need to click on its toggle as well!
if (hasClass(parent.lastElementChild, "collapse-toggle")) {
parent.lastElementChild.click();
}
}
}
}
} }
function highlightSourceLines(match, ev) { function highlightSourceLines(match, ev) {
@ -228,6 +258,16 @@ function getSearchElement() {
} }
} }
function onHashChange(ev) {
// If we're in mobile mode, we should hide the sidebar in any case.
hideSidebar();
var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
if (match) {
return highlightSourceLines(match, ev);
}
handleHashes();
}
function expandSection(id) { function expandSection(id) {
var elem = document.getElementById(id); var elem = document.getElementById(id);
if (elem && isHidden(elem)) { if (elem && isHidden(elem)) {
@ -246,9 +286,6 @@ function getSearchElement() {
} }
} }
highlightSourceLines();
window.onhashchange = onHashChange;
// Gets the human-readable string for the virtual-key code of the // Gets the human-readable string for the virtual-key code of the
// given KeyboardEvent, ev. // given KeyboardEvent, ev.
// //
@ -2615,6 +2652,10 @@ function getSearchElement() {
insertAfter(popup, getSearchElement()); insertAfter(popup, getSearchElement());
} }
handleHashes();
highlightSourceLines();
window.onhashchange = onHashChange;
buildHelperPopup(); buildHelperPopup();
}()); }());