]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/static/source-script.js
Merge commit '9a0c32934ebe376128230aa8da3275697b2053e7' into sync_cg_clif-2021-03-05
[rust.git] / src / librustdoc / html / static / source-script.js
1 // From rust:
2 /* global search, sourcesIndex */
3
4 // Local js definitions:
5 /* global addClass, getCurrentValue, hasClass, removeClass, updateLocalStorage */
6
7 function getCurrentFilePath() {
8     var parts = window.location.pathname.split("/");
9     var rootPathParts = window.rootPath.split("/");
10
11     for (var i = 0, len = rootPathParts.length; i < len; ++i) {
12         if (rootPathParts[i] === "..") {
13             parts.pop();
14         }
15     }
16     var file = window.location.pathname.substring(parts.join("/").length);
17     if (file.startsWith("/")) {
18         file = file.substring(1);
19     }
20     return file.substring(0, file.length - 5);
21 }
22
23 function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
24     var name = document.createElement("div");
25     name.className = "name";
26
27     fullPath += elem["name"] + "/";
28
29     name.onclick = function() {
30         if (hasClass(this, "expand")) {
31             removeClass(this, "expand");
32         } else {
33             addClass(this, "expand");
34         }
35     };
36     name.innerText = elem["name"];
37
38     var i, len;
39
40     var children = document.createElement("div");
41     children.className = "children";
42     var folders = document.createElement("div");
43     folders.className = "folders";
44     if (elem.dirs) {
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");
49                 hasFoundFile = true;
50             }
51         }
52     }
53     children.appendChild(folders);
54
55     var files = document.createElement("div");
56     files.className = "files";
57     if (elem.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");
66                 hasFoundFile = true;
67             }
68             files.appendChild(file);
69         }
70     }
71     search.fullPath = fullPath;
72     children.appendChild(files);
73     parent.appendChild(name);
74     parent.appendChild(children);
75     return hasFoundFile === true && currentFile.startsWith(fullPath);
76 }
77
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 = "";
83         this.style.left = "";
84         child.innerText = "<";
85         updateLocalStorage("rustdoc-source-sidebar-show", "true");
86     } else {
87         sidebar.style.left = "-300px";
88         this.style.left = "0";
89         child.innerText = ">";
90         updateLocalStorage("rustdoc-source-sidebar-show", "false");
91     }
92 }
93
94 function createSidebarToggle() {
95     var sidebarToggle = document.createElement("div");
96     sidebarToggle.id = "sidebar-toggle";
97     sidebarToggle.onclick = toggleSidebar;
98
99     var inner1 = document.createElement("div");
100     inner1.style.position = "relative";
101
102     var inner2 = document.createElement("div");
103     inner2.style.paddingTop = "3px";
104     if (getCurrentValue("rustdoc-source-sidebar-show") === "true") {
105         inner2.innerText = "<";
106     } else {
107         inner2.innerText = ">";
108         sidebarToggle.style.left = "0";
109     }
110
111     inner1.appendChild(inner2);
112     sidebarToggle.appendChild(inner1);
113     return sidebarToggle;
114 }
115
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 += "/";
121     }
122     var main = document.getElementById("main");
123
124     var sidebarToggle = createSidebarToggle();
125     main.insertBefore(sidebarToggle, main.firstChild);
126
127     var sidebar = document.createElement("div");
128     sidebar.id = "source-sidebar";
129     if (getCurrentValue("rustdoc-source-sidebar-show") !== "true") {
130         sidebar.style.left = "-300px";
131     }
132
133     var currentFile = getCurrentFilePath();
134     var hasFoundFile = false;
135
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);
144     });
145
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();
151     }
152 }