Skip to content

Commit da58e40

Browse files
authored
Merge pull request docker#11543 from thaJeztah/async_json
Load metadata and toc asynchronous
2 parents cf1fdf5 + 5c8580c commit da58e40

File tree

8 files changed

+35
-22
lines changed

8 files changed

+35
-22
lines changed

_includes/body-landing.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ <h5 class="title">Educational resources</h5>
473473
<script src="/js/theme-switcher.js"></script>
474474
<script src="/js/jquery.js"></script>
475475
<script src="/js/bootstrap.min.js"></script>
476-
<script defer src="/js/metadata.js"></script>
477476
<script defer src="/js/search.js"></script>
478477

479478
<script>

_includes/body.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ <h1>{{ page.title }}</h1>
7777
<script defer src="/js/anchorlinks.js"></script>
7878
<script src="/js/jquery.js"></script>
7979
<script src="/js/bootstrap.min.js"></script>
80-
<script defer src="/js/metadata.js"></script>
8180
<script defer src="/js/docs.js"></script>
82-
<script defer src="/js/toc.js"></script>
8381
<script defer src="/js/search.js"></script>
8482
{%- include analytics/polldaddy.html -%}
8583
</body>

js/docs.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,21 @@ function walkTree(tree) {
8282
}
8383
}
8484

85-
function renderNav(docstoc) {
86-
for (let i = 0; i < docstoc.horizontalnav.length; i++) {
87-
if (docstoc.horizontalnav[i].path === pageURL || pageIsInSection(docstoc[docstoc.horizontalnav[i].node])) {
88-
// This is the current section. Set the corresponding header-nav link
89-
// to active, and build the left-hand (vertical) navigation
90-
document.getElementById(docstoc.horizontalnav[i].node).closest("li").classList.add("active")
91-
walkTree(docstoc[docstoc.horizontalnav[i].node]);
92-
document.getElementById("jsTOCLeftNav").innerHTML = outputLetNav.join("");
85+
function renderNav() {
86+
getJSON( "/js/toc.json", function( data ) {
87+
for (const item of data.horizontalnav) {
88+
if (item.path === pageURL || pageIsInSection(data[item.node])) {
89+
// This is the current section. Set the corresponding header-nav link
90+
// to active, and build the left-hand (vertical) navigation
91+
_('#'+item.node).closest("li").classList.add("active")
92+
walkTree(data[item.node]);
93+
_("#jsTOCLeftNav").innerHTML = outputLetNav.join("");
94+
}
9395
}
94-
}
95-
// Scroll the current menu item into view. We actually pick the item *above*
96-
// the current item to give some headroom above
97-
scrollMenuItem("#jsTOCLeftNav a.currentPage")
96+
// Scroll the current menu item into view. We actually pick the item *above*
97+
// the current item to give some headroom above
98+
scrollMenuItem("#jsTOCLeftNav a.currentPage")
99+
});
98100
}
99101

100102
// Scroll the given menu item into view. We actually pick the item *above*
@@ -256,3 +258,5 @@ window.onload = function () {
256258
$('.nav-tabs > li > a[data-group="' + group + '"]').tab("show");
257259
});
258260
};
261+
262+
ready(renderNav);

js/metadata.js renamed to js/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: null
33
---
4-
const pages = [
4+
[
55
{%- for page in site.html_pages -%}
66
{%- if page.hide_from_sitemap != nil or page.notoc != nil -%}
77
{%- continue -%}

js/search.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,9 @@ function queryString()
191191
return vars;
192192
}
193193

194-
$(document).ready(bindSearch);
194+
let pages = []
195+
196+
ready(() => {
197+
getJSON( "/js/metadata.json", data => pages = data);
198+
bindSearch()
199+
})

js/theme-switcher.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
const _ = s => document.querySelector(s);
33
const ready = f => document.readyState !== 'loading' ? f() : document.addEventListener('DOMContentLoaded', f)
44

5+
function getJSON(url, fn) {
6+
const xhr = new XMLHttpRequest();
7+
xhr.open('GET', url, true);
8+
xhr.responseType = 'json';
9+
xhr.onload = () => xhr.status === 200 ? fn(xhr.response) : null;
10+
xhr.send();
11+
}
12+
513
const darkMode = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches
614
const selectedTheme = window.localStorage ? localStorage.getItem("theme") : null;
715

js/toc.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

js/toc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
layout: null
3+
---
4+
{{ site.data.toc | jsonify }}

0 commit comments

Comments
 (0)