diff options
Diffstat (limited to 'src/librustdoc/html/static/js/source-script.js')
| -rw-r--r-- | src/librustdoc/html/static/js/source-script.js | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js index 1e9bfa5cc89..8286e9201e6 100644 --- a/src/librustdoc/html/static/js/source-script.js +++ b/src/librustdoc/html/static/js/source-script.js @@ -10,7 +10,11 @@ (function() { const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value; -let oldScrollPosition = 0; +let oldScrollPosition = null; + +const NAME_OFFSET = 0; +const DIRS_OFFSET = 1; +const FILES_OFFSET = 2; function closeSidebarIfMobile() { if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) { @@ -24,16 +28,16 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) { dirEntry.className = "dir-entry"; - fullPath += elem["name"] + "/"; + fullPath += elem[NAME_OFFSET] + "/"; - summary.innerText = elem["name"]; + summary.innerText = elem[NAME_OFFSET]; dirEntry.appendChild(summary); const folders = document.createElement("div"); folders.className = "folders"; - if (elem.dirs) { - for (const dir of elem.dirs) { - if (createDirEntry(dir, folders, fullPath, hasFoundFile)) { + if (elem[DIRS_OFFSET]) { + for (const dir of elem[DIRS_OFFSET]) { + if (createDirEntry(dir, folders, fullPath, false)) { dirEntry.open = true; hasFoundFile = true; } @@ -43,8 +47,8 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) { const files = document.createElement("div"); files.className = "files"; - if (elem.files) { - for (const file_text of elem.files) { + if (elem[FILES_OFFSET]) { + for (const file_text of elem[FILES_OFFSET]) { const file = document.createElement("a"); file.innerText = file_text; file.href = rootPath + "src/" + fullPath + file_text + ".html"; @@ -71,18 +75,21 @@ function toggleSidebar() { oldScrollPosition = window.scrollY; document.body.style.position = "fixed"; document.body.style.top = `-${oldScrollPosition}px`; + } else { + oldScrollPosition = null; } addClass(document.documentElement, "source-sidebar-expanded"); child.innerText = "<"; updateLocalStorage("source-sidebar-show", "true"); } else { - if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) { + if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT && oldScrollPosition !== null) { // This is to keep the scroll position on mobile. document.body.style.position = ""; document.body.style.top = ""; // The scroll position is lost when resetting the style, hence why we store it in - // `oldScroll`. + // `oldScrollPosition`. window.scrollTo(0, oldScrollPosition); + oldScrollPosition = null; } removeClass(document.documentElement, "source-sidebar-expanded"); child.innerText = ">"; @@ -90,6 +97,17 @@ function toggleSidebar() { } } +window.addEventListener("resize", () => { + if (window.innerWidth >= window.RUSTDOC_MOBILE_BREAKPOINT && oldScrollPosition !== null) { + // If the user opens the sidebar in "mobile" mode, and then grows the browser window, + // we need to switch away from mobile mode and make the main content area scrollable. + document.body.style.position = ""; + document.body.style.top = ""; + window.scrollTo(0, oldScrollPosition); + oldScrollPosition = null; + } +}); + function createSidebarToggle() { const sidebarToggle = document.createElement("div"); sidebarToggle.id = "sidebar-toggle"; @@ -125,7 +143,7 @@ function createSourceSidebar() { title.innerText = "Files"; sidebar.appendChild(title); Object.keys(sourcesIndex).forEach(key => { - sourcesIndex[key].name = key; + sourcesIndex[key][NAME_OFFSET] = key; hasFoundFile = createDirEntry(sourcesIndex[key], sidebar, "", hasFoundFile); }); @@ -165,7 +183,7 @@ function highlightSourceLines(match) { if (x) { x.scrollIntoView(); } - onEachLazy(document.getElementsByClassName("line-numbers"), e => { + onEachLazy(document.getElementsByClassName("src-line-numbers"), e => { onEachLazy(e.getElementsByTagName("span"), i_e => { removeClass(i_e, "line-highlighted"); }); @@ -227,7 +245,7 @@ window.addEventListener("hashchange", () => { } }); -onEachLazy(document.getElementsByClassName("line-numbers"), el => { +onEachLazy(document.getElementsByClassName("src-line-numbers"), el => { el.addEventListener("click", handleSourceHighlight); }); |
