1 line
7.7 KiB
JavaScript
1 line
7.7 KiB
JavaScript
const u=typeof window<"u",j=u&&!("onscroll"in window)||typeof navigator<"u"&&/(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent),J=u&&"IntersectionObserver"in window,U=u&&"classList"in document.createElement("p"),q=u&&window.devicePixelRatio>1,pt={elements_selector:".lazy",container:j||u?document:null,threshold:300,thresholds:null,data_src:"src",data_srcset:"srcset",data_sizes:"sizes",data_bg:"bg",data_bg_hidpi:"bg-hidpi",data_bg_multi:"bg-multi",data_bg_multi_hidpi:"bg-multi-hidpi",data_bg_set:"bg-set",data_poster:"poster",class_applied:"applied",class_loading:"loading",class_loaded:"loaded",class_error:"error",class_entered:"entered",class_exited:"exited",unobserve_completed:!0,unobserve_entered:!1,cancel_on_exit:!0,callback_enter:null,callback_exit:null,callback_applied:null,callback_loading:null,callback_loaded:null,callback_error:null,callback_finish:null,callback_cancel:null,use_native:!1,restore_on_error:!1},W=t=>Object.assign({},pt,t),B=function(t,o){let r;const s="LazyLoad::Initialized",a=new t(o);try{r=new CustomEvent(s,{detail:{instance:a}})}catch{r=document.createEvent("CustomEvent"),r.initCustomEvent(s,!1,!1,{instance:a})}window.dispatchEvent(r)},vt=(t,o)=>{if(o)if(!o.length)B(t,o);else for(let r=0,s;s=o[r];r+=1)B(t,s)},i="src",C="srcset",w="sizes",Z="poster",v="llOriginalAttrs",K="data",y="loading",Q="loaded",X="applied",Et="entered",x="error",Y="native",m="data-",tt="ll-status",c=(t,o)=>t.getAttribute(m+o),It=(t,o,r)=>{const s=m+o;if(r===null){t.removeAttribute(s);return}t.setAttribute(s,r)},E=t=>c(t,tt),f=(t,o)=>It(t,tt,o),L=t=>f(t,null),N=t=>E(t)===null,Lt=t=>E(t)===y,St=t=>E(t)===x,V=t=>E(t)===Y,At=[y,Q,X,x],Ot=t=>At.indexOf(E(t))>=0,l=(t,o,r,s)=>{if(!(!t||typeof t!="function")){if(s!==void 0){t(o,r,s);return}if(r!==void 0){t(o,r);return}t(o)}},h=(t,o)=>{if(o!==""){if(U){t.classList.add(o);return}t.className+=(t.className?" ":"")+o}},n=(t,o)=>{if(o!==""){if(U){t.classList.remove(o);return}t.className=t.className.replace(new RegExp("(^|\\s+)"+o+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")}},kt=t=>{t.llTempImage=document.createElement("IMG")},Tt=t=>{delete t.llTempImage},ot=t=>t.llTempImage,S=(t,o)=>{if(!o)return;const r=o._observer;r&&r.unobserve(t)},Ct=t=>{t.disconnect()},wt=(t,o,r)=>{o.unobserve_entered&&S(t,r)},D=(t,o)=>{t&&(t.loadingCount+=o)},yt=t=>{t&&(t.toLoadCount-=1)},rt=(t,o)=>{t&&(t.toLoadCount=o)},xt=t=>t.loadingCount>0,Nt=t=>t.toLoadCount>0,st=t=>{let o=[];for(let r=0,s;s=t.children[r];r+=1)s.tagName==="SOURCE"&&o.push(s);return o},H=(t,o)=>{const r=t.parentNode;if(!r||r.tagName!=="PICTURE")return;st(r).forEach(o)},at=(t,o)=>{st(t).forEach(o)},A=[i],ct=[i,Z],p=[i,C,w],et=[K],O=t=>!!t[v],nt=t=>t[v],it=t=>delete t[v],b=(t,o)=>{if(O(t))return;const r={};o.forEach(s=>{r[s]=t.getAttribute(s)}),t[v]=r},Vt=t=>{O(t)||(t[v]={backgroundImage:t.style.backgroundImage})},Dt=(t,o,r)=>{if(!r){t.removeAttribute(o);return}t.setAttribute(o,r)},g=(t,o)=>{if(!O(t))return;const r=nt(t);o.forEach(s=>{Dt(t,s,r[s])})},Ht=t=>{if(!O(t))return;const o=nt(t);t.style.backgroundImage=o.backgroundImage},dt=(t,o,r)=>{h(t,o.class_applied),f(t,X),r&&(o.unobserve_completed&&S(t,o),l(o.callback_applied,t,r))},ut=(t,o,r)=>{h(t,o.class_loading),f(t,y),r&&(D(r,1),l(o.callback_loading,t,r))},d=(t,o,r)=>{r&&t.setAttribute(o,r)},F=(t,o)=>{d(t,w,c(t,o.data_sizes)),d(t,C,c(t,o.data_srcset)),d(t,i,c(t,o.data_src))},Rt=(t,o)=>{H(t,r=>{b(r,p),F(r,o)}),b(t,p),F(t,o)},zt=(t,o)=>{b(t,A),d(t,i,c(t,o.data_src))},Mt=(t,o)=>{at(t,r=>{b(r,A),d(r,i,c(r,o.data_src))}),b(t,ct),d(t,Z,c(t,o.data_poster)),d(t,i,c(t,o.data_src)),t.load()},Bt=(t,o)=>{b(t,et),d(t,K,c(t,o.data_src))},Ft=(t,o,r)=>{const s=c(t,o.data_bg),a=c(t,o.data_bg_hidpi),e=q&&a?a:s;e&&(t.style.backgroundImage=`url("${e}")`,ot(t).setAttribute(i,e),ut(t,o,r))},Gt=(t,o,r)=>{const s=c(t,o.data_bg_multi),a=c(t,o.data_bg_multi_hidpi),e=q&&a?a:s;e&&(t.style.backgroundImage=e,dt(t,o,r))},Pt=(t,o,r)=>{const s=c(t,o.data_bg_set);if(!s)return;const a=s.split("|");let e=a.map(_=>`image-set(${_})`);t.style.backgroundImage=e.join(),t.style.backgroundImage===""&&(e=a.map(_=>`-webkit-image-set(${_})`),t.style.backgroundImage=e.join()),dt(t,o,r)},lt={IMG:Rt,IFRAME:zt,VIDEO:Mt,OBJECT:Bt},$t=(t,o)=>{const r=lt[t.tagName];r&&r(t,o)},jt=(t,o,r)=>{const s=lt[t.tagName];s&&(s(t,o),ut(t,o,r))},Jt=["IMG","IFRAME","VIDEO","OBJECT"],Ut=t=>Jt.indexOf(t.tagName)>-1,_t=(t,o)=>{o&&!xt(o)&&!Nt(o)&&l(t.callback_finish,o)},G=(t,o,r)=>{t.addEventListener(o,r),t.llEvLisnrs[o]=r},qt=(t,o,r)=>{t.removeEventListener(o,r)},R=t=>!!t.llEvLisnrs,Wt=(t,o,r)=>{R(t)||(t.llEvLisnrs={});const s=t.tagName==="VIDEO"?"loadeddata":"load";G(t,s,o),G(t,"error",r)},T=t=>{if(!R(t))return;const o=t.llEvLisnrs;for(let r in o){const s=o[r];qt(t,r,s)}delete t.llEvLisnrs},gt=(t,o,r)=>{Tt(t),D(r,-1),yt(r),n(t,o.class_loading),o.unobserve_completed&&S(t,r)},Zt=(t,o,r,s)=>{const a=V(o);gt(o,r,s),h(o,r.class_loaded),f(o,Q),l(r.callback_loaded,o,s),a||_t(r,s)},Kt=(t,o,r,s)=>{const a=V(o);gt(o,r,s),h(o,r.class_error),f(o,x),l(r.callback_error,o,s),r.restore_on_error&&g(o,p),a||_t(r,s)},z=(t,o,r)=>{const s=ot(t)||t;if(R(s))return;Wt(s,_=>{Zt(_,t,o,r),T(s)},_=>{Kt(_,t,o,r),T(s)})},Qt=(t,o,r)=>{kt(t),z(t,o,r),Vt(t),Ft(t,o,r),Gt(t,o,r),Pt(t,o,r)},Xt=(t,o,r)=>{z(t,o,r),jt(t,o,r)},M=(t,o,r)=>{Ut(t)?Xt(t,o,r):Qt(t,o,r)},Yt=(t,o,r)=>{t.setAttribute("loading","lazy"),z(t,o,r),$t(t,o),f(t,Y)},P=t=>{t.removeAttribute(i),t.removeAttribute(C),t.removeAttribute(w)},mt=t=>{H(t,o=>{P(o)}),P(t)},ft=t=>{H(t,o=>{g(o,p)}),g(t,p)},to=t=>{at(t,o=>{g(o,A)}),g(t,ct),t.load()},oo=t=>{g(t,A)},ro=t=>{g(t,et)},so={IMG:ft,IFRAME:oo,VIDEO:to,OBJECT:ro},ao=t=>{const o=so[t.tagName];if(!o){Ht(t);return}o(t)},co=(t,o)=>{N(t)||V(t)||(n(t,o.class_entered),n(t,o.class_exited),n(t,o.class_applied),n(t,o.class_loading),n(t,o.class_loaded),n(t,o.class_error))},eo=(t,o)=>{ao(t),co(t,o),L(t),it(t)},no=(t,o,r,s)=>{r.cancel_on_exit&&Lt(t)&&t.tagName==="IMG"&&(T(t),mt(t),ft(t),n(t,r.class_loading),D(s,-1),L(t),l(r.callback_cancel,t,o,s))},io=(t,o,r,s)=>{const a=Ot(t);f(t,Et),h(t,r.class_entered),n(t,r.class_exited),wt(t,r,s),l(r.callback_enter,t,o,s),!a&&M(t,r,s)},uo=(t,o,r,s)=>{N(t)||(h(t,r.class_exited),no(t,o,r,s),l(r.callback_exit,t,o,s))},lo=["IMG","IFRAME","VIDEO"],bt=t=>t.use_native&&"loading"in HTMLImageElement.prototype,_o=(t,o,r)=>{t.forEach(s=>{lo.indexOf(s.tagName)!==-1&&Yt(s,o,r)}),rt(r,0)},go=t=>t.isIntersecting||t.intersectionRatio>0,fo=t=>({root:t.container===document?null:t.container,rootMargin:t.thresholds||t.threshold+"px"}),bo=(t,o,r)=>{t.forEach(s=>go(s)?io(s.target,s,o,r):uo(s.target,s,o,r))},ho=(t,o)=>{o.forEach(r=>{t.observe(r)})},po=(t,o)=>{Ct(t),ho(t,o)},vo=(t,o)=>{!J||bt(t)||(o._observer=new IntersectionObserver(r=>{bo(r,t,o)},fo(t)))},ht=t=>Array.prototype.slice.call(t),I=t=>t.container.querySelectorAll(t.elements_selector),Eo=t=>ht(t).filter(N),Io=t=>St(t),Lo=t=>ht(t).filter(Io),$=(t,o)=>Eo(t||I(o)),So=(t,o)=>{Lo(I(t)).forEach(s=>{n(s,t.class_error),L(s)}),o.update()},Ao=(t,o)=>{u&&(o._onlineHandler=()=>{So(t,o)},window.addEventListener("online",o._onlineHandler))},Oo=t=>{u&&window.removeEventListener("online",t._onlineHandler)},k=function(t,o){const r=W(t);this._settings=r,this.loadingCount=0,vo(r,this),Ao(r,this),this.update(o)};k.prototype={update:function(t){const o=this._settings,r=$(t,o);if(rt(this,r.length),j||!J){this.loadAll(r);return}if(bt(o)){_o(r,o,this);return}po(this._observer,r)},destroy:function(){this._observer&&this._observer.disconnect(),Oo(this),I(this._settings).forEach(t=>{it(t)}),delete this._observer,delete this._settings,delete this._onlineHandler,delete this.loadingCount,delete this.toLoadCount},loadAll:function(t){const o=this._settings;$(t,o).forEach(s=>{S(s,this),M(s,o,this)})},restoreAll:function(){const t=this._settings;I(t).forEach(o=>{eo(o,t)})}};k.load=(t,o)=>{const r=W(o);M(t,r)};k.resetStatus=t=>{L(t)};u&&vt(k,window.lazyLoadOptions);export{k as L};
|