|
@ -1,300 +1,172 @@ |
|
|
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; |
|
|
/*! |
|
|
|
|
|
* Lazy Load - JavaScript plugin for lazy loading images |
|
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; |
|
|
* |
|
|
|
|
|
* Copyright (c) 2007-2017 Mika Tuupola |
|
|
(function (global, factory) { |
|
|
* |
|
|
(typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : global.LazyLoad = factory(); |
|
|
* Licensed under the MIT license: |
|
|
})(this, function () { |
|
|
* http://www.opensource.org/licenses/mit-license.php
|
|
|
'use strict'; |
|
|
* |
|
|
|
|
|
* Project home: |
|
|
var getInstanceSettings = function getInstanceSettings(customSettings) { |
|
|
* https://appelsiini.net/projects/lazyload
|
|
|
var defaultSettings = { |
|
|
* |
|
|
elements_selector: "img", |
|
|
* Version: 2.0.0-beta.2 |
|
|
container: document, |
|
|
* |
|
|
threshold: 300, |
|
|
*/ |
|
|
data_src: "src", |
|
|
|
|
|
data_srcset: "srcset", |
|
|
(function (root, factory) { |
|
|
data_sizes: "sizes", |
|
|
if (typeof exports === "object") { |
|
|
class_loading: "loading", |
|
|
module.exports = factory(root); |
|
|
class_loaded: "loaded", |
|
|
} else if (typeof define === "function" && define.amd) { |
|
|
class_error: "error", |
|
|
define([], factory(root)); |
|
|
callback_load: null, |
|
|
} else { |
|
|
callback_error: null, |
|
|
root.LazyLoad = factory(root); |
|
|
callback_set: null, |
|
|
} |
|
|
callback_enter: null |
|
|
}) (typeof global !== "undefined" ? global : this.window || this.global, function (root) { |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return _extends({}, defaultSettings, customSettings); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var dataPrefix = "data-"; |
|
|
|
|
|
var processedDataName = "was-processed"; |
|
|
|
|
|
var processedDataValue = "true"; |
|
|
|
|
|
|
|
|
|
|
|
var getData = function getData(element, attribute) { |
|
|
|
|
|
return element.getAttribute(dataPrefix + attribute); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var setData = function setData(element, attribute, value) { |
|
|
"use strict"; |
|
|
return element.setAttribute(dataPrefix + attribute, value); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var setWasProcessed = function setWasProcessed(element) { |
|
|
const defaults = { |
|
|
return setData(element, processedDataName, processedDataValue); |
|
|
src: "data-src", |
|
|
|
|
|
srcset: "data-srcset", |
|
|
|
|
|
selector: ".lazyload" |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
var getWasProcessed = function getWasProcessed(element) { |
|
|
/** |
|
|
return getData(element, processedDataName) === processedDataValue; |
|
|
* Merge two or more objects. Returns a new object. |
|
|
}; |
|
|
* @private |
|
|
|
|
|
* @param {Boolean} deep If true, do a deep (or recursive) merge [optional] |
|
|
|
|
|
* @param {Object} objects The objects to merge together |
|
|
|
|
|
* @returns {Object} Merged values of defaults and options |
|
|
|
|
|
*/ |
|
|
|
|
|
const extend = function () { |
|
|
|
|
|
|
|
|
function purgeElements(elements) { |
|
|
let extended = {}; |
|
|
return elements.filter(function (element) { |
|
|
let deep = false; |
|
|
return !getWasProcessed(element); |
|
|
let i = 0; |
|
|
}); |
|
|
let length = arguments.length; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Creates instance and notifies it through the window element */ |
|
|
/* Check if a deep merge */ |
|
|
var createInstance = function createInstance(classObj, options) { |
|
|
if (Object.prototype.toString.call(arguments[0]) === "[object Boolean]") { |
|
|
var event; |
|
|
deep = arguments[0]; |
|
|
var eventString = "LazyLoad::Initialized"; |
|
|
i++; |
|
|
var instance = new classObj(options); |
|
|
|
|
|
try { |
|
|
|
|
|
// Works in modern browsers
|
|
|
|
|
|
event = new CustomEvent(eventString, { detail: { instance: instance } }); |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
// Works in Internet Explorer (all versions)
|
|
|
|
|
|
event = document.createEvent("CustomEvent"); |
|
|
|
|
|
event.initCustomEvent(eventString, false, false, { instance: instance }); |
|
|
|
|
|
} |
|
|
} |
|
|
window.dispatchEvent(event); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/* Auto initialization of one or more instances of lazyload, depending on the |
|
|
/* Merge the object into the extended object */ |
|
|
options passed in (plain object or an array) */ |
|
|
let merge = function (obj) { |
|
|
function autoInitialize(classObj, options) { |
|
|
for (let prop in obj) { |
|
|
if (!options.length) { |
|
|
if (Object.prototype.hasOwnProperty.call(obj, prop)) { |
|
|
// Plain object
|
|
|
/* If deep merge and property is an object, merge properties */ |
|
|
createInstance(classObj, options); |
|
|
if (deep && Object.prototype.toString.call(obj[prop]) === "[object Object]") { |
|
|
|
|
|
extended[prop] = extend(true, extended[prop], obj[prop]); |
|
|
} else { |
|
|
} else { |
|
|
// Array of objects
|
|
|
extended[prop] = obj[prop]; |
|
|
for (var i = 0, optionsItem; optionsItem = options[i]; i += 1) { |
|
|
|
|
|
createInstance(classObj, optionsItem); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var setSourcesInChildren = function setSourcesInChildren(parentTag, attrName, dataAttrName) { |
|
|
|
|
|
for (var i = 0, childTag; childTag = parentTag.children[i]; i += 1) { |
|
|
|
|
|
if (childTag.tagName === "SOURCE") { |
|
|
|
|
|
var attributeValue = getData(childTag, dataAttrName); |
|
|
|
|
|
if (attributeValue) { |
|
|
|
|
|
childTag.setAttribute(attrName, attributeValue); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
var setAttributeIfNotNullOrEmpty = function setAttributeIfNotNullOrEmpty(element, attrName, value) { |
|
|
/* Loop through each object and conduct a merge */ |
|
|
if (!value) { |
|
|
for (; i < length; i++) { |
|
|
return; |
|
|
let obj = arguments[i]; |
|
|
|
|
|
merge(obj); |
|
|
} |
|
|
} |
|
|
element.setAttribute(attrName, value); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var setSources = function setSources(element, settings) { |
|
|
|
|
|
var sizesDataName = settings.data_sizes, |
|
|
|
|
|
srcsetDataName = settings.data_srcset, |
|
|
|
|
|
srcDataName = settings.data_src; |
|
|
|
|
|
|
|
|
|
|
|
var srcDataValue = getData(element, srcDataName); |
|
|
return extended; |
|
|
switch (element.tagName) { |
|
|
|
|
|
case "IMG": |
|
|
|
|
|
{ |
|
|
|
|
|
var parent = element.parentNode; |
|
|
|
|
|
if (parent && parent.tagName === "PICTURE") { |
|
|
|
|
|
setSourcesInChildren(parent, "srcset", srcsetDataName); |
|
|
|
|
|
} |
|
|
|
|
|
var sizesDataValue = getData(element, sizesDataName); |
|
|
|
|
|
setAttributeIfNotNullOrEmpty(element, "sizes", sizesDataValue); |
|
|
|
|
|
var srcsetDataValue = getData(element, srcsetDataName); |
|
|
|
|
|
setAttributeIfNotNullOrEmpty(element, "srcset", srcsetDataValue); |
|
|
|
|
|
setAttributeIfNotNullOrEmpty(element, "src", srcDataValue); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
case "IFRAME": |
|
|
|
|
|
setAttributeIfNotNullOrEmpty(element, "src", srcDataValue); |
|
|
|
|
|
break; |
|
|
|
|
|
case "VIDEO": |
|
|
|
|
|
setSourcesInChildren(element, "src", srcDataName); |
|
|
|
|
|
setAttributeIfNotNullOrEmpty(element, "src", srcDataValue); |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
if (srcDataValue) { |
|
|
|
|
|
element.style.backgroundImage = 'url("' + srcDataValue + '")'; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
var isBot = "onscroll" in window && !/glebot/.test(navigator.userAgent); |
|
|
function LazyLoad(images, options) { |
|
|
|
|
|
this.settings = extend(defaults, options || {}); |
|
|
var runningOnBrowser = typeof window !== "undefined"; |
|
|
this.images = images || document.querySelectorAll(this.settings.selector); |
|
|
|
|
|
this.observer = null; |
|
|
var supportsIntersectionObserver = runningOnBrowser && "IntersectionObserver" in window; |
|
|
this.init(); |
|
|
|
|
|
|
|
|
var supportsClassList = runningOnBrowser && "classList" in document.createElement("p"); |
|
|
|
|
|
|
|
|
|
|
|
var addClass = function addClass(element, className) { |
|
|
|
|
|
if (supportsClassList) { |
|
|
|
|
|
element.classList.add(className); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
element.className += (element.className ? " " : "") + className; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var removeClass = function removeClass(element, className) { |
|
|
LazyLoad.prototype = { |
|
|
if (supportsClassList) { |
|
|
init: function() { |
|
|
element.classList.remove(className); |
|
|
|
|
|
|
|
|
/* Without observers load everything and bail out early. */ |
|
|
|
|
|
if (!root.IntersectionObserver) { |
|
|
|
|
|
this.loadImages(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
element.className = element.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), " ").replace(/^\s+/, "").replace(/\s+$/, ""); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var callCallback = function callCallback(callback, argument) { |
|
|
let self = this; |
|
|
if (callback) { |
|
|
let observerConfig = { |
|
|
callback(argument); |
|
|
root: null, |
|
|
} |
|
|
rootMargin: "0px", |
|
|
|
|
|
threshold: [0] |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
var loadString = "load"; |
|
|
this.observer = new IntersectionObserver(function(entries) { |
|
|
var errorString = "error"; |
|
|
entries.forEach(function (entry) { |
|
|
|
|
|
if (entry.intersectionRatio > 0) { |
|
|
var removeListeners = function removeListeners(element, loadHandler, errorHandler) { |
|
|
self.observer.unobserve(entry.target); |
|
|
element.removeEventListener(loadString, loadHandler); |
|
|
let src = entry.target.getAttribute(self.settings.src); |
|
|
element.removeEventListener(errorString, errorHandler); |
|
|
let srcset = entry.target.getAttribute(self.settings.srcset); |
|
|
}; |
|
|
if ("img" === entry.target.tagName.toLowerCase()) { |
|
|
|
|
|
if (src) { |
|
|
var addOneShotListeners = function addOneShotListeners(element, settings) { |
|
|
entry.target.src = src; |
|
|
var onLoad = function onLoad(event) { |
|
|
|
|
|
onEvent(event, true, settings); |
|
|
|
|
|
removeListeners(element, onLoad, onError); |
|
|
|
|
|
}; |
|
|
|
|
|
var onError = function onError(event) { |
|
|
|
|
|
onEvent(event, false, settings); |
|
|
|
|
|
removeListeners(element, onLoad, onError); |
|
|
|
|
|
}; |
|
|
|
|
|
element.addEventListener(loadString, onLoad); |
|
|
|
|
|
element.addEventListener(errorString, onError); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var onEvent = function onEvent(event, success, settings) { |
|
|
|
|
|
var element = event.target; |
|
|
|
|
|
removeClass(element, settings.class_loading); |
|
|
|
|
|
addClass(element, success ? settings.class_loaded : settings.class_error); // Setting loaded or error class
|
|
|
|
|
|
callCallback(success ? settings.callback_load : settings.callback_error, element); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function revealElement(element, settings, force) { |
|
|
|
|
|
if (!force && getWasProcessed(element)) { |
|
|
|
|
|
return; // element has already been processed and force wasn't true
|
|
|
|
|
|
} |
|
|
|
|
|
callCallback(settings.callback_enter, element); |
|
|
|
|
|
if (["IMG", "IFRAME", "VIDEO"].indexOf(element.tagName) > -1) { |
|
|
|
|
|
addOneShotListeners(element, settings); |
|
|
|
|
|
addClass(element, settings.class_loading); |
|
|
|
|
|
} |
|
|
} |
|
|
setSources(element, settings); |
|
|
if (srcset) { |
|
|
setWasProcessed(element); |
|
|
entry.target.srcset = srcset; |
|
|
callCallback(settings.callback_set, element); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} else { |
|
|
/* entry.isIntersecting needs fallback because is null on some versions of MS Edge, and |
|
|
entry.target.style.backgroundImage = "url(" + src + ")"; |
|
|
entry.intersectionRatio is not enough alone because it could be 0 on some intersecting elements */ |
|
|
|
|
|
var isIntersecting = function isIntersecting(element) { |
|
|
|
|
|
return element.isIntersecting || element.intersectionRatio > 0; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var getObserverSettings = function getObserverSettings(settings) { |
|
|
|
|
|
return { |
|
|
|
|
|
root: settings.container === document ? null : settings.container, |
|
|
|
|
|
rootMargin: settings.threshold + "px" |
|
|
|
|
|
}; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var LazyLoad = function LazyLoad(customSettings, elements) { |
|
|
|
|
|
this._settings = getInstanceSettings(customSettings); |
|
|
|
|
|
this._setObserver(); |
|
|
|
|
|
this.update(elements); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
LazyLoad.prototype = { |
|
|
|
|
|
_setObserver: function _setObserver() { |
|
|
|
|
|
var _this = this; |
|
|
|
|
|
|
|
|
|
|
|
if (!supportsIntersectionObserver) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
var revealIntersectingElements = function revealIntersectingElements(entries) { |
|
|
|
|
|
entries.forEach(function (entry) { |
|
|
|
|
|
if (isIntersecting(entry)) { |
|
|
|
|
|
var element = entry.target; |
|
|
|
|
|
_this.load(element); |
|
|
|
|
|
_this._observer.unobserve(element); |
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
_this._elements = purgeElements(_this._elements); |
|
|
}, observerConfig); |
|
|
}; |
|
|
|
|
|
this._observer = new IntersectionObserver(revealIntersectingElements, getObserverSettings(this._settings)); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
loadAll: function loadAll() { |
|
|
this.images.forEach(function (image) { |
|
|
var _this2 = this; |
|
|
self.observer.observe(image); |
|
|
|
|
|
|
|
|
this._elements.forEach(function (element) { |
|
|
|
|
|
_this2.load(element); |
|
|
|
|
|
}); |
|
|
}); |
|
|
this._elements = purgeElements(this._elements); |
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
update: function update(elements) { |
|
|
loadAndDestroy: function () { |
|
|
var _this3 = this; |
|
|
if (!this.settings) { return; } |
|
|
|
|
|
this.loadImages(); |
|
|
var settings = this._settings; |
|
|
this.destroy(); |
|
|
var nodeSet = elements || settings.container.querySelectorAll(settings.elements_selector); |
|
|
}, |
|
|
|
|
|
|
|
|
this._elements = purgeElements(Array.prototype.slice.call(nodeSet)); // nodeset to array for IE compatibility
|
|
|
loadImages: function () { |
|
|
|
|
|
if (!this.settings) { return; } |
|
|
|
|
|
|
|
|
if (isBot || !this._observer) { |
|
|
let self = this; |
|
|
this.loadAll(); |
|
|
this.images.forEach(function (image) { |
|
|
return; |
|
|
let src = image.getAttribute(self.settings.src); |
|
|
|
|
|
let srcset = image.getAttribute(self.settings.srcset); |
|
|
|
|
|
if ("img" === image.tagName.toLowerCase()) { |
|
|
|
|
|
if (src) { |
|
|
|
|
|
image.src = src; |
|
|
|
|
|
} |
|
|
|
|
|
if (srcset) { |
|
|
|
|
|
image.srcset = srcset; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
image.style.backgroundImage = "url(" + src + ")"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this._elements.forEach(function (element) { |
|
|
|
|
|
_this3._observer.observe(element); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
destroy: function destroy() { |
|
|
destroy: function () { |
|
|
var _this4 = this; |
|
|
if (!this.settings) { return; } |
|
|
|
|
|
this.observer.disconnect(); |
|
|
if (this._observer) { |
|
|
this.settings = null; |
|
|
purgeElements(this._elements).forEach(function (element) { |
|
|
|
|
|
_this4._observer.unobserve(element); |
|
|
|
|
|
}); |
|
|
|
|
|
this._observer = null; |
|
|
|
|
|
} |
|
|
} |
|
|
this._elements = null; |
|
|
}; |
|
|
this._settings = null; |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
load: function load(element, force) { |
|
|
root.lazyload = function(images, options) { |
|
|
revealElement(element, this._settings, force); |
|
|
return new LazyLoad(images, options); |
|
|
} |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/* Automatic instances creation if required (useful for async script loading!) */ |
|
|
if (root.jQuery) { |
|
|
var autoInitOptions = window.lazyLoadOptions; |
|
|
const $ = root.jQuery; |
|
|
if (runningOnBrowser && autoInitOptions) { |
|
|
$.fn.lazyload = function (options) { |
|
|
autoInitialize(LazyLoad, autoInitOptions); |
|
|
options = options || {}; |
|
|
|
|
|
options.attribute = options.attribute || "data-src"; |
|
|
|
|
|
new LazyLoad($.makeArray(this), options); |
|
|
|
|
|
return this; |
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return LazyLoad; |
|
|
return LazyLoad; |
|
|