Skip to content

Commit

Permalink
Merge pull request #33 from wp-media/branch-1.4.5
Browse files Browse the repository at this point in the history
1.4.5
  • Loading branch information
remyperona authored Dec 19, 2017
2 parents 7f48a11 + e8a6367 commit 255c49c
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 34 deletions.
249 changes: 249 additions & 0 deletions assets/js/lazyload-10.3.5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
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; };

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; };

(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();
})(this, function () {
'use strict';

var defaultSettings = {
elements_selector: "img",
container: document,
threshold: 300,
data_src: "src",
data_srcset: "srcset",
class_loading: "loading",
class_loaded: "loaded",
class_error: "error",
callback_load: null,
callback_error: null,
callback_set: null
};

var dataPrefix = "data-";

var getData = function getData(element, attribute) {
return element.getAttribute(dataPrefix + attribute);
};

var setData = function setData(element, attribute, value) {
return element.setAttribute(dataPrefix + attribute, value);
};

var purgeElements = function purgeElements(elements) {
return elements.filter(function (element) {
return !getData(element, "was-processed");
});
};

/* Creates instance and notifies it through the window element */
var createInstance = function createInstance(classObj, options) {
var event;
var eventString = "LazyLoad::Initialized";
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
options passed in (plain object or an array) */
var autoInitialize = function autoInitialize(classObj, options) {
if (!options.length) {
// Plain object
createInstance(classObj, options);
} else {
// Array of objects
for (var i = 0, optionsItem; optionsItem = options[i]; i += 1) {
createInstance(classObj, optionsItem);
}
}
};

var setSourcesForPicture = function setSourcesForPicture(element, settings) {
var dataSrcSet = settings.data_srcset;

var parent = element.parentNode;
if (parent.tagName !== "PICTURE") {
return;
}
for (var i = 0, pictureChild; pictureChild = parent.children[i]; i += 1) {
if (pictureChild.tagName === "SOURCE") {
var sourceSrcset = getData(pictureChild, dataSrcSet);
if (sourceSrcset) {
pictureChild.setAttribute("srcset", sourceSrcset);
}
}
}
};

var setSources = function setSources(element, settings) {
var dataSrc = settings.data_src,
dataSrcSet = settings.data_srcset;

var tagName = element.tagName;
var elementSrc = getData(element, dataSrc);
if (tagName === "IMG") {
setSourcesForPicture(element, settings);
var imgSrcset = getData(element, dataSrcSet);
if (imgSrcset) {
element.setAttribute("srcset", imgSrcset);
}
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
return;
}
if (tagName === "IFRAME") {
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
return;
}
if (elementSrc) {
element.style.backgroundImage = 'url("' + elementSrc + '")';
}
};

var supportsClassList = "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) {
if (supportsClassList) {
element.classList.remove(className);
return;
}
element.className = element.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), " ").replace(/^\s+/, "").replace(/\s+$/, "");
};

var callCallback = function callCallback(callback, argument) {
if (callback) {
callback(argument);
}
};

var loadString = "load";
var errorString = "error";

var removeListeners = function removeListeners(element, loadHandler, errorHandler) {
element.removeEventListener(loadString, loadHandler);
element.removeEventListener(errorString, errorHandler);
};

var addOneShotListeners = function addOneShotListeners(element, settings) {
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); // Calling loaded or error callback
};

var revealElement = function revealElement(element, settings) {
if (["IMG", "IFRAME"].indexOf(element.tagName) > -1) {
addOneShotListeners(element, settings);
addClass(element, settings.class_loading);
}
setSources(element, settings);
setData(element, "was-processed", true);
callCallback(settings.callback_set, element);
};

var LazyLoad = function LazyLoad(instanceSettings, elements) {
this._settings = _extends({}, defaultSettings, instanceSettings);
this._setObserver();
this.update(elements);
};

LazyLoad.prototype = {
_setObserver: function _setObserver() {
var _this = this;

if (!("IntersectionObserver" in window)) {
return;
}

var settings = this._settings;
var onIntersection = function onIntersection(entries) {
entries.forEach(function (entry) {
if (entry.intersectionRatio > 0) {
var element = entry.target;
revealElement(element, settings);
_this._observer.unobserve(element);
}
});
_this._elements = purgeElements(_this._elements);
};
this._observer = new IntersectionObserver(onIntersection, {
root: settings.container === document ? null : settings.container,
rootMargin: settings.threshold + "px"
});
},

update: function update(elements) {
var _this2 = this;

var settings = this._settings;
var nodeSet = elements || settings.container.querySelectorAll(settings.elements_selector);

this._elements = purgeElements(Array.prototype.slice.call(nodeSet)); // nodeset to array for IE compatibility
if (this._observer) {
this._elements.forEach(function (element) {
_this2._observer.observe(element);
});
return;
}
// Fallback: load all elements at once
this._elements.forEach(function (element) {
revealElement(element, settings);
});
this._elements = purgeElements(this._elements);
},

destroy: function destroy() {
var _this3 = this;

if (this._observer) {
purgeElements(this._elements).forEach(function (element) {
_this3._observer.unobserve(element);
});
this._observer = null;
}
this._elements = null;
this._settings = null;
}
};

/* Automatic instances creation if required (useful for async script loading!) */
var autoInitOptions = window.lazyLoadOptions;
if (autoInitOptions) {
autoInitialize(LazyLoad, autoInitOptions);
}

return LazyLoad;
});
1 change: 1 addition & 0 deletions assets/js/lazyload-10.3.5.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Contributors: creativejuiz, tabrisrp, wp_media
Tags: lazyload, lazy load, images, iframes, thumbnail, thumbnails, smiley, smilies, avatar, gravatar
Requires at least: 3.0
Tested up to: 4.8
Requires PHP: 5.3
Stable tag: 1.4.5

The tiny Lazy Load script for WordPress without jQuery, works for images and iframes.
Expand Down Expand Up @@ -67,6 +68,8 @@ Some plugins are not compatible without lazy loading. Please open a support thre
= 1.4.5 =
* Rename Setting Page Name in WP Menu
* New Product Banner in Settings Page
* Conditionally load a different version of the script depending on browser support of IntersectionObserver
* Fix a bug where images initially hidden are not correctly displayed when coming into view (slider, tabs, accordion)

= 1.4.4 =
* Admin Redesign
Expand Down
59 changes: 25 additions & 34 deletions rocket-lazy-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
define( 'ROCKET_LL_3RD_PARTY_PATH', ROCKET_LL_PATH . '3rd-party/' );
define( 'ROCKET_LL_ASSETS_URL', plugin_dir_url( __FILE__ ) . 'assets/' );
define( 'ROCKET_LL_FRONT_JS_URL', ROCKET_LL_ASSETS_URL . 'js/' );
define( 'ROCKET_LL_JS_VERSION' , '8.2' );


/**
Expand Down Expand Up @@ -106,37 +105,54 @@ function rocket_lazyload_script() {
return;
}

$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

/**
* Filters the threshold at which lazyload is triggered
*
* @since 1.2
* @author Remy Perona
*
* @param int $threshold Threshold value.
*/
$threshold = apply_filters( 'rocket_lazyload_threshold', 300 );

echo <<<HTML
<script>window.lazyLoadOptions = {
echo '<script>(function(w, d){
var b = d.getElementsByTagName("body")[0];
var s = d.createElement("script"); s.async = true;
var v = !("IntersectionObserver" in w) ? "8.5.2" : "10.3.5";
s.src = "' . ROCKET_LL_FRONT_JS_URL . 'lazyload-v' . $suffix . '.js";
s.src = s.src.replace( "-v", "-" + v );
w.lazyLoadOptions = {
elements_selector: "img, iframe",
data_src: "lazy-src",
data_srcset: "lazy-srcset",
skip_invisible: false,
class_loading: "lazyloading",
class_loaded: "lazyloaded",
threshold: $threshold,
threshold: ' . $threshold . ',
callback_load: function(element) {
if ( element.tagName === "IFRAME" && element.dataset.rocketLazyload == "fitvidscompatible" ) {
if (element.classList.contains("lazyloaded") ) {
if (typeof window.jQuery != 'undefined') {
if (typeof window.jQuery != "undefined") {
if (jQuery.fn.fitVids) {
jQuery(element).parent().fitVids();
}
}
}
}
}
};</script>
HTML;
}
};
b.appendChild(s);
}(window, document));</script>';

if ( rocket_lazyload_get_option( 'youtube' ) ) {
echo <<<HTML
<script>function lazyLoadThumb(e){var t='<img src="https://i.ytimg.com/vi/ID/hqdefault.jpg">',a='<div class="play"></div>';return t.replace("ID",e)+a}function lazyLoadYoutubeIframe(){var e=document.createElement("iframe"),t="https://www.youtube.com/embed/ID?autoplay=1";e.setAttribute("src",t.replace("ID",this.dataset.id)),e.setAttribute("frameborder","0"),e.setAttribute("allowfullscreen","1"),this.parentNode.replaceChild(e,this)}document.addEventListener("DOMContentLoaded",function(){var e,t,a=document.getElementsByClassName("rll-youtube-player");for(t=0;t<a.length;t++)e=document.createElement("div"),e.setAttribute("data-id",a[t].dataset.id),e.innerHTML=lazyLoadThumb(a[t].dataset.id),e.onclick=lazyLoadYoutubeIframe,a[t].appendChild(e)});</script>
HTML;
}
}
add_action( 'wp_footer', 'rocket_lazyload_script', 9 );
add_action( 'wp_footer', 'rocket_lazyload_script', PHP_INT_MAX );

/**
* Enqueue the lazyload script
Expand All @@ -149,11 +165,6 @@ function rocket_lazyload_enqueue() {
return;
}

$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$ll_url = ROCKET_LL_FRONT_JS_URL . 'lazyload-' . ROCKET_LL_JS_VERSION . $suffix . '.js';

wp_enqueue_script( 'rocket-lazyload', $ll_url, null, null, true );

if ( rocket_lazyload_get_option( 'youtube' ) ) {
$css = '.rll-youtube-player{position:relative;padding-bottom:56.23%;height:0;overflow:hidden;max-width:100%;background:#000;margin:5px}.rll-youtube-player iframe{position:absolute;top:0;left:0;width:100%;height:100%;z-index:100;background:0 0}.rll-youtube-player img{bottom:0;display:block;left:0;margin:auto;max-width:100%;width:100%;position:absolute;right:0;top:0;border:none;height:auto;cursor:pointer;-webkit-transition:.4s all;-moz-transition:.4s all;transition:.4s all}.rll-youtube-player img:hover{-webkit-filter:brightness(75%)}.rll-youtube-player .play{height:72px;width:72px;left:50%;top:50%;margin-left:-36px;margin-top:-36px;position:absolute;background:url(' . ROCKET_LL_ASSETS_URL . 'img/play.png) no-repeat;cursor:pointer}';

Expand All @@ -164,26 +175,6 @@ function rocket_lazyload_enqueue() {
}
add_action( 'wp_enqueue_scripts', 'rocket_lazyload_enqueue', PHP_INT_MAX );

/**
* Add tags to the lazyload script to async and prevent concatenation
*
* @since 1.2
* @author Remy Perona
*
* @param string $tag HTML for the script.
* @param string $handle Handle for the script.
*
* @return string Updated HTML
*/
function rocket_lazyload_async_script( $tag, $handle ) {
if ( 'rocket-lazyload' === $handle ) {
return str_replace( '<script', '<script async data-minify="1"', $tag );
}

return $tag;
}
add_filter( 'script_loader_tag', 'rocket_lazyload_async_script', 10, 2 );

/**
* Replace Gravatar, thumbnails, images in post content and in widget text by LazyLoad
*
Expand Down

0 comments on commit 255c49c

Please sign in to comment.