From 1ba81da1bd4d2c8c1f9d13e4f151cd4e90f4c8b8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 8 Oct 2020 15:01:15 +0200 Subject: [PATCH] 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 --- js/docs.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/js/docs.js b/js/docs.js index 3ee6e31cfa..90cb712a7f 100644 --- a/js/docs.js +++ b/js/docs.js @@ -29,30 +29,30 @@ function navClicked(sourceLink) { var outputLetNav = []; var totalTopics = 0; -function findMyTopic(tree) { +function pageIsInSection(tree) { function processBranch(branch) { - for (var k = 0; k < branch.length; k++) { + for (let k = 0; k < branch.length; k++) { if (branch[k].section) { processBranch(branch[k].section); } else { if (branch[k].path === pageURL && !branch[k].nosync) { - thisIsIt = true; + found = true; break; } } } } - var thisIsIt = false; + let found = false; processBranch(tree); - return thisIsIt; + return found; } function walkTree(tree) { - for (var j = 0; j < tree.length; j++) { + for (let j = 0; j < tree.length; j++) { totalTopics++; if (tree[j].section) { - var sectionHasPath = findMyTopic(tree[j].section); + let sectionHasPath = pageIsInSection(tree[j].section); outputLetNav.push('
  • '); - var subTree = tree[j].section; - walkTree(subTree); + walkTree(tree[j].section); outputLetNav.push("
  • "); } else { // 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) { - for (var i = 0; i < docstoc.horizontalnav.length; i++) { - var itsHere = findMyTopic(docstoc[docstoc.horizontalnav[i].node]); - if (itsHere || docstoc.horizontalnav[i].path === pageURL) { + for (let i = 0; i < docstoc.horizontalnav.length; i++) { + if (docstoc.horizontalnav[i].path === pageURL || pageIsInSection(docstoc[docstoc.horizontalnav[i].node])) { // This is the current section. Set the corresponding header-nav link // to active, and build the left-hand (vertical) navigation document.getElementById(docstoc.horizontalnav[i].node).closest("li").classList.add("active")