IW4M-Admin/WebfrontCore/wwwroot/js/loader.js

85 lines
2.4 KiB
JavaScript
Raw Normal View History

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;
let startAt = null;
let isLoaderLoading = false;
let loadUri = '';
let loaderResponseId = '';
let additionalParams = [];
function initLoader(location, loaderId, count = 10, start = count, additional = []) {
loadUri = location;
loaderResponseId = loaderId;
2018-06-01 20:55:26 -04:00
loadCount = count;
loaderOffset = start;
additionalParams = additional;
2022-04-19 19:43:58 -04:00
2022-04-28 12:42:23 -04:00
try {
setupMonitor();
}
catch {
// ignored (can happen when the action modal loader exists but no page level loader does)
}
2022-04-19 19:43:58 -04:00
$('#loaderLoad').click(function () {
loadMoreItems();
});
2022-04-28 12:42:23 -04:00
$('.loader-load-more').click(function() {
loadMoreItems();
})
2022-04-19 19:43:58 -04:00
}
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);
}
function loadMoreItems() {
2019-03-30 18:21:01 -04:00
if (isLoaderLoading || loaderReachedEnd) {
return false;
}
showLoader();
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++) {
let param = additionalParams[i];
2022-04-19 19:43:58 -04:00
params[param.name] = param.value instanceof Function ? param.value() : param.value;
}
$.get(loadUri, params)
.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-31 20:17:52 -04:00
$(document).trigger("loaderFinished", response);
startAt = $(response).filter('.loader-data-time').last().data('time');
hideLoader();
isLoaderLoading = false;
})
2022-04-19 19:43:58 -04:00
.fail(function () {
errorLoader();
2022-04-19 19:43:58 -04:00
halfmoon.initStickyAlert({
content: 'Could not load more items...',
title: 'Error',
alertType: 'alert-danger',
fillType: 'filled'
});
isLoaderLoading = false;
});
loaderOffset += loadCount;
}