forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource-script.js
148 lines (129 loc) · 4.64 KB
/
source-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// From rust:
/* global search, sourcesIndex */
// Local js definitions:
/* global addClass, getCurrentValue, hasClass, removeClass, updateLocalStorage */
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";
if (elem.dirs) {
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";
if (elem.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.paddingTop = "3px";
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);
// Focus on the current file in the source files sidebar.
var selected_elem = sidebar.getElementsByClassName("selected")[0];
if (typeof selected_elem !== "undefined") {
selected_elem.focus();
}
}