105 lines
3.1 KiB
JavaScript
105 lines
3.1 KiB
JavaScript
(function (run) {
|
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
|
else run();
|
|
})(function () {
|
|
(function () {
|
|
var AGE_KEY = 'vino-age-ok';
|
|
var BAR_KEY = 'vino-cookie-ok';
|
|
var bar = document.querySelector('[data-cookie-bar]');
|
|
var gate = document.querySelector('[data-age-gate]');
|
|
|
|
function stored(key) {
|
|
try { return localStorage.getItem(key) === '1'; } catch (e) { return false; }
|
|
}
|
|
function store(key) {
|
|
try { localStorage.setItem(key, '1'); } catch (e) {}
|
|
}
|
|
function choice() {
|
|
try { return localStorage.getItem(BAR_KEY) || ''; } catch (e) { return ''; }
|
|
}
|
|
function choose(value) {
|
|
try { localStorage.setItem(BAR_KEY, value); } catch (e) {}
|
|
}
|
|
|
|
function notify() {
|
|
document.dispatchEvent(new CustomEvent('vino:consent'));
|
|
}
|
|
|
|
window.VinoConsent = {
|
|
allowed: function () {
|
|
var value = choice();
|
|
return value === 'accept' || value === '1';
|
|
},
|
|
accept: function () {
|
|
if (bar) answer('accept');
|
|
else {
|
|
choose('accept');
|
|
notify();
|
|
}
|
|
}
|
|
};
|
|
|
|
function showBar() {
|
|
if (!bar || choice()) return;
|
|
bar.hidden = false;
|
|
void bar.offsetWidth;
|
|
bar.classList.add('vino-cookie--on');
|
|
}
|
|
|
|
function answer(value) {
|
|
choose(value);
|
|
notify();
|
|
bar.classList.remove('vino-cookie--on');
|
|
setTimeout(function () { bar.hidden = true; }, 300);
|
|
}
|
|
|
|
if (bar) {
|
|
var ok = bar.querySelector('[data-cookie-ok]');
|
|
var decline = bar.querySelector('[data-cookie-decline]');
|
|
if (ok) ok.addEventListener('click', function () { answer('accept'); });
|
|
if (decline) decline.addEventListener('click', function () { answer('decline'); });
|
|
document.addEventListener('click', function (e) {
|
|
if (!e.target.closest('[data-cookie-settings]')) return;
|
|
try { localStorage.removeItem(BAR_KEY); } catch (err) {}
|
|
showBar();
|
|
});
|
|
}
|
|
|
|
if (!gate || stored(AGE_KEY)) {
|
|
if (gate) gate.remove();
|
|
showBar();
|
|
return;
|
|
}
|
|
|
|
gate.hidden = false;
|
|
document.documentElement.style.overflow = 'hidden';
|
|
var yes = gate.querySelector('[data-age-yes]');
|
|
var no = gate.querySelector('[data-age-no]');
|
|
var card = gate.querySelector('.vino-age__card');
|
|
if (card) card.focus();
|
|
function trap(event) {
|
|
if (event.key !== 'Tab' || !yes || !no) return;
|
|
var active = document.activeElement;
|
|
if (event.shiftKey && (active === yes || active === card)) {
|
|
event.preventDefault();
|
|
no.focus();
|
|
} else if (!event.shiftKey && (active === no || !gate.contains(active))) {
|
|
event.preventDefault();
|
|
yes.focus();
|
|
}
|
|
}
|
|
document.addEventListener('keydown', trap);
|
|
if (yes) yes.addEventListener('click', function () {
|
|
store(AGE_KEY);
|
|
document.removeEventListener('keydown', trap);
|
|
document.documentElement.style.overflow = '';
|
|
gate.classList.add('vino-age--leaving');
|
|
setTimeout(function () { gate.remove(); }, 400);
|
|
setTimeout(showBar, 500);
|
|
});
|
|
if (no) no.addEventListener('click', function () {
|
|
window.location.href = gate.getAttribute('data-exit') || '/';
|
|
});
|
|
})();
|
|
});
|