From fca98d952d836cdcd1d911698534696c9cae1b0a Mon Sep 17 00:00:00 2001 From: Colby Melvin Date: Fri, 18 Oct 2019 15:41:24 -0500 Subject: [PATCH] Fix scrollheight calculation bug (#4279) [ci skip] --- .../initializers/initScrolling.js.erb | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/initializers/initScrolling.js.erb b/app/assets/javascripts/initializers/initScrolling.js.erb index 241ff77b0..9b7411446 100644 --- a/app/assets/javascripts/initializers/initScrolling.js.erb +++ b/app/assets/javascripts/initializers/initScrolling.js.erb @@ -24,21 +24,38 @@ function checkIfNearBottomOfPage() { } function fetchNextPageIfNearBottom() { - var elCheck = document.getElementById("index-container") && !document.getElementById("query-wrapper"); + var indexContainer = document.getElementById("index-container"); + var elCheck = indexContainer && !document.getElementById("query-wrapper"); if (!elCheck) { return; } - if ( !done && !fetching && (window.scrollY) > (document.documentElement.scrollHeight - 3700) ) { + + var indexWhich = indexContainer.dataset.which; + + var fetchCallback; + var scrollableElemId; + if (indexWhich == "podcast-episodes") { + scrollableElemId = "articles-list"; + fetchCallback = function() { + fetchNextPodcastPage(indexContainer); + }; + } else if (indexWhich == "videos") { + scrollableElemId = "video-collection"; + fetchCallback = function() { + fetchNextVideoPage(indexContainer); + }; + } else { + scrollableElemId = "articles-list"; + fetchCallback = function() { + algoliaPaginate(indexContainer.dataset.algoliaTag); + }; + } + + var scrollableElem = document.getElementById(scrollableElemId); + + if ( !done && !fetching && (window.scrollY) > (scrollableElem.scrollHeight - 3700) ) { fetching = true; - var el = document.getElementById("index-container"); - var indexWhich = el.dataset.which; - if (indexWhich == "podcast-episodes") { - fetchNextPodcastPage(el); - } else if (indexWhich == "videos") { - fetchNextVideoPage(el) - } else { - algoliaPaginate(el.dataset.algoliaTag); - } + fetchCallback(); } }