-
Notifications
You must be signed in to change notification settings - Fork 0
/
wistia-lazyload.min.js
6 lines (6 loc) · 3.51 KB
/
wistia-lazyload.min.js
1
2
3
4
5
6
/*!
* wistia-lazyload.js v1.0.0 (https://github.com/edbartholomew/wistia-lazyload.js)
* Copyright 2022 Ed Bartholomew (https://edbartholomew.com)
* Licensed under MIT
*/
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.wistiaLazyload=t():e.wistiaLazyload=t()}(self,(()=>(()=>{"use strict";var e={d:(t,i)=>{for(var a in i)e.o(i,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:i[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};function i(e,t){for(var i=0;i<t.length;i++){var a=t[i];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}e.r(t),e.d(t,{default:()=>a});const a=new(function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.pluginName="wistia-lazyload.js",this.loadingClassName="wistia-lazyload",this.loadedClassName="wistia-lazyloaded",this.loadedVideos=[],this.init()}var t,a;return t=e,(a=[{key:"lazyLoad",value:function(e){var t=this,i=e.lastElementChild.dataset.videoId;if(this.loadedVideos.includes(i))console.warn("".concat(this.pluginName," - Duplicate Wistia embed (video ID: ").concat(i,"). Only the first instance will be loaded."));else{this.loadedVideos.push(i);var a=e.childElementCount,o=document.querySelector(".".concat(this.loadingClassName,' > [data-video-id="').concat(i,'"]')),n=o.parentElement.querySelector("img");a>1&&n.dataset.src&&(n.style.visibility="hidden",n.onload=function(){n.style.length>1?n.style.removeProperty("visibility"):n.removeAttribute("style")},n.src=n.dataset.src),o.classList.add("wistia_embed","wistia_async_".concat(i));var s=document.createElement("script");if(s.src="https://fast.wistia.com/embed/medias/".concat(i,".jsonp"),document.body.appendChild(s),"undefined"==typeof Wistia){var r=document.createElement("script");r.src="https://fast.wistia.com/assets/external/E-v1.js",document.body.appendChild(r)}window._wq=window._wq||[],_wq.push({id:i,onReady:function(){e.classList.add(t.loadedClassName)}})}}},{key:"createObserver",value:function(e){var t=this,i=new IntersectionObserver((function(e){e.forEach((function(e){e.isIntersecting&&(t.lazyLoad(e.target),i.unobserve(e.target))}))}),{rootMargin:"10%"});i.observe(e)}},{key:"injectStyles",value:function(){var e=document.createElement("style");e.id="".concat(this.loadingClassName,"-injected-styles"),e.appendChild(document.createTextNode(".wistia-lazyload{position:relative;width:100%;height:100%;box-sizing:border-box;aspect-ratio:16/9;background:rgba(0,0,0,.05)}.wistia-lazyload>img{width:100%;height:100%;object-fit:cover}.wistia-lazyload>div{position:absolute;top:0;left:0;visibility:hidden;width:100%;height:100%}.wistia-lazyloaded>div{visibility:visible}")),document.head.appendChild(e)}},{key:"init",value:function(){var e=this;window.addEventListener("load",(function(){var t=Array.from(document.querySelectorAll(".".concat(e.loadingClassName)));e.injectStyles(),"IntersectionObserver"in window?t.forEach((function(t){return e.createObserver(t)})):(t.forEach((function(t){return e.lazyLoad(t)})),console.warn("".concat(e.pluginName," - Intersection Observer API not support by your browser. Wistia videos will not be lazy loaded.")))}))}}])&&i(t.prototype,a),Object.defineProperty(t,"prototype",{writable:!1}),e}());return t})()));