syriastreet.com/assets/js/lib/scrollMonitor.js
2023-10-11 18:20:29 +00:00

1 line
No EOL
5.1 KiB
JavaScript

!function(t){"undefined"!=typeof define&&define.amd?define([],t):"undefined"!=typeof module&&module.exports?module.exports=t():t()}(function(){function t(){return h?0:window.pageYOffset||document.documentElement&&document.documentElement.scrollTop||document.body.scrollTop}function i(){return h?0:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight,document.body.offsetHeight,document.documentElement.offsetHeight,document.documentElement.clientHeight)}var o,w,e,s,n,h="undefined"==typeof window,c=!h,u={},l=[],m="visibilityChange",d="enterViewport",f="fullyEnterViewport",b="exitViewport",v="partiallyExitViewport",g="locationChange",V="stateChange",y=[m,d,f,b,v,g,V],I={top:0,bottom:0},r=function(){return h?0:window.innerHeight||document.documentElement.clientHeight};function a(){if(u.viewportTop=t(),u.viewportBottom=u.viewportTop+u.viewportHeight,u.documentHeight=i(),u.documentHeight!==o){for(e=l.length;e--;)l[e].recalculateLocation();o=u.documentHeight}}function p(){u.viewportHeight=r(),a(),H()}function k(){clearTimeout(s),s=setTimeout(p,100)}function H(){for(n=l.length;n--;)l[n].update();for(n=l.length;n--;)l[n].triggerCallbacks()}function E(t,i){var o=this;this.watchItem=t,this.offsets=i?i===+i?{top:i,bottom:i}:{top:i.top||I.top,bottom:i.bottom||I.bottom}:I,this.callbacks={};for(var e,s,n,h,c,l,r=0,a=y.length;r<a;r++)o.callbacks[y[r]]=[];function p(t){if(0!==t.length)for(c=t.length;c--;)(l=t[c]).callback.call(o,w),l.isOne&&t.splice(c,1)}this.locked=!1,this.triggerCallbacks=function(){switch(this.isInViewport&&!e&&p(this.callbacks[d]),this.isFullyInViewport&&!s&&p(this.callbacks[f]),this.isAboveViewport!==n&&this.isBelowViewport!==h&&(p(this.callbacks[m]),s||this.isFullyInViewport||(p(this.callbacks[f]),p(this.callbacks[v])),e||this.isInViewport||(p(this.callbacks[d]),p(this.callbacks[b]))),!this.isFullyInViewport&&s&&p(this.callbacks[v]),!this.isInViewport&&e&&p(this.callbacks[b]),this.isInViewport!==e&&p(this.callbacks[m]),!0){case e!==this.isInViewport:case s!==this.isFullyInViewport:case n!==this.isAboveViewport:case h!==this.isBelowViewport:p(this.callbacks[V])}e=this.isInViewport,s=this.isFullyInViewport,n=this.isAboveViewport,h=this.isBelowViewport},this.recalculateLocation=function(){var t,i,o,e;this.locked||(t=this.top,i=this.bottom,this.watchItem.nodeName?("none"===(o=this.watchItem.style.display)&&(this.watchItem.style.display=""),e=this.watchItem.getBoundingClientRect(),this.top=e.top+u.viewportTop,this.bottom=e.bottom+u.viewportTop,"none"===o&&(this.watchItem.style.display=o)):this.watchItem===+this.watchItem?0<this.watchItem?this.top=this.bottom=this.watchItem:this.top=this.bottom=u.documentHeight-this.watchItem:(this.top=this.watchItem.top,this.bottom=this.watchItem.bottom),this.top-=this.offsets.top,this.bottom+=this.offsets.bottom,this.height=this.bottom-this.top,void 0===t&&void 0===i)||this.top===t&&this.bottom===i||p(this.callbacks[g])},this.recalculateLocation(),this.update(),e=this.isInViewport,s=this.isFullyInViewport,n=this.isAboveViewport,h=this.isBelowViewport}u.viewportTop=null,u.viewportBottom=null,u.documentHeight=null,u.viewportHeight=r(),E.prototype={on:function(t,i,o){switch(!0){case t===m&&!this.isInViewport&&this.isAboveViewport:case t===d&&this.isInViewport:case t===f&&this.isFullyInViewport:case t===b&&this.isAboveViewport&&!this.isInViewport:case t===v&&this.isAboveViewport:if(i.call(this,w),o)return}if(!this.callbacks[t])throw new Error("Tried to add a scroll monitor listener of type "+t+". Your options are: "+y.join(", "));this.callbacks[t].push({callback:i,isOne:o||!1})},off:function(t,i){if(!this.callbacks[t])throw new Error("Tried to remove a scroll monitor listener of type "+t+". Your options are: "+y.join(", "));for(var o,e=0;o=this.callbacks[t][e];e++)if(o.callback===i){this.callbacks[t].splice(e,1);break}},one:function(t,i){this.on(t,i,!0)},recalculateSize:function(){this.height=this.watchItem.offsetHeight+this.offsets.top+this.offsets.bottom,this.bottom=this.top+this.height},update:function(){this.isAboveViewport=this.top<u.viewportTop,this.isBelowViewport=this.bottom>u.viewportBottom,this.isInViewport=this.top<u.viewportBottom&&this.bottom>u.viewportTop,this.isFullyInViewport=this.top>=u.viewportTop&&this.bottom<=u.viewportBottom||this.isAboveViewport&&this.isBelowViewport},destroy:function(){var t=l.indexOf(this);l.splice(t,1);for(var i=0,o=y.length;i<o;i++)this.callbacks[y[i]].length=0},lock:function(){this.locked=!0},unlock:function(){this.locked=!1}};for(var T=0,B=y.length;T<B;T++){var A=y[T];E.prototype[A]=function(o){return function(t,i){this.on.call(this,o,t,i)}}(A)}if(c)try{a()}catch(t){try{window.$(a)}catch(t){throw new Error("If you must put scrollMonitor in the <head>, you must use jQuery.")}}function F(t){w=t,a(),H()}return c&&(window.addEventListener?(window.addEventListener("scroll",F),window.addEventListener("resize",k)):(window.attachEvent("onscroll",F),window.attachEvent("onresize",k))),u.beget=u.create=function(t,i){"string"==typeof t?t=document.querySelector(t):t&&0<t.length&&(t=t[0]);t=new E(t,i);return l.push(t),t.update(),t},u.update=function(){w=null,a(),H()},u.recalculateLocations=function(){u.documentHeight=0,u.update()},u.eventTypes=y,c&&(window.scrollMonitor=u),u});