2 /* global search, sourcesIndex */
4 // Local js definitions:
5 /* global addClass, getCurrentValue, hasClass, removeClass, updateLocalStorage */
7 function getCurrentFilePath() {
8 var parts = window.location.pathname.split("/");
9 var rootPathParts = window.rootPath.split("/");
11 for (var i = 0, len = rootPathParts.length; i < len; ++i) {
12 if (rootPathParts[i] === "..") {
16 var file = window.location.pathname.substring(parts.join("/").length);
17 if (file.startsWith("/")) {
18 file = file.substring(1);
20 return file.substring(0, file.length - 5);
23 function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
24 var name = document.createElement("div");
25 name.className = "name";
27 fullPath += elem["name"] + "/";
29 name.onclick = function() {
30 if (hasClass(this, "expand")) {
31 removeClass(this, "expand");
33 addClass(this, "expand");
36 name.innerText = elem["name"];
40 var children = document.createElement("div");
41 children.className = "children";
42 var folders = document.createElement("div");
43 folders.className = "folders";
45 for (i = 0, len = elem.dirs.length; i < len; ++i) {
46 if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
47 hasFoundFile) === true) {
48 addClass(name, "expand");
53 children.appendChild(folders);
55 var files = document.createElement("div");
56 files.className = "files";
58 for (i = 0, len = elem.files.length; i < len; ++i) {
59 var file = document.createElement("a");
60 file.innerText = elem.files[i];
61 file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
62 if (hasFoundFile === false &&
63 currentFile === fullPath + elem.files[i]) {
64 file.className = "selected";
65 addClass(name, "expand");
68 files.appendChild(file);
71 search.fullPath = fullPath;
72 children.appendChild(files);
73 parent.appendChild(name);
74 parent.appendChild(children);
75 return hasFoundFile === true && currentFile.startsWith(fullPath);
78 function toggleSidebar() {
79 var sidebar = document.getElementById("source-sidebar");
80 var child = this.children[0].children[0];
81 if (child.innerText === ">") {
82 sidebar.style.left = "";
84 child.innerText = "<";
85 updateLocalStorage("rustdoc-source-sidebar-show", "true");
87 sidebar.style.left = "-300px";
88 this.style.left = "0";
89 child.innerText = ">";
90 updateLocalStorage("rustdoc-source-sidebar-show", "false");
94 function createSidebarToggle() {
95 var sidebarToggle = document.createElement("div");
96 sidebarToggle.id = "sidebar-toggle";
97 sidebarToggle.onclick = toggleSidebar;
99 var inner1 = document.createElement("div");
100 inner1.style.position = "relative";
102 var inner2 = document.createElement("div");
103 inner2.style.paddingTop = "3px";
104 if (getCurrentValue("rustdoc-source-sidebar-show") === "true") {
105 inner2.innerText = "<";
107 inner2.innerText = ">";
108 sidebarToggle.style.left = "0";
111 inner1.appendChild(inner2);
112 sidebarToggle.appendChild(inner1);
113 return sidebarToggle;
116 // This function is called from "source-files.js", generated in `html/render/mod.rs`.
117 // eslint-disable-next-line no-unused-vars
118 function createSourceSidebar() {
119 if (window.rootPath.endsWith("/") === false) {
120 window.rootPath += "/";
122 var main = document.getElementById("main");
124 var sidebarToggle = createSidebarToggle();
125 main.insertBefore(sidebarToggle, main.firstChild);
127 var sidebar = document.createElement("div");
128 sidebar.id = "source-sidebar";
129 if (getCurrentValue("rustdoc-source-sidebar-show") !== "true") {
130 sidebar.style.left = "-300px";
133 var currentFile = getCurrentFilePath();
134 var hasFoundFile = false;
136 var title = document.createElement("div");
137 title.className = "title";
138 title.innerText = "Files";
139 sidebar.appendChild(title);
140 Object.keys(sourcesIndex).forEach(function(key) {
141 sourcesIndex[key].name = key;
142 hasFoundFile = createDirEntry(sourcesIndex[key], sidebar, "",
143 currentFile, hasFoundFile);
146 main.insertBefore(sidebar, main.firstChild);
147 // Focus on the current file in the source files sidebar.
148 var selected_elem = sidebar.getElementsByClassName("selected")[0];
149 if (typeof selected_elem !== "undefined") {
150 selected_elem.focus();