From 27dcebeb3e88b97420a6d8f9279102892e27c775 Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Tue, 14 Jun 2022 23:25:51 -0700 Subject: Improve loading of crates.js and sidebar-items.js Now that the "All Crates" dropdown is only rendered on the search results page, there is no need to load crates.js on most pages. Load it only on crate pages. Also, add the `defer` attribute so it does not block page rendering. For sidebar-items.js, move the script tag to ``. Since it already has the defer attribute it won't block loading. The defer attribute does preserve ordering between scripts, so instead of the callback on load, it can set a global variable on load, which is slightly simpler. Also, since it is required to finish rendering the page, beginning its load earlier is better. Remove generation and handling of sidebar-vars. Everything there can be computed with information available in JS via other means. Remove the "other" wrapper in the sidebar. It was unnecessary. Remove excess script fields --- src/librustdoc/html/static/js/source-script.js | 41 ++++++++------------------ 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'src/librustdoc/html/static/js/source-script.js') diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js index 14d8a942977..10f93a1c058 100644 --- a/src/librustdoc/html/static/js/source-script.js +++ b/src/librustdoc/html/static/js/source-script.js @@ -9,33 +9,19 @@ (function() { -function getCurrentFilePath() { - const parts = window.location.pathname.split("/"); - const rootPathParts = window.rootPath.split("/"); +const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value; - for (const rootPathPart of rootPathParts) { - if (rootPathPart === "..") { - parts.pop(); - } - } - let file = window.location.pathname.substring(parts.join("/").length); - if (file.startsWith("/")) { - file = file.substring(1); - } - return file.substring(0, file.length - 5); -} - -function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) { +function createDirEntry(elem, parent, fullPath, hasFoundFile) { const name = document.createElement("div"); name.className = "name"; fullPath += elem["name"] + "/"; - name.onclick = () => { - if (hasClass(name, "expand")) { - removeClass(name, "expand"); + name.onclick = ev => { + if (hasClass(ev.target, "expand")) { + removeClass(ev.target, "expand"); } else { - addClass(name, "expand"); + addClass(ev.target, "expand"); } }; name.innerText = elem["name"]; @@ -46,7 +32,7 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) { folders.className = "folders"; if (elem.dirs) { for (const dir of elem.dirs) { - if (createDirEntry(dir, folders, fullPath, currentFile, hasFoundFile)) { + if (createDirEntry(dir, folders, fullPath, hasFoundFile)) { addClass(name, "expand"); hasFoundFile = true; } @@ -60,8 +46,9 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) { for (const file_text of elem.files) { const file = document.createElement("a"); file.innerText = file_text; - file.href = window.rootPath + "src/" + fullPath + file_text + ".html"; - if (!hasFoundFile && currentFile === fullPath + file_text) { + file.href = rootPath + "src/" + fullPath + file_text + ".html"; + const w = window.location.href.split("#")[0]; + if (!hasFoundFile && w === file.href) { file.className = "selected"; addClass(name, "expand"); hasFoundFile = true; @@ -72,7 +59,7 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) { children.appendChild(files); parent.appendChild(name); parent.appendChild(children); - return hasFoundFile && currentFile.startsWith(fullPath); + return hasFoundFile; } function toggleSidebar() { @@ -109,9 +96,6 @@ function createSidebarToggle() { // This function is called from "source-files.js", generated in `html/render/mod.rs`. // eslint-disable-next-line no-unused-vars function createSourceSidebar() { - if (!window.rootPath.endsWith("/")) { - window.rootPath += "/"; - } const container = document.querySelector("nav.sidebar"); const sidebarToggle = createSidebarToggle(); @@ -125,7 +109,6 @@ function createSourceSidebar() { container.classList.add("expanded"); } - const currentFile = getCurrentFilePath(); let hasFoundFile = false; const title = document.createElement("div"); @@ -135,7 +118,7 @@ function createSourceSidebar() { Object.keys(sourcesIndex).forEach(key => { sourcesIndex[key].name = key; hasFoundFile = createDirEntry(sourcesIndex[key], sidebar, "", - currentFile, hasFoundFile); + hasFoundFile); }); container.appendChild(sidebar); -- cgit 1.4.1-3-g733a5