sentinel-dom
Version:
Lightweight, powerful and reliable library to determine the visibility of DOM elements.
1 lines • 7.33 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("Sentinel",[],e):"object"==typeof exports?exports.Sentinel=e():t.Sentinel=e()}(window,function(){return function(t){function e(n){if(o[n])return o[n].exports;var r=o[n]={i:n,l:!1,exports:{}};return t[n].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var o={};return e.m=t,e.c=o,e.d=function(t,o,n){e.o(t,o)||Object.defineProperty(t,o,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,o){if(1&o&&(t=e(t)),8&o)return t;if(4&o&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&o&&"string"!=typeof t)for(var r in t)e.d(n,r,function(e){return t[e]}.bind(null,r));return n},e.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(o,"a",o),o},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,o){"use strict";function n(t){return null!=t}function r(t){return Array.isArray(t)?t:t instanceof HTMLCollection?Array.from(t):[t]}function i(t,e){return t.map(function(t){var o=Object.assign({},t);return o.targets&&(o.iterableTargets=r(o.targets)),l.forEach(function(r){var i=Object.prototype.hasOwnProperty.call(t,r),s=e[r];!i&&n(s)&&(o[r]=s)}),o})}function s(t){return(s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function a(t){!function(t,e){Object.keys(e).forEach(function(o){var n=t[o],r=e[o];if(r.required&&!n)throw new Error('Property "'.concat(o,'" is required, but was not found in the provided properties.'));if(n&&!("function"==typeof r.expect?r.expect(n):s(n)===r.expect)){var i='"'.concat(n,'" (').concat(s(n),")"),a=r.message?r.message({propName:o,propValue:n,receivedProp:i}):'Expected property "'.concat(o,'" to be an instance of type ').concat(r.expect,", but got: ").concat(i,".");throw new Error(a)}})}(t,{targets:{expect:function(t){return t instanceof HTMLCollection||t instanceof HTMLElement||Array.isArray(t)},message:function(t){var e=t.propName,o=t.receivedProp;return'Expected property "'.concat(e,'" to be an instance of HTMLElement, or an HTMLCollection, but got: ').concat(o,".")}},bounds:{expect:function(t){return t instanceof HTMLElement||t===window},message:function(t){var e=t.propName,o=t.receivedProp;return'Expected property "'.concat(e,'" to be an instance of HTMLElement, but got: ').concat(o,".")}},snapshots:{required:!0,expect:function(t){return Array.isArray(t)},message:function(t){var e=t.propName,o=t.receivedProp;return'Expected property "'.concat(e,'" to be an Array, but got: ').concat(o,".")}},throttle:{expect:"number"},once:{expect:"boolean"}})}function c(t,e){for(var o,n=0;n<e.length;n++)(o=e[n]).enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}function u(t,e){for(var o,n=0;n<e.length;n++)(o=e[n]).enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}o.r(e);var f=function t(e){var o=this;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.bounds=window,this.throttle=250,this.offsetX=0,this.offsetY=0,this.thresholdX=100,this.thresholdY=100,this.once=!1,this.manual=!1,this.debug=!1,Object.keys(e).forEach(function(t){return o[t]=e[t]}),this},l=["offsetX","offsetY","thresholdY","thresholdY","edgeX","edgeY"],h={absolute:!1},p={weak:!1},d=function(){function t(o,n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t);var r,i=Object.assign({},h,n).absolute;return r=o===window?{top:window.scrollY,left:window.scrollX,height:window.innerHeight,width:window.innerWidth}:o instanceof HTMLElement?o.getBoundingClientRect():o,this.top=r.top,this.right=r.right||e(r.left+r.width),this.bottom=r.bottom||e(r.top+r.height),this.left=r.left,this.height=r.height||e(this.bottom-this.top),this.width=r.width||e(this.right-this.left),i?this.toAbsolute():this}var e=Math.floor;return function(t,e,o){e&&c(t.prototype,e),o&&c(t,o)}(t,[{key:"toAbsolute",value:function(){return new t({top:e(this.top+window.scrollY),right:e(this.right+window.scrollX),bottom:e(this.bottom+window.scrollY),left:e(this.left+window.scrollX)})}},{key:"contains",value:function(t,e){var o=this;return Object.assign({},p,e).weak?t.left<=o.right&&t.top<=o.bottom:o.top<=t.top&&o.right>=t.right&&o.bottom>=t.bottom&&o.left<=t.left}},{key:"containsEdge",value:function(t){var e=t.x,o=t.y,n=!e||e>=this.left&&e<=this.right,r=!o||o>=this.top&&o<=this.bottom;return n&&r}},{key:"intersect",value:function(e){if(this.contains(e))return e;var o=e.right<this.right&&e.left<this.left,n=e.bottom<this.bottom&&e.top<this.top,r={x:e.left>this.left&&e.right<this.right,y:e.top>this.top&&e.bottom<this.bottom};return new t({top:n?this.top:e.top,right:o||r.x?e.right:this.right,bottom:n||r.y?e.bottom:this.bottom,left:o?this.left:e.left})}}]),t}(),b=function(){function t(e){var o=this;(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")})(this,t),this.track=function(){var t=o.options,e=new d(window);o.debug(function(){}),t.snapshots.forEach(function(i,s){var a=i.bounds||t.bounds,c=a===window,u=c?e:new d(a,{absolute:!0});if(c||e.contains(u,{weak:!0})){var f=r(i.targets||t.targets),l=(n(i.once)?i.once:t.once)||!1;o.debug(function(){}),f.forEach(function(t){var n=o.pool[s],r=n&&n.includes(t);if(o.debug(function(){}),l&&r)o.debug(function(){});else{var a=new d(t);o.debug(function(){}),o.takeSnapshot({snapshot:i,targetArea:a.toAbsolute(),boundsArea:u,viewportArea:e,isBoundsViewport:c}).matches&&(o.debug(function(){}),!r&&(n?o.pool[s].push(t):o.pool[s]=[t]),i.callback({DOMElement:t,pool:o.pool}))}})}else o.debug(function(){})}),o.debug(function(){})},a(e);var s=new f(e);return this.options=Object.assign({},s,{snapshots:i(s.snapshots,s)}),this.pool={},s.manual||window.addEventListener("scroll",function(t,e,o){var n,r;return function(){for(var i=arguments.length,s=Array(i),a=0;a<i;a++)s[a]=arguments[a];var c=o||this,u=+new Date;n&&u<n+e?(clearTimeout(r),r=setTimeout(function(){n=u,t.apply(c,s)},e+n-u)):(n=u,t.apply(c,s))}}(this.track,s.throttle),!1),this}return function(t,e,o){e&&u(t.prototype,e),o&&u(t,o)}(t,[{key:"takeSnapshot",value:function(t){var e=t.snapshot,o=t.targetArea,r=t.boundsArea,i=t.viewportArea,s=t.isBoundsViewport,a=(e.name,e.offsetX),c=e.offsetY,u=e.thresholdX,f=e.thresholdY,l=e.edgeX,h=e.edgeY,p=a||0,d=c||0;if(n(l)||n(h)){var b={x:l&&o.left+o.width*l/100+p,y:h&&o.top+o.height*h/100+d};return{matches:i.containsEdge(b)&&r.containsEdge(b)}}var g=u||100,y=f||100,w=o.width*(g/100),m=o.height*(y/100),v=i.intersect(o),x={byX:v.width>=w+p,byY:v.height>=m+d},E=i.contains(v),j=s?E:r.contains(v);return this.debug(function(){}),{matches:x.byX&&x.byY&&E&&j}}},{key:"debug",value:function(t){this.options.debug&&t()}}]),t}();o.d(e,"Observer",function(){return b}),o.d(e,"Area",function(){return d})}])});