Files
com.xaymar.www/assets/site.js
T
Michael Fabian 'Xaymar' Dirks 5127fb0cd8 Fix lazy loading breaking the site
2022-01-12 06:16:37 +01:00

47 lines
1.1 KiB
JavaScript

(function() {
'use strict';
// Support for Mobile Devices
document.querySelector("#navigation-toggle").addEventListener("click", (ev) => {
document.querySelector("#header #navigation").classList.toggle("open");
});
{ // Lazily load Media
let info = {}
new Promise((resolve, reject) => {
// Figure out what to load.
info.elements = document.querySelectorAll(".block-media > [data-lazyload]");
info.stack = Array.from(info.elements);
console.debug("Lazily loading " + info.stack.length + " elements...");
// Begin processing at 1ms intervals.
info.cbid = 0;
info.cbid = window.setInterval(() => {
let el = info.stack.pop();
if (el === undefined) {
reject();
return;
}
let lazycontent = el.querySelector("noscript");
if (lazycontent === undefined) {
reject();
return;
}
el.innerHTML = lazycontent.innerHTML;
if (info.stack.length == 0) {
window.clearInterval(info.cbid);
resolve();
return;
}
}, 1);
}).then(() => {
window.clearInterval(info.cbid);
}).catch(() => {
}).finally(() => {
console.debug("Lazy loading complete.");
});
}
})();