2018-04-10 09:04:43 +00:00
|
|
|
/* global maxPages */
|
|
|
|
|
2017-06-21 12:10:09 +00:00
|
|
|
// Code snippet inspired by https://github.com/douglasrodrigues5/ghost-blog-infinite-scroll
|
2017-06-27 11:03:51 +00:00
|
|
|
$(function ($) {
|
2017-07-10 12:53:42 +00:00
|
|
|
var currentPage = 1;
|
|
|
|
var pathname = window.location.pathname;
|
|
|
|
var $document = $(document);
|
|
|
|
var $result = $('.post-feed');
|
2018-04-09 10:03:48 +00:00
|
|
|
var buffer = 300;
|
2017-06-21 12:10:09 +00:00
|
|
|
|
2017-07-10 12:53:42 +00:00
|
|
|
var ticking = false;
|
|
|
|
var isLoading = false;
|
|
|
|
|
|
|
|
var lastScrollY = window.scrollY;
|
|
|
|
var lastWindowHeight = window.innerHeight;
|
|
|
|
var lastDocumentHeight = $document.height();
|
|
|
|
|
|
|
|
function onScroll() {
|
|
|
|
lastScrollY = window.scrollY;
|
|
|
|
requestTick();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onResize() {
|
|
|
|
lastWindowHeight = window.innerHeight;
|
|
|
|
lastDocumentHeight = $document.height();
|
|
|
|
requestTick();
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestTick() {
|
|
|
|
if (!ticking) {
|
2018-04-09 10:03:48 +00:00
|
|
|
requestAnimationFrame(infiniteScroll);
|
2017-07-10 12:53:42 +00:00
|
|
|
}
|
|
|
|
ticking = true;
|
|
|
|
}
|
|
|
|
|
2018-04-10 09:04:43 +00:00
|
|
|
function sanitizePathname(path) {
|
|
|
|
var paginationRegex = /(?:page\/)(\d)(?:\/)$/i;
|
|
|
|
|
|
|
|
// remove hash params from path
|
|
|
|
path = path.replace(/#(.*)$/g, '').replace('////g', '/');
|
|
|
|
|
|
|
|
// remove pagination from the path and replace the current pages
|
|
|
|
// with the actual requested page. E. g. `/page/3/` indicates that
|
|
|
|
// the user actually requested page 3, so we should request page 4
|
|
|
|
// next, unless it's the last page already.
|
|
|
|
if (path.match(paginationRegex)) {
|
|
|
|
currentPage = parseInt(path.match(paginationRegex)[1]);
|
|
|
|
|
|
|
|
path = path.replace(paginationRegex, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-04-09 10:03:48 +00:00
|
|
|
function infiniteScroll() {
|
2018-04-10 09:04:43 +00:00
|
|
|
// sanitize the pathname from possible pagination or hash params
|
|
|
|
pathname = sanitizePathname(pathname);
|
|
|
|
|
2017-07-10 12:53:42 +00:00
|
|
|
// return if already loading
|
|
|
|
if (isLoading) {
|
2017-06-27 11:03:51 +00:00
|
|
|
return;
|
2017-06-21 12:10:09 +00:00
|
|
|
}
|
2017-06-27 11:03:51 +00:00
|
|
|
|
2017-07-10 12:53:42 +00:00
|
|
|
// return if not scroll to the bottom
|
|
|
|
if (lastScrollY + lastWindowHeight <= lastDocumentHeight - buffer) {
|
|
|
|
ticking = false;
|
|
|
|
return;
|
2017-06-27 11:03:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 09:04:43 +00:00
|
|
|
/**
|
|
|
|
* maxPages is defined in default.hbs and is the value
|
|
|
|
* of the amount of pagination pages.
|
|
|
|
* If we reached the last page or are past it,
|
|
|
|
* we return and disable the listeners.
|
|
|
|
*/
|
|
|
|
if (currentPage >= maxPages) {
|
|
|
|
window.removeEventListener('scroll', onScroll, {passive: true});
|
|
|
|
window.removeEventListener('resize', onResize);
|
2017-07-11 05:18:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-10 12:53:42 +00:00
|
|
|
isLoading = true;
|
|
|
|
|
2017-06-27 11:03:51 +00:00
|
|
|
// next page
|
2018-04-10 09:04:43 +00:00
|
|
|
currentPage += 1;
|
2017-06-27 11:03:51 +00:00
|
|
|
|
|
|
|
// Load more
|
2017-07-10 12:53:42 +00:00
|
|
|
var nextPage = pathname + 'page/' + currentPage + '/';
|
|
|
|
|
|
|
|
$.get(nextPage, function (content) {
|
2018-04-09 10:03:48 +00:00
|
|
|
var parse = document.createRange().createContextualFragment(content);
|
|
|
|
var posts = parse.querySelectorAll('.post');
|
|
|
|
if (posts.length) {
|
|
|
|
[].forEach.call(posts, function (post) {
|
|
|
|
$result[0].appendChild(post);
|
|
|
|
});
|
|
|
|
}
|
2017-07-10 12:53:42 +00:00
|
|
|
}).fail(function (xhr) {
|
|
|
|
// 404 indicates we've run out of pages
|
|
|
|
if (xhr.status === 404) {
|
|
|
|
window.removeEventListener('scroll', onScroll, {passive: true});
|
|
|
|
window.removeEventListener('resize', onResize);
|
|
|
|
}
|
|
|
|
}).always(function () {
|
|
|
|
lastDocumentHeight = $document.height();
|
|
|
|
isLoading = false;
|
|
|
|
ticking = false;
|
2017-06-27 11:03:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-10 12:53:42 +00:00
|
|
|
window.addEventListener('scroll', onScroll, {passive: true});
|
|
|
|
window.addEventListener('resize', onResize);
|
|
|
|
|
|
|
|
infiniteScroll();
|
2017-06-21 12:10:09 +00:00
|
|
|
});
|