1 line
7.8 KiB
JavaScript
1 line
7.8 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",T="srcset",C="sizes",Z="poster",v="llOriginalAttrs",K="data",y="loading",Q="loaded",X="applied",Et="entered",N="error",Y="native",m="data-",tt="ll-status",e=(t,o)=>t.getAttribute(m+o),Lt=(t,o,r)=>{const s=m+o;if(r===null){t.removeAttribute(s);return}t.setAttribute(s,r)},E=t=>e(t,tt),f=(t,o)=>Lt(t,tt,o),S=t=>f(t,null),x=t=>E(t)===null,It=t=>E(t)===y,St=t=>E(t)===N,V=t=>E(t)===Y,At=[y,Q,X,N],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}},c=(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")},wt=t=>{delete t.llTempImage},ot=t=>t.llTempImage,A=(t,o)=>{if(!o)return;const r=o._observer;r&&r.unobserve(t)},Tt=t=>{t.disconnect()},Ct=(t,o,r)=>{o.unobserve_entered&&A(t,r)},z=(t,o)=>{t&&(t.loadingCount+=o)},yt=t=>{t&&(t.toLoadCount-=1)},rt=(t,o)=>{t&&(t.toLoadCount=o)},Nt=t=>t.loadingCount>0,xt=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},D=(t,o)=>{const r=t.parentNode;if(!r||r.tagName!=="PICTURE")return;st(r).forEach(o)},at=(t,o)=>{st(t).forEach(o)},O=[i],et=[i,Z],p=[i,T,C],nt=[K],k=t=>!!t[v],ct=t=>t[v],it=t=>delete t[v],b=(t,o)=>{if(k(t))return;const r={};o.forEach(s=>{r[s]=t.getAttribute(s)}),t[v]=r},Vt=t=>{k(t)||(t[v]={backgroundImage:t.style.backgroundImage})},zt=(t,o,r)=>{if(!r){t.removeAttribute(o);return}t.setAttribute(o,r)},g=(t,o)=>{if(!k(t))return;const r=ct(t);o.forEach(s=>{zt(t,s,r[s])})},Dt=t=>{if(!k(t))return;const o=ct(t);t.style.backgroundImage=o.backgroundImage},dt=(t,o,r)=>{h(t,o.class_applied),f(t,X),r&&(o.unobserve_completed&&A(t,o),l(o.callback_applied,t,r))},ut=(t,o,r)=>{h(t,o.class_loading),f(t,y),r&&(z(r,1),l(o.callback_loading,t,r))},d=(t,o,r)=>{r&&t.setAttribute(o,r)},F=(t,o)=>{d(t,C,e(t,o.data_sizes)),d(t,T,e(t,o.data_srcset)),d(t,i,e(t,o.data_src))},Ht=(t,o)=>{D(t,r=>{b(r,p),F(r,o)}),b(t,p),F(t,o)},Rt=(t,o)=>{b(t,O),d(t,i,e(t,o.data_src))},Mt=(t,o)=>{at(t,r=>{b(r,O),d(r,i,e(r,o.data_src))}),b(t,et),d(t,Z,e(t,o.data_poster)),d(t,i,e(t,o.data_src)),t.load()},Bt=(t,o)=>{b(t,nt),d(t,K,e(t,o.data_src))},Ft=(t,o,r)=>{const s=e(t,o.data_bg),a=e(t,o.data_bg_hidpi),n=q&&a?a:s;n&&(t.style.backgroundImage=`url("${n}")`,ot(t).setAttribute(i,n),ut(t,o,r))},Gt=(t,o,r)=>{const s=e(t,o.data_bg_multi),a=e(t,o.data_bg_multi_hidpi),n=q&&a?a:s;n&&(t.style.backgroundImage=n,dt(t,o,r))},Pt=(t,o,r)=>{const s=e(t,o.data_bg_set);if(!s)return;const a=s.split("|");let n=a.map(_=>`image-set(${_})`);t.style.backgroundImage=n.join(),t.style.backgroundImage===""&&(n=a.map(_=>`-webkit-image-set(${_})`),t.style.backgroundImage=n.join()),dt(t,o,r)},lt={IMG:Ht,IFRAME:Rt,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&&!Nt(o)&&!xt(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)},H=t=>!!t.llEvLisnrs,Wt=(t,o,r)=>{H(t)||(t.llEvLisnrs={});const s=t.tagName==="VIDEO"?"loadeddata":"load";G(t,s,o),G(t,"error",r)},w=t=>{if(!H(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)=>{wt(t),z(r,-1),yt(r),c(t,o.class_loading),o.unobserve_completed&&A(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,N),l(r.callback_error,o,s),r.restore_on_error&&g(o,p),a||_t(r,s)},R=(t,o,r)=>{const s=ot(t)||t;if(H(s))return;Wt(s,_=>{Zt(_,t,o,r),w(s)},_=>{Kt(_,t,o,r),w(s)})},Qt=(t,o,r)=>{kt(t),R(t,o,r),Vt(t),Ft(t,o,r),Gt(t,o,r),Pt(t,o,r)},Xt=(t,o,r)=>{R(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"),R(t,o,r),$t(t,o),f(t,Y)},P=t=>{t.removeAttribute(i),t.removeAttribute(T),t.removeAttribute(C)},mt=t=>{D(t,o=>{P(o)}),P(t)},ft=t=>{D(t,o=>{g(o,p)}),g(t,p)},to=t=>{at(t,o=>{g(o,O)}),g(t,et),t.load()},oo=t=>{g(t,O)},ro=t=>{g(t,nt)},so={IMG:ft,IFRAME:oo,VIDEO:to,OBJECT:ro},ao=t=>{const o=so[t.tagName];if(!o){Dt(t);return}o(t)},eo=(t,o)=>{x(t)||V(t)||(c(t,o.class_entered),c(t,o.class_exited),c(t,o.class_applied),c(t,o.class_loading),c(t,o.class_loaded),c(t,o.class_error))},no=(t,o)=>{ao(t),eo(t,o),S(t),it(t)},co=(t,o,r,s)=>{r.cancel_on_exit&&It(t)&&t.tagName==="IMG"&&(w(t),mt(t),ft(t),c(t,r.class_loading),z(s,-1),S(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),c(t,r.class_exited),Ct(t,r,s),l(r.callback_enter,t,o,s),!a&&M(t,r,s)},uo=(t,o,r,s)=>{x(t)||(h(t,r.class_exited),co(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)=>{Tt(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(x),Lo=t=>St(t),Io=t=>ht(t).filter(Lo),$=(t,o)=>Eo(t||I(o)),So=(t,o)=>{Io(I(t)).forEach(s=>{c(s,t.class_error),S(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)},L=function(t,o){const r=W(t);this._settings=r,this.loadingCount=0,vo(r,this),Ao(r,this),this.update(o)};L.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=>{A(s,this),M(s,o,this)})},restoreAll:function(){const t=this._settings;I(t).forEach(o=>{no(o,t)})}};L.load=(t,o)=>{const r=W(o);M(t,r)};L.resetStatus=t=>{S(t)};u&&vt(L,window.lazyLoadOptions);window.LazyLoad=L;window.lLoad=new window.LazyLoad({elements_selector:".lazy"});
|