/* globals IntersectionObserver, jQuery */
var jetpackLazyImagesModule = function( $ ) {
var images,
config = {
// If the image gets within 200px in the Y axis, start the download.
rootMargin: '200px 0px',
threshold: 0.01
},
imageCount = 0,
observer,
image,
i;
$( document ).ready( function() {
lazy_load_init();
// Lazy load images that are brought in from Infinite Scroll
$( 'body' ).bind( 'post-load', lazy_load_init );
// Add event to provide optional compatibility for other code.
$( 'body' ).bind( 'jetpack-lazy-images-load', lazy_load_init );
} );
function lazy_load_init() {
images = document.querySelectorAll( 'img.jetpack-lazy-image:not(.jetpack-lazy-image--handled)' );
imageCount = images.length;
// If initialized, then disconnect the observer
if ( observer ) {
observer.disconnect();
}
// If we don't have support for intersection observer, loads the images immediately
if ( ! ( 'IntersectionObserver' in window ) ) {
loadImagesImmediately( images );
} else {
// It is supported, load the images
observer = new IntersectionObserver( onIntersection, config );
// foreach() is not supported in IE
for ( i = 0; i < images.length; i++ ) {
image = images[ i ];
if ( image.getAttribute( 'data-lazy-loaded' ) ) {
continue;
}
observer.observe( image );
}
}
}
/**
* Load all of the images immediately
* @param {NodeListOf} immediateImages List of lazy-loaded images to load immediately.
*/
function loadImagesImmediately( immediateImages ) {
var i;
// foreach() is not supported in IE
for ( i = 0; i < immediateImages.length; i++ ) {
var image = immediateImages[ i ];
applyImage( image );
}
}
/**
* On intersection
* @param {array} entries List of elements being observed.
*/
function onIntersection( entries ) {
var i;
// Disconnect if we've already loaded all of the images
if ( imageCount === 0 ) {
observer.disconnect();
}
// Loop through the entries
for ( i = 0; i < entries.length; i++ ) {
var entry = entries[ i ];
// Are we in viewport?
if ( entry.intersectionRatio > 0 ) {
imageCount--;
// Stop watching and load the image
observer.unobserve( entry.target );
applyImage( entry.target );
}
}
}
/**
* Apply the image
* @param {object} image The image object.
*/
function applyImage( image ) {
var theImage = $( image ),
srcset,
sizes,
theClone;
if ( ! theImage.length ) {
return;
}
srcset = theImage.attr( 'data-lazy-srcset' );
sizes = theImage.attr( 'data-lazy-sizes' );
theClone = theImage.clone(true);
// Remove lazy attributes from the clone.
theClone.removeAttr( 'data-lazy-srcset' ),
theClone.removeAttr( 'data-lazy-sizes' );
theClone.removeAttr( 'data-lazy-src' );
// Add the attributes we want on the finished image.
theClone.addClass( 'jetpack-lazy-image--handled' );
theClone.attr( 'data-lazy-loaded', 1 );
if ( ! srcset ) {
theClone.removeAttr( 'srcset' );
} else {
theClone.attr( 'srcset', srcset );
}
if ( sizes ) {
theClone.attr( 'sizes', sizes );
}
theImage.replaceWith( theClone );
// Fire an event so that third-party code can perform actions after an image is loaded.
theClone.trigger( 'jetpack-lazy-loaded-image' );
}
};
/**
* The following is an Intersection observer polyfill which is licensed under
* the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE and can be found at:
* https://github.com/w3c/IntersectionObserver/tree/master/polyfill
*/
/* jshint ignore:start */
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE.
*
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*
*/
(function(window, document) {
'use strict';
// Exits early if all IntersectionObserver and IntersectionObserverEntry
// features are natively supported.
if ('IntersectionObserver' in window &&
'IntersectionObserverEntry' in window &&
'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
// Minimal polyfill for Edge 15's lack of `isIntersecting`
// See: https://github.com/w3c/IntersectionObserver/issues/211
if (!('isIntersecting' in window.IntersectionObserverEntry.prototype)) {
Object.defineProperty(window.IntersectionObserverEntry.prototype,
'isIntersecting', {
get: function () {
return this.intersectionRatio > 0;
}
});
}
return;
}
/**
* An IntersectionObserver registry. This registry exists to hold a strong
* reference to IntersectionObserver instances currently observering a target
* element. Without this registry, instances without another reference may be
* garbage collected.
*/
var registry = [];
/**
* Creates the global IntersectionObserverEntry constructor.
* https://w3c.github.io/IntersectionObserver/#intersection-observer-entry
* @param {Object} entry A dictionary of instance properties.
* @constructor
*/
function IntersectionObserverEntry(entry) {
this.time = entry.time;
this.target = entry.target;
this.rootBounds = entry.rootBounds;
this.boundingClientRect = entry.boundingClientRect;
this.intersectionRect = entry.intersectionRect || getEmptyRect();
this.isIntersecting = !!entry.intersectionRect;
// Calculates the intersection ratio.
var targetRect = this.boundingClientRect;
var targetArea = targetRect.width * targetRect.height;
var intersectionRect = this.intersectionRect;
var intersectionArea = intersectionRect.width * intersectionRect.height;
// Sets intersection ratio.
if (targetArea) {
this.intersectionRatio = intersectionArea / targetArea;
} else {
// If area is zero and is intersecting, sets to 1, otherwise to 0
this.intersectionRatio = this.isIntersecting ? 1 : 0;
}
}
/**
* Creates the global IntersectionObserver constructor.
* https://w3c.github.io/IntersectionObserver/#intersection-observer-interface
* @param {Function} callback The function to be invoked after intersection
* changes have queued. The function is not invoked if the queue has
* been emptied by calling the `takeRecords` method.
* @param {Object=} opt_options Optional configuration options.
* @constructor
*/
function IntersectionObserver(callback, opt_options) {
var options = opt_options || {};
if (typeof callback != 'function') {
throw new Error('callback must be a function');
}
if (options.root && options.root.nodeType != 1) {
throw new Error('root must be an Element');
}
// Binds and throttles `this._checkForIntersections`.
this._checkForIntersections = throttle(
this._checkForIntersections.bind(this), this.THROTTLE_TIMEOUT);
// Private properties.
this._callback = callback;
this._observationTargets = [];
this._queuedEntries = [];
this._rootMarginValues = this._parseRootMargin(options.rootMargin);
// Public properties.
this.thresholds = this._initThresholds(options.threshold);
this.root = options.root || null;
this.rootMargin = this._rootMarginValues.map(function(margin) {
return margin.value + margin.unit;
}).join(' ');
}
/**
* The minimum interval within which the document will be checked for
* intersection changes.
*/
IntersectionObserver.prototype.THROTTLE_TIMEOUT = 100;
/**
* The frequency in which the polyfill polls for intersection changes.
* this can be updated on a per instance basis and must be set prior to
* calling `observe` on the first target.
*/
IntersectionObserver.prototype.POLL_INTERVAL = null;
/**
* Use a mutation observer on the root element
* to detect intersection changes.
*/
IntersectionObserver.prototype.USE_MUTATION_OBSERVER = true;
/**
* Starts observing a target element for intersection changes based on
* the thresholds values.
* @param {Element} target The DOM element to observe.
*/
IntersectionObserver.prototype.observe = function(target) {
var isTargetAlreadyObserved = this._observationTargets.some(function(item) {
return item.element == target;
});
if (isTargetAlreadyObserved) {
return;
}
if (!(target && target.nodeType == 1)) {
throw new Error('target must be an Element');
}
this._registerInstance();
this._observationTargets.push({element: target, entry: null});
this._monitorIntersections();
this._checkForIntersections();
};
/**
* Stops observing a target element for intersection changes.
* @param {Element} target The DOM element to observe.
*/
IntersectionObserver.prototype.unobserve = function(target) {
this._observationTargets =
this._observationTargets.filter(function(item) {
return item.element != target;
});
if (!this._observationTargets.length) {
this._unmonitorIntersections();
this._unregisterInstance();
}
};
/**
* Stops observing all target elements for intersection changes.
*/
IntersectionObserver.prototype.disconnect = function() {
this._observationTargets = [];
this._unmonitorIntersections();
this._unregisterInstance();
};
/**
* Returns any queue entries that have not yet been reported to the
* callback and clears the queue. This can be used in conjunction with the
* callback to obtain the absolute most up-to-date intersection information.
* @return {Array} The currently queued entries.
*/
IntersectionObserver.prototype.takeRecords = function() {
var records = this._queuedEntries.slice();
this._queuedEntries = [];
return records;
};
/**
* Accepts the threshold value from the user configuration object and
* returns a sorted array of unique threshold values. If a value is not
* between 0 and 1 and error is thrown.
* @private
* @param {Array|number=} opt_threshold An optional threshold value or
* a list of threshold values, defaulting to [0].
* @return {Array} A sorted list of unique and valid threshold values.
*/
IntersectionObserver.prototype._initThresholds = function(opt_threshold) {
var threshold = opt_threshold || [0];
if (!Array.isArray(threshold)) threshold = [threshold];
return threshold.sort().filter(function(t, i, a) {
if (typeof t != 'number' || isNaN(t) || t < 0 || t > 1) {
throw new Error('threshold must be a number between 0 and 1 inclusively');
}
return t !== a[i - 1];
});
};
/**
* Accepts the rootMargin value from the user configuration object
* and returns an array of the four margin values as an object containing
* the value and unit properties. If any of the values are not properly
* formatted or use a unit other than px or %, and error is thrown.
* @private
* @param {string=} opt_rootMargin An optional rootMargin value,
* defaulting to '0px'.
* @return {Array