123movies-seo/resources/js/load-more.js

70 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2024-08-26 20:50:06 +03:00
// import LazyLoad from "vanilla-lazyload";
2024-08-24 23:08:42 +03:00
window.lazyFunctions = {
more: function (element) {
const elementHold = element;
const params = JSON.parse(atob(element.dataset.params)),
myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Accept", "application/json");
// console.log(params);
const postBody = {
page: params.page,
route: params.route
};
if ('genre' in params.route_parameters) {
postBody.genre = params.route_parameters.genre;
}
2024-08-29 05:49:54 +03:00
if ('person_id' in params.route_parameters) {
postBody.person_id = params.route_parameters.person_id;
}
2024-08-27 18:46:07 +03:00
if ('search' in params) {
postBody.search = params.search;
}
2024-08-24 23:08:42 +03:00
const requestOptions = {
method: "POST",
headers: myHeaders,
body: JSON.stringify(postBody),
// redirect: "follow"
};
fetch(params.route, requestOptions)
.then((response) => response.json())
.then((result) => {
const parentNode = element.parentNode;
parentNode.innerHTML += result.html;
window.lLoad.update();
element.remove();
document.getElementById(element.getAttribute('id')).remove()
if (result.has_more_pages) {
params.page = result.current_page + 1;
elementHold.setAttribute('href', elementHold.getAttribute('href').replace(new RegExp(result.current_page + '$'), result.current_page + 1));
elementHold.setAttribute('data-params', btoa(JSON.stringify(params)));
elementHold.removeAttribute('data-ll-status');
elementHold.classList.remove('entered');
parentNode.appendChild(elementHold);
window.lMore.update();
}
})
.catch((error) => console.error(error));
},
};
function executeLazyFunction(element) {
var lazyFunctionName = element.getAttribute("data-lazy-function");
var lazyFunction = window.lazyFunctions[lazyFunctionName];
if (!lazyFunction) return;
lazyFunction(element);
}
2024-08-26 20:50:06 +03:00
window.lMore = new window.LazyLoad({
2024-08-24 23:08:42 +03:00
elements_selector: "#next-page",
unobserve_entered: true, // <- Avoid executing the function multiple times
callback_enter: executeLazyFunction, // Assigning the function defined above
});