jQuery(function($) { // tocItems are headings in the main content area that have a representation in the TOC // (not all headings are present in the TOC). let tocItems = $('#page-content-wrapper .toc-item'); function getCurrentlyVisibleSection(scrollPosition) { // Walk the list from the bottom up and check; // each TOC item is the immediate child of a
that // carries the TOC item's ID. for (let i = tocItems.length - 1; i >= 0; --i) { let item = $(tocItems.get(i)); let offsetTop = item.offset().top - 50; if (scrollPosition >= offsetTop) { return item.parent().attr('id'); } } return null; } function updateNavigationState(visibleSection) { let selectedLink = $('#navigation a.selected'); let selectedSection = selectedLink.length === 0 ? null : selectedLink.attr('href').replace(/#/, ''); // nothing to do :) if (visibleSection === selectedSection) { return; } // un-select whatever was previously selected if (selectedLink.length > 0) { selectedLink.removeClass('selected'); } if (visibleSection !== null) { // show the leaf node in the navigation for the activeSection, plus // all nodes that lead to it (in a->b->c->d, if c is active, show // b and c because a is always shown already). let activeSectionLink = '#' + visibleSection; let link = $('#navigation a[href="' + activeSectionLink + '"]'); let listItem = link.parent(); link.addClass('selected'); while (listItem.data('level') > 1) { let ul = listItem.parent(); ul.show('fast'); listItem = ul.parent(); } // expand the currently selected item, i.e. do not just show the // parent path, but also the immediate child