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

103 lines
2.8 KiB
JavaScript
Raw Permalink 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 = '';
2023-01-23 17:38:16 -05:00
let additionalParams = undefined;
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};
2023-01-23 17:38:16 -05:00
if (additionalParams instanceof Function) {
params = {
...params,
...flatParams(additionalParams())
}
} else {
for (let i = 0; i < additionalParams.length; i++) {
let param = additionalParams[i];
params[param.name] = param.value instanceof Function ? param.value() : param.value;
}
}
$.get(loadUri, params)
.done(function (response) {
$(loaderResponseId).append(response);
2023-01-23 17:38:16 -05:00
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({
2022-07-05 13:02:43 -04:00
content: _localization['WEBFRONT_SCRIPT_LOADER_ERROR'],
2022-04-19 19:43:58 -04:00
title: 'Error',
alertType: 'alert-danger',
fillType: 'filled'
});
isLoaderLoading = false;
});
loaderOffset += loadCount;
}
2023-01-23 17:38:16 -05:00
function flatParams(params) {
return params.map(function (b) {
return {[b.name]: b.value}
}).reduce(function (prev, curr) {
for (const key in curr) prev[key] = curr[key];
return prev;
})
}