Skip to content

Commit 5ac899c

Browse files
committed
Cleanup rustbook.js and add 'use strict'
Mostly indentation fixes, but a little refactoring, too.
1 parent f0666b4 commit 5ac899c

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

src/rustbook/static/rustbook.js

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,66 +8,71 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
/*jslint browser: true, es5: true */
12+
/*globals $: true, rootPath: true */
1113

12-
document.addEventListener("DOMContentLoaded", function(event) {
14+
document.addEventListener('DOMContentLoaded', function() {
15+
'use strict';
1316

14-
document.getElementById("toggle-nav").onclick = toggleNav;
17+
document.getElementById('toggle-nav').onclick = function(e) {
18+
var toc = document.getElementById('toc');
19+
var pagewrapper = document.getElementById('page-wrapper');
20+
toggleClass(toc, 'mobile-hidden');
21+
toggleClass(pagewrapper, 'mobile-hidden');
22+
};
1523

16-
function toggleNav() {
17-
var toc = document.getElementById("toc");
18-
var pagewrapper = document.getElementById("page-wrapper");
19-
toggleClass(toc, "mobile-hidden");
20-
toggleClass(pagewrapper, "mobile-hidden");
21-
}
24+
function toggleClass(el, className) {
25+
// from http://youmightnotneedjquery.com/
26+
if (el.classList) {
27+
el.classList.toggle(className);
28+
} else {
29+
var classes = el.className.split(' ');
30+
var existingIndex = classes.indexOf(className);
2231

23-
function toggleClass(el, className) {
24-
// from http://youmightnotneedjquery.com/
25-
if (el.classList) {
26-
el.classList.toggle(className);
27-
} else {
28-
var classes = el.className.split(' ');
29-
var existingIndex = classes.indexOf(className);
32+
if (existingIndex >= 0) {
33+
classes.splice(existingIndex, 1);
34+
} else {
35+
classes.push(className);
36+
}
3037

31-
if (existingIndex >= 0) {
32-
classes.splice(existingIndex, 1);
33-
} else {
34-
classes.push(className);
35-
}
36-
37-
el.className = classes.join(' ');
38-
}
39-
}
38+
el.className = classes.join(' ');
39+
}
40+
}
4041

41-
// The below code is used to add prev and next navigation links to the bottom
42-
// of each of the sections.
43-
// It works by extracting the current page based on the url and iterates over
44-
// the menu links until it finds the menu item for the current page. We then
45-
// create a copy of the preceding and following menu links and add the
46-
// correct css class and insert them into the bottom of the page.
47-
var toc = document.getElementById('toc').getElementsByTagName('a');
48-
var href = document.location.pathname.split('/').pop();
49-
if (href === 'index.html' || href === '') {
50-
href = 'README.html';
51-
}
42+
// The below code is used to add prev and next navigation links to the
43+
// bottom of each of the sections.
44+
// It works by extracting the current page based on the url and iterates
45+
// over the menu links until it finds the menu item for the current page. We
46+
// then create a copy of the preceding and following menu links and add the
47+
// correct css class and insert them into the bottom of the page.
48+
var toc = document.getElementById('toc').getElementsByTagName('a');
49+
var href = document.location.pathname.split('/').pop();
5250

53-
for (var i = 0; i < toc.length; i++) {
54-
if (toc[i].attributes.href.value.split('/').pop() === href) {
55-
var nav = document.createElement('p');
56-
if (i > 0) {
57-
var prevNode = toc[i-1].cloneNode(true);
58-
prevNode.className = 'left';
59-
prevNode.setAttribute('rel', 'prev');
60-
nav.appendChild(prevNode);
61-
}
62-
if (i < toc.length - 1) {
63-
var nextNode = toc[i+1].cloneNode(true);
64-
nextNode.className = 'right';
65-
nextNode.setAttribute('rel', 'next');
66-
nav.appendChild(nextNode);
67-
}
68-
document.getElementById('page').appendChild(nav);
69-
break;
51+
if (href === 'index.html' || href === '') {
52+
href = 'README.html';
7053
}
71-
}
7254

55+
for (var i = 0; i < toc.length; i++) {
56+
if (toc[i].attributes.href.value.split('/').pop() === href) {
57+
var nav = document.createElement('p');
58+
59+
if (i > 0) {
60+
var prevNode = toc[i-1].cloneNode(true);
61+
prevNode.className = 'left';
62+
prevNode.setAttribute('rel', 'prev');
63+
nav.appendChild(prevNode);
64+
}
65+
66+
if (i < toc.length - 1) {
67+
var nextNode = toc[i+1].cloneNode(true);
68+
nextNode.className = 'right';
69+
nextNode.setAttribute('rel', 'next');
70+
nav.appendChild(nextNode);
71+
}
72+
73+
document.getElementById('page').appendChild(nav);
74+
75+
break;
76+
}
77+
}
7378
});

0 commit comments

Comments
 (0)