UNPKG

@vime/core

Version:

Customizable, extensible, accessible and framework agnostic media player.

1 lines 1.47 kB
import{I as t}from"./p-d84b1c8a.js";import{n as i}from"./p-b018976f.js";class s{constructor(t,s,h){var r;this.el=t,this.attributes=s,this.onLoad=h,this.hasLoaded=!1,i(this.el)||(this.intersectionObs=this.canObserveIntersection()?new IntersectionObserver(this.onIntersection.bind(this)):void 0,this.mutationObs=this.canObserveMutations()?new MutationObserver(this.onMutation.bind(this)):void 0,null===(r=this.mutationObs)||void 0===r||r.observe(this.el,{childList:!0,subtree:!0,attributeFilter:this.attributes}),this.lazyLoad())}didLoad(){return this.hasLoaded}destroy(){var t,i;null===(t=this.intersectionObs)||void 0===t||t.disconnect(),null===(i=this.mutationObs)||void 0===i||i.disconnect()}canObserveIntersection(){return t&&window.IntersectionObserver}canObserveMutations(){return t&&window.MutationObserver}lazyLoad(){var t;this.canObserveIntersection()?null===(t=this.intersectionObs)||void 0===t||t.observe(this.el):this.load()}onIntersection(t){t.forEach((t=>{(t.intersectionRatio>0||t.isIntersecting)&&(this.load(),this.intersectionObs.unobserve(t.target))}))}onMutation(){this.hasLoaded&&this.load()}getLazyElements(){return(i(this.el.shadowRoot)?this.el:this.el.shadowRoot).querySelectorAll(".lazy")}load(){window.requestAnimationFrame((()=>{this.getLazyElements().forEach(this.loadEl.bind(this))}))}loadEl(t){var i,s;null===(i=this.intersectionObs)||void 0===i||i.unobserve(t),this.hasLoaded=!0,null===(s=this.onLoad)||void 0===s||s.call(this,t)}}export{s as L}