1
+ /* eslint-env es6 */
2
+ /* eslint no-var: "error" */
3
+ /* eslint prefer-const: "error" */
4
+
1
5
// From rust:
2
6
/* global search, sourcesIndex */
3
7
7
11
( function ( ) {
8
12
9
13
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 ( "/" ) ;
12
16
13
- for ( var i = 0 , len = rootPathParts . length ; i < len ; ++ i ) {
14
- if ( rootPathParts [ i ] === ".." ) {
17
+ for ( const rootPathPart of rootPathParts ) {
18
+ if ( rootPathPart === ".." ) {
15
19
parts . pop ( ) ;
16
20
}
17
21
}
18
- var file = window . location . pathname . substring ( parts . join ( "/" ) . length ) ;
22
+ let file = window . location . pathname . substring ( parts . join ( "/" ) . length ) ;
19
23
if ( file . startsWith ( "/" ) ) {
20
24
file = file . substring ( 1 ) ;
21
25
}
22
26
return file . substring ( 0 , file . length - 5 ) ;
23
27
}
24
28
25
29
function createDirEntry ( elem , parent , fullPath , currentFile , hasFoundFile ) {
26
- var name = document . createElement ( "div" ) ;
30
+ const name = document . createElement ( "div" ) ;
27
31
name . className = "name" ;
28
32
29
33
fullPath += elem [ "name" ] + "/" ;
@@ -37,31 +41,28 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
37
41
} ;
38
42
name . innerText = elem [ "name" ] ;
39
43
40
- var i , len ;
41
-
42
- var children = document . createElement ( "div" ) ;
44
+ const children = document . createElement ( "div" ) ;
43
45
children . className = "children" ;
44
- var folders = document . createElement ( "div" ) ;
46
+ const folders = document . createElement ( "div" ) ;
45
47
folders . className = "folders" ;
46
48
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 ) ) {
50
51
addClass ( name , "expand" ) ;
51
52
hasFoundFile = true ;
52
53
}
53
54
}
54
55
}
55
56
children . appendChild ( folders ) ;
56
57
57
- var files = document . createElement ( "div" ) ;
58
+ const files = document . createElement ( "div" ) ;
58
59
files . className = "files" ;
59
60
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 ) {
65
66
file . className = "selected" ;
66
67
addClass ( name , "expand" ) ;
67
68
hasFoundFile = true ;
@@ -77,8 +78,8 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
77
78
}
78
79
79
80
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 ] ;
82
83
if ( child . innerText === ">" ) {
83
84
sidebar . classList . add ( "expanded" ) ;
84
85
child . innerText = "<" ;
@@ -91,11 +92,11 @@ function toggleSidebar() {
91
92
}
92
93
93
94
function createSidebarToggle ( ) {
94
- var sidebarToggle = document . createElement ( "div" ) ;
95
+ const sidebarToggle = document . createElement ( "div" ) ;
95
96
sidebarToggle . id = "sidebar-toggle" ;
96
97
sidebarToggle . onclick = toggleSidebar ;
97
98
98
- var inner = document . createElement ( "div" ) ;
99
+ const inner = document . createElement ( "div" ) ;
99
100
100
101
if ( getCurrentValue ( "source-sidebar-show" ) === "true" ) {
101
102
inner . innerText = "<" ;
@@ -113,23 +114,23 @@ function createSourceSidebar() {
113
114
if ( ! window . rootPath . endsWith ( "/" ) ) {
114
115
window . rootPath += "/" ;
115
116
}
116
- var container = document . querySelector ( "nav.sidebar" ) ;
117
+ const container = document . querySelector ( "nav.sidebar" ) ;
117
118
118
- var sidebarToggle = createSidebarToggle ( ) ;
119
+ const sidebarToggle = createSidebarToggle ( ) ;
119
120
container . insertBefore ( sidebarToggle , container . firstChild ) ;
120
121
121
- var sidebar = document . createElement ( "div" ) ;
122
+ const sidebar = document . createElement ( "div" ) ;
122
123
sidebar . id = "source-sidebar" ;
123
124
if ( getCurrentValue ( "source-sidebar-show" ) !== "true" ) {
124
125
container . classList . remove ( "expanded" ) ;
125
126
} else {
126
127
container . classList . add ( "expanded" ) ;
127
128
}
128
129
129
- var currentFile = getCurrentFilePath ( ) ;
130
- var hasFoundFile = false ;
130
+ const currentFile = getCurrentFilePath ( ) ;
131
+ let hasFoundFile = false ;
131
132
132
- var title = document . createElement ( "div" ) ;
133
+ const title = document . createElement ( "div" ) ;
133
134
title . className = "title" ;
134
135
title . innerText = "Files" ;
135
136
sidebar . appendChild ( title ) ;
@@ -141,13 +142,13 @@ function createSourceSidebar() {
141
142
142
143
container . appendChild ( sidebar ) ;
143
144
// 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 ] ;
145
146
if ( typeof selected_elem !== "undefined" ) {
146
147
selected_elem . focus ( ) ;
147
148
}
148
149
}
149
150
150
- var lineNumbersRegex = / ^ # ? ( \d + ) (?: - ( \d + ) ) ? $ / ;
151
+ const lineNumbersRegex = / ^ # ? ( \d + ) (?: - ( \d + ) ) ? $ / ;
151
152
152
153
function highlightSourceLines ( match ) {
153
154
if ( typeof match === "undefined" ) {
@@ -156,21 +157,21 @@ function highlightSourceLines(match) {
156
157
if ( ! match ) {
157
158
return ;
158
159
}
159
- var from = parseInt ( match [ 1 ] , 10 ) ;
160
- var to = from ;
160
+ let from = parseInt ( match [ 1 ] , 10 ) ;
161
+ let to = from ;
161
162
if ( typeof match [ 2 ] !== "undefined" ) {
162
163
to = parseInt ( match [ 2 ] , 10 ) ;
163
164
}
164
165
if ( to < from ) {
165
- var tmp = to ;
166
+ const tmp = to ;
166
167
to = from ;
167
168
from = tmp ;
168
169
}
169
- var elem = document . getElementById ( from ) ;
170
+ let elem = document . getElementById ( from ) ;
170
171
if ( ! elem ) {
171
172
return ;
172
173
}
173
- var x = document . getElementById ( from ) ;
174
+ const x = document . getElementById ( from ) ;
174
175
if ( x ) {
175
176
x . scrollIntoView ( ) ;
176
177
}
@@ -179,7 +180,7 @@ function highlightSourceLines(match) {
179
180
removeClass ( i_e , "line-highlighted" ) ;
180
181
} ) ;
181
182
} ) ;
182
- for ( var i = from ; i <= to ; ++ i ) {
183
+ for ( let i = from ; i <= to ; ++ i ) {
183
184
elem = document . getElementById ( i ) ;
184
185
if ( ! elem ) {
185
186
break ;
@@ -188,11 +189,11 @@ function highlightSourceLines(match) {
188
189
}
189
190
}
190
191
191
- var handleSourceHighlight = ( function ( ) {
192
- var prev_line_id = 0 ;
192
+ const handleSourceHighlight = ( function ( ) {
193
+ let prev_line_id = 0 ;
193
194
194
- var set_fragment = function ( name ) {
195
- var x = window . scrollX ,
195
+ const set_fragment = function ( name ) {
196
+ const x = window . scrollX ,
196
197
y = window . scrollY ;
197
198
if ( searchState . browserSupportsHistoryApi ( ) ) {
198
199
history . replaceState ( null , null , "#" + name ) ;
@@ -205,13 +206,13 @@ var handleSourceHighlight = (function() {
205
206
} ;
206
207
207
208
return function ( ev ) {
208
- var cur_line_id = parseInt ( ev . target . id , 10 ) ;
209
+ let cur_line_id = parseInt ( ev . target . id , 10 ) ;
209
210
ev . preventDefault ( ) ;
210
211
211
212
if ( ev . shiftKey && prev_line_id ) {
212
213
// Swap selection if needed
213
214
if ( prev_line_id > cur_line_id ) {
214
- var tmp = prev_line_id ;
215
+ const tmp = prev_line_id ;
215
216
prev_line_id = cur_line_id ;
216
217
cur_line_id = tmp ;
217
218
}
@@ -226,7 +227,7 @@ var handleSourceHighlight = (function() {
226
227
} ( ) ) ;
227
228
228
229
window . addEventListener ( "hashchange" , function ( ) {
229
- var match = window . location . hash . match ( lineNumbersRegex ) ;
230
+ const match = window . location . hash . match ( lineNumbersRegex ) ;
230
231
if ( match ) {
231
232
return highlightSourceLines ( match ) ;
232
233
}
0 commit comments