| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
var STORAGE_KEY_PREFIX = 'bkbg_nb_'; |
| 5 |
|
| 6 |
function isDismissed(key) { |
| 7 |
try { |
| 8 |
var val = localStorage.getItem(key); |
| 9 |
if (!val) return false; |
| 10 |
var data = JSON.parse(val); |
| 11 |
if (!data.expires) return true; |
| 12 |
return Date.now() < data.expires; |
| 13 |
} catch (e) { return false; } |
| 14 |
} |
| 15 |
|
| 16 |
function setDismissed(key, days) { |
| 17 |
try { |
| 18 |
var expires = days > 0 ? Date.now() + days * 86400000 : 0; |
| 19 |
localStorage.setItem(key, JSON.stringify({ dismissed: true, expires: expires })); |
| 20 |
} catch (e) { /* ignore */ } |
| 21 |
} |
| 22 |
|
| 23 |
function initBar(dataWrap) { |
| 24 |
var d = dataWrap.dataset; |
| 25 |
var position = d.position || 'top'; |
| 26 |
var animation = d.animation || 'slide'; |
| 27 |
var sticky = d.sticky !== '0'; |
| 28 |
var delay = parseInt(d.delay, 10) || 0; |
| 29 |
var cookieName = d.cookie || STORAGE_KEY_PREFIX + 'dismissed'; |
| 30 |
var cookieDays = parseInt(d.cookieDays, 10); |
| 31 |
if (isNaN(cookieDays)) cookieDays = 1; |
| 32 |
|
| 33 |
/* already dismissed? skip */ |
| 34 |
if (isDismissed(cookieName)) return; |
| 35 |
|
| 36 |
/* create the outer wrapper that gets appended to body */ |
| 37 |
var wrapper = document.createElement('div'); |
| 38 |
wrapper.className = 'bkbg-nb-wrap wp-block-blockenberg-notification-bar'; |
| 39 |
|
| 40 |
/* move the saved bar HTML inside the new wrapper */ |
| 41 |
var bar = dataWrap.querySelector('.bkbg-nb-bar'); |
| 42 |
if (!bar) return; |
| 43 |
|
| 44 |
wrapper.appendChild(bar); |
| 45 |
document.body.appendChild(wrapper); |
| 46 |
|
| 47 |
/* hide original placeholder */ |
| 48 |
dataWrap.style.display = 'none'; |
| 49 |
|
| 50 |
function show() { |
| 51 |
/* position class */ |
| 52 |
if (sticky) { |
| 53 |
wrapper.classList.add('bkbg-nb-wrap--' + position + '-sticky'); |
| 54 |
/* push body so content isn't hidden under bar */ |
| 55 |
requestAnimationFrame(function () { |
| 56 |
var h = wrapper.offsetHeight; |
| 57 |
if (position === 'top') { |
| 58 |
document.body.classList.add('bkbg-nb-push-top'); |
| 59 |
document.body.style.paddingTop = (parseFloat(getComputedStyle(document.body).paddingTop) + h) + 'px'; |
| 60 |
} else { |
| 61 |
document.body.classList.add('bkbg-nb-push-bottom'); |
| 62 |
document.body.style.paddingBottom = (parseFloat(getComputedStyle(document.body).paddingBottom) + h) + 'px'; |
| 63 |
} |
| 64 |
}); |
| 65 |
} |
| 66 |
|
| 67 |
/* animation class */ |
| 68 |
if (animation !== 'none') { |
| 69 |
wrapper.classList.add('bkbg-nb-wrap--anim-' + animation); |
| 70 |
} |
| 71 |
wrapper.classList.add('bkbg-nb-wrap--visible'); |
| 72 |
wrapper.style.opacity = ''; |
| 73 |
} |
| 74 |
|
| 75 |
function dismiss() { |
| 76 |
setDismissed(cookieName, cookieDays); |
| 77 |
wrapper.style.opacity = '0'; |
| 78 |
wrapper.style.transition = 'opacity 0.25s ease'; |
| 79 |
setTimeout(function () { |
| 80 |
wrapper.style.display = 'none'; |
| 81 |
/* remove body padding */ |
| 82 |
if (position === 'top') { |
| 83 |
document.body.classList.remove('bkbg-nb-push-top'); |
| 84 |
document.body.style.paddingTop = ''; |
| 85 |
} else { |
| 86 |
document.body.classList.remove('bkbg-nb-push-bottom'); |
| 87 |
document.body.style.paddingBottom = ''; |
| 88 |
} |
| 89 |
}, 280); |
| 90 |
} |
| 91 |
|
| 92 |
/* close btn */ |
| 93 |
var closeBtn = wrapper.querySelector('.bkbg-nb-close'); |
| 94 |
if (closeBtn) { |
| 95 |
closeBtn.addEventListener('click', dismiss); |
| 96 |
} |
| 97 |
|
| 98 |
/* show with optional delay */ |
| 99 |
if (delay > 0) { |
| 100 |
setTimeout(show, delay); |
| 101 |
} else { |
| 102 |
show(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
document.addEventListener('DOMContentLoaded', function () { |
| 107 |
/* Blocks are saved as <div data-position data-animation …> */ |
| 108 |
document.querySelectorAll('[data-position][data-animation][data-cookie].wp-block-blockenberg-notification-bar').forEach(function (el) { |
| 109 |
/* Only if not already inside a .bkbg-nb-wrap we created */ |
| 110 |
if (!el.closest('.bkbg-nb-wrap') || el.dataset.position) { |
| 111 |
initBar(el); |
| 112 |
} |
| 113 |
}); |
| 114 |
}); |
| 115 |
})(); |
| 116 |
|