From 7aa203150fc1a2ec79c681c13f0d486d531746eb Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Sun, 12 Mar 2023 23:57:48 +0200 Subject: error pages; js system to support back/front-end --- html/assets/js/images/lazyload.js | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 html/assets/js/images/lazyload.js (limited to 'html/assets/js/images') diff --git a/html/assets/js/images/lazyload.js b/html/assets/js/images/lazyload.js new file mode 100644 index 0000000..4f954af --- /dev/null +++ b/html/assets/js/images/lazyload.js @@ -0,0 +1,61 @@ +$$plugins.define('lazyload', function (element, options) { + const plugin = this; + plugin.logger.debug("lazy loading"); + const mutationObserver = + new MutationObserver(function (mutations) { + mutations.forEach(function (mutation, index) { + if (mutation.type === 'childList') { + mutation.addedNodes.forEach(update); + } + }); + }), + observer = + new IntersectionObserver(function (entries, self) { + entries.forEach(function (entry) { + if (entry.isIntersecting) { + load(entry.target); + self.unobserve(entry.target); + } + }); + }, options || { + rootMargin: '0px 0px 1250px 0px', + threshold: 0 + }), + elms = element.querySelectorAll('.lazy'); + elms.forEach(elm => observer.observe(elm)); + mutationObserver.observe(element, { childList: true, subtree: true }); + + function update(element) { + if (!(element instanceof (HTMLElement))) { + return; + } + const container = element || document; + if (!container.querySelectorAll) { + return; + } + const elms = container.querySelectorAll('.lazy'); + Array.prototype.forEach.call(elms, (elm) => !elm.getAttribute('data-loaded') && observer.observe(elm)); + } + + function load(elm) { + let i = 0, source, attr, picture = elm.parentElement, sources = picture.querySelectorAll('.lazysrcset'); + if (sources.length) { + for (; i < sources.length; i++) { + source = sources[i]; + attr = source.getAttribute('data-srcset'); + if (attr) { + source.srcset = source.getAttribute('data-srcset'); + } + } + } + + if (!elm.getAttribute('data-loaded') && elm.getAttribute('data-src')) { + elm.src = elm.getAttribute('data-src'); + elm.setAttribute('data-loaded', true); + } + } + + return { + update: update + }; +}); \ No newline at end of file -- cgit v1.2.3