Fix anchors issue when everything is collapsed

This commit is contained in:
Guillaume Gomez 2018-04-04 16:16:53 +02:00
parent 135f334e0a
commit 238583a50f
2 changed files with 23 additions and 10 deletions

View File

@ -56,6 +56,14 @@
};
}
function getPageId() {
var id = document.location.href.split('#')[1];
if (id) {
return id.split('?')[0].split('&')[0];
}
return null;
}
function hasClass(elem, className) {
if (elem && className && elem.className) {
var elemClass = elem.className;
@ -1643,7 +1651,7 @@
}
}
function toggleAllDocs() {
function toggleAllDocs(pageId) {
var toggle = document.getElementById("toggle-all-docs");
if (hasClass(toggle, "will-expand")) {
updateLocalStorage("rustdoc-collapse", "false");
@ -1664,12 +1672,12 @@
toggle.title = "expand all docs";
onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
collapseDocs(e, "hide");
collapseDocs(e, "hide", pageId);
});
}
}
function collapseDocs(toggle, mode) {
function collapseDocs(toggle, mode, pageId) {
if (!toggle || !toggle.parentNode) {
return;
}
@ -1745,14 +1753,18 @@
}
}
var relatedDoc = toggle.parentNode;
var parentElem = toggle.parentNode;
var relatedDoc = parentElem;
var docblock = relatedDoc.nextElementSibling;
while (!hasClass(relatedDoc, "impl-items")) {
relatedDoc = relatedDoc.nextElementSibling;
}
if (!relatedDoc && !hasClass(docblock, "docblock")) {
if ((!relatedDoc && !hasClass(docblock, "docblock")) ||
(pageId && onEach(relatedDoc.childNodes, function(e) {
return e.id === pageId;
}) === true)) {
return;
}
@ -1782,7 +1794,7 @@
}
}
function autoCollapseAllImpls() {
function autoCollapseAllImpls(pageId) {
// Automatically minimize all non-inherent impls
onEach(document.getElementsByClassName('impl'), function(n) {
// inherent impl ids are like 'impl' or impl-<number>'
@ -1790,7 +1802,7 @@
if (!inherent) {
onEach(n.childNodes, function(m) {
if (hasClass(m, "collapse-toggle")) {
collapseDocs(m, "hide");
collapseDocs(m, "hide", pageId);
}
});
}
@ -1900,7 +1912,7 @@
}
})
autoCollapseAllImpls();
autoCollapseAllImpls(getPageId());
function createToggleWrapper() {
var span = document.createElement('span');
@ -2030,7 +2042,7 @@
};
if (getCurrentValue("rustdoc-collapse") === "true") {
toggleAllDocs();
toggleAllDocs(getPageId());
}
}());

View File

@ -19,10 +19,11 @@ function onEach(arr, func) {
if (arr && arr.length > 0 && func) {
for (var i = 0; i < arr.length; i++) {
if (func(arr[i]) === true) {
break;
return true;
}
}
}
return false;
}
function updateLocalStorage(name, value) {