|
36 | 36 | elt.querySelector(".rust").scrollTo(0, scrollOffset);
|
37 | 37 | }
|
38 | 38 |
|
39 |
| - function updateScrapedExample(example, isHidden) { |
40 |
| - const locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent); |
| 39 | + function createScrapeButton(parent, className, content) { |
| 40 | + const button = document.createElement("button"); |
| 41 | + button.className = className; |
| 42 | + button.innerText = content; |
| 43 | + parent.insertBefore(button, parent.firstChild); |
| 44 | + return button; |
| 45 | + } |
| 46 | + |
| 47 | + window.updateScrapedExample = (example, buttonHolder) => { |
41 | 48 | let locIndex = 0;
|
42 | 49 | const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight"));
|
43 | 50 | const link = example.querySelector(".scraped-example-title a");
|
| 51 | + let expandButton = null; |
| 52 | + |
| 53 | + if (!example.classList.contains("expanded")) { |
| 54 | + expandButton = createScrapeButton(buttonHolder, "expand", "↕"); |
| 55 | + } |
| 56 | + const isHidden = example.parentElement.classList.contains("more-scraped-examples"); |
44 | 57 |
|
| 58 | + const locs = example.locs; |
45 | 59 | if (locs.length > 1) {
|
| 60 | + const next = createScrapeButton(buttonHolder, "next", "≻"); |
| 61 | + const prev = createScrapeButton(buttonHolder, "prev", "≺"); |
| 62 | + |
46 | 63 | // Toggle through list of examples in a given file
|
47 | 64 | const onChangeLoc = changeIndex => {
|
48 | 65 | removeClass(highlights[locIndex], "focus");
|
|
57 | 74 | link.innerHTML = title;
|
58 | 75 | };
|
59 | 76 |
|
60 |
| - example.querySelector(".prev") |
61 |
| - .addEventListener("click", () => { |
62 |
| - onChangeLoc(() => { |
63 |
| - locIndex = (locIndex - 1 + locs.length) % locs.length; |
64 |
| - }); |
| 77 | + prev.addEventListener("click", () => { |
| 78 | + onChangeLoc(() => { |
| 79 | + locIndex = (locIndex - 1 + locs.length) % locs.length; |
65 | 80 | });
|
| 81 | + }); |
66 | 82 |
|
67 |
| - example.querySelector(".next") |
68 |
| - .addEventListener("click", () => { |
69 |
| - onChangeLoc(() => { |
70 |
| - locIndex = (locIndex + 1) % locs.length; |
71 |
| - }); |
| 83 | + next.addEventListener("click", () => { |
| 84 | + onChangeLoc(() => { |
| 85 | + locIndex = (locIndex + 1) % locs.length; |
72 | 86 | });
|
| 87 | + }); |
73 | 88 | }
|
74 | 89 |
|
75 |
| - const expandButton = example.querySelector(".expand"); |
76 | 90 | if (expandButton) {
|
77 | 91 | expandButton.addEventListener("click", () => {
|
78 | 92 | if (hasClass(example, "expanded")) {
|
|
83 | 97 | }
|
84 | 98 | });
|
85 | 99 | }
|
| 100 | + }; |
86 | 101 |
|
| 102 | + function setupLoc(example, isHidden) { |
| 103 | + example.locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent); |
87 | 104 | // Start with the first example in view
|
88 |
| - scrollToLoc(example, locs[0][0], isHidden); |
| 105 | + scrollToLoc(example, example.locs[0][0], isHidden); |
89 | 106 | }
|
90 | 107 |
|
91 | 108 | const firstExamples = document.querySelectorAll(".scraped-example-list > .scraped-example");
|
92 |
| - onEachLazy(firstExamples, el => updateScrapedExample(el, false)); |
| 109 | + onEachLazy(firstExamples, el => setupLoc(el, false)); |
93 | 110 | onEachLazy(document.querySelectorAll(".more-examples-toggle"), toggle => {
|
94 | 111 | // Allow users to click the left border of the <details> section to close it,
|
95 | 112 | // since the section can be large and finding the [+] button is annoying.
|
|
102 | 119 | const moreExamples = toggle.querySelectorAll(".scraped-example");
|
103 | 120 | toggle.querySelector("summary").addEventListener("click", () => {
|
104 | 121 | // Wrapping in setTimeout ensures the update happens after the elements are actually
|
105 |
| - // visible. This is necessary since updateScrapedExample calls scrollToLoc which |
| 122 | + // visible. This is necessary since setupLoc calls scrollToLoc which |
106 | 123 | // depends on offsetHeight, a property that requires an element to be visible to
|
107 | 124 | // compute correctly.
|
108 | 125 | setTimeout(() => {
|
109 |
| - onEachLazy(moreExamples, el => updateScrapedExample(el, true)); |
| 126 | + onEachLazy(moreExamples, el => setupLoc(el, true)); |
110 | 127 | });
|
111 | 128 | }, {once: true});
|
112 | 129 | });
|
|
0 commit comments