js/docs: don't traverse full TOC if we have a direct match

There's no need to traverse the whole TOC if the URL matches
the current URL.

Also some small cleaning up / refactoring.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-10-08 15:01:15 +02:00
parent fc89461836
commit 1ba81da1bd
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 10 additions and 12 deletions

View File

@ -29,30 +29,30 @@ function navClicked(sourceLink) {
var outputLetNav = []; var outputLetNav = [];
var totalTopics = 0; var totalTopics = 0;
function findMyTopic(tree) { function pageIsInSection(tree) {
function processBranch(branch) { function processBranch(branch) {
for (var k = 0; k < branch.length; k++) { for (let k = 0; k < branch.length; k++) {
if (branch[k].section) { if (branch[k].section) {
processBranch(branch[k].section); processBranch(branch[k].section);
} else { } else {
if (branch[k].path === pageURL && !branch[k].nosync) { if (branch[k].path === pageURL && !branch[k].nosync) {
thisIsIt = true; found = true;
break; break;
} }
} }
} }
} }
var thisIsIt = false; let found = false;
processBranch(tree); processBranch(tree);
return thisIsIt; return found;
} }
function walkTree(tree) { function walkTree(tree) {
for (var j = 0; j < tree.length; j++) { for (let j = 0; j < tree.length; j++) {
totalTopics++; totalTopics++;
if (tree[j].section) { if (tree[j].section) {
var sectionHasPath = findMyTopic(tree[j].section); let sectionHasPath = pageIsInSection(tree[j].section);
outputLetNav.push('<li><a onclick="navClicked(' + totalTopics + ')" data-target="#item' + totalTopics + '" data-toggle="collapse" data-parent="#stacked-menu"') outputLetNav.push('<li><a onclick="navClicked(' + totalTopics + ')" data-target="#item' + totalTopics + '" data-toggle="collapse" data-parent="#stacked-menu"')
if (sectionHasPath) { if (sectionHasPath) {
outputLetNav.push('aria-expanded="true"') outputLetNav.push('aria-expanded="true"')
@ -69,8 +69,7 @@ function walkTree(tree) {
outputLetNav.push("false"); outputLetNav.push("false");
} }
outputLetNav.push('">'); outputLetNav.push('">');
var subTree = tree[j].section; walkTree(tree[j].section);
walkTree(subTree);
outputLetNav.push("</ul></li>"); outputLetNav.push("</ul></li>");
} else { } else {
// just a regular old topic; this is a leaf, not a branch; render a link! // just a regular old topic; this is a leaf, not a branch; render a link!
@ -84,9 +83,8 @@ function walkTree(tree) {
} }
function renderNav(docstoc) { function renderNav(docstoc) {
for (var i = 0; i < docstoc.horizontalnav.length; i++) { for (let i = 0; i < docstoc.horizontalnav.length; i++) {
var itsHere = findMyTopic(docstoc[docstoc.horizontalnav[i].node]); if (docstoc.horizontalnav[i].path === pageURL || pageIsInSection(docstoc[docstoc.horizontalnav[i].node])) {
if (itsHere || docstoc.horizontalnav[i].path === pageURL) {
// This is the current section. Set the corresponding header-nav link // This is the current section. Set the corresponding header-nav link
// to active, and build the left-hand (vertical) navigation // to active, and build the left-hand (vertical) navigation
document.getElementById(docstoc.horizontalnav[i].node).closest("li").classList.add("active") document.getElementById(docstoc.horizontalnav[i].node).closest("li").classList.add("active")