Skip to content

Commit

Permalink
Releasing 8.8.0 with video support
Browse files Browse the repository at this point in the history
  • Loading branch information
verlok committed Jun 23, 2018
1 parent 6223ae7 commit 2d9da68
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 79 deletions.
56 changes: 30 additions & 26 deletions dist/lazyload.es2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,46 +112,50 @@ const setData = (element, attribute, value) => {
return element.setAttribute(dataPrefix + attribute, value);
};

const setSourcesForPicture = function (element, srcsetDataAttribute) {
const parent = element.parentNode;
if (parent && parent.tagName !== "PICTURE") {
return;
}
for (let i = 0; i < parent.children.length; i++) {
let pictureChild = parent.children[i];
if (pictureChild.tagName === "SOURCE") {
let sourceSrcset = getData(pictureChild, srcsetDataAttribute);
if (sourceSrcset) {
pictureChild.setAttribute("srcset", sourceSrcset);
const setSourcesInChildren = function(parentTag, attrName, dataAttrName) {
for (let i = 0, childTag; childTag = parentTag.children[i]; i += 1) {
if (childTag.tagName === "SOURCE") {
let attributeValue = getData(childTag, dataAttrName);
if (attributeValue) {
childTag.setAttribute(attrName, attributeValue);
}
}
}
};

var setSources = function (element, srcsetDataAttribute, srcDataAttribute) {
const setAttributeIfNotNullOrEmpty = function(element, attrName, value) {
if (!value) {return;}
element.setAttribute(attrName, value);
};

function setSources(element, settings) {
const dataAttrSrcName = settings.data_src;
const elementSrc = getData(element, dataAttrSrcName);
const tagName = element.tagName;
const elementSrc = getData(element, srcDataAttribute);
if (tagName === "IMG") {
setSourcesForPicture(element, srcsetDataAttribute);
const imgSrcset = getData(element, srcsetDataAttribute);
if (imgSrcset) {
element.setAttribute("srcset", imgSrcset);
}
if (elementSrc) {
element.setAttribute("src", elementSrc);
const dataAttrSrcSetName = settings.data_srcset;
const elementSrcSet = getData(element, dataAttrSrcSetName);
const parent = element.parentNode;
if (parent && parent.tagName === "PICTURE") {
setSourcesInChildren(parent, "srcset", dataAttrSrcSetName);
}
setAttributeIfNotNullOrEmpty(element, "srcset", elementSrcSet);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "IFRAME") {
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "VIDEO") {
setSourcesInChildren(element, "src", dataAttrSrcName);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (elementSrc) {
element.style.backgroundImage = `url("${elementSrc}")`;
}
};
}

const runningOnBrowser = (typeof window !== "undefined");

Expand Down Expand Up @@ -220,12 +224,12 @@ LazyLoad.prototype = {
};

callCallback(settings.callback_enter, element);
if (element.tagName === "IMG" || element.tagName === "IFRAME") {
if (["IMG", "IFRAME", "VIDEO"].indexOf(element.tagName) > -1) {
element.addEventListener("load", loadCallback);
element.addEventListener("error", errorCallback);
addClass(element, settings.class_loading);
}
setSources(element, settings.data_srcset, settings.data_src);
setSources(element, settings);
callCallback(settings.callback_set, element);
},

Expand Down
58 changes: 32 additions & 26 deletions dist/lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,46 +110,52 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
return element.setAttribute(dataPrefix + attribute, value);
};

var setSourcesForPicture = function setSourcesForPicture(element, srcsetDataAttribute) {
var parent = element.parentNode;
if (parent && parent.tagName !== "PICTURE") {
return;
}
for (var i = 0; i < parent.children.length; i++) {
var pictureChild = parent.children[i];
if (pictureChild.tagName === "SOURCE") {
var sourceSrcset = getData(pictureChild, srcsetDataAttribute);
if (sourceSrcset) {
pictureChild.setAttribute("srcset", sourceSrcset);
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 setSources = function setSources(element, srcsetDataAttribute, srcDataAttribute) {
var setAttributeIfNotNullOrEmpty = function setAttributeIfNotNullOrEmpty(element, attrName, value) {
if (!value) {
return;
}
element.setAttribute(attrName, value);
};

function setSources(element, settings) {
var dataAttrSrcName = settings.data_src;
var elementSrc = getData(element, dataAttrSrcName);
var tagName = element.tagName;
var elementSrc = getData(element, srcDataAttribute);
if (tagName === "IMG") {
setSourcesForPicture(element, srcsetDataAttribute);
var imgSrcset = getData(element, srcsetDataAttribute);
if (imgSrcset) {
element.setAttribute("srcset", imgSrcset);
}
if (elementSrc) {
element.setAttribute("src", elementSrc);
var dataAttrSrcSetName = settings.data_srcset;
var elementSrcSet = getData(element, dataAttrSrcSetName);
var parent = element.parentNode;
if (parent && parent.tagName === "PICTURE") {
setSourcesInChildren(parent, "srcset", dataAttrSrcSetName);
}
setAttributeIfNotNullOrEmpty(element, "srcset", elementSrcSet);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "IFRAME") {
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "VIDEO") {
setSourcesInChildren(element, "src", dataAttrSrcName);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (elementSrc) {
element.style.backgroundImage = 'url("' + elementSrc + '")';
}
};
}

var runningOnBrowser = typeof window !== "undefined";

Expand Down Expand Up @@ -222,12 +228,12 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol
};

callCallback(settings.callback_enter, element);
if (element.tagName === "IMG" || element.tagName === "IFRAME") {
if (["IMG", "IFRAME", "VIDEO"].indexOf(element.tagName) > -1) {
element.addEventListener("load", loadCallback);
element.addEventListener("error", errorCallback);
addClass(element, settings.class_loading);
}
setSources(element, settings.data_srcset, settings.data_src);
setSources(element, settings);
callCallback(settings.callback_set, element);
},

Expand Down
2 changes: 1 addition & 1 deletion dist/lazyload.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vanilla-lazyload",
"version": "8.7.1",
"version": "8.8.0",
"description": "A fast, lightweight script to load images as they enter the viewport. SEO friendly, it supports responsive images (both srcset + sizes and picture) and progressive JPEG",
"main": "dist/lazyload.min.js",
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ LazyLoad.prototype = {
};

callCallback(settings.callback_enter, element);
if (element.tagName === "IMG" || element.tagName === "IFRAME") {
if (["IMG", "IFRAME", "VIDEO"].indexOf(element.tagName) > -1) {
element.addEventListener("load", loadCallback);
element.addEventListener("error", errorCallback);
addClass(element, settings.class_loading);
}
setSources(element, settings.data_srcset, settings.data_src);
setSources(element, settings);
callCallback(settings.callback_set, element);
},

Expand Down
Loading

0 comments on commit 2d9da68

Please sign in to comment.