Skip to content

Commit

Permalink
Merge pull request #2 from binchoo/fix-active-content
Browse files Browse the repository at this point in the history
  • Loading branch information
binchoo authored May 17, 2021
2 parents 20adfaa + 96e93be commit 36f258d
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions src/js/gumshoe/gumshoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
nested: false,
nestedClass: 'active',

// Offset & reflow
// Offset & reflow & click
offset: 0,
reflow: false,
click: false,

// Event support
events: true
Expand Down Expand Up @@ -161,7 +162,7 @@
* @return {Boolean} If true, page is at the bottom of the viewport
*/
var isAtBottom = function () {
if (window.innerHeight + window.pageYOffset >= getDocumentHeight()) return true;
if (Math.round(window.innerHeight + window.pageYOffset) >= getDocumentHeight()) return true;
return false;
};

Expand Down Expand Up @@ -374,6 +375,32 @@

};

/**
* Get which content is clicked and to be activated.
*/
publicAPIs.click = function (clicked) {

// if there's no active content, deactivate and bail
if (!clicked) {
if (current) {
deactivate(current, settings);
current = null;
}
return;
}

// If the active content is the one currently active, do nothing
if (current && clicked.content === current.content) return;

// Deactivate the current content and activate the new content
deactivate(current, settings);
activate(clicked, settings);

// Update the currently active content
current = clicked;

};

/**
* Detect the active content on scroll
* Debounced for performance
Expand Down Expand Up @@ -409,6 +436,43 @@

};

/**
* Add scrollHandler on next scroll
* Not debounced
*/
var attachScrollHandler = function (event) {
window.addEventListener('scroll', scrollHandler, false);
window.removeEventListener('scroll', attachScrollHandler, false);
}

/**
* Activate content clicked, and deactivate current
* Not debounced
*/
var clickHandler = function (event) {
var nav = event.path[0];

// If nav is invalid element
if (!nav || !nav.hash) return;

// If there's a timer, cancel it
if (timeout) {
window.cancelAnimationFrame(timeout);
}

sortContents(contents);

// Block scrollHandler
window.removeEventListener('scroll', scrollHandler, false);
publicAPIs.click({ nav: nav,
content: document.getElementById(decodeURIComponent(nav.hash.substr(1)))
});

// Reattach scrollHandler on next scroll-up
window.addEventListener('scroll', attachScrollHandler, false);

};

/**
* Destroy the current instantiation
*/
Expand All @@ -424,6 +488,9 @@
if (settings.reflow) {
window.removeEventListener('resize', resizeHandler, false);
}
if (settings.click) {
window.removeEventListener('click', clickHandler, false);
}

// Reset variables
contents = null;
Expand Down Expand Up @@ -453,6 +520,9 @@
if (settings.reflow) {
window.addEventListener('resize', resizeHandler, false);
}
if (settings.click) {
window.addEventListener('click', clickHandler, false);
}

};

Expand Down

0 comments on commit 36f258d

Please sign in to comment.