Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
reygcalantaol committed Dec 25, 2022
1 parent 868b37d commit 399424f
Showing 1 changed file with 68 additions and 70 deletions.
138 changes: 68 additions & 70 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,85 @@ import "./style.scss";

jQuery(window).on("load", function() {
jQuery("img").on("load", function() {
// If the image is already wrapped with <a>, skip.
if (ziorLB.disable_on_href == "1" && jQuery(this).parent("a").length > 0) {
return false;
setTimeout(() => {
// If the image is already wrapped with <a>, skip.
if (ziorLB.disable_on_href == "1" && jQuery(this).parent("a").length > 0) {
return false;
}
if (ziorLB.allowed_classes.length > 0 && !isAllowedClasses(this, ziorLB.allowed_classes)) {
return false;
}
if (ziorLB.allowed_parent_classes.length > 0 && !isAllowedParentClasses(this, ziorLB.allowed_parent_classes)) {
return false;
}
if (ziorLB.disabled_classes.length > 0 && isDisabledClasses(this, ziorLB.disabled_classes)) {
return false;
}
if (ziorLB.disabled_classes.length > 0 && isDisabledParentClasses(this, ziorLB.disabled_parent_classes)) {
return false;
}
// If this is cloudinary URL, get the full image
var imgTarget = this.src;
if (this.src.includes("res.cloudinary.com")) {
imgTarget = this.src.replace(/\/image\/upload\/(.*?)\//g, "/image/upload/");
}else{
// Try to get full image from srcset
if (this.srcset) {
var srcset = this.srcset.match(/(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png|webp|avif)/g);
imgTarget = srcset.pop() || "";
}
}
jQuery(this).wrap(`<a href="${imgTarget}" data-lightbox="lightbox"></a>`);
}, 500);
}).each(function() {
if (this.complete) {
jQuery(this).load();
} else if(this.error) {
jQuery(this).error();
}
});
});

if (ziorLB.allowed_classes.length > 0 && !isAllowedClasses(this, ziorLB.allowed_classes)) {
function isAllowedClasses(img, $classes) {
var allowed = false;
jQuery($classes).each(function(index, item) {
if (jQuery(img).hasClass(item)) {
allowed = true;
return false;
}
});
return allowed;
}

if (ziorLB.allowed_parent_classes.length > 0 && !isAllowedParentClasses(this, ziorLB.allowed_parent_classes)) {
function isAllowedParentClasses(img, $classes) {
var allowed = false;
jQuery($classes).each(function(index, item) {
if (jQuery(img).parents(`.${item}`).length > 0) {
allowed = true;
return false;
}
});
return allowed;
}

if (ziorLB.disabled_classes.length > 0 && isDisabledClasses(this, ziorLB.disabled_classes)) {
function isDisabledClasses(img, $classes) {
var notAllowed = false;
jQuery($classes).each(function(index, item) {
if (jQuery(img).hasClass(item)) {
notAllowed = true;
return false;
}
});
return notAllowed;
}

if (ziorLB.disabled_classes.length > 0 && isDisabledParentClasses(this, ziorLB.disabled_parent_classes)) {
function isDisabledParentClasses(img, $classes) {
var notAllowed = false;
jQuery($classes).each(function(index, item) {
if (jQuery(img).parents(`.${item}`).length > 0) {
notAllowed = true;
return false;
}

// If this is cloudinary URL, get the full image
var imgTarget = this.src;
if (this.src.includes("res.cloudinary.com")) {
imgTarget = this.src.replace(/\/image\/upload\/(.*?)\//g, "/image/upload/");
}else{
// Try to get full image from srcset
if (this.srcset) {
var srcset = this.srcset.match(/(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png|webp|avif)/g);
imgTarget = srcset.pop() || "";
}
}

jQuery(this).wrap(`<a href="${imgTarget}" data-lightbox="lightbox"></a>`);
})
.each(function() {
if (this.complete) {
jQuery(this).load();
} else if(this.error) {
jQuery(this).error();
}
});

function isAllowedClasses(img, $classes) {
var allowed = true;
jQuery($classes).each(function(index, item) {
if (!jQuery(img).hasClass(item)) {
return false;
}
});

return allowed;
}

function isAllowedParentClasses(img, $classes) {
var allowed = true;
jQuery($classes).each(function(index, item){
if (!jQuery(img).parent(`.${item}`).length > 0) {
return false;
}
});
return allowed;
}

function isDisabledClasses(img, $classes) {
var allowed = true;
jQuery($classes).each(function(index, item){
if (jQuery(img).hasClass(item)) {
return false;
}
});
return allowed;
}

function isDisabledParentClasses(img, $classes) {
var allowed = true;
jQuery($classes).each(function(index, item){
if (jQuery(img).parent(`.${item}`).length > 0) {
return false;
}
});
return allowed;
}
});
return notAllowed;
}

0 comments on commit 399424f

Please sign in to comment.