Cleaned up some page-show JS

This commit is contained in:
Dan Brown 2017-12-07 19:10:31 +00:00
parent 6063ac4a11
commit 91444e83fd
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
1 changed files with 31 additions and 30 deletions

View File

@ -102,23 +102,28 @@ let setupPageShow = window.setupPageShow = function (pageId) {
let $window = $(window); let $window = $(window);
let $sidebar = $("#sidebar .scroll-body"); let $sidebar = $("#sidebar .scroll-body");
let $bookTreeParent = $sidebar.parent(); let $bookTreeParent = $sidebar.parent();
// Check the page is scrollable and the content is taller than the tree // Check the page is scrollable and the content is taller than the tree
let pageScrollable = ($(document).height() > $window.height()) && ($sidebar.height() < $('.page-content').height()); let pageScrollable = ($(document).height() > $window.height()) && ($sidebar.height() < $('.page-content').height());
// Get current tree's width and header height // Get current tree's width and header height
let headerHeight = $("#header").height() + $(".toolbar").height(); let headerHeight = $("#header").height() + $(".toolbar").height();
let isFixed = $window.scrollTop() > headerHeight; let isFixed = $window.scrollTop() > headerHeight;
// Function to fix the tree as a sidebar
// Fix the tree as a sidebar
function stickTree() { function stickTree() {
$sidebar.width($bookTreeParent.width() + 15); $sidebar.width($bookTreeParent.width() + 15);
$sidebar.addClass("fixed"); $sidebar.addClass("fixed");
isFixed = true; isFixed = true;
} }
// Function to un-fix the tree back into position
// Un-fix the tree back into position
function unstickTree() { function unstickTree() {
$sidebar.css('width', 'auto'); $sidebar.css('width', 'auto');
$sidebar.removeClass("fixed"); $sidebar.removeClass("fixed");
isFixed = false; isFixed = false;
} }
// Checks if the tree stickiness state should change // Checks if the tree stickiness state should change
function checkTreeStickiness(skipCheck) { function checkTreeStickiness(skipCheck) {
let shouldBeFixed = $window.scrollTop() > headerHeight; let shouldBeFixed = $window.scrollTop() > headerHeight;
@ -160,25 +165,22 @@ let setupPageShow = window.setupPageShow = function (pageId) {
} }
function addPageHighlighting() { function addPageHighlighting() {
let $pageNav = null; let pageNav = document.querySelector('.sidebar-page-nav');
$(document).ready(function () { // fetch all the headings.
// fetch all the headings. let headings = document.querySelector('.page-content').querySelectorAll('h1, h2, h3, h4, h5, h6');
let headings = document.querySelector('.page-content').querySelectorAll('h1, h2, h3, h4, h5, h6'); // if headings are present, add observers.
// if headings are present, add observers. if (headings.length > 0) {
if (headings.length > 0) { addNavObserver(headings);
addNavObserver(headings); }
}
});
function addNavObserver(headings) { function addNavObserver(headings) {
// Setup the intersection observer. // Setup the intersection observer.
let intersectOpts = { let intersectOpts = {
rootMargin: '0px 0px 0px 0px', rootMargin: '0px 0px 0px 0px',
threshold: 1.0 threshold: 1.0
} };
$pageNav = $('.sidebar-page-nav'); let pageNavObserver = new IntersectionObserver(headingVisibilityChange, intersectOpts);
let pageNavObserver = new IntersectionObserver(cbHeadingVisible, intersectOpts);
// observe each heading // observe each heading
for (let i = 0; i !== headings.length; ++i) { for (let i = 0; i !== headings.length; ++i) {
@ -186,26 +188,25 @@ let setupPageShow = window.setupPageShow = function (pageId) {
} }
} }
function cbHeadingVisible(entries, observer) { function headingVisibilityChange(entries, observer) {
for (let i = 0; i !== entries.length; ++i) { for (let i = 0; i < entries.length; i++) {
let currentEntry = entries[i]; let currentEntry = entries[i];
let element = currentEntry.target; let isVisible = (currentEntry.intersectionRatio === 1);
// check if its currently visible toggleAnchorHighlighting(currentEntry.target.id, isVisible);
if (currentEntry.intersectionRatio === 1) {
highlightElement(element.id);
} else {
removeHighlight(element.id);
}
} }
} }
function highlightElement(elementId) { function toggleAnchorHighlighting(elementId, shouldHighlight) {
$pageNav.find('a[href="#' + elementId + '"]').addClass('current-heading'); let anchorsToHighlight = pageNav.querySelectorAll('a[href="#' + elementId + '"]');
} for (let i = 0; i < anchorsToHighlight.length; i++) {
// Change below to use classList.toggle when IE support is dropped.
function removeHighlight(elementId) { if (shouldHighlight) {
$pageNav.find('a[href="#' + elementId + '"]').removeClass('current-heading'); anchorsToHighlight[i].classList.add('current-heading');
} } else {
anchorsToHighlight[i].classList.remove('current-heading');
}
}
}
} }
}; };