// LiteBox v1.3, Copyright 2014, Joe Mottershaw, https://github.com/joemottershaw/
// ===============================================================================
window.AFLitebox = window.AFLitebox || {};
window.AFLitebox.closeModal = function () {
$('.litebox-close:visible').trigger('click');
};
;(function($, window, document, undefined) {
const pluginName = 'liteBox';
const defaults = {
revealSpeed: 0,
background: 'rgba(0,0,0,.8)',
overlayClose: true,
escKey: true,
navKey: true,
closeTip: 'tip-l-fade',
closeTipText: 'Close',
prevTip: 'tip-t-fade',
prevTipText: 'Previous',
nextTip: 'tip-t-fade',
nextTipText: 'Next',
callbackInit: function() {},
callbackBeforeOpen: function() {},
callbackAfterOpen: function() {},
callbackBeforeClose: function() {},
callbackAfterClose: function() {},
callbackError: function() {},
callbackPrev: function() {},
callbackNext: function() {},
errorMessage: 'Error loading content.',
ajaxPopulate: false,
leftRightEdge: "20%",
appendTargetIfNonexistent: false
};
function liteBox(element, options) {
this.element = element;
this.$element = $(this.element);
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
function winHeight() { return window.innerHeight ? window.innerHeight : $(window).height(); }
function preloadImageArray(images) {
$(images).each(function () {
const image = new Image();
image.src = this;
if (image.width > 0)
{$('').attr('src', this).addClass('litebox-preload').appendTo('body').hide();}
});
}
liteBox.prototype = {
init: function () {
// Variables
const $this = this;
// Element click
this.$element.on('click', function (e) {
e.preventDefault();
$this.openLitebox();
});
// Interaction
keyEsc = 27,
keyLeft = 37,
keyRight = 39;
$('body').off('keyup').on('keyup', function (e) {
if ($this.options.escKey && e.keyCode == keyEsc)
{$this.closeLitebox();}
if ($this.options.navKey && e.keyCode == keyLeft)
{$('.litebox-prev').trigger('click');}
if ($this.options.navKey && e.keyCode == keyRight)
{$('.litebox-next').trigger('click');}
});
// Callback
this.options.callbackInit.call(this);
},
openLitebox: function () {
// Variables
var $this = this;
// Before callback
this.options.callbackBeforeOpen.call(this);
// Build
$this.buildLitebox();
// Populate
let link = this.$element;
if (this.options.ajaxPopulate) {
$('body').toggleClass('wait');
$(link.attr('href')).load(link.attr('data-litebox-ajaxHref'), function () {
$this.populateLitebox(link);
$('body').toggleClass('wait');
});
} else {
$this.populateLitebox(link);
}
// Interactions
if ($this.options.overlayClose)
{$litebox.on('click', function (e) {
if (e.target === this || $(e.target).hasClass('litebox-container') || $(e.target).hasClass('litebox-error'))
{$this.closeLitebox();}
});}
$close.on('click', function () {
$this.closeLitebox();
});
// Groups
if (this.$element.attr('data-litebox-group')) {
var $this = this;
const groupName = this.$element.attr('data-litebox-group');
const group = $('[data-litebox-group="' + this.$element.attr('data-litebox-group') + '"]');
const imageArray = [];
$('[data-litebox-group="' + groupName + '"]').each(function () {
const src = $(this).attr('href');
imageArray.push(src);
});
preloadImageArray(imageArray);
$('.litebox-nav').show();
$prevNav.off('click').on('click', function () {
$this.options.callbackPrev.call(this);
const index = group.index(link);
link = group.eq(index - 1);
if (!$(link).length)
{link = group.last();}
$this.populateLitebox(link);
});
$nextNav.off('click').on('click', function () {
$this.options.callbackNext.call(this);
const index = group.index(link);
link = group.eq(index + 1);
if (!$(link).length)
{link = group.first();}
$this.populateLitebox(link);
});
}
// After callback
this.options.callbackAfterOpen.call(this);
$('body').on('click', '[data-role="closemodal"]', window.AFLitebox.closeModal);
},
buildLitebox: function() {
// Variables
const $this = this;
const element = $this.$element;
const targetDiv = $(element).attr('href');
const leftRightEdge = $(element).attr('leftRightEdge') || this.options.leftRightEdge;
$litebox = $('