1// Local js definitions:2/* global addClass, onEachLazy, removeClass, browserSupportsHistoryApi */3/* global updateLocalStorage, getVar, nonnull */456"use strict";78(function() {910const rootPath = getVar("root-path");1112const NAME_OFFSET = 0;13const DIRS_OFFSET = 1;14const FILES_OFFSET = 2;1516// WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY17// If you update this line, then you also need to update the media query with the same18// warning in rustdoc.css19const RUSTDOC_MOBILE_BREAKPOINT = 700;2021function closeSidebarIfMobile() {22 if (window.innerWidth < RUSTDOC_MOBILE_BREAKPOINT) {23 updateLocalStorage("source-sidebar-show", "false");24 }25}2627/**28 * @param {rustdoc.Dir} elem29 * @param {HTMLElement} parent30 * @param {string} fullPath31 * @param {boolean} hasFoundFile32 *33 * @returns {boolean} - new value for hasFoundFile34 */35function createDirEntry(elem, parent, fullPath, hasFoundFile) {36 const dirEntry = document.createElement("details");37 const summary = document.createElement("summary");3839 dirEntry.className = "dir-entry";4041 fullPath += elem[NAME_OFFSET] + "/";4243 summary.innerText = elem[NAME_OFFSET];44 dirEntry.appendChild(summary);4546 const folders = document.createElement("div");47 folders.className = "folders";48 if (elem[DIRS_OFFSET]) {49 for (const dir of elem[DIRS_OFFSET]) {50 if (createDirEntry(dir, folders, fullPath, false)) {51 dirEntry.open = true;52 hasFoundFile = true;53 }54 }55 }56 dirEntry.appendChild(folders);5758 const files = document.createElement("div");59 files.className = "files";60 if (elem[FILES_OFFSET]) {61 const w = window.location.href.split("#")[0];62 for (const file_text of elem[FILES_OFFSET]) {63 const file = document.createElement("a");64 file.innerText = file_text;65 file.href = rootPath + "src/" + fullPath + file_text + ".html";66 file.addEventListener("click", closeSidebarIfMobile);67 if (!hasFoundFile && w === file.href) {68 file.className = "selected";69 dirEntry.open = true;70 hasFoundFile = true;71 }72 files.appendChild(file);73 }74 }75 dirEntry.appendChild(files);76 parent.appendChild(dirEntry);77 return hasFoundFile;78}7980window.rustdocCloseSourceSidebar = () => {81 removeClass(document.documentElement, "src-sidebar-expanded");82 updateLocalStorage("source-sidebar-show", "false");83};8485window.rustdocShowSourceSidebar = () => {86 addClass(document.documentElement, "src-sidebar-expanded");87 updateLocalStorage("source-sidebar-show", "true");88};8990window.rustdocToggleSrcSidebar = () => {91 if (document.documentElement.classList.contains("src-sidebar-expanded")) {92 window.rustdocCloseSourceSidebar();93 } else {94 window.rustdocShowSourceSidebar();95 }96};9798// This function is called from "src-files.js", generated in `html/render/write_shared.rs`.99// eslint-disable-next-line no-unused-vars100/**101 * @param {string} srcIndexStr - strinified json map from crate name to dir structure102 */103function createSrcSidebar(srcIndexStr) {104 const container = nonnull(document.querySelector("nav.sidebar"));105106 const sidebar = document.createElement("div");107 sidebar.id = "src-sidebar";108 const srcIndex = new Map(JSON.parse(srcIndexStr));109110 let hasFoundFile = false;111112 for (const [key, source] of srcIndex) {113 source[NAME_OFFSET] = key;114 hasFoundFile = createDirEntry(source, sidebar, "", hasFoundFile);115 }116117 container.appendChild(sidebar);118 // Focus on the current file in the source files sidebar.119 const selected_elem = sidebar.getElementsByClassName("selected")[0];120 if (typeof selected_elem !== "undefined") {121 // @ts-expect-error122 selected_elem.focus();123 }124}125126function highlightSrcLines() {127 const match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);128 if (!match) {129 return;130 }131 let from = parseInt(match[1], 10);132 let to = from;133 if (typeof match[2] !== "undefined") {134 to = parseInt(match[2], 10);135 }136 if (to < from) {137 const tmp = to;138 to = from;139 from = tmp;140 }141 const from_s = "" + from;142 let elem = document.getElementById(from_s);143 if (!elem) {144 return;145 }146 const x = document.getElementById(from_s);147 if (x) {148 x.scrollIntoView();149 }150 onEachLazy(document.querySelectorAll("a[data-nosnippet]"), e => {151 removeClass(e, "line-highlighted");152 });153 for (let i = from; i <= to; ++i) {154 elem = document.getElementById("" + i);155 if (!elem) {156 break;157 }158 addClass(elem, "line-highlighted");159 }160}161162const handleSrcHighlight = (function() {163 let prev_line_id = 0;164165 /** @type {function(string): void} */166 const set_fragment = name => {167 const x = window.scrollX,168 y = window.scrollY;169 if (browserSupportsHistoryApi()) {170 history.replaceState(null, "", "#" + name);171 highlightSrcLines();172 } else {173 location.replace("#" + name);174 }175 // Prevent jumps when selecting one or many lines176 window.scrollTo(x, y);177 };178179 // @ts-expect-error180 return ev => {181 let cur_line_id = parseInt(ev.target.id, 10);182 // This event handler is attached to the entire line number column, but it should only183 // be run if one of the anchors is clicked. It also shouldn't do anything if the anchor184 // is clicked with a modifier key (to open a new browser tab).185 if (isNaN(cur_line_id) ||186 ev.ctrlKey ||187 ev.altKey ||188 ev.metaKey) {189 return;190 }191 ev.preventDefault();192193 if (ev.shiftKey && prev_line_id) {194 // Swap selection if needed195 if (prev_line_id > cur_line_id) {196 const tmp = prev_line_id;197 prev_line_id = cur_line_id;198 cur_line_id = tmp;199 }200201 set_fragment(prev_line_id + "-" + cur_line_id);202 } else {203 prev_line_id = cur_line_id;204205 set_fragment("" + cur_line_id);206 }207 };208}());209210window.addEventListener("hashchange", highlightSrcLines);211212onEachLazy(document.querySelectorAll("a[data-nosnippet]"), el => {213 el.addEventListener("click", handleSrcHighlight);214});215216highlightSrcLines();217218window.createSrcSidebar = createSrcSidebar;219})();
Findings
✓ No findings reported for this file.