about summary refs log tree commit diff
path: root/src/librustdoc/html/static/source-script.js
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-12-04 07:00:19 +0000
committerbors <bors@rust-lang.org>2018-12-04 07:00:19 +0000
commit596e10fd3221e50e6cdb182b3ad76cc6d660f170 (patch)
treefa59c224d3b1c52eba2964e3ed54fe23b84643f6 /src/librustdoc/html/static/source-script.js
parent91d5d56c00d8e2926ccf856f14a4e52ef480d039 (diff)
parent82a7b6fde819e7e402ea9be6af14fe2c1575950e (diff)
downloadrust-596e10fd3221e50e6cdb182b3ad76cc6d660f170.tar.gz
rust-596e10fd3221e50e6cdb182b3ad76cc6d660f170.zip
Auto merge of #55707 - GuillaumeGomez:file-sidebar, r=QuietMisdreavus
Add source file sidebar

This is just a start currently but that gives a good overview of what it'll look like:

<img width="1440" alt="screenshot 2018-11-06 at 01 39 15" src="https://user-images.githubusercontent.com/3050060/48035592-05336180-e165-11e8-82e1-5ead0c345eb9.png">

r? @QuietMisdreavus
Diffstat (limited to 'src/librustdoc/html/static/source-script.js')
-rw-r--r--src/librustdoc/html/static/source-script.js147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/librustdoc/html/static/source-script.js b/src/librustdoc/html/static/source-script.js
new file mode 100644
index 00000000000..1db8218dae6
--- /dev/null
+++ b/src/librustdoc/html/static/source-script.js
@@ -0,0 +1,147 @@
+/*!
+ * Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+ * file at the top-level directory of this distribution and at
+ * http://rust-lang.org/COPYRIGHT.
+ *
+ * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+ * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+ * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+ * option. This file may not be copied, modified, or distributed
+ * except according to those terms.
+ */
+
+function getCurrentFilePath() {
+    var parts = window.location.pathname.split("/");
+    var rootPathParts = window.rootPath.split("/");
+
+    for (var i = 0; i < rootPathParts.length; ++i) {
+        if (rootPathParts[i] === "..") {
+            parts.pop();
+        }
+    }
+    var 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) {
+    var name = document.createElement("div");
+    name.className = "name";
+
+    fullPath += elem["name"] + "/";
+
+    name.onclick = function() {
+        if (hasClass(this, "expand")) {
+            removeClass(this, "expand");
+        } else {
+            addClass(this, "expand");
+        }
+    };
+    name.innerText = elem["name"];
+
+    var children = document.createElement("div");
+    children.className = "children";
+    var folders = document.createElement("div");
+    folders.className = "folders";
+    for (var i = 0; i < elem.dirs.length; ++i) {
+        if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
+                           hasFoundFile) === true) {
+            addClass(name, "expand");
+            hasFoundFile = true;
+        }
+    }
+    children.appendChild(folders);
+
+    var files = document.createElement("div");
+    files.className = "files";
+    for (i = 0; i < elem.files.length; ++i) {
+        var file = document.createElement("a");
+        file.innerText = elem.files[i];
+        file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
+        if (hasFoundFile === false &&
+                currentFile === fullPath + elem.files[i]) {
+            file.className = "selected";
+            addClass(name, "expand");
+            hasFoundFile = true;
+        }
+        files.appendChild(file);
+    }
+    search.fullPath = fullPath;
+    children.appendChild(files);
+    parent.appendChild(name);
+    parent.appendChild(children);
+    return hasFoundFile === true && currentFile.startsWith(fullPath);
+}
+
+function toggleSidebar() {
+    var sidebar = document.getElementById("source-sidebar");
+    var child = this.children[0].children[0];
+    if (child.innerText === ">") {
+        sidebar.style.left = "";
+        this.style.left = "";
+        child.innerText = "<";
+        updateLocalStorage("rustdoc-source-sidebar-show", "true");
+    } else {
+        sidebar.style.left = "-300px";
+        this.style.left = "0";
+        child.innerText = ">";
+        updateLocalStorage("rustdoc-source-sidebar-show", "false");
+    }
+}
+
+function createSidebarToggle() {
+    var sidebarToggle = document.createElement("div");
+    sidebarToggle.id = "sidebar-toggle";
+    sidebarToggle.onclick = toggleSidebar;
+
+    var inner1 = document.createElement("div");
+    inner1.style.position = "relative";
+
+    var inner2 = document.createElement("div");
+    inner2.style.marginTop = "-2px";
+    if (getCurrentValue("rustdoc-source-sidebar-show") === "true") {
+        inner2.innerText = "<";
+    } else {
+        inner2.innerText = ">";
+        sidebarToggle.style.left = "0";
+    }
+
+    inner1.appendChild(inner2);
+    sidebarToggle.appendChild(inner1);
+    return sidebarToggle;
+}
+
+function createSourceSidebar() {
+    if (window.rootPath.endsWith("/") === false) {
+        window.rootPath += "/";
+    }
+    var main = document.getElementById("main");
+
+    var sidebarToggle = createSidebarToggle();
+    main.insertBefore(sidebarToggle, main.firstChild);
+
+    var sidebar = document.createElement("div");
+    sidebar.id = "source-sidebar";
+    if (getCurrentValue("rustdoc-source-sidebar-show") !== "true") {
+        sidebar.style.left = "-300px";
+    }
+
+    var currentFile = getCurrentFilePath();
+    var hasFoundFile = false;
+
+    var title = document.createElement("div");
+    title.className = "title";
+    title.innerText = "Files";
+    sidebar.appendChild(title);
+    Object.keys(sourcesIndex).forEach(function(key) {
+        sourcesIndex[key].name = key;
+        hasFoundFile = createDirEntry(sourcesIndex[key], sidebar, "",
+                                      currentFile, hasFoundFile);
+    });
+
+    main.insertBefore(sidebar, main.firstChild);
+}
+
+createSourceSidebar();