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

No more hidden elements #66123

Merged
merged 3 commits into from
Nov 8, 2019
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
62 changes: 51 additions & 11 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// Local js definitions:
/* global addClass, getCurrentValue, hasClass */
/* global isHidden, onEach, removeClass, updateLocalStorage */
/* global onEach, removeClass, updateLocalStorage */

if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
Expand Down Expand Up @@ -161,17 +161,18 @@ function getSearchElement() {
return window.history && typeof window.history.pushState === "function";
}

function isHidden(elem) {
return elem.offsetHeight === 0;
}

Comment on lines +164 to +167
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems odd to have, there's code further down which called an isHidden() function you don't appear to have removed, so is this shadowing one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right! I removed the old one.

var main = document.getElementById("main");
var savedHash = "";

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);
}
function handleHashes(ev) {
var search = getSearchElement();
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");
removeClass(main, "hidden");
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
Expand All @@ -183,6 +184,35 @@ function getSearchElement() {
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) {
Expand Down Expand Up @@ -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(ev);
}

function expandSection(id) {
var elem = document.getElementById(id);
if (elem && isHidden(elem)) {
Expand All @@ -246,9 +286,6 @@ function getSearchElement() {
}
}

highlightSourceLines();
window.onhashchange = onHashChange;

// Gets the human-readable string for the virtual-key code of the
// given KeyboardEvent, ev.
//
Expand Down Expand Up @@ -2615,6 +2652,9 @@ function getSearchElement() {
insertAfter(popup, getSearchElement());
}

onHashChange();
window.onhashchange = onHashChange;

buildHelperPopup();
}());

Expand Down
4 changes: 0 additions & 4 deletions src/librustdoc/html/static/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ function removeClass(elem, className) {
elem.classList.remove(className);
}

function isHidden(elem) {
return elem.offsetParent === null;
}

function onEach(arr, func, reversed) {
if (arr && arr.length > 0 && func) {
var length = arr.length;
Expand Down