]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/static/source-script.js
Rollup merge of #58303 - GuillaumeGomez:stability-tags-display, r=QuietMisdreavus
[rust.git] / src / librustdoc / html / static / source-script.js
1 // From rust:
2 /* global 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; i < rootPathParts.length; ++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 children = document.createElement("div");
39     children.className = "children";
40     var folders = document.createElement("div");
41     folders.className = "folders";
42     for (var i = 0; i < elem.dirs.length; ++i) {
43         if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
44                            hasFoundFile) === true) {
45             addClass(name, "expand");
46             hasFoundFile = true;
47         }
48     }
49     children.appendChild(folders);
50
51     var files = document.createElement("div");
52     files.className = "files";
53     for (i = 0; i < elem.files.length; ++i) {
54         var file = document.createElement("a");
55         file.innerText = elem.files[i];
56         file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
57         if (hasFoundFile === false &&
58                 currentFile === fullPath + elem.files[i]) {
59             file.className = "selected";
60             addClass(name, "expand");
61             hasFoundFile = true;
62         }
63         files.appendChild(file);
64     }
65     search.fullPath = fullPath;
66     children.appendChild(files);
67     parent.appendChild(name);
68     parent.appendChild(children);
69     return hasFoundFile === true && currentFile.startsWith(fullPath);
70 }
71
72 function toggleSidebar() {
73     var sidebar = document.getElementById("source-sidebar");
74     var child = this.children[0].children[0];
75     if (child.innerText === ">") {
76         sidebar.style.left = "";
77         this.style.left = "";
78         child.innerText = "<";
79         updateLocalStorage("rustdoc-source-sidebar-show", "true");
80     } else {
81         sidebar.style.left = "-300px";
82         this.style.left = "0";
83         child.innerText = ">";
84         updateLocalStorage("rustdoc-source-sidebar-show", "false");
85     }
86 }
87
88 function createSidebarToggle() {
89     var sidebarToggle = document.createElement("div");
90     sidebarToggle.id = "sidebar-toggle";
91     sidebarToggle.onclick = toggleSidebar;
92
93     var inner1 = document.createElement("div");
94     inner1.style.position = "relative";
95
96     var inner2 = document.createElement("div");
97     inner2.style.paddingTop = "3px";
98     if (getCurrentValue("rustdoc-source-sidebar-show") === "true") {
99         inner2.innerText = "<";
100     } else {
101         inner2.innerText = ">";
102         sidebarToggle.style.left = "0";
103     }
104
105     inner1.appendChild(inner2);
106     sidebarToggle.appendChild(inner1);
107     return sidebarToggle;
108 }
109
110 function createSourceSidebar() {
111     if (window.rootPath.endsWith("/") === false) {
112         window.rootPath += "/";
113     }
114     var main = document.getElementById("main");
115
116     var sidebarToggle = createSidebarToggle();
117     main.insertBefore(sidebarToggle, main.firstChild);
118
119     var sidebar = document.createElement("div");
120     sidebar.id = "source-sidebar";
121     if (getCurrentValue("rustdoc-source-sidebar-show") !== "true") {
122         sidebar.style.left = "-300px";
123     }
124
125     var currentFile = getCurrentFilePath();
126     var hasFoundFile = false;
127
128     var title = document.createElement("div");
129     title.className = "title";
130     title.innerText = "Files";
131     sidebar.appendChild(title);
132     Object.keys(sourcesIndex).forEach(function(key) {
133         sourcesIndex[key].name = key;
134         hasFoundFile = createDirEntry(sourcesIndex[key], sidebar, "",
135                                       currentFile, hasFoundFile);
136     });
137
138     main.insertBefore(sidebar, main.firstChild);
139 }