rustdoc: clean up main.js and src-script.js

* Run the querySelector for the toggleLabel only once, and store
  the result.
* Use querySelector to find the resizer and sidebar.
* Add comments to main.js sections.
This commit is contained in:
Michael Howell 2023-09-26 16:31:33 -07:00
parent 0983438faa
commit 210c88fc7a
2 changed files with 13 additions and 6 deletions

View file

@ -1273,6 +1273,7 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
searchState.setup();
}());
// This section handles sidebar resizing
(function() {
const sidebarButton = document.getElementById("sidebar-button");
if (sidebarButton) {
@ -1283,8 +1284,8 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
});
}
let currentPointerId = null;
const resizer = document.getElementsByClassName("sidebar-resizer")[0];
const sidebar = document.getElementsByClassName("sidebar")[0];
const resizer = document.querySelector(".sidebar-resizer");
const sidebar = document.querySelector(".sidebar");
if (!resizer || !sidebar) {
return;
}
@ -1379,6 +1380,7 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/how-to-read-rustdoc.html\
resizer.addEventListener("pointerdown", initResize, false);
}());
// This section handles the copy button that appears next to the path breadcrumbs
(function() {
let reset_button_timeout = null;

View file

@ -71,17 +71,22 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
return hasFoundFile;
}
let toggleLabel;
function getToggleLabel() {
toggleLabel = toggleLabel || document.querySelector("#src-sidebar-toggle button");
return toggleLabel;
}
window.rustdocCloseSourceSidebar = () => {
const toggleLabel = document.querySelector("#src-sidebar-toggle button");
removeClass(document.documentElement, "src-sidebar-expanded");
toggleLabel.innerText = ">";
getToggleLabel().innerText = ">";
updateLocalStorage("source-sidebar-show", "false");
};
window.rustdocShowSourceSidebar = () => {
const toggleLabel = document.querySelector("#src-sidebar-toggle button");
addClass(document.documentElement, "src-sidebar-expanded");
toggleLabel.innerText = "<";
getToggleLabel().innerText = "<";
updateLocalStorage("source-sidebar-show", "true");
};