// 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 = $('
', { 'class': 'litebox-overlay' }), $close = $('
', { 'class': 'litebox-close ' + this.options.closeTip, 'data-tooltip': this.options.closeTipText }), $text = $('
', { 'class': 'litebox-text' }), $error = $('
' + this.options.errorMessage + '
'), $prevNav = $('
', { 'class': 'litebox-nav litebox-prev ' + this.options.prevTip, 'data-tooltip': this.options.prevTipText }), $nextNav = $('
', { 'class': 'litebox-nav litebox-next ' + this.options.nextTip, 'data-tooltip': this.options.nextTipText }), $container = $('
', { 'class': 'litebox-container' }), $loader = $('
', { 'class': 'litebox-loader' }); $container.css({ left: leftRightEdge, right: leftRightEdge }); // Insert into document $('body').prepend($litebox.css({ 'background-color': this.options.background })); if (targetDiv && !$(targetDiv).length && $this.options.appendTargetIfNonexistent) { $('body').append($('
', { 'id': targetDiv.substring(1, targetDiv.length) })); } $litebox.append($close, $text, $prevNav, $nextNav, $container); $litebox.fadeIn(this.options.revealSpeed); }, populateLitebox: function (link) { // Variables const $this = this; const href = link.attr('href'); const $currentContent = $('.litebox-content'); // Show loader $litebox.append($loader); // Show image description const $text = link.attr('data-litebox-text'); if (typeof $text === 'undefined' || $text == '') { $('.litebox-text').removeClass('active'); $('.litebox-text').html(); } else { $('.litebox-text').html($text); $('.litebox-text').addClass('active'); } // Process if (href.match(/\.(jpeg|jpg|gif|png|bmp)/i) !== null) { const $img = $('', { 'src': href, 'class': 'litebox-content' }); $this.transitionContent('image', $currentContent, $img); $('img.litebox-content').imagesLoaded(function () { $loader.remove(); }); $img.error(function () { $this.liteboxError(); $loader.remove(); }); } else if (videoURL = href.match(/(youtube|youtu|vimeo|dailymotion|kickstarter)\.(com|be)\/((watch\?v=([-\w]+))|(video\/([-\w]+))|(projects\/([-\w]+)\/([-\w]+))|([-\w]+))/)) { let src = ''; if (videoURL[1] == 'youtube') {src = 'http://www.youtube.com/v/' + videoURL[5];} if (videoURL[1] == 'youtu') {src = 'http://www.youtube.com/v/' + videoURL[3];} if (videoURL[1] == 'vimeo') {src = 'http://player.vimeo.com/video/' + videoURL[3];} if (videoURL[1] == 'dailymotion') {src = 'https://www.dailymotion.com/embed/video/' + videoURL[7];} if (videoURL[1] == 'kickstarter') {src = 'https://www.kickstarter.com/projects/' + videoURL[9] + '/' + videoURL[10] + '/widget/video.html';} if (src) { var $iframe = $('