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 = '';
|
2020-08-17 22:21:11 -04:00
|
|
|
|
let additionalParams = [];
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2020-08-20 14:10:43 -04:00
|
|
|
|
function initLoader(location, loaderId, count = 10, start = count, additional = []) {
|
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;
|
2020-08-20 14:10:43 -04:00
|
|
|
|
additionalParams = additional;
|
2022-04-19 19:43:58 -04:00
|
|
|
|
|
|
|
|
|
setupMonitor();
|
|
|
|
|
|
|
|
|
|
$('#loaderLoad').click(function () {
|
|
|
|
|
loadMoreItems();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setupMonitor() {
|
|
|
|
|
const element = document.querySelector('#loaderLoad')
|
|
|
|
|
const observer = new window.IntersectionObserver(([entry]) => {
|
|
|
|
|
if (entry.isIntersecting && $('.content-wrapper').scrollTop() > 10) {
|
|
|
|
|
loadMoreItems();
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
root: null,
|
|
|
|
|
threshold: 1,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
observer.observe(element);
|
2018-05-28 21:30:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2022-04-19 19:43:58 -04:00
|
|
|
|
let params = {offset: loaderOffset, count: loadCount, startAt: startAt};
|
|
|
|
|
for (let i = 0; i < additionalParams.length; i++) {
|
2020-08-17 22:21:11 -04:00
|
|
|
|
let param = additionalParams[i];
|
2022-04-19 19:43:58 -04:00
|
|
|
|
params[param.name] = param.value instanceof Function ? param.value() : param.value;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$.get(loadUri, params)
|
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;
|
2022-04-19 19:43:58 -04:00
|
|
|
|
$('.loader-load-more').remove('text-primary').addClass('text-muted');
|
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
|
|
|
|
})
|
2022-04-19 19:43:58 -04:00
|
|
|
|
.fail(function () {
|
2018-05-28 21:30:31 -04:00
|
|
|
|
errorLoader();
|
2022-04-19 19:43:58 -04:00
|
|
|
|
halfmoon.initStickyAlert({
|
|
|
|
|
content: 'Could not load more items...',
|
|
|
|
|
title: 'Error',
|
|
|
|
|
alertType: 'alert-danger',
|
|
|
|
|
fillType: 'filled'
|
|
|
|
|
});
|
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
|
|
|
|
}
|