meta-lightbox/src/js/meta-lightbox.js

687 lines
19 KiB
JavaScript
Raw Normal View History

2019-11-25 06:59:54 +01:00
/*
2020-06-18 01:18:13 +02:00
* MetaLightbox
2019-11-25 06:59:54 +01:00
* https://tony.twma.pro
*
*/
2020-06-18 01:18:13 +02:00
// optional:
2019-11-25 06:59:54 +01:00
//=require ../../bower_components/jquery-zoom/jquery.zoom.js
2020-06-19 02:11:52 +02:00
'use strict';
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
import $ from 'jquery';
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
import Events from './_events';
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
const MetaLightboxUI = (($) => {
const W = window;
2020-09-09 17:28:13 +02:00
const $W = $(W);
2020-06-18 01:18:13 +02:00
const D = document;
const $Body = $('body');
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
const NAME = 'MetaLightboxUI';
2020-09-02 00:50:58 +02:00
const NETWORK_ERROR =
2020-12-24 23:53:50 +01:00
'<div class="meta-lightbox-error"><div class="alert alert-error alert-danger">Connection failure.</div></div>';
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
class MetaLightboxUI {
static init() {
2020-09-09 17:06:20 +02:00
console.log(`${NAME}: init ...`);
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
const ui = this;
ui.isMSIE = /*@cc_on!@*/ 0;
2020-06-19 02:11:52 +02:00
try {
ui.isHidpi = ui.is_hdpi();
} catch (e) {
2020-09-09 17:06:20 +02:00
console.log(`${NAME}: catch`);
2020-06-19 02:11:52 +02:00
}
2019-11-25 06:59:54 +01:00
2020-06-19 02:11:52 +02:00
$(`.js${NAME},[data-toggle="lightbox"],[data-lightbox-gallery]`).on(
'click',
(e) => {
e.preventDefault();
e.stopPropagation();
const $link = $(e.currentTarget);
2019-11-25 06:59:54 +01:00
2020-06-19 02:11:52 +02:00
ui.show($link);
},
);
2020-12-24 23:53:50 +01:00
$(`.js${NAME}-close-inline`).on('click', (e) => {
const $el = $(e.currentTarget);
$el.parents('.meta-lightbox-overlay').removeClass('meta-lightbox-open');
});
2020-06-18 01:18:13 +02:00
}
2019-11-25 06:59:54 +01:00
2020-06-19 02:11:52 +02:00
static is_hdpi() {
2020-06-18 01:18:13 +02:00
console.log(`${NAME}: isHidpi`);
const mediaQuery =
2020-12-24 23:53:50 +01:00
'(-webkit-min-device-pixel-ratio: 1.5),\
2020-06-18 01:18:13 +02:00
(min--moz-device-pixel-ratio: 1.5),\
(-o-min-device-pixel-ratio: 3/2),\
(min-resolution: 1.5dppx)';
if (W.devicePixelRatio > 1) return true;
return W.matchMedia && W.matchMedia(mediaQuery).matches;
}
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
static show($link) {
console.log(`${NAME}: show`);
const ui = this;
2019-11-25 06:59:54 +01:00
2020-09-02 00:50:58 +02:00
const $lightbox = ui.constructLightbox();
2020-06-18 01:18:13 +02:00
if (!$lightbox) return;
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
const $content = ui.$content;
if (!$content) return;
2019-11-25 06:59:54 +01:00
2020-09-09 18:13:05 +02:00
$Body.addClass(`meta-lightbox-body-effect-fade`);
2019-11-25 06:59:54 +01:00
// Add content
2020-06-18 01:18:13 +02:00
ui.process($content, $link);
2019-11-25 06:59:54 +01:00
// Nav
2020-06-18 01:18:13 +02:00
if ($link.data('lightbox-gallery')) {
2020-06-19 02:11:52 +02:00
const $galleryItems = $(
2020-12-24 23:53:50 +01:00
`[data-lightbox-gallery="${$link.data('lightbox-gallery')}"]`,
2020-06-19 02:11:52 +02:00
);
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
if ($galleryItems.length === 1) {
2019-11-25 06:59:54 +01:00
$('.meta-lightbox-nav').hide();
} else {
$('.meta-lightbox-nav').show();
}
// Prev
2020-04-20 10:35:43 +02:00
$('.meta-lightbox-prev')
.off('click')
2020-06-18 01:18:13 +02:00
.on('click', (e) => {
2020-04-20 10:35:43 +02:00
e.preventDefault();
2020-07-25 14:49:56 +02:00
const index = $galleryItems.index($link);
2020-06-18 01:18:13 +02:00
let $currentLink = $galleryItems.eq(index - 1);
2020-12-24 23:53:50 +01:00
if (!$currentLink.length) $currentLink = $galleryItems.last();
2020-09-02 00:50:58 +02:00
//ui.hide();
setTimeout(() => {
ui.show($currentLink);
}, 10);
2020-04-20 10:35:43 +02:00
});
2019-11-25 06:59:54 +01:00
// Next
2020-04-20 10:35:43 +02:00
$('.meta-lightbox-next')
.off('click')
2020-06-18 01:18:13 +02:00
.on('click', (e) => {
2020-04-20 10:35:43 +02:00
e.preventDefault();
2020-07-25 14:49:56 +02:00
const index = $galleryItems.index($link);
2020-09-02 00:50:58 +02:00
let $currentLink = $galleryItems.eq(index + 1);
2020-12-24 23:53:50 +01:00
if (!$currentLink.length) $currentLink = $galleryItems.first();
2020-09-02 00:50:58 +02:00
//ui.hide();
setTimeout(() => {
ui.show($currentLink);
}, 10);
2020-04-20 10:35:43 +02:00
});
2019-11-25 06:59:54 +01:00
}
2020-06-18 01:18:13 +02:00
setTimeout(() => {
ui.$overlay.addClass('meta-lightbox-open');
2019-11-25 06:59:54 +01:00
}, 1); // For CSS transitions
2020-06-18 01:18:13 +02:00
}
static constructLightbox() {
console.log(`${NAME}: constructLightbox`);
const ui = this;
const overlay = $('<div>', {
2020-06-19 02:11:52 +02:00
class:
2020-12-24 23:53:50 +01:00
'meta-lightbox-overlay meta-lightbox-theme-default meta-lightbox-effect-fade',
2020-06-18 01:18:13 +02:00
});
const wrap = $('<div>', {
class: 'meta-lightbox-wrap',
});
const content = $('<div>', {
2020-06-18 01:18:44 +02:00
class: 'meta-lightbox-content',
2020-06-18 01:18:13 +02:00
});
const nav = $(
'<a href="#" class="meta-lightbox-nav meta-lightbox-prev"><i class="fas fa fa-chevron-left"></i> <span class="sr-only">Previous</span></a><a href="#" class="meta-lightbox-nav meta-lightbox-next"><i class="fa fas fa-chevron-right"></i> <span class="sr-only">Next</span></a>',
);
const close = $(
'<a href="#" class="meta-lightbox-close fas fa fa-times" title="Close"><span class="sr-only">Close</span></a>',
);
const title = $('<div>', {
class: 'meta-lightbox-title-wrap',
});
if (ui.$overlay) return ui.$overlay;
if (ui.isMSIE) overlay.addClass('meta-lightbox-ie');
wrap.append(content);
wrap.append(title);
overlay.append(wrap);
overlay.append(nav);
overlay.append(close);
2020-09-09 18:13:05 +02:00
$Body.append(overlay);
2020-06-18 01:18:13 +02:00
overlay.on('click', (e) => {
e.preventDefault();
ui.hide();
});
close.on('click', (e) => {
e.preventDefault();
ui.hide();
});
ui.$overlay = overlay;
ui.$content = content;
ui.$title = title;
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
return ui.$overlay;
}
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
static setTitle(str) {
const ui = this;
ui.$title.html(str);
}
static process($content, $link) {
console.log(`${NAME}: process`);
const ui = this;
2020-09-10 01:54:14 +02:00
const href =
2020-12-24 23:53:50 +01:00
$link.attr('href') && $link.attr('href').length
? $link.attr('href')
: $link.data('href');
2020-09-09 18:13:05 +02:00
// add custom link specific class
ui.$content.attr('class', 'meta-lightbox-content');
ui.$content.addClass($link.data('lightbox-class'));
2020-06-18 01:18:13 +02:00
if (!href.length) {
console.log($link);
2020-06-19 02:11:52 +02:00
console.error(`${NAME}: href(attr/data) is missing`);
2019-11-25 06:59:54 +01:00
}
2020-09-02 00:50:58 +02:00
const $pageSpinner = $('#PageLoading .loading-spinner');
2020-12-24 23:53:50 +01:00
const loadingContent = $pageSpinner.length ? $pageSpinner.clone() : '';
ui.$content.append(loadingContent).addClass('meta-lightbox-loading');
2020-06-18 01:18:13 +02:00
2019-11-25 06:59:54 +01:00
// Image
2021-01-20 16:18:36 +01:00
if (
href.match(/\.(jpeg|jpg|gif|png|svg)$/i) ||
$link.data('force') === 'image'
) {
2020-09-02 00:50:58 +02:00
$.ajax({
url: href,
success: () => {
const img = $('<img>', { src: href });
2020-12-24 23:53:50 +01:00
const wrap = $('<div class="meta-lightbox-image"></div>');
2020-09-02 00:50:58 +02:00
const imgwrapper = $(
2020-08-02 17:35:51 +02:00
'<span class="meta-lightbox-zoom-wrapper"></span>',
);
2019-11-25 06:59:54 +01:00
2020-09-02 00:50:58 +02:00
imgwrapper.append(img);
wrap.append(imgwrapper);
2020-06-18 01:18:13 +02:00
2020-09-02 00:50:58 +02:00
// Vertically center images
2019-11-25 06:59:54 +01:00
wrap.css({
2020-04-20 10:35:43 +02:00
'line-height': `${$content.height()}px`,
height: `${$content.height()}px`, // For Firefox
2019-11-25 06:59:54 +01:00
});
2020-09-09 17:28:13 +02:00
$W.resize(() => {
2020-09-02 00:50:58 +02:00
wrap.css({
'line-height': `${$content.height()}px`,
height: `${$content.height()}px`, // For Firefox
});
});
2019-11-25 06:59:54 +01:00
2020-09-02 00:50:58 +02:00
if (typeof imgwrapper['zoom'] !== 'undefined') {
imgwrapper.zoom();
} else {
imgwrapper.addClass('no-zoom');
}
2019-11-25 06:59:54 +01:00
2020-09-02 00:50:58 +02:00
ui.$content.html(wrap);
ui.contentLoaded();
},
error: (jqXHR, status) => {
const wrap = $(NETWORK_ERROR);
2020-06-18 01:18:13 +02:00
2020-09-02 00:50:58 +02:00
ui.$content.html(wrap);
ui.contentLoaded();
},
2019-11-25 06:59:54 +01:00
});
// Set the title
2020-06-19 02:11:52 +02:00
const title = $link.data('title')
? $link.data('title')
: $link.attr('title');
2020-06-18 01:18:13 +02:00
ui.setTitle(title);
2019-11-25 06:59:54 +01:00
// google analytics
if (typeof ga === 'function') {
ga('send', 'event', 'meta', 'Image Click', href);
}
}
// Video (Youtube/Vimeo)
2020-06-19 02:11:52 +02:00
else if (
href.match(
/(youtube|youtube-nocookie|youtu|vimeo)\.(com|be)\/(watch\?v=([\w-]+)|([\w-]+))/,
2021-01-20 16:18:36 +01:00
) ||
$link.data('force') === 'youtube' ||
$link.data('force') === 'vimeo'
2020-06-19 02:11:52 +02:00
) {
const video = href.match(
/(youtube|youtube-nocookie|youtu|vimeo)\.(com|be)\/(watch\?v=([\w-]+)|([\w-]+))/,
);
2020-06-18 01:18:13 +02:00
let classTerm = 'meta-lightbox-video';
let src;
2019-11-25 06:59:54 +01:00
if (video[1] == 'youtube') {
2020-04-20 10:35:43 +02:00
src = `https://www.youtube.com/embed/${video[4]}`;
2020-06-19 02:11:52 +02:00
classTerm = `${classTerm} meta-lightbox-youtube`;
2019-11-25 06:59:54 +01:00
}
if (video[1] == 'youtu') {
2020-04-20 10:35:43 +02:00
src = `https://www.youtube.com/embed/${video[3]}`;
2020-06-19 02:11:52 +02:00
classTerm = `${classTerm} meta-lightbox-youtube`;
2019-11-25 06:59:54 +01:00
}
if (video[1] == 'youtube-nocookie') {
2020-04-20 10:35:43 +02:00
src = `https://www.youtube-nocookie.com/embed/${video[4]}`;
2020-06-18 01:18:44 +02:00
classTerm = `${classTerm} meta-lightbox-youtube`;
2019-11-25 06:59:54 +01:00
}
if (video[1] == 'vimeo') {
2020-04-20 10:35:43 +02:00
src = `https://player.vimeo.com/video/${video[3]}`;
2020-06-19 02:11:52 +02:00
classTerm = `${classTerm} meta-lightbox-vimeo`;
2019-11-25 06:59:54 +01:00
}
2020-09-10 23:58:11 +02:00
if (!src) {
console.warn(`${NAME}: Video loading bad URL`);
return false;
}
const $iframe = ui.loadIframe(src, classTerm);
if (!$iframe) {
$link.addClass('meta-offline');
2020-09-10 23:58:11 +02:00
return false;
2019-11-25 06:59:54 +01:00
}
$link.removeClass('meta-offline');
2019-11-25 06:59:54 +01:00
// Set the title
2020-06-19 02:11:52 +02:00
const title = $link.data('title')
? $link.data('title')
: $link.attr('title');
2020-06-18 01:18:13 +02:00
ui.setTitle(title);
2019-11-25 06:59:54 +01:00
// google analytics
if (typeof ga === 'function') {
ga('send', 'event', 'meta', 'Video Click', video);
}
}
// Inline HTML
2021-01-20 16:18:36 +01:00
else if (
href.substring(0, 1) == '#' ||
$link.data('force') === 'inline'
) {
2019-11-25 06:59:54 +01:00
if ($(href).length) {
wrap = $('<div class="meta-lightbox-inline" />');
2020-06-18 01:18:13 +02:00
wrap.append($(href).clone().show());
2019-11-25 06:59:54 +01:00
// Vertically center html
2020-09-02 00:50:58 +02:00
if (wrap.outerHeight() < ui.$content.height()) {
2019-11-25 06:59:54 +01:00
wrap.css({
2020-04-20 10:35:43 +02:00
position: 'relative',
top: '50%',
'margin-top': `${-(wrap.outerHeight() / 2)}px`,
2019-11-25 06:59:54 +01:00
});
}
2020-09-09 17:28:13 +02:00
$W.resize(() => {
2020-09-02 00:50:58 +02:00
if (wrap.outerHeight() < ui.$content.height()) {
2019-11-25 06:59:54 +01:00
wrap.css({
2020-04-20 10:35:43 +02:00
position: 'relative',
top: '50%',
'margin-top': `${-(wrap.outerHeight() / 2)}px`,
2019-11-25 06:59:54 +01:00
});
}
});
2020-06-18 01:18:13 +02:00
ui.$content.html(wrap);
ui.contentLoaded();
2019-11-25 06:59:54 +01:00
} else {
2020-09-02 00:50:58 +02:00
wrap = $(NETWORK_ERROR);
2020-06-18 01:18:13 +02:00
ui.$content.html(wrap);
ui.contentLoaded();
2019-11-25 06:59:54 +01:00
}
$('.meta-lightbox-title-wrap').html('');
// google analytics
if (typeof ga === 'function') {
ga('send', 'event', 'meta', 'inline HTML click', href);
}
}
// AJAX/iFrame (default)
else {
2021-01-20 16:18:36 +01:00
if ($link.data('force-iframe') || $link.data('force') === 'iframe') {
2020-09-09 17:06:20 +02:00
console.log(`${NAME}: IFrame forced`);
2020-12-24 23:53:50 +01:00
const $iframe = ui.loadIframe(href, 'meta-lightbox-iframe-content');
2020-09-10 23:58:11 +02:00
if (!$iframe) {
$link.addClass('meta-offline');
2020-09-10 23:58:11 +02:00
return false;
}
$link.removeClass('meta-offline');
2020-09-10 23:58:11 +02:00
return true;
2020-09-09 17:06:20 +02:00
}
console.log(`${NAME}: loading AJAX`);
2019-11-25 06:59:54 +01:00
$.ajax({
sync: false,
async: true,
url: href,
dataType: 'html',
method: 'GET',
cache: false,
statusCode: {
2020-06-18 01:18:13 +02:00
404: function () {
2020-09-09 17:06:20 +02:00
console.log(`${NAME}: page not found`);
2020-09-09 18:13:05 +02:00
W.location.href = url;
2019-11-25 06:59:54 +01:00
},
2020-06-18 01:18:13 +02:00
302: function () {
2020-09-09 17:06:20 +02:00
console.log(`${NAME}: redirect 302`);
2020-09-09 18:13:05 +02:00
W.location.href = url;
2019-11-25 06:59:54 +01:00
},
},
2020-09-02 00:50:58 +02:00
error: function (jqXHR, status) {
2020-12-24 23:53:50 +01:00
console.log(`${NAME}: AJAX request failure.${jqXHR.statusText}`);
2019-11-25 06:59:54 +01:00
2020-09-02 00:50:58 +02:00
var wrap = $(NETWORK_ERROR);
2020-06-18 01:18:13 +02:00
ui.$content.html(wrap);
ui.contentLoaded();
2019-11-25 06:59:54 +01:00
// google analytics
if (typeof ga === 'function') {
2020-12-24 23:53:50 +01:00
ga('send', 'event', 'error', 'AJAX ERROR', jqXHR.statusText);
2019-11-25 06:59:54 +01:00
}
},
2020-06-18 01:18:13 +02:00
success: function (data, status, jqXHR) {
console.log(`${NAME}: AJAX success`);
2019-11-25 06:59:54 +01:00
try {
const dataJson = $.parseJSON(data);
2020-04-20 10:35:43 +02:00
if (typeof dataJson === 'object') {
console.log(`${NAME}: AJAX JSON`);
2019-11-25 06:59:54 +01:00
// Replace regions
2020-04-20 10:35:43 +02:00
if (
typeof dataJson['regions'] === 'object' &&
2020-12-24 23:53:50 +01:00
typeof dataJson['regions']['LayoutAjax'] !== 'undefinded'
2020-04-20 10:35:43 +02:00
) {
2020-12-24 23:53:50 +01:00
var wrap = $('<div class="meta-lightbox-ajax" />');
wrap.html(dataJson['regions']['LayoutAjax']);
2020-09-02 00:50:58 +02:00
ui.$content.html(wrap);
ui.contentLoaded();
2019-11-25 06:59:54 +01:00
}
// trigger events
/*if (typeof (data['events']) === 'object') {
for (var eventName in data.events) {
2020-09-09 18:13:05 +02:00
$(D).trigger(eventName, [data['events'][eventName]]);
2019-11-25 06:59:54 +01:00
}
}*/
var title = jqXHR.getResponseHeader('X-Title'),
link = jqXHR.getResponseHeader('X-Link');
if (
2020-04-20 10:35:43 +02:00
title &&
2020-12-24 23:53:50 +01:00
title.length &&
link &&
link.length &&
link !== W.location.href &&
link.substring(0, link.indexOf('#')) !==
W.location.href.replace($('base').attr('href'), '/')
2019-11-25 06:59:54 +01:00
) {
2020-12-24 23:53:50 +01:00
$('.meta-lightbox-ajax').data('curr-title', D.title);
$('.meta-lightbox-ajax').data('curr-link', W.location.href);
2019-11-25 06:59:54 +01:00
2020-12-24 23:53:50 +01:00
if (typeof W.localStorage !== 'undefined' && link !== '/') {
W.localStorage.setItem('current-page', link);
2019-11-25 06:59:54 +01:00
}
if (
2020-09-09 18:13:05 +02:00
D.URL !== link &&
2020-12-24 23:53:50 +01:00
D.URL !== $('base').attr('href') + link &&
D.URL !== `${$('base').attr('href')}/${link}`
2019-11-25 06:59:54 +01:00
) {
2020-09-09 18:13:05 +02:00
W.history.pushState(
2020-04-20 10:35:43 +02:00
{
title,
page: link,
ajax: 'true',
},
2019-11-25 06:59:54 +01:00
title,
2020-04-20 10:35:43 +02:00
link,
);
2019-11-25 06:59:54 +01:00
}
$('.meta-lightbox-title-wrap').html('');
// google analytics
if (typeof ga === 'function') {
ga('set', {
2020-12-24 23:53:50 +01:00
page: link.replace($('base').attr('href'), ''),
2019-11-25 06:59:54 +01:00
title,
});
ga('send', 'pageview');
}
}
}
} catch (e) {
console.log(`${NAME}: AJAX HTML`);
2020-12-24 23:53:50 +01:00
const $wrap = $('<div class="meta-lightbox-ajax" />');
2020-09-09 17:06:20 +02:00
$wrap.append(data);
ui.$content.html($wrap);
2020-09-02 00:50:58 +02:00
ui.contentLoaded();
2019-11-25 06:59:54 +01:00
}
// Vertically center html
2020-09-02 00:50:58 +02:00
/*if (wrap.outerHeight() < ui.$content.height()) {
2019-11-25 06:59:54 +01:00
wrap.css({
2020-04-20 10:35:43 +02:00
position: 'relative',
top: '50%',
'margin-top': `${-(wrap.outerHeight() / 2)}px`,
2019-11-25 06:59:54 +01:00
});
}
2020-09-09 17:28:13 +02:00
$W.resize(() => {
2020-09-02 00:50:58 +02:00
if (wrap.outerHeight() < ui.$content.height()) {
2019-11-25 06:59:54 +01:00
wrap.css({
2020-04-20 10:35:43 +02:00
position: 'relative',
top: '50%',
2020-09-02 00:50:58 +02:00
'margin-top': `${-(wrap.outerHeight() / 2)}px`,
2019-11-25 06:59:54 +01:00
});
}
2020-09-02 00:50:58 +02:00
});*/
2019-11-25 06:59:54 +01:00
2020-09-02 00:50:58 +02:00
/*setTimeout(() => {
2020-09-09 17:28:13 +02:00
$W.resize();
2019-11-25 06:59:54 +01:00
2020-09-09 18:13:05 +02:00
if (typeof W.imagesLoaded === 'function') {
W.imagesLoaded().then(() => {
2020-09-09 17:28:13 +02:00
$W.resize();
2020-04-20 10:35:43 +02:00
});
}
2020-09-02 00:50:58 +02:00
}, 500);*/
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
ui.contentLoaded();
2019-11-25 06:59:54 +01:00
},
});
}
2020-06-19 02:11:52 +02:00
}
2019-11-25 06:59:54 +01:00
2020-09-09 17:06:20 +02:00
static loadIframe(href, classTerm) {
const ui = this;
const $iframe = $('<iframe>', {
src: href,
class: classTerm,
frameborder: 0,
vspace: 0,
hspace: 0,
scrolling: 'auto',
allowtransparency: 'true',
});
console.log(`${NAME}: loading iframe`);
$Body.append(
'<div id="MetaIFramePreload" class="hidden d-none iframe-preload" style="display:none"></div>',
2020-09-09 17:06:20 +02:00
);
const $preload = $('#MetaIFramePreload');
2020-09-09 17:06:20 +02:00
$preload.html($iframe);
$iframe.on('load', () => {
// don't load on offline
if ($Body.hasClass('is-offline')) {
console.warn(`${NAME}: Unable to load iframe offline`);
return false;
}
2020-09-11 00:43:35 +02:00
ui.finishIFrameLoading();
2020-09-09 17:06:20 +02:00
});
return $iframe;
}
2020-09-11 00:43:35 +02:00
static finishIFrameLoading() {
const ui = this;
2020-09-11 00:43:35 +02:00
const $preload = $('#MetaIFramePreload');
if (!$preload.length) {
console.warn(`${NAME}: iframe preload not found`);
return false;
}
const $iframe = $preload.find('iframe');
if (!$iframe.length) {
console.warn(`${NAME}: iframe preload > iframe not found`);
return false;
}
console.log(`${NAME}: the iframe was loaded`);
$preload.html('');
$preload.remove();
ui.$content.addClass('iframe-delay');
ui.$content.html($iframe);
ui.contentLoaded();
setTimeout(() => {
ui.$content.removeClass('iframe-delay');
}, 1000);
}
2020-06-18 01:18:13 +02:00
static contentLoaded() {
const ui = this;
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
ui.$content.removeClass('meta-lightbox-loading');
2020-12-24 23:53:50 +01:00
$(
`.meta-lightbox-content .js${NAME},.meta-lightbox-content [data-toggle="lightbox"],.meta-lightbox-content [data-lightbox-gallery]`,
).on('click', (e) => {
e.preventDefault();
e.stopPropagation();
const $link = $(e.currentTarget);
2020-12-24 23:53:50 +01:00
ui.show($link);
});
$(`.js${NAME}-close`).on('click', (e) => {
e.preventDefault();
ui.hide();
});
2019-11-25 06:59:54 +01:00
setTimeout(() => {
2020-09-09 17:28:13 +02:00
$W.trigger('meta-lightbox-loaded');
2019-11-25 06:59:54 +01:00
}, 1); // For CSS transitions
setTimeout(() => {
2020-06-18 01:18:13 +02:00
$Body.addClass('meta-lightbox-body-effect-fade');
2019-11-25 06:59:54 +01:00
}, 600);
2020-06-19 02:11:52 +02:00
}
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
static hide(callback) {
const ui = this;
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
const $overlay = ui.$overlay;
2019-11-25 06:59:54 +01:00
var title = $('.meta-lightbox-ajax').data('curr-title'),
link = $('.meta-lightbox-ajax').data('curr-link');
if (title && link) {
2020-09-09 18:13:05 +02:00
if (typeof W.localStorage !== 'undefined' && link !== '/') {
W.localStorage.setItem('current-page', link);
2019-11-25 06:59:54 +01:00
}
if (
2020-09-09 18:13:05 +02:00
D.URL !== link &&
2020-12-24 23:53:50 +01:00
D.URL !== $('base').attr('href') + link &&
D.URL !== `${$('base').attr('href')}/${link}`
2019-11-25 06:59:54 +01:00
) {
2020-09-09 18:13:05 +02:00
W.history.replaceState(
2020-04-20 10:35:43 +02:00
{
title,
page: link,
ajax: 'true',
},
2019-11-25 06:59:54 +01:00
title,
2020-04-20 10:35:43 +02:00
link,
);
2019-11-25 06:59:54 +01:00
}
}
$overlay.removeClass('meta-lightbox-open');
2020-06-18 01:18:13 +02:00
$Body.removeClass('meta-lightbox-body-effect-fade');
2020-06-19 02:11:52 +02:00
$('.meta-lightbox-content .meta-lightbox-zoom-wrapper').trigger(
'zoom.destroy',
);
2019-11-25 06:59:54 +01:00
// For IE
2020-06-18 01:18:13 +02:00
if (ui.isMSIE) {
2020-04-20 10:35:43 +02:00
$overlay.find('iframe').attr('src', ' ');
2019-11-25 06:59:54 +01:00
$overlay.find('iframe').remove();
}
$('.meta-lightbox-prev').off('click');
// Remove click handlers
$('.meta-lightbox-next').off('click');
// Empty content (for videos)
$('.meta-lightbox-content').empty();
2020-06-18 01:18:13 +02:00
$Body.removeClass('meta-lightbox-body-effect-fade');
}
}
2019-11-25 06:59:54 +01:00
2020-09-09 17:28:13 +02:00
$W.on(`MetaLightboxUI.init ${Events.AJAX} ${Events.LOADED}`, () => {
2020-06-18 01:18:13 +02:00
MetaLightboxUI.init();
});
2019-11-25 06:59:54 +01:00
2020-09-10 23:26:11 +02:00
$W.on(`${Events.BACKONLINE}`, () => {
$('.meta-offline').removeClass('meta-offline');
2020-09-10 23:26:11 +02:00
console.log(`${NAME}: reloading iframe`);
2020-09-11 00:43:35 +02:00
MetaLightboxUI.finishIFrameLoading();
const $iframe = $('.meta-lightbox-content iframe');
if ($iframe.length) {
$iframe.attr('src', $iframe.attr('src'));
}
2020-09-10 23:26:11 +02:00
});
2020-06-18 01:18:13 +02:00
W.MetaLightboxUI = MetaLightboxUI;
return MetaLightboxUI;
})($);
2019-11-25 06:59:54 +01:00
2020-06-18 01:18:13 +02:00
export default MetaLightboxUI;