UNPKG

gluebert

Version:

gluebert.js is a tiny helper orchestrating interactive html elements with on demand dynamic import Edit

1 lines 6.54 kB
'use strict';(function(a,b){'use strict';function c(a){this.time=a.time,this.target=a.target,this.rootBounds=a.rootBounds,this.boundingClientRect=a.boundingClientRect,this.intersectionRect=a.intersectionRect||k();try{this.isIntersecting=!!a.intersectionRect}catch(a){}var b=this.boundingClientRect,c=b.width*b.height,d=this.intersectionRect,e=d.width*d.height;this.intersectionRatio=c?e/c:this.isIntersecting?1:0}function d(a,b){var c=b||{};if('function'!=typeof a)throw new Error('callback must be a function');if(c.root&&1!=c.root.nodeType)throw new Error('root must be an Element');this._checkForIntersections=f(this._checkForIntersections.bind(this),this.THROTTLE_TIMEOUT),this._callback=a,this._observationTargets=[],this._queuedEntries=[],this._rootMarginValues=this._parseRootMargin(c.rootMargin),this.thresholds=this._initThresholds(c.threshold),this.root=c.root||null,this.rootMargin=this._rootMarginValues.map(function(a){return a.value+a.unit}).join(' ')}function e(){return a.performance&&performance.now&&performance.now()}function f(a,b){var c=null;return function(){c||(c=setTimeout(function(){a(),c=null},b))}}function g(a,b,c,d){'function'==typeof a.addEventListener?a.addEventListener(b,c,d||!1):'function'==typeof a.attachEvent&&a.attachEvent('on'+b,c)}function h(a,b,c,d){'function'==typeof a.removeEventListener?a.removeEventListener(b,c,d||!1):'function'==typeof a.detatchEvent&&a.detatchEvent('on'+b,c)}function i(a,b){var c=Math.min,d=Math.max,e=d(a.top,b.top),f=c(a.bottom,b.bottom),g=d(a.left,b.left),h=c(a.right,b.right),i=h-g,j=f-e;return 0<=i&&0<=j&&{top:e,bottom:f,left:g,right:h,width:i,height:j}}function j(a){var b;try{b=a.getBoundingClientRect()}catch(a){}return b?(b.width&&b.height||(b={top:b.top,right:b.right,bottom:b.bottom,left:b.left,width:b.right-b.left,height:b.bottom-b.top}),b):k()}function k(){return{top:0,bottom:0,left:0,right:0,width:0,height:0}}function l(a,b){for(var c=b;c;){if(c==a)return!0;c=m(c)}return!1}function m(a){var b=a.parentNode;return b&&11==b.nodeType&&b.host?b.host:b}if(!('IntersectionObserver'in a&&'IntersectionObserverEntry'in a&&'intersectionRatio'in a.IntersectionObserverEntry.prototype)){var n=[];d.prototype.THROTTLE_TIMEOUT=100,d.prototype.POLL_INTERVAL=null,d.prototype.observe=function(a){if(!this._observationTargets.some(function(b){return b.element==a})){if(!(a&&1==a.nodeType))throw new Error('target must be an Element');this._registerInstance(),this._observationTargets.push({element:a,entry:null}),this._monitorIntersections()}},d.prototype.unobserve=function(a){this._observationTargets=this._observationTargets.filter(function(b){return b.element!=a}),this._observationTargets.length||(this._unmonitorIntersections(),this._unregisterInstance())},d.prototype.disconnect=function(){this._observationTargets=[],this._unmonitorIntersections(),this._unregisterInstance()},d.prototype.takeRecords=function(){var a=this._queuedEntries.slice();return this._queuedEntries=[],a},d.prototype._initThresholds=function(a){var b=a||[0];return Array.isArray(b)||(b=[b]),b.sort().filter(function(b,c,d){if('number'!=typeof b||isNaN(b)||0>b||1<b)throw new Error('threshold must be a number between 0 and 1 inclusively');return b!==d[c-1]})},d.prototype._parseRootMargin=function(a){var b=(a||'0px').split(/\s+/).map(function(a){var b=/^(-?\d*\.?\d+)(px|%)$/.exec(a);if(!b)throw new Error('rootMargin must be specified in pixels or percent');return{value:parseFloat(b[1]),unit:b[2]}});return b[1]=b[1]||b[0],b[2]=b[2]||b[0],b[3]=b[3]||b[1],b},d.prototype._monitorIntersections=function(){this._monitoringIntersections||(this._monitoringIntersections=!0,this._checkForIntersections(),this.POLL_INTERVAL?this._monitoringInterval=setInterval(this._checkForIntersections,this.POLL_INTERVAL):(g(a,'resize',this._checkForIntersections,!0),g(b,'scroll',this._checkForIntersections,!0),'MutationObserver'in a&&(this._domObserver=new MutationObserver(this._checkForIntersections),this._domObserver.observe(b,{attributes:!0,childList:!0,characterData:!0,subtree:!0}))))},d.prototype._unmonitorIntersections=function(){this._monitoringIntersections&&(this._monitoringIntersections=!1,clearInterval(this._monitoringInterval),this._monitoringInterval=null,h(a,'resize',this._checkForIntersections,!0),h(b,'scroll',this._checkForIntersections,!0),this._domObserver&&(this._domObserver.disconnect(),this._domObserver=null))},d.prototype._checkForIntersections=function(){var a=this._rootIsInDom(),b=a?this._getRootRect():k();this._observationTargets.forEach(function(d){var f=d.element,g=j(f),h=this._rootContainsTarget(f),i=d.entry,k=a&&h&&this._computeTargetAndRootIntersection(f,b),l=d.entry=new c({time:e(),target:f,boundingClientRect:g,rootBounds:b,intersectionRect:k});i?a&&h?this._hasCrossedThreshold(i,l)&&this._queuedEntries.push(l):i&&i.isIntersecting&&this._queuedEntries.push(l):this._queuedEntries.push(l)},this),this._queuedEntries.length&&this._callback(this.takeRecords(),this)},d.prototype._computeTargetAndRootIntersection=function(c,d){if('none'!=a.getComputedStyle(c).display){for(var e=j(c),f=e,g=m(c),h=!1;!h;){var k=null,l=1==g.nodeType?a.getComputedStyle(g):{};if('none'==l.display)return;if(g==this.root||g==b?(h=!0,k=d):g!=b.body&&g!=b.documentElement&&'visible'!=l.overflow&&(k=j(g)),k&&(f=i(k,f),!f))break;g=m(g)}return f}},d.prototype._getRootRect=function(){var a;if(this.root)a=j(this.root);else{var c=b.documentElement,d=b.body;a={top:0,left:0,right:c.clientWidth||d.clientWidth,width:c.clientWidth||d.clientWidth,bottom:c.clientHeight||d.clientHeight,height:c.clientHeight||d.clientHeight}}return this._expandRectByRootMargin(a)},d.prototype._expandRectByRootMargin=function(a){var b=this._rootMarginValues.map(function(b,c){return'px'==b.unit?b.value:b.value*(c%2?a.width:a.height)/100}),c={top:a.top-b[0],right:a.right+b[1],bottom:a.bottom+b[2],left:a.left-b[3]};return c.width=c.right-c.left,c.height=c.bottom-c.top,c},d.prototype._hasCrossedThreshold=function(a,b){var c=a&&a.isIntersecting?a.intersectionRatio||0:-1,d=b.isIntersecting?b.intersectionRatio||0:-1;if(c!==d)for(var e,f=0;f<this.thresholds.length;f++)if(e=this.thresholds[f],e==c||e==d||e<c!=e<d)return!0},d.prototype._rootIsInDom=function(){return!this.root||l(b,this.root)},d.prototype._rootContainsTarget=function(a){return l(this.root||b,a)},d.prototype._registerInstance=function(){0>n.indexOf(this)&&n.push(this)},d.prototype._unregisterInstance=function(){var a=n.indexOf(this);-1!=a&&n.splice(a,1)},a.IntersectionObserver=d,a.IntersectionObserverEntry=c}})(window,document);