Skip to content

Commit 509b145

Browse files
Migrate scrape-examples.js to ES6
1 parent 724c4bd commit 509b145

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/librustdoc/html/static/js/scrape-examples.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
/* global addClass, hasClass, removeClass, onEach */
1+
/* eslint-env es6 */
2+
/* eslint no-var: "error" */
3+
/* eslint prefer-const: "error" */
4+
/* global addClass, hasClass, removeClass, onEachLazy */
25

36
(function () {
47
// Number of lines shown when code viewer is not expanded
58
const MAX_LINES = 10;
69

710
// Scroll code block to the given code location
811
function scrollToLoc(elt, loc) {
9-
var lines = elt.querySelector('.line-numbers');
10-
var scrollOffset;
12+
const lines = elt.querySelector('.line-numbers');
13+
let scrollOffset;
1114

1215
// If the block is greater than the size of the viewer,
1316
// then scroll to the top of the block. Otherwise scroll
1417
// to the middle of the block.
1518
if (loc[1] - loc[0] > MAX_LINES) {
16-
var line = Math.max(0, loc[0] - 1);
19+
const line = Math.max(0, loc[0] - 1);
1720
scrollOffset = lines.children[line].offsetTop;
1821
} else {
19-
var wrapper = elt.querySelector(".code-wrapper");
20-
var halfHeight = wrapper.offsetHeight / 2;
21-
var offsetMid = (lines.children[loc[0]].offsetTop
22+
const wrapper = elt.querySelector(".code-wrapper");
23+
const halfHeight = wrapper.offsetHeight / 2;
24+
const offsetMid = (lines.children[loc[0]].offsetTop
2225
+ lines.children[loc[1]].offsetTop) / 2;
2326
scrollOffset = offsetMid - halfHeight;
2427
}
@@ -28,21 +31,21 @@
2831
}
2932

3033
function updateScrapedExample(example) {
31-
var locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
32-
var locIndex = 0;
33-
var highlights = example.querySelectorAll('.highlight');
34-
var link = example.querySelector('.scraped-example-title a');
34+
const locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
35+
let locIndex = 0;
36+
const highlights = Array.prototype.slice.call(example.querySelectorAll('.highlight'));
37+
const link = example.querySelector('.scraped-example-title a');
3538

3639
if (locs.length > 1) {
3740
// Toggle through list of examples in a given file
38-
var onChangeLoc = function(changeIndex) {
41+
const onChangeLoc = function(changeIndex) {
3942
removeClass(highlights[locIndex], 'focus');
4043
changeIndex();
4144
scrollToLoc(example, locs[locIndex][0]);
4245
addClass(highlights[locIndex], 'focus');
4346

44-
var url = locs[locIndex][1];
45-
var title = locs[locIndex][2];
47+
const url = locs[locIndex][1];
48+
const title = locs[locIndex][2];
4649

4750
link.href = url;
4851
link.innerHTML = title;
@@ -63,7 +66,7 @@
6366
});
6467
}
6568

66-
var expandButton = example.querySelector('.expand');
69+
const expandButton = example.querySelector('.expand');
6770
if (expandButton) {
6871
expandButton.addEventListener('click', function () {
6972
if (hasClass(example, "expanded")) {
@@ -79,24 +82,24 @@
7982
scrollToLoc(example, locs[0][0]);
8083
}
8184

82-
var firstExamples = document.querySelectorAll('.scraped-example-list > .scraped-example');
83-
onEach(firstExamples, updateScrapedExample);
84-
onEach(document.querySelectorAll('.more-examples-toggle'), function(toggle) {
85+
const firstExamples = document.querySelectorAll('.scraped-example-list > .scraped-example');
86+
onEachLazy(firstExamples, updateScrapedExample);
87+
onEachLazy(document.querySelectorAll('.more-examples-toggle'), function(toggle) {
8588
// Allow users to click the left border of the <details> section to close it,
8689
// since the section can be large and finding the [+] button is annoying.
87-
toggle.querySelectorAll('.toggle-line, .hide-more').forEach(button => {
90+
onEachLazy(toggle.querySelectorAll('.toggle-line, .hide-more'), button => {
8891
button.addEventListener('click', function() {
8992
toggle.open = false;
9093
});
9194
});
9295

93-
var moreExamples = toggle.querySelectorAll('.scraped-example');
96+
const moreExamples = toggle.querySelectorAll('.scraped-example');
9497
toggle.querySelector('summary').addEventListener('click', function() {
9598
// Wrapping in setTimeout ensures the update happens after the elements are actually
9699
// visible. This is necessary since updateScrapedExample calls scrollToLoc which
97100
// depends on offsetHeight, a property that requires an element to be visible to
98101
// compute correctly.
99-
setTimeout(function() { onEach(moreExamples, updateScrapedExample); });
102+
setTimeout(function() { onEachLazy(moreExamples, updateScrapedExample); });
100103
}, {once: true});
101104
});
102105
})();

0 commit comments

Comments
 (0)