2018-06-01 20:55:26 -04:00
|
|
|
|
let loaderOffset = 10;
|
|
|
|
|
let loadCount = 10;
|
2019-03-30 18:21:01 -04:00
|
|
|
|
let loaderReachedEnd = false;
|
2019-03-29 22:56:56 -04:00
|
|
|
|
let startAt = null;
|
2018-05-30 21:50:20 -04:00
|
|
|
|
let isLoaderLoading = false;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
let loadUri = '';
|
|
|
|
|
let loaderResponseId = '';
|
|
|
|
|
|
2019-02-27 21:13:15 -05:00
|
|
|
|
function initLoader(location, loaderId, count = 10, start = count) {
|
2018-05-28 21:30:31 -04:00
|
|
|
|
loadUri = location;
|
|
|
|
|
loaderResponseId = loaderId;
|
2018-06-01 20:55:26 -04:00
|
|
|
|
loadCount = count;
|
2019-02-27 21:13:15 -05:00
|
|
|
|
loaderOffset = start;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
setupListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadMoreItems() {
|
2019-03-30 18:21:01 -04:00
|
|
|
|
if (isLoaderLoading || loaderReachedEnd) {
|
2018-05-28 21:30:31 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showLoader();
|
2018-05-30 21:50:20 -04:00
|
|
|
|
isLoaderLoading = true;
|
2019-03-29 22:56:56 -04:00
|
|
|
|
$.get(loadUri, { offset: loaderOffset, count: loadCount, startAt: startAt })
|
2018-05-28 21:30:31 -04:00
|
|
|
|
.done(function (response) {
|
|
|
|
|
$(loaderResponseId).append(response);
|
|
|
|
|
if (response.trim().length === 0) {
|
|
|
|
|
staleLoader();
|
2019-03-30 18:21:01 -04:00
|
|
|
|
loaderReachedEnd = true;
|
|
|
|
|
$('.loader-load-more').addClass('disabled');
|
2018-05-28 21:30:31 -04:00
|
|
|
|
}
|
2018-05-31 20:17:52 -04:00
|
|
|
|
$(document).trigger("loaderFinished", response);
|
2019-03-29 22:56:56 -04:00
|
|
|
|
startAt = $(response).filter('.loader-data-time').last().data('time');
|
2018-05-28 21:30:31 -04:00
|
|
|
|
hideLoader();
|
2018-05-30 21:50:20 -04:00
|
|
|
|
isLoaderLoading = false;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
})
|
|
|
|
|
.fail(function (jqxhr, statis, error) {
|
|
|
|
|
errorLoader();
|
2018-05-30 21:50:20 -04:00
|
|
|
|
isLoaderLoading = false;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
});
|
2018-05-30 21:50:20 -04:00
|
|
|
|
loaderOffset += loadCount;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 18:06:07 -04:00
|
|
|
|
var hasScrollBar = false;
|
|
|
|
|
|
2019-07-24 11:36:37 -04:00
|
|
|
|
function _ScrollHandler(e) {
|
2019-07-19 15:54:39 -04:00
|
|
|
|
//throttle event:
|
2019-08-04 18:06:07 -04:00
|
|
|
|
/*
|
|
|
|
|
https://stackoverflow.com/questions/3898130/check-if-a-user-has-scrolled-to-the-bottom
|
|
|
|
|
*/
|
|
|
|
|
var $window = $(window);
|
|
|
|
|
var $document = $(document);
|
2019-07-19 15:54:39 -04:00
|
|
|
|
hasScrollBar = true;
|
2019-08-04 18:06:07 -04:00
|
|
|
|
let _throttleTimer = null;
|
|
|
|
|
let _throttleDelay = 100;
|
|
|
|
|
|
2019-07-19 15:54:39 -04:00
|
|
|
|
clearTimeout(_throttleTimer);
|
|
|
|
|
_throttleTimer = setTimeout(function () {
|
|
|
|
|
|
|
|
|
|
//do work
|
|
|
|
|
if ($window.scrollTop() + $window.height() > $document.height() - 100) {
|
|
|
|
|
loadMoreItems();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, _throttleDelay);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 21:30:31 -04:00
|
|
|
|
function setupListeners() {
|
2019-03-29 22:56:56 -04:00
|
|
|
|
if ($(loaderResponseId).length === 1) {
|
|
|
|
|
/*
|
|
|
|
|
https://stackoverflow.com/questions/19731730/jquery-js-detect-users-scroll-attempt-without-any-window-overflow-to-scroll
|
|
|
|
|
*/
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-03-29 22:56:56 -04:00
|
|
|
|
$('html').bind('mousewheel DOMMouseScroll', function (e) {
|
|
|
|
|
var delta = e.originalEvent.wheelDelta || -e.originalEvent.detail;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-03-29 22:56:56 -04:00
|
|
|
|
if (delta < 0 && !hasScrollBar) {
|
|
|
|
|
loadMoreItems();
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-04 18:06:07 -04:00
|
|
|
|
$(document).ready(function () {
|
|
|
|
|
$(window)
|
2019-07-24 11:36:37 -04:00
|
|
|
|
.off('scroll', _ScrollHandler)
|
|
|
|
|
.on('scroll', _ScrollHandler);
|
2019-03-30 18:21:01 -04:00
|
|
|
|
$('.loader-load-more:not(.disabled)').click(function (e) {
|
2019-03-29 22:56:56 -04:00
|
|
|
|
if (!isLoaderLoading) {
|
|
|
|
|
loadMoreItems();
|
|
|
|
|
}
|
2019-07-19 15:54:39 -04:00
|
|
|
|
});
|
2019-03-29 22:56:56 -04:00
|
|
|
|
});
|
2018-05-28 21:30:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|