Skip to content

Commit 6faa40d

Browse files
Migrate source-script to ES6
1 parent 016334a commit 6faa40d

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

src/librustdoc/html/static/js/source-script.js

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* eslint-env es6 */
2+
/* eslint no-var: "error" */
3+
/* eslint prefer-const: "error" */
4+
15
// From rust:
26
/* global search, sourcesIndex */
37

@@ -7,23 +11,23 @@
711
(function() {
812

913
function getCurrentFilePath() {
10-
var parts = window.location.pathname.split("/");
11-
var rootPathParts = window.rootPath.split("/");
14+
const parts = window.location.pathname.split("/");
15+
const rootPathParts = window.rootPath.split("/");
1216

13-
for (var i = 0, len = rootPathParts.length; i < len; ++i) {
14-
if (rootPathParts[i] === "..") {
17+
for (const rootPathPart of rootPathParts) {
18+
if (rootPathPart === "..") {
1519
parts.pop();
1620
}
1721
}
18-
var file = window.location.pathname.substring(parts.join("/").length);
22+
let file = window.location.pathname.substring(parts.join("/").length);
1923
if (file.startsWith("/")) {
2024
file = file.substring(1);
2125
}
2226
return file.substring(0, file.length - 5);
2327
}
2428

2529
function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
26-
var name = document.createElement("div");
30+
const name = document.createElement("div");
2731
name.className = "name";
2832

2933
fullPath += elem["name"] + "/";
@@ -37,31 +41,28 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
3741
};
3842
name.innerText = elem["name"];
3943

40-
var i, len;
41-
42-
var children = document.createElement("div");
44+
const children = document.createElement("div");
4345
children.className = "children";
44-
var folders = document.createElement("div");
46+
const folders = document.createElement("div");
4547
folders.className = "folders";
4648
if (elem.dirs) {
47-
for (i = 0, len = elem.dirs.length; i < len; ++i) {
48-
if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
49-
hasFoundFile)) {
49+
for (const dir of elem.dirs) {
50+
if (createDirEntry(dir, folders, fullPath, currentFile, hasFoundFile)) {
5051
addClass(name, "expand");
5152
hasFoundFile = true;
5253
}
5354
}
5455
}
5556
children.appendChild(folders);
5657

57-
var files = document.createElement("div");
58+
const files = document.createElement("div");
5859
files.className = "files";
5960
if (elem.files) {
60-
for (i = 0, len = elem.files.length; i < len; ++i) {
61-
var file = document.createElement("a");
62-
file.innerText = elem.files[i];
63-
file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
64-
if (!hasFoundFile && currentFile === fullPath + elem.files[i]) {
61+
for (const file_text of elem.files) {
62+
const file = document.createElement("a");
63+
file.innerText = file_text;
64+
file.href = window.rootPath + "src/" + fullPath + file_text + ".html";
65+
if (!hasFoundFile && currentFile === fullPath + file_text) {
6566
file.className = "selected";
6667
addClass(name, "expand");
6768
hasFoundFile = true;
@@ -77,8 +78,8 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
7778
}
7879

7980
function toggleSidebar() {
80-
var sidebar = document.querySelector("nav.sidebar");
81-
var child = this.children[0];
81+
const sidebar = document.querySelector("nav.sidebar");
82+
const child = this.children[0];
8283
if (child.innerText === ">") {
8384
sidebar.classList.add("expanded");
8485
child.innerText = "<";
@@ -91,11 +92,11 @@ function toggleSidebar() {
9192
}
9293

9394
function createSidebarToggle() {
94-
var sidebarToggle = document.createElement("div");
95+
const sidebarToggle = document.createElement("div");
9596
sidebarToggle.id = "sidebar-toggle";
9697
sidebarToggle.onclick = toggleSidebar;
9798

98-
var inner = document.createElement("div");
99+
const inner = document.createElement("div");
99100

100101
if (getCurrentValue("source-sidebar-show") === "true") {
101102
inner.innerText = "<";
@@ -113,23 +114,23 @@ function createSourceSidebar() {
113114
if (!window.rootPath.endsWith("/")) {
114115
window.rootPath += "/";
115116
}
116-
var container = document.querySelector("nav.sidebar");
117+
const container = document.querySelector("nav.sidebar");
117118

118-
var sidebarToggle = createSidebarToggle();
119+
const sidebarToggle = createSidebarToggle();
119120
container.insertBefore(sidebarToggle, container.firstChild);
120121

121-
var sidebar = document.createElement("div");
122+
const sidebar = document.createElement("div");
122123
sidebar.id = "source-sidebar";
123124
if (getCurrentValue("source-sidebar-show") !== "true") {
124125
container.classList.remove("expanded");
125126
} else {
126127
container.classList.add("expanded");
127128
}
128129

129-
var currentFile = getCurrentFilePath();
130-
var hasFoundFile = false;
130+
const currentFile = getCurrentFilePath();
131+
let hasFoundFile = false;
131132

132-
var title = document.createElement("div");
133+
const title = document.createElement("div");
133134
title.className = "title";
134135
title.innerText = "Files";
135136
sidebar.appendChild(title);
@@ -141,13 +142,13 @@ function createSourceSidebar() {
141142

142143
container.appendChild(sidebar);
143144
// Focus on the current file in the source files sidebar.
144-
var selected_elem = sidebar.getElementsByClassName("selected")[0];
145+
const selected_elem = sidebar.getElementsByClassName("selected")[0];
145146
if (typeof selected_elem !== "undefined") {
146147
selected_elem.focus();
147148
}
148149
}
149150

150-
var lineNumbersRegex = /^#?(\d+)(?:-(\d+))?$/;
151+
const lineNumbersRegex = /^#?(\d+)(?:-(\d+))?$/;
151152

152153
function highlightSourceLines(match) {
153154
if (typeof match === "undefined") {
@@ -156,21 +157,21 @@ function highlightSourceLines(match) {
156157
if (!match) {
157158
return;
158159
}
159-
var from = parseInt(match[1], 10);
160-
var to = from;
160+
let from = parseInt(match[1], 10);
161+
let to = from;
161162
if (typeof match[2] !== "undefined") {
162163
to = parseInt(match[2], 10);
163164
}
164165
if (to < from) {
165-
var tmp = to;
166+
const tmp = to;
166167
to = from;
167168
from = tmp;
168169
}
169-
var elem = document.getElementById(from);
170+
let elem = document.getElementById(from);
170171
if (!elem) {
171172
return;
172173
}
173-
var x = document.getElementById(from);
174+
const x = document.getElementById(from);
174175
if (x) {
175176
x.scrollIntoView();
176177
}
@@ -179,7 +180,7 @@ function highlightSourceLines(match) {
179180
removeClass(i_e, "line-highlighted");
180181
});
181182
});
182-
for (var i = from; i <= to; ++i) {
183+
for (let i = from; i <= to; ++i) {
183184
elem = document.getElementById(i);
184185
if (!elem) {
185186
break;
@@ -188,11 +189,11 @@ function highlightSourceLines(match) {
188189
}
189190
}
190191

191-
var handleSourceHighlight = (function() {
192-
var prev_line_id = 0;
192+
const handleSourceHighlight = (function() {
193+
let prev_line_id = 0;
193194

194-
var set_fragment = function(name) {
195-
var x = window.scrollX,
195+
const set_fragment = function(name) {
196+
const x = window.scrollX,
196197
y = window.scrollY;
197198
if (searchState.browserSupportsHistoryApi()) {
198199
history.replaceState(null, null, "#" + name);
@@ -205,13 +206,13 @@ var handleSourceHighlight = (function() {
205206
};
206207

207208
return function(ev) {
208-
var cur_line_id = parseInt(ev.target.id, 10);
209+
let cur_line_id = parseInt(ev.target.id, 10);
209210
ev.preventDefault();
210211

211212
if (ev.shiftKey && prev_line_id) {
212213
// Swap selection if needed
213214
if (prev_line_id > cur_line_id) {
214-
var tmp = prev_line_id;
215+
const tmp = prev_line_id;
215216
prev_line_id = cur_line_id;
216217
cur_line_id = tmp;
217218
}
@@ -226,7 +227,7 @@ var handleSourceHighlight = (function() {
226227
}());
227228

228229
window.addEventListener("hashchange", function() {
229-
var match = window.location.hash.match(lineNumbersRegex);
230+
const match = window.location.hash.match(lineNumbersRegex);
230231
if (match) {
231232
return highlightSourceLines(match);
232233
}

0 commit comments

Comments
 (0)