Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix anchors issue when everything is collapsed #49652

Merged
merged 1 commit into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1782,15 +1794,15 @@
}
}

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>'
var inherent = (n.id.match(/^impl(?:-\d+)?$/) !== null);
if (!inherent) {
onEach(n.childNodes, function(m) {
if (hasClass(m, "collapse-toggle")) {
collapseDocs(m, "hide");
collapseDocs(m, "hide", pageId);
}
});
}
Expand Down Expand Up @@ -1900,7 +1912,7 @@
}
})

autoCollapseAllImpls();
autoCollapseAllImpls(getPageId());

function createToggleWrapper() {
var span = document.createElement('span');
Expand Down Expand Up @@ -2030,7 +2042,7 @@
};

if (getCurrentValue("rustdoc-collapse") === "true") {
toggleAllDocs();
toggleAllDocs(getPageId());
}
}());

Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/static/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down