@fancyapps/ui
Version:
Robust JavaScript UI Component Library
1 lines • 2.18 kB
JavaScript
!/*! License details at fancyapps.com/license */function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).window=e.window||{})}(this,function(e){"use strict";let t=(e,t="")=>{e&&e.classList&&t.split(" ").forEach(t=>{t&&e.classList.add(t)})},a=(e,t="")=>{e&&e.classList&&t.split(" ").forEach(t=>{t&&e.classList.remove(t)})},o={showLoading:!0,preload:1},l="is-lazyloading",s="is-lazyloaded",i="has-lazyerror";e.Lazyload=()=>{let e;function n(){let t=e?.getOptions().Lazyload;return"object"==typeof t&&null!==t&&t.constructor===Object&&"[object Object]"===Object.prototype.toString.call(t)?{...o,...t}:o}function d(){if(!e)return;let o=[...e.getVisibleSlides()],d=n().preload;if(d>0){let t=e.getPosition(),a=e.getViewportDim();o.push(...e.getVisibleSlides(t+a*d),...e.getVisibleSlides(t-a*d))}for(let d of o)!function(o){let d=o.el;if(!d)return;let r="[data-lazy-src],[data-lazy-srcset],[data-lazy-bg]",c=Array.from(d.querySelectorAll(r));for(let f of(d.matches(r)&&c.push(d),c)){let d=f.dataset.lazySrc,r=f.dataset.lazySrcset,c=f.dataset.lazySizes,y=f.dataset.lazyBg,u=(f instanceof HTMLImageElement||f instanceof HTMLSourceElement)&&(d||r),z=f instanceof HTMLElement&&y;if(!(u||z))continue;let g=d||r||y;if(g){if(u&&g){let y=f.parentElement?.classList.contains("f-panzoom__wrapper");n().showLoading&&e?.showLoading(o),f.addEventListener("load",()=>{e?.hideLoading(o),a(f,i),f instanceof HTMLImageElement?f.decode().then(()=>{a(f,l),t(f,s)}):(a(f,l),t(f,s)),y||e?.emit("lazyLoad:loaded",o,f,g)}),f.addEventListener("error",()=>{e?.hideLoading(o),a(f,l),t(f,i),y||e?.emit("lazyLoad:error",o,f,g)}),f.classList.add("f-lazyload"),f.classList.add(l),y||e?.emit("lazyLoad:load",o,f,g),d&&(f.src=d),r&&(f.srcset=r),c&&(f.sizes=c)}else z&&(document.body.contains(f)||(document.createElement("img").src=y),f.style.backgroundImage=`url('${y}')`);delete f.dataset.lazySrc,delete f.dataset.lazySrcset,delete f.dataset.lazySizes,delete f.dataset.lazyBg}}}(d)}return{init:function(t){(e=t).on("render",d)},destroy:function(){e?.off("render",d),e=void 0}}}});