EventBuzz_Backend/microsite-v2-templates/event-up/assets/js/marketing-popup.js

277 lines
7.5 KiB
JavaScript

(function () {
'use strict';
var popupEl = null;
var autoCloseTimer = null;
var popupBound = false;
function createPopup() {
if (popupEl) {
return popupEl;
}
popupEl = document.createElement('div');
popupEl.className = 'eventup-marketing-popup';
popupEl.setAttribute('data-marketing-popup', '');
popupEl.setAttribute('role', 'dialog');
popupEl.setAttribute('aria-modal', 'true');
popupEl.setAttribute('aria-hidden', 'true');
popupEl.innerHTML =
'<div class="eventup-marketing-popup__backdrop"></div>'
+ '<div class="eventup-marketing-popup__card">'
+ '<p class="eventup-marketing-popup__heading" data-marketing-heading>Introducing</p>'
+ '<div class="eventup-marketing-popup__logo-wrap">'
+ '<button type="button" class="eventup-marketing-popup__close" data-marketing-close aria-label="Close">'
+ '<span aria-hidden="true">&times;</span>'
+ '</button>'
+ '<img class="eventup-marketing-popup__logo" data-marketing-logo src="" alt="">'
+ '<div class="eventup-marketing-popup__media" data-marketing-media></div>'
+ '</div>'
+ '</div>';
document.body.appendChild(popupEl);
bindPopupEvents(popupEl);
return popupEl;
}
function bindPopupEvents(popup) {
if (!popup || popupBound) {
return;
}
popupBound = true;
var closeBtn = popup.querySelector('[data-marketing-close]');
if (closeBtn) {
closeBtn.addEventListener('click', hidePopup);
}
popup.addEventListener('click', function (event) {
if (event.target === popup.querySelector('.eventup-marketing-popup__backdrop')) {
hidePopup();
}
});
document.addEventListener('keydown', function (event) {
if (event.key === 'Escape' && popupEl && popupEl.classList.contains('is-visible')) {
hidePopup();
}
});
}
function toYouTubeEmbed(url) {
if (!url) {
return '';
}
var id = '';
var match = String(url).match(
/(?:youtube\.com\/(?:watch\?v=|embed\/|v\/|shorts\/)|youtu\.be\/)([\w-]{11})/
);
if (match) {
id = match[1];
} else if (/^[\w-]{11}$/.test(String(url))) {
id = String(url);
}
if (!id) {
return '';
}
return 'https://www.youtube.com/embed/' + id + '?autoplay=1&mute=1&rel=0&playsinline=1';
}
function renderMedia(container, media) {
if (!container) {
return;
}
container.innerHTML = '';
if (!media || !media.type || media.type === 'none') {
container.style.display = 'none';
return;
}
var el = null;
if (media.type === 'image' && media.image) {
el = document.createElement('img');
el.className = 'eventup-marketing-popup__media-image';
el.src = media.image;
el.alt = media.alt || '';
} else if (media.type === 'video' && media.video) {
el = document.createElement('video');
el.className = 'eventup-marketing-popup__media-video';
el.src = media.video;
el.controls = true;
el.autoplay = true;
el.muted = true;
el.playsInline = true;
el.setAttribute('playsinline', '');
} else if (media.type === 'youtube') {
var embed = toYouTubeEmbed(media.youtube);
if (embed) {
el = document.createElement('iframe');
el.className = 'eventup-marketing-popup__media-frame';
el.src = embed;
el.setAttribute('frameborder', '0');
el.setAttribute('allow', 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture');
el.setAttribute('allowfullscreen', '');
}
}
if (el) {
container.style.display = 'block';
container.appendChild(el);
} else {
container.style.display = 'none';
}
}
function clearMedia() {
if (!popupEl) {
return;
}
var container = popupEl.querySelector('[data-marketing-media]');
if (container) {
container.innerHTML = '';
}
}
function hidePreloader() {
var preloader = document.getElementById('preloader');
if (!preloader) {
return;
}
preloader.style.display = 'none';
preloader.style.visibility = 'hidden';
preloader.style.opacity = '0';
}
function hidePopup() {
if (!popupEl) {
return;
}
if (autoCloseTimer) {
window.clearTimeout(autoCloseTimer);
autoCloseTimer = null;
}
popupEl.classList.remove('is-visible');
popupEl.setAttribute('aria-hidden', 'true');
document.body.classList.remove('eventup-marketing-popup-open');
clearMedia();
window.setTimeout(function () {
if (popupEl) {
popupEl.style.display = 'none';
}
}, 400);
}
function showPopup(config) {
var popup = createPopup();
var heading = popup.querySelector('[data-marketing-heading]');
var logo = popup.querySelector('[data-marketing-logo]');
var mediaContainer = popup.querySelector('[data-marketing-media]');
var logoConfig = (config && config.logo) || {};
var media = (config && config.media) || {};
var hasMedia = media && media.type && media.type !== 'none';
var autoCloseMs = config && config.autoCloseDuration != null
? Number(config.autoCloseDuration)
: 3000;
// Video / YouTube popups stay open until dismissed so the clip can play.
if (hasMedia && (media.type === 'video' || media.type === 'youtube')) {
autoCloseMs = 0;
}
if (heading) {
heading.textContent = (config && config.heading) || 'Introducing';
}
if (logo) {
var logoUrl = typeof logoConfig === 'string' ? logoConfig : logoConfig.url;
if (logoUrl) {
logo.src = logoUrl;
logo.alt = (typeof logoConfig === 'object' && logoConfig.alt) || '';
logo.style.display = '';
} else {
logo.removeAttribute('src');
logo.style.display = 'none';
}
}
renderMedia(mediaContainer, media);
hidePreloader();
popup.style.display = 'flex';
popup.setAttribute('aria-hidden', 'false');
document.body.classList.add('eventup-marketing-popup-open');
window.requestAnimationFrame(function () {
popup.classList.add('is-visible');
markShown();
});
if (autoCloseTimer) {
window.clearTimeout(autoCloseTimer);
}
if (autoCloseMs > 0) {
autoCloseTimer = window.setTimeout(hidePopup, autoCloseMs);
}
}
function shouldShow(config) {
if (!config || config.show === false) {
return false;
}
if (config.showOncePerSession !== false) {
try {
if (window.sessionStorage.getItem('eventup-marketing-popup-v2') === '1') {
return false;
}
} catch (error) {
/* ignore */
}
}
return true;
}
function markShown() {
try {
window.sessionStorage.setItem('eventup-marketing-popup-v2', '1');
} catch (error) {
/* ignore */
}
}
function init(config) {
if (!shouldShow(config)) {
if (popupEl) {
popupEl.style.display = 'none';
}
return;
}
function launch() {
window.setTimeout(function () {
showPopup(config);
}, 150);
}
if (document.readyState === 'complete') {
launch();
} else {
window.addEventListener('load', launch);
}
}
window.EventUpMarketingPopup = {
init: init,
show: showPopup,
hide: hidePopup
};
})();