luyadev/luya

View on GitHub
core/resources/lazyload/intersectionOberserver.polyfill.js

Summary

Maintainability
A
0 mins
Test Coverage
function _typeof(t){return(_typeof="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)}window.getComputedStyle||(window.getComputedStyle=function(n,t){return this.el=n,this.getPropertyValue=function(t){var e=/(\-([a-z]){1})/g;return"float"==t&&(t="styleFloat"),e.test(t)&&(t=t.replace(e,function(){return arguments[2].toUpperCase()})),n.currentStyle[t]?n.currentStyle[t]:null},this}),function(t,e){"use strict";"function"==typeof define&&define.amd?define(e):"object"===("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=e():t.returnExports=e()}(this,function(){var s,c,u,l,t=Function.call,h=Object.prototype,a=t.bind(h.hasOwnProperty),f=t.bind(h.propertyIsEnumerable),n=t.bind(h.toString),p=a(h,"__defineGetter__");p&&(s=t.bind(h.__defineGetter__),c=t.bind(h.__defineSetter__),u=t.bind(h.__lookupGetter__),l=t.bind(h.__lookupSetter__));function d(t){return null==t||"object"!==_typeof(t)&&"function"!=typeof t}if(Object.getPrototypeOf||(Object.getPrototypeOf=function(t){var e=t.__proto__;return e||null===e?e:"[object Function]"===n(t.constructor)?t.constructor.prototype:t instanceof Object?h:null}),Object.defineProperty){function e(t){try{return(t.sentinel=0)===Object.getOwnPropertyDescriptor(t,"sentinel").value}catch(t){return!1}}var o=e({});if(!("undefined"==typeof document||e(document.createElement("div")))||!o)var b=Object.getOwnPropertyDescriptor}if(!Object.getOwnPropertyDescriptor||b){Object.getOwnPropertyDescriptor=function(t,e){if(d(t))throw new TypeError("Object.getOwnPropertyDescriptor called on a non-object: "+t);if(b)try{return b.call(Object,t,e)}catch(t){}var n;if(!a(t,e))return n;if(n={enumerable:f(t,e),configurable:!0},p){var o=t.__proto__,r=t!==h;r&&(t.__proto__=h);var i=u(t,e),s=l(t,e);if(r&&(t.__proto__=o),i||s)return i&&(n.get=i),s&&(n.set=s),n}return n.value=t[e],n.writable=!0,n}}if(Object.getOwnPropertyNames||(Object.getOwnPropertyNames=function(t){return Object.keys(t)}),!Object.create){var r,i=!({__proto__:null}instanceof Object);r=i||"undefined"==typeof document?function(){return{__proto__:null}}:function(){var t=function(){if(!document.domain)return!1;try{return!!new ActiveXObject("htmlfile")}catch(t){return!1}}()?function(){var t,e;return(e=new ActiveXObject("htmlfile")).write("<script><\/script>"),e.close(),t=e.parentWindow.Object.prototype,e=null,t}():function(){var t,e=document.createElement("iframe"),n=document.body||document.documentElement;return e.style.display="none",n.appendChild(e),e.src="javascript:",t=e.contentWindow.Object.prototype,n.removeChild(e),e=null,t}();delete t.constructor,delete t.hasOwnProperty,delete t.propertyIsEnumerable,delete t.isPrototypeOf,delete t.toLocaleString,delete t.toString,delete t.valueOf;function e(){}return e.prototype=t,r=function(){return new e},new e},Object.create=function(t,e){function n(){}var o;if(null===t)o=r();else{if(d(t))throw new TypeError("Object prototype may only be an Object or null");n.prototype=t,(o=new n).__proto__=t}return void 0!==e&&Object.defineProperties(o,e),o}}function y(t){try{return Object.defineProperty(t,"sentinel",{}),"sentinel"in t}catch(t){return!1}}if(Object.defineProperty){var _=y({}),m="undefined"==typeof document||y(document.createElement("div"));if(!_||!m)var g=Object.defineProperty,w=Object.defineProperties}if(!Object.defineProperty||g){Object.defineProperty=function(t,e,n){if(d(t))throw new TypeError("Object.defineProperty called on non-object: "+t);if(d(n))throw new TypeError("Property description must be an object: "+n);if(g)try{return g.call(Object,t,e,n)}catch(t){}if("value"in n)if(p&&(u(t,e)||l(t,e))){var o=t.__proto__;t.__proto__=h,delete t[e],t[e]=n.value,t.__proto__=o}else t[e]=n.value;else{var r="get"in n,i="set"in n;if(!p&&(r||i))throw new TypeError("getters & setters can not be defined on this javascript engine");r&&s(t,e,n.get),i&&c(t,e,n.set)}return t}}Object.defineProperties&&!w||(Object.defineProperties=function(e,n){if(w)try{return w.call(Object,e,n)}catch(t){}return Object.keys(n).forEach(function(t){"__proto__"!==t&&Object.defineProperty(e,t,n[t])}),e}),Object.seal||(Object.seal=function(t){if(Object(t)!==t)throw new TypeError("Object.seal can only be called on Objects.");return t}),Object.freeze||(Object.freeze=function(t){if(Object(t)!==t)throw new TypeError("Object.freeze can only be called on Objects.");return t});try{Object.freeze(function(){})}catch(t){Object.freeze=function(e){return function(t){return"function"==typeof t?t:e(t)}}(Object.freeze)}Object.preventExtensions||(Object.preventExtensions=function(t){if(Object(t)!==t)throw new TypeError("Object.preventExtensions can only be called on Objects.");return t}),Object.isSealed||(Object.isSealed=function(t){if(Object(t)!==t)throw new TypeError("Object.isSealed can only be called on Objects.");return!1}),Object.isFrozen||(Object.isFrozen=function(t){if(Object(t)!==t)throw new TypeError("Object.isFrozen can only be called on Objects.");return!1}),Object.isExtensible||(Object.isExtensible=function(t){if(Object(t)!==t)throw new TypeError("Object.isExtensible can only be called on Objects.");for(var e="";a(t,e);)e+="?";t[e]=!0;var n=a(t,e);return delete t[e],n})}),function(){"use strict";if("object"===("undefined"==typeof window?"undefined":_typeof(window)))if("IntersectionObserver"in window&&"IntersectionObserverEntry"in window&&"intersectionRatio"in window.IntersectionObserverEntry.prototype)"isIntersecting"in window.IntersectionObserverEntry.prototype||Object.defineProperty(window.IntersectionObserverEntry.prototype,"isIntersecting",{get:function(){return 0<this.intersectionRatio}});else{var w=function(){for(var t=window.document,e=s(t);e;)e=s(t=e.ownerDocument);return t}(),n=[],v=null,O=null;t.prototype.THROTTLE_TIMEOUT=100,t.prototype.POLL_INTERVAL=null,t.prototype.USE_MUTATION_OBSERVER=!0,t._setupCrossOriginUpdater=function(){return v=v||function(t,e){O=t&&e?E(t,e):{top:0,bottom:0,left:0,right:0,width:0,height:0},n.forEach(function(t){t._checkForIntersections()})}},t._resetCrossOriginUpdater=function(){O=v=null},t.prototype.observe=function(e){if(!this._observationTargets.some(function(t){return t.element==e})){if(!e||1!=e.nodeType)throw new Error("target must be an Element");this._registerInstance(),this._observationTargets.push({element:e,entry:null}),this._monitorIntersections(e.ownerDocument),this._checkForIntersections()}},t.prototype.unobserve=function(e){this._observationTargets=this._observationTargets.filter(function(t){return t.element!=e}),this._unmonitorIntersections(e.ownerDocument),0==this._observationTargets.length&&this._unregisterInstance()},t.prototype.disconnect=function(){this._observationTargets=[],this._unmonitorAllIntersections(),this._unregisterInstance()},t.prototype.takeRecords=function(){var t=this._queuedEntries.slice();return this._queuedEntries=[],t},t.prototype._initThresholds=function(t){var e=t||[0];return Array.isArray(e)||(e=[e]),e.sort().filter(function(t,e,n){if("number"!=typeof t||isNaN(t)||t<0||1<t)throw new Error("threshold must be a number between 0 and 1 inclusively");return t!==n[e-1]})},t.prototype._parseRootMargin=function(t){var e=(t||"0px").split(/\s+/).map(function(t){var e=/^(-?\d*\.?\d+)(px|%)$/.exec(t);if(!e)throw new Error("rootMargin must be specified in pixels or percent");return{value:parseFloat(e[1]),unit:e[2]}});return e[1]=e[1]||e[0],e[2]=e[2]||e[0],e[3]=e[3]||e[1],e},t.prototype._monitorIntersections=function(e){var t=e.defaultView;if(t&&-1==this._monitoringDocuments.indexOf(e)){var n=this._checkForIntersections,o=null,r=null;if(this.POLL_INTERVAL?o=t.setInterval(n,this.POLL_INTERVAL):(c(t,"resize",n,!0),c(e,"scroll",n,!0),this.USE_MUTATION_OBSERVER&&"MutationObserver"in t&&(r=new t.MutationObserver(n)).observe(e,{attributes:!0,childList:!0,characterData:!0,subtree:!0})),this._monitoringDocuments.push(e),this._monitoringUnsubscribes.push(function(){var t=e.defaultView;t&&(o&&t.clearInterval(o),u(t,"resize",n,!0)),u(e,"scroll",n,!0),r&&r.disconnect()}),e!=(this.root&&this.root.ownerDocument||w)){var i=s(e);i&&this._monitorIntersections(i.ownerDocument)}}},t.prototype._unmonitorIntersections=function(o){var t=this._monitoringDocuments.indexOf(o);if(-1!=t){var r=this.root&&this.root.ownerDocument||w;if(!this._observationTargets.some(function(t){var e=t.element.ownerDocument;if(e==o)return!0;for(;e&&e!=r;){var n=s(e);if((e=n&&n.ownerDocument)==o)return!0}return!1})){var e=this._monitoringUnsubscribes[t];if(this._monitoringDocuments.splice(t,1),this._monitoringUnsubscribes.splice(t,1),e(),o!=r){var n=s(o);n&&this._unmonitorIntersections(n.ownerDocument)}}}},t.prototype._unmonitorAllIntersections=function(){var t=this._monitoringUnsubscribes.slice(0);this._monitoringDocuments.length=0;for(var e=this._monitoringUnsubscribes.length=0;e<t.length;e++)t[e]()},t.prototype._checkForIntersections=function(){if(this.root||!v||O){var c=this._rootIsInDom(),u=c?this._getRootRect():{top:0,bottom:0,left:0,right:0,width:0,height:0};this._observationTargets.forEach(function(t){var e=t.element,n=j(e),o=this._rootContainsTarget(e),r=t.entry,i=c&&o&&this._computeTargetAndRootIntersection(e,n,u),s=t.entry=new l({time:window.performance&&performance.now&&performance.now(),target:e,boundingClientRect:n,rootBounds:v&&!this.root?null:u,intersectionRect:i});r?c&&o?this._hasCrossedThreshold(r,s)&&this._queuedEntries.push(s):r&&r.isIntersecting&&this._queuedEntries.push(s):this._queuedEntries.push(s)},this),this._queuedEntries.length&&this._callback(this.takeRecords(),this)}},t.prototype._computeTargetAndRootIntersection=function(t,e,n){if("none"!=window.getComputedStyle(t).display){for(var o,r,i,s,c,u,l,h,a=e,f=T(t),p=!1;!p&&f;){var d=null,b=1==f.nodeType?window.getComputedStyle(f):{};if("none"==b.display)return null;if(f==this.root||9==f.nodeType)if(p=!0,f==this.root||f==w)v&&!this.root?!O||0==O.width&&0==O.height?a=d=f=null:d=O:d=n;else{var y=T(f),_=y&&j(y),m=y&&this._computeTargetAndRootIntersection(y,_,n);_&&m?(f=y,d=E(_,m)):a=f=null}else{var g=f.ownerDocument;f!=g.body&&f!=g.documentElement&&"visible"!=b.overflow&&(d=j(f))}if(d&&(o=d,r=a,void 0,i=Math.max(o.top,r.top),s=Math.min(o.bottom,r.bottom),c=Math.max(o.left,r.left),u=Math.min(o.right,r.right),h=s-i,a=0<=(l=u-c)&&0<=h&&{top:i,bottom:s,left:c,right:u,width:l,height:h}||null),!a)break;f=f&&T(f)}return a}},t.prototype._getRootRect=function(){var t;if(this.root)t=j(this.root);else{var e=w.documentElement,n=w.body;t={top:0,left:0,right:e.clientWidth||n.clientWidth,width:e.clientWidth||n.clientWidth,bottom:e.clientHeight||n.clientHeight,height:e.clientHeight||n.clientHeight}}return this._expandRectByRootMargin(t)},t.prototype._expandRectByRootMargin=function(n){var t=this._rootMarginValues.map(function(t,e){return"px"==t.unit?t.value:t.value*(e%2?n.width:n.height)/100}),e={top:n.top-t[0],right:n.right+t[1],bottom:n.bottom+t[2],left:n.left-t[3]};return e.width=e.right-e.left,e.height=e.bottom-e.top,e},t.prototype._hasCrossedThreshold=function(t,e){var n=t&&t.isIntersecting?t.intersectionRatio||0:-1,o=e.isIntersecting?e.intersectionRatio||0:-1;if(n!==o)for(var r=0;r<this.thresholds.length;r++){var i=this.thresholds[r];if(i==n||i==o||i<n!=i<o)return!0}},t.prototype._rootIsInDom=function(){return!this.root||e(w,this.root)},t.prototype._rootContainsTarget=function(t){return e(this.root||w,t)&&(!this.root||this.root.ownerDocument==t.ownerDocument)},t.prototype._registerInstance=function(){n.indexOf(this)<0&&n.push(this)},t.prototype._unregisterInstance=function(){var t=n.indexOf(this);-1!=t&&n.splice(t,1)},window.IntersectionObserver=t,window.IntersectionObserverEntry=l}function s(t){try{return t.defaultView&&t.defaultView.frameElement||null}catch(t){return null}}function l(t){this.time=t.time,this.target=t.target,this.rootBounds=i(t.rootBounds),this.boundingClientRect=i(t.boundingClientRect),this.intersectionRect=i(t.intersectionRect||{top:0,bottom:0,left:0,right:0,width:0,height:0}),this.isIntersecting=!!t.intersectionRect;var e=this.boundingClientRect,n=e.width*e.height,o=this.intersectionRect,r=o.width*o.height;this.intersectionRatio=n?Number((r/n).toFixed(4)):this.isIntersecting?1:0}function t(t,e){var n=e||{};if("function"!=typeof t)throw new Error("callback must be a function");if(n.root&&1!=n.root.nodeType)throw new Error("root must be an Element");this._checkForIntersections=function(t,e){var n=null;return function(){n=n||setTimeout(function(){t(),n=null},e)}}(this._checkForIntersections.bind(this),this.THROTTLE_TIMEOUT),this._callback=t,this._observationTargets=[],this._queuedEntries=[],this._rootMarginValues=this._parseRootMargin(n.rootMargin),this.thresholds=this._initThresholds(n.threshold),this.root=n.root||null,this.rootMargin=this._rootMarginValues.map(function(t){return t.value+t.unit}).join(" "),this._monitoringDocuments=[],this._monitoringUnsubscribes=[]}function c(t,e,n,o){"function"==typeof t.addEventListener?t.addEventListener(e,n,o||!1):"function"==typeof t.attachEvent&&t.attachEvent("on"+e,n)}function u(t,e,n,o){"function"==typeof t.removeEventListener?t.removeEventListener(e,n,o||!1):"function"==typeof t.detatchEvent&&t.detatchEvent("on"+e,n)}function j(t){var e;try{e=t.getBoundingClientRect()}catch(t){}return e?(e.width&&e.height||(e={top:e.top,right:e.right,bottom:e.bottom,left:e.left,width:e.right-e.left,height:e.bottom-e.top}),e):{top:0,bottom:0,left:0,right:0,width:0,height:0}}function i(t){return!t||"x"in t?t:{top:t.top,y:t.top,bottom:t.bottom,left:t.left,x:t.left,right:t.right,width:t.width,height:t.height}}function E(t,e){var n=e.top-t.top,o=e.left-t.left;return{top:n,left:o,height:e.height,width:e.width,bottom:n+e.height,right:o+e.width}}function e(t,e){for(var n=e;n;){if(n==t)return!0;n=T(n)}return!1}function T(t){var e=t.parentNode;return 9==t.nodeType&&t!=w?s(t):e&&11==e.nodeType&&e.host?e.host:e&&e.assignedSlot?e.assignedSlot.parentNode:e}}();