Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate loaded check for custom load() function. #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 47 additions & 30 deletions dist/lozad.es.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! lozad.js - v1.16.0 - 2020-09-10
/*! lozad.js - v1.16.0 - 2021-10-13
* https://github.com/ApoorvSaxena/lozad.js
* Copyright (c) 2020 Apoorv Saxena; Licensed MIT */
* Copyright (c) 2021 Apoorv Saxena; Licensed MIT */


/**
Expand All @@ -22,6 +22,7 @@ const validAttribute = ['data-iesrc', 'data-alt', 'data-src', 'data-srcset', 'da
const defaultConfig = {
rootMargin: '0px',
threshold: 0,
checkKey: 'default',
enableAutoReload: false,
load(element) {
if (element.nodeName.toLowerCase() === 'picture') {
Expand Down Expand Up @@ -98,8 +99,12 @@ const defaultConfig = {
loaded() {}
};

function markAsLoaded(element) {
element.setAttribute('data-loaded', true);
function getAttr (element) {
return element.getAttribute('data-lozaded') || ''
}

function markAsLoaded (element, checkKey) {
element.setAttribute('data-lozaded', `${getAttr(element)} ${checkKey}`.trim());
}

function preLoad(element) {
Expand All @@ -108,26 +113,28 @@ function preLoad(element) {
}
}

const isLoaded = element => element.getAttribute('data-loaded') === 'true';
function isLoaded (element, checkKey) {
return -1 !== getAttr(element).indexOf(checkKey)
}

const onIntersection = (load, loaded) => (entries, observer) => {
const onIntersection = (config) => (entries, observer) => {
entries.forEach(entry => {
if (entry.intersectionRatio > 0 || entry.isIntersecting) {
observer.unobserve(entry.target);

if (!isLoaded(entry.target)) {
load(entry.target);
markAsLoaded(entry.target);
loaded(entry.target);
config.load(entry.target);
markAsLoaded(entry.target, config.checkKey);
config.loaded(entry.target);
}
}
});
};

const onMutation = load => entries => {
const onMutation = config => entries => {
entries.forEach(entry => {
if (isLoaded(entry.target) && entry.type === 'attributes' && validAttribute.indexOf(entry.attributeName) > -1) {
load(entry.target);
if (isLoaded(entry.target, config.checkKey) && entry.type === 'attributes' && validAttribute.indexOf(entry.attributeName) > -1) {
config.load(entry.target);
}
});
};
Expand All @@ -144,58 +151,68 @@ const getElements = (selector, root = document) => {
return root.querySelectorAll(selector)
};

let checkKeyIncrement = 1;

function lozad (selector = '.lozad', options = {}) {
const {root, rootMargin, threshold, enableAutoReload, load, loaded} = Object.assign({}, defaultConfig, options);
// set auto-generated checkKey for custom `load` callback if checkKey is not specified
if( options.load && ! options.checkKey ){
options.checkKey = `loadfunc${ checkKeyIncrement++ }`;
}

let config = Object.assign({}, defaultConfig, options);
let observer;
let mutationObserver;
if (support('IntersectionObserver')) {
observer = new IntersectionObserver(onIntersection(load, loaded), {
root,
rootMargin,
threshold
observer = new IntersectionObserver(onIntersection(config), {
root: config.root,
rootMargin: config.rootMargin,
threshold: config.threshold
});
}

if (support('MutationObserver') && enableAutoReload) {
mutationObserver = new MutationObserver(onMutation(load, loaded));
if (support('MutationObserver') && config.enableAutoReload) {
mutationObserver = new MutationObserver(onMutation(config));
}

const elements = getElements(selector, root);
for (let i = 0; i < elements.length; i++) {
preLoad(elements[i]);
// do preLoad() only for default load callback
if( 'default' === config.checkKey ){
const elements = getElements(selector, config.root);
for (let i = 0; i < elements.length; i++) {
preLoad(elements[i]);
}
}

return {
observe() {
const elements = getElements(selector, root);
const elements = getElements(selector, config.root);

for (let i = 0; i < elements.length; i++) {
if (isLoaded(elements[i])) {
continue
}

if (observer) {
if (mutationObserver && enableAutoReload) {
if (mutationObserver && config.enableAutoReload) {
mutationObserver.observe(elements[i], {subtree: true, attributes: true, attributeFilter: validAttribute});
}

observer.observe(elements[i]);
continue
}

load(elements[i]);
markAsLoaded(elements[i]);
loaded(elements[i]);
config.load(elements[i]);
markAsLoaded(elements[i], config.checkKey);
config.loaded(elements[i]);
}
},
triggerLoad(element) {
if (isLoaded(element)) {
return
}

load(element);
markAsLoaded(element);
loaded(element);
config.load(element);
markAsLoaded(element, config.checkKey);
config.loaded(element);
},
observer,
mutationObserver
Expand Down
84 changes: 46 additions & 38 deletions dist/lozad.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! lozad.js - v1.16.0 - 2020-09-10
/*! lozad.js - v1.16.0 - 2021-10-13
* https://github.com/ApoorvSaxena/lozad.js
* Copyright (c) 2020 Apoorv Saxena; Licensed MIT */
* Copyright (c) 2021 Apoorv Saxena; Licensed MIT */


(function (global, factory) {
Expand Down Expand Up @@ -30,6 +30,7 @@
var defaultConfig = {
rootMargin: '0px',
threshold: 0,
checkKey: 'default',
enableAutoReload: false,
load: function load(element) {
if (element.nodeName.toLowerCase() === 'picture') {
Expand Down Expand Up @@ -106,8 +107,12 @@
loaded: function loaded() {}
};

function markAsLoaded(element) {
element.setAttribute('data-loaded', true);
function getAttr(element) {
return element.getAttribute('data-lozaded') || '';
}

function markAsLoaded(element, checkKey) {
element.setAttribute('data-lozaded', (getAttr(element) + ' ' + checkKey).trim());
}

function preLoad(element) {
Expand All @@ -116,31 +121,31 @@
}
}

var isLoaded = function isLoaded(element) {
return element.getAttribute('data-loaded') === 'true';
};
function isLoaded(element, checkKey) {
return -1 !== getAttr(element).indexOf(checkKey);
}

var onIntersection = function onIntersection(load, loaded) {
var onIntersection = function onIntersection(config) {
return function (entries, observer) {
entries.forEach(function (entry) {
if (entry.intersectionRatio > 0 || entry.isIntersecting) {
observer.unobserve(entry.target);

if (!isLoaded(entry.target)) {
load(entry.target);
markAsLoaded(entry.target);
loaded(entry.target);
config.load(entry.target);
markAsLoaded(entry.target, config.checkKey);
config.loaded(entry.target);
}
}
});
};
};

var onMutation = function onMutation(load) {
var onMutation = function onMutation(config) {
return function (entries) {
entries.forEach(function (entry) {
if (isLoaded(entry.target) && entry.type === 'attributes' && validAttribute.indexOf(entry.attributeName) > -1) {
load(entry.target);
if (isLoaded(entry.target, config.checkKey) && entry.type === 'attributes' && validAttribute.indexOf(entry.attributeName) > -1) {
config.load(entry.target);
}
});
};
Expand All @@ -160,68 +165,71 @@
return root.querySelectorAll(selector);
};

var checkKeyIncrement = 1;

function lozad () {
var selector = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '.lozad';
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

var _Object$assign = Object.assign({}, defaultConfig, options),
root = _Object$assign.root,
rootMargin = _Object$assign.rootMargin,
threshold = _Object$assign.threshold,
enableAutoReload = _Object$assign.enableAutoReload,
load = _Object$assign.load,
loaded = _Object$assign.loaded;
// set auto-generated checkKey for custom `load` callback if checkKey is not specified
if (options.load && !options.checkKey) {
options.checkKey = 'loadfunc' + checkKeyIncrement++;
}

var config = Object.assign({}, defaultConfig, options);
var observer = void 0;
var mutationObserver = void 0;
if (support('IntersectionObserver')) {
observer = new IntersectionObserver(onIntersection(load, loaded), {
root: root,
rootMargin: rootMargin,
threshold: threshold
observer = new IntersectionObserver(onIntersection(config), {
root: config.root,
rootMargin: config.rootMargin,
threshold: config.threshold
});
}

if (support('MutationObserver') && enableAutoReload) {
mutationObserver = new MutationObserver(onMutation(load, loaded));
if (support('MutationObserver') && config.enableAutoReload) {
mutationObserver = new MutationObserver(onMutation(config));
}

var elements = getElements(selector, root);
for (var i = 0; i < elements.length; i++) {
preLoad(elements[i]);
// do preLoad() only for default load callback
if ('default' === config.checkKey) {
var elements = getElements(selector, config.root);
for (var i = 0; i < elements.length; i++) {
preLoad(elements[i]);
}
}

return {
observe: function observe() {
var elements = getElements(selector, root);
var elements = getElements(selector, config.root);

for (var _i = 0; _i < elements.length; _i++) {
if (isLoaded(elements[_i])) {
continue;
}

if (observer) {
if (mutationObserver && enableAutoReload) {
if (mutationObserver && config.enableAutoReload) {
mutationObserver.observe(elements[_i], { subtree: true, attributes: true, attributeFilter: validAttribute });
}

observer.observe(elements[_i]);
continue;
}

load(elements[_i]);
markAsLoaded(elements[_i]);
loaded(elements[_i]);
config.load(elements[_i]);
markAsLoaded(elements[_i], config.checkKey);
config.loaded(elements[_i]);
}
},
triggerLoad: function triggerLoad(element) {
if (isLoaded(element)) {
return;
}

load(element);
markAsLoaded(element);
loaded(element);
config.load(element);
markAsLoaded(element, config.checkKey);
config.loaded(element);
},

observer: observer,
Expand Down
Loading