Vinoteca: wine shop on 1C-Bitrix (module, template, components, pages)
This commit is contained in:
commit
52795535ae
586 files changed
+27619
No files matched your search
@@ -0,0 +1,46 @@
|
||||
(function () {
|
||||
function config() {
|
||||
try {
|
||||
return JSON.parse(document.getElementById('vino-config').textContent) || {};
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function arm(guard) {
|
||||
var cfg = config();
|
||||
if (!guard || !cfg.altcha) return;
|
||||
if (!document.querySelector('script[data-altcha]')) {
|
||||
var script = document.createElement('script');
|
||||
script.type = 'module';
|
||||
script.src = cfg.altcha;
|
||||
script.setAttribute('data-altcha', '');
|
||||
document.head.appendChild(script);
|
||||
return;
|
||||
}
|
||||
if (typeof guard.getState === 'function' && ['verified', 'verifying'].indexOf(guard.getState()) < 0) guard.verify();
|
||||
}
|
||||
|
||||
function armed(guard) {
|
||||
return new Promise(function (resolve) {
|
||||
var tick = 0;
|
||||
(function poll() {
|
||||
var state = guard && typeof guard.getState === 'function' ? guard.getState() : '';
|
||||
if (state === 'verified') return resolve(true);
|
||||
var failed = state === 'error' || state === 'expired';
|
||||
var idle = state === 'unverified' && tick >= 30;
|
||||
if ((failed || idle) && tick % 30 === 0) guard.verify();
|
||||
if (++tick > 150) return resolve(false);
|
||||
setTimeout(poll, 100);
|
||||
})();
|
||||
});
|
||||
}
|
||||
|
||||
function reset(guard) {
|
||||
if (!guard) return;
|
||||
if (typeof guard.reset === 'function') guard.reset();
|
||||
if (typeof guard.verify === 'function') guard.verify();
|
||||
}
|
||||
|
||||
window.VinoAltcha = { arm: arm, armed: armed, reset: reset };
|
||||
})();
|
||||
@@ -0,0 +1,34 @@
|
||||
(function () {
|
||||
var layer = null;
|
||||
var label = null;
|
||||
|
||||
function build() {
|
||||
layer = document.createElement('div');
|
||||
layer.className = 'vino-busy';
|
||||
layer.hidden = true;
|
||||
layer.setAttribute('role', 'status');
|
||||
layer.setAttribute('aria-live', 'polite');
|
||||
var spinner = document.createElement('span');
|
||||
spinner.className = 'vino-busy__spinner';
|
||||
label = document.createElement('p');
|
||||
label.className = 'vino-busy__text';
|
||||
layer.appendChild(spinner);
|
||||
layer.appendChild(label);
|
||||
document.body.appendChild(layer);
|
||||
}
|
||||
|
||||
window.addEventListener('pageshow', function (e) {
|
||||
if (e.persisted && layer) layer.hidden = true;
|
||||
});
|
||||
|
||||
window.VinoBusy = {
|
||||
show: function (text) {
|
||||
if (!layer) build();
|
||||
label.textContent = text || '';
|
||||
layer.hidden = false;
|
||||
},
|
||||
hide: function () {
|
||||
if (layer) layer.hidden = true;
|
||||
}
|
||||
};
|
||||
})();
|
||||
@@ -0,0 +1,97 @@
|
||||
(function () {
|
||||
if (!window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var items = {};
|
||||
var timers = {};
|
||||
var version = 0;
|
||||
var cfg = {};
|
||||
try { cfg = JSON.parse(document.getElementById('vino-config').textContent); } catch (e) {}
|
||||
var max = cfg.cartMax;
|
||||
|
||||
function total() {
|
||||
return Object.keys(items).reduce(function (sum, id) { return sum + items[id]; }, 0);
|
||||
}
|
||||
|
||||
function counters(count) {
|
||||
document.querySelectorAll('[data-cart-count]').forEach(function (c) {
|
||||
c.textContent = count > 99 ? '99+' : count;
|
||||
c.hidden = !count;
|
||||
});
|
||||
}
|
||||
|
||||
function render() {
|
||||
document.querySelectorAll('[data-qty]').forEach(function (box) {
|
||||
var qty = items[box.getAttribute('data-qty')] || 0;
|
||||
box.classList.toggle('vino-card__qty--active', qty > 0);
|
||||
var out = box.querySelector('.vino-card__count');
|
||||
if (out) out.textContent = qty || 1;
|
||||
});
|
||||
counters(total());
|
||||
}
|
||||
|
||||
function apply(data, sent) {
|
||||
if (!data || !data.items || sent !== version) return;
|
||||
items = {};
|
||||
Object.keys(data.items).forEach(function (id) { items[id] = data.items[id]; });
|
||||
render();
|
||||
}
|
||||
|
||||
function load() {
|
||||
var sent = version;
|
||||
BX.ajax.runAction('jinove:vinoteca.basket.state', { method: 'GET' })
|
||||
.then(function (res) { apply(res.data, sent); }, function () {});
|
||||
}
|
||||
|
||||
function send(id) {
|
||||
var sent = version;
|
||||
BX.ajax.runAction('jinove:vinoteca.basket.set', { data: { id: id, qty: items[id] || 0 } })
|
||||
.then(function (res) { apply(res.data, sent); }, function () { if (sent === version) load(); });
|
||||
}
|
||||
|
||||
document.addEventListener('click', function (e) {
|
||||
var step = e.target.closest('[data-qty-step]');
|
||||
if (!step) return;
|
||||
var box = step.closest('[data-qty]');
|
||||
if (!box) return;
|
||||
e.preventDefault();
|
||||
var id = box.getAttribute('data-qty');
|
||||
var qty = Math.max(0, Math.min(max, (items[id] || 0) + parseInt(step.getAttribute('data-qty-step'), 10)));
|
||||
if (qty) items[id] = qty; else delete items[id];
|
||||
version++;
|
||||
render();
|
||||
clearTimeout(timers[id]);
|
||||
timers[id] = setTimeout(function () { send(id); }, 350);
|
||||
});
|
||||
|
||||
document.addEventListener('click', function (e) {
|
||||
var b = e.target.closest('[data-add-cart]');
|
||||
if (!b) return;
|
||||
e.preventDefault();
|
||||
var id = b.getAttribute('data-add-cart');
|
||||
if (!id || b.disabled) return;
|
||||
var qty = 1;
|
||||
var box = b.closest('.vino-pdp__buy') || b.closest('.vino-pdp__cart') || document;
|
||||
var out = box.querySelector('[data-stepper] output');
|
||||
if (out) {
|
||||
var q = parseInt(out.textContent, 10);
|
||||
if (q > 0) qty = q;
|
||||
}
|
||||
items[id] = Math.min(max, (items[id] || 0) + qty);
|
||||
version++;
|
||||
render();
|
||||
clearTimeout(timers[id]);
|
||||
timers[id] = setTimeout(function () { send(id); }, 350);
|
||||
});
|
||||
|
||||
function boot() {
|
||||
if (!window.VinoState) return load();
|
||||
var sent = version;
|
||||
VinoState.first('basket').then(function (res) { apply(res.data, sent); }, load);
|
||||
}
|
||||
|
||||
boot();
|
||||
document.addEventListener('vino:content', render);
|
||||
document.addEventListener('vino:basket', function (e) {
|
||||
version++;
|
||||
apply(e.detail, version);
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,103 @@
|
||||
(function () {
|
||||
if (!window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var cfg = {};
|
||||
try { cfg = JSON.parse(document.getElementById('vino-config').textContent); } catch (e) {}
|
||||
var text = cfg.text || {};
|
||||
var MAX = cfg.compareLimit;
|
||||
var items = [];
|
||||
var timers = {};
|
||||
var version = 0;
|
||||
var toast = null;
|
||||
|
||||
function render() {
|
||||
document.querySelectorAll('[data-compare-count]').forEach(function (counter) {
|
||||
counter.textContent = items.length;
|
||||
counter.hidden = !items.length;
|
||||
});
|
||||
document.querySelectorAll('[data-compare-toggle]').forEach(function (button) {
|
||||
var on = items.indexOf(Number(button.getAttribute('data-compare-toggle'))) >= 0;
|
||||
var label = on ? text.compareRemove : text.compareAdd;
|
||||
button.classList.toggle(button.classList[0] + '--active', on);
|
||||
button.setAttribute('aria-pressed', on ? 'true' : 'false');
|
||||
button.setAttribute('aria-label', label || '');
|
||||
button.title = label || '';
|
||||
});
|
||||
}
|
||||
|
||||
function say(message, link) {
|
||||
if (!toast) {
|
||||
toast = document.createElement('div');
|
||||
toast.className = 'vino-toast';
|
||||
toast.setAttribute('role', 'status');
|
||||
document.body.appendChild(toast);
|
||||
}
|
||||
toast.textContent = message;
|
||||
var anchor = document.createElement('a');
|
||||
anchor.className = 'vino-toast__link';
|
||||
anchor.href = link.href;
|
||||
anchor.textContent = link.label;
|
||||
toast.appendChild(anchor);
|
||||
toast.classList.add('vino-toast--on');
|
||||
clearTimeout(toast.timer);
|
||||
toast.timer = setTimeout(function () { toast.classList.remove('vino-toast--on'); }, 3500);
|
||||
}
|
||||
|
||||
function warnFull() {
|
||||
say((text.compareMax || '').replace('#N#', MAX), { href: (cfg.siteDir || '/') + 'compare/', label: text.compareOpen || '' });
|
||||
}
|
||||
|
||||
function apply(response, sent) {
|
||||
if (sent !== version) return;
|
||||
var data = response.data || {};
|
||||
items = data.items || [];
|
||||
render();
|
||||
if (data.full) warnFull();
|
||||
}
|
||||
|
||||
function load() {
|
||||
var sent = version;
|
||||
BX.ajax.runAction('jinove:vinoteca.compare.state', { method: 'GET' }).then(function (response) { apply(response, sent); }, function () {});
|
||||
}
|
||||
|
||||
function send(id) {
|
||||
var sent = ++version;
|
||||
var on = items.indexOf(id) >= 0;
|
||||
BX.ajax.runAction('jinove:vinoteca.compare.set', { data: { id: id, on: on ? 'Y' : 'N' } })
|
||||
.then(function (response) { apply(response, sent); }, function () { if (sent === version) load(); });
|
||||
}
|
||||
|
||||
function boot() {
|
||||
if (!window.VinoState) return load();
|
||||
var sent = version;
|
||||
VinoState.first('compare').then(function (response) { apply(response, sent); }, load);
|
||||
}
|
||||
|
||||
boot();
|
||||
document.addEventListener('vino:content', render);
|
||||
document.addEventListener('vino:compare', function (event) {
|
||||
version++;
|
||||
items = (event.detail && event.detail.items) || [];
|
||||
render();
|
||||
});
|
||||
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-compare-toggle]');
|
||||
if (!button) return;
|
||||
event.preventDefault();
|
||||
var id = Number(button.getAttribute('data-compare-toggle'));
|
||||
var index = items.indexOf(id);
|
||||
if (index >= 0) {
|
||||
items.splice(index, 1);
|
||||
} else {
|
||||
if (items.length >= MAX) {
|
||||
warnFull();
|
||||
return;
|
||||
}
|
||||
items.push(id);
|
||||
}
|
||||
version++;
|
||||
render();
|
||||
clearTimeout(timers[id]);
|
||||
timers[id] = setTimeout(function () { send(id); }, 350);
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,63 @@
|
||||
(function () {
|
||||
if (!window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var items = [];
|
||||
var timers = {};
|
||||
var version = 0;
|
||||
|
||||
function render() {
|
||||
document.querySelectorAll('[data-fav-count]').forEach(function (counter) {
|
||||
counter.textContent = items.length > 99 ? '99+' : items.length;
|
||||
counter.hidden = !items.length;
|
||||
});
|
||||
document.querySelectorAll('[data-fav-toggle]').forEach(function (button) {
|
||||
var on = items.indexOf(Number(button.getAttribute('data-fav-toggle'))) >= 0;
|
||||
button.classList.toggle(button.classList[0] + '--active', on);
|
||||
button.setAttribute('aria-pressed', on ? 'true' : 'false');
|
||||
});
|
||||
}
|
||||
|
||||
function apply(response, sent) {
|
||||
if (sent !== version) return;
|
||||
items = (response.data && response.data.items) || [];
|
||||
render();
|
||||
}
|
||||
|
||||
function load() {
|
||||
var sent = version;
|
||||
BX.ajax.runAction('jinove:vinoteca.favorite.state', { method: 'GET' }).then(function (response) { apply(response, sent); }, function () {});
|
||||
}
|
||||
|
||||
function send(id) {
|
||||
var sent = ++version;
|
||||
var on = items.indexOf(id) >= 0;
|
||||
BX.ajax.runAction('jinove:vinoteca.favorite.set', { data: { id: id, on: on ? 'Y' : 'N' } })
|
||||
.then(function (response) { apply(response, sent); }, function () { if (sent === version) load(); });
|
||||
}
|
||||
|
||||
function boot() {
|
||||
if (!window.VinoState) return load();
|
||||
var sent = version;
|
||||
VinoState.first('favorite').then(function (response) { apply(response, sent); }, load);
|
||||
}
|
||||
|
||||
boot();
|
||||
document.addEventListener('vino:content', render);
|
||||
document.addEventListener('vino:favorite', function (event) {
|
||||
version++;
|
||||
items = (event.detail && event.detail.items) || [];
|
||||
render();
|
||||
});
|
||||
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-fav-toggle]');
|
||||
if (!button) return;
|
||||
event.preventDefault();
|
||||
var id = Number(button.getAttribute('data-fav-toggle'));
|
||||
var index = items.indexOf(id);
|
||||
if (index >= 0) items.splice(index, 1); else items.push(id);
|
||||
version++;
|
||||
render();
|
||||
clearTimeout(timers[id]);
|
||||
timers[id] = setTimeout(function () { send(id); }, 350);
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,33 @@
|
||||
(function (run) {
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
||||
else run();
|
||||
})(function () {
|
||||
var root = document.querySelector('[data-favorites]');
|
||||
if (!root || !window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var status = root.querySelector('[data-favorites-status]');
|
||||
document.addEventListener('click', function (event) {
|
||||
var button = event.target.closest('[data-favorites] [data-fav-toggle]');
|
||||
if (!button) return;
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
var card = button.closest('.vino-card');
|
||||
if (!card) return;
|
||||
var parent = card.parentNode;
|
||||
var next = card.nextSibling;
|
||||
var neighbour = card.nextElementSibling || card.previousElementSibling;
|
||||
card.remove();
|
||||
if (status) status.textContent = root.getAttribute('data-favorites-removed') || '';
|
||||
var focus = (neighbour && neighbour.querySelector('.vino-card__link')) || document.querySelector('h1');
|
||||
if (focus) {
|
||||
if (focus.tagName === 'H1') focus.setAttribute('tabindex', '-1');
|
||||
focus.focus();
|
||||
}
|
||||
BX.ajax.runAction('jinove:vinoteca.favorite.set', { data: { id: button.getAttribute('data-fav-toggle'), on: 'N' } }).then(function (response) {
|
||||
document.dispatchEvent(new CustomEvent('vino:favorite', { detail: response.data }));
|
||||
if (!root.querySelector('.vino-card')) window.location.href = window.location.pathname;
|
||||
}, function () {
|
||||
parent.insertBefore(card, next);
|
||||
if (status) status.textContent = '';
|
||||
});
|
||||
}, true);
|
||||
});
|
||||
@@ -0,0 +1,104 @@
|
||||
(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') || '/';
|
||||
});
|
||||
})();
|
||||
});
|
||||
@@ -0,0 +1,107 @@
|
||||
(function (run) {
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
||||
else run();
|
||||
})(function () {
|
||||
(function () {
|
||||
var header = document.querySelector('[data-header]');
|
||||
if (header) {
|
||||
var onScroll = function () { header.classList.toggle('vino-header--scrolled', window.scrollY > 20); };
|
||||
onScroll();
|
||||
window.addEventListener('scroll', onScroll, { passive: true });
|
||||
}
|
||||
var toggle = document.querySelector('[data-menu-toggle]');
|
||||
var menu = document.querySelector('[data-menu]');
|
||||
if (toggle && menu) {
|
||||
var setMenu = function (open) {
|
||||
menu.classList.toggle('vino-nav--open', open);
|
||||
toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
|
||||
};
|
||||
toggle.addEventListener('click', function (e) {
|
||||
e.stopPropagation();
|
||||
setMenu(!menu.classList.contains('vino-nav--open'));
|
||||
});
|
||||
menu.addEventListener('click', function (e) { if (e.target.closest('a, button')) setMenu(false); });
|
||||
document.addEventListener('click', function (e) { if (!menu.contains(e.target)) setMenu(false); });
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (e.key !== 'Escape' || !menu.classList.contains('vino-nav--open')) return;
|
||||
setMenu(false);
|
||||
toggle.focus();
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
(function () {
|
||||
var box = document.querySelector('[data-lang]');
|
||||
if (!box) return;
|
||||
var btn = box.querySelector('[data-lang-toggle]');
|
||||
function set(open) {
|
||||
box.setAttribute('data-open', open ? 'true' : 'false');
|
||||
btn.setAttribute('aria-expanded', open ? 'true' : 'false');
|
||||
}
|
||||
btn.addEventListener('click', function (e) {
|
||||
e.stopPropagation();
|
||||
set(box.getAttribute('data-open') !== 'true');
|
||||
});
|
||||
document.addEventListener('click', function (e) { if (!box.contains(e.target)) set(false); });
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (e.key !== 'Escape' || box.getAttribute('data-open') !== 'true') return;
|
||||
set(false);
|
||||
btn.focus();
|
||||
});
|
||||
})();
|
||||
|
||||
(function () {
|
||||
var root = document.querySelector('[data-search]');
|
||||
if (!root) return;
|
||||
var input = root.querySelector('[data-search-input]');
|
||||
var hideTimer = 0;
|
||||
var opener = null;
|
||||
var openers = document.querySelectorAll('[data-search-open]');
|
||||
function focusable() {
|
||||
return Array.prototype.filter.call(root.querySelectorAll('a[href], button:not([disabled]), input:not([disabled])'), function (el) { return el.offsetParent !== null; });
|
||||
}
|
||||
function open(e) {
|
||||
if (e) e.preventDefault();
|
||||
opener = e && e.currentTarget ? e.currentTarget : document.activeElement;
|
||||
openers.forEach(function (o) { o.setAttribute('aria-expanded', 'true'); });
|
||||
clearTimeout(hideTimer);
|
||||
root.hidden = false;
|
||||
void root.offsetWidth;
|
||||
root.classList.add('vino-search--open');
|
||||
if (input) input.focus();
|
||||
document.documentElement.style.overflow = 'hidden';
|
||||
}
|
||||
function close() {
|
||||
root.classList.remove('vino-search--open');
|
||||
if (input) input.value = '';
|
||||
document.dispatchEvent(new CustomEvent('vino:search-close'));
|
||||
hideTimer = setTimeout(function () { root.hidden = true; }, 280);
|
||||
document.documentElement.style.overflow = '';
|
||||
openers.forEach(function (o) { o.setAttribute('aria-expanded', 'false'); });
|
||||
if (opener && opener.focus) opener.focus();
|
||||
}
|
||||
document.querySelectorAll('[data-search-open]').forEach(function (o) { o.addEventListener('click', open); });
|
||||
root.querySelectorAll('[data-search-close]').forEach(function (b) { b.addEventListener('click', close); });
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (root.hidden) return;
|
||||
if (e.key === 'Escape') {
|
||||
close();
|
||||
return;
|
||||
}
|
||||
if (e.key !== 'Tab') return;
|
||||
var items = focusable();
|
||||
if (!items.length) return;
|
||||
var first = items[0], last = items[items.length - 1];
|
||||
if (e.shiftKey && document.activeElement === first) {
|
||||
e.preventDefault();
|
||||
last.focus();
|
||||
} else if (!e.shiftKey && document.activeElement === last) {
|
||||
e.preventDefault();
|
||||
first.focus();
|
||||
} else if (!root.contains(document.activeElement)) {
|
||||
e.preventDefault();
|
||||
first.focus();
|
||||
}
|
||||
});
|
||||
})();
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
(function () {
|
||||
function init() {
|
||||
document.querySelectorAll('[data-password-toggle]').forEach(function (toggle) {
|
||||
var input = document.getElementById(toggle.getAttribute('aria-controls'));
|
||||
var form = toggle.closest('form');
|
||||
if (!input || !form) return;
|
||||
|
||||
function sync() {
|
||||
var empty = input.value === '';
|
||||
toggle.hidden = empty;
|
||||
if (empty && input.type === 'text') {
|
||||
input.type = 'password';
|
||||
toggle.textContent = form.dataset.labelShow;
|
||||
toggle.setAttribute('aria-pressed', 'false');
|
||||
}
|
||||
}
|
||||
|
||||
toggle.addEventListener('click', function () {
|
||||
var show = input.type === 'password';
|
||||
input.type = show ? 'text' : 'password';
|
||||
toggle.textContent = show ? form.dataset.labelHide : form.dataset.labelShow;
|
||||
toggle.setAttribute('aria-pressed', show ? 'true' : 'false');
|
||||
input.focus();
|
||||
});
|
||||
['input', 'change', 'focus'].forEach(function (name) {
|
||||
input.addEventListener(name, sync);
|
||||
});
|
||||
sync();
|
||||
});
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,165 @@
|
||||
(function (run) {
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
||||
else run();
|
||||
})(function () {
|
||||
(function () {
|
||||
function setup(input) {
|
||||
var code = input.getAttribute('data-phone-code');
|
||||
var length = Number(input.getAttribute('data-phone-digits'));
|
||||
var prefix = '+' + code + ' ';
|
||||
var error = input.getAttribute('data-phone-error');
|
||||
var groups = (input.getAttribute('data-phone-groups') || '').split(',').map(Number).filter(Boolean);
|
||||
var bounds = [0];
|
||||
while (bounds[bounds.length - 1] < length) {
|
||||
var size = groups[bounds.length - 1] || groups[groups.length - 1] || 3;
|
||||
bounds.push(Math.min(length, bounds[bounds.length - 1] + size));
|
||||
}
|
||||
|
||||
function digitsOf(value) {
|
||||
return value.slice(prefix.length).replace(/\D/g, '');
|
||||
}
|
||||
|
||||
function validate() {
|
||||
if (!error) return;
|
||||
var count = digitsOf(input.value).length;
|
||||
input.setCustomValidity(count !== length && (count || input.required) ? error : '');
|
||||
}
|
||||
|
||||
function format(digits) {
|
||||
var parts = [];
|
||||
for (var i = 1; i < bounds.length && bounds[i - 1] < digits.length; i++) {
|
||||
parts.push(digits.slice(bounds[i - 1], bounds[i]));
|
||||
}
|
||||
return prefix + parts.join(' ');
|
||||
}
|
||||
|
||||
function before(index) {
|
||||
return bounds.filter(function (b) { return b > 0 && b < index; }).length;
|
||||
}
|
||||
|
||||
function positionOf(index) {
|
||||
return index <= 0 ? prefix.length : prefix.length + index + before(index);
|
||||
}
|
||||
|
||||
function indexOf(position) {
|
||||
return input.value.slice(prefix.length, Math.max(position, prefix.length)).replace(/\D/g, '').length;
|
||||
}
|
||||
|
||||
function render(digits, caret) {
|
||||
if (caret === undefined) caret = digits.length;
|
||||
input.value = format(digits);
|
||||
var position = positionOf(caret);
|
||||
input.setSelectionRange(position, position);
|
||||
validate();
|
||||
}
|
||||
|
||||
function normalize(raw) {
|
||||
var digits = raw.replace(/\D/g, '');
|
||||
if (digits.length > length) {
|
||||
if (digits.indexOf('00' + code) === 0) digits = digits.slice(code.length + 2);
|
||||
else if (digits.indexOf(code) === 0) digits = digits.slice(code.length);
|
||||
else if (code === '7' && digits.length === length + 1 && digits.charAt(0) === '8') digits = digits.slice(1);
|
||||
}
|
||||
return digits;
|
||||
}
|
||||
|
||||
function replace(text, isPaste) {
|
||||
var current = digitsOf(input.value);
|
||||
var start = indexOf(input.selectionStart);
|
||||
var end = indexOf(input.selectionEnd);
|
||||
var whole = start === 0 && end === current.length;
|
||||
var room = length - current.length + (end - start);
|
||||
var clean = isPaste && whole ? normalize(text) : text.replace(/\D/g, '');
|
||||
if (!isPaste && clean.length > room) {
|
||||
var fixed = normalize(current.slice(0, start) + clean + current.slice(end));
|
||||
if (fixed.length <= length) {
|
||||
render(fixed);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (isPaste && clean.length > room) return;
|
||||
var inserted = clean.slice(0, room);
|
||||
if (!inserted) return;
|
||||
render(current.slice(0, start) + inserted + current.slice(end), start + inserted.length);
|
||||
}
|
||||
|
||||
function lower(index) {
|
||||
return bounds.filter(function (b) { return b < index; }).pop() || 0;
|
||||
}
|
||||
|
||||
function upper(index, max) {
|
||||
var next = bounds.filter(function (b) { return b > index; })[0];
|
||||
return Math.min(max, next === undefined ? max : next);
|
||||
}
|
||||
|
||||
function remove(type) {
|
||||
var current = digitsOf(input.value);
|
||||
var start = indexOf(input.selectionStart);
|
||||
var end = indexOf(input.selectionEnd);
|
||||
if (start === end) {
|
||||
if (type.indexOf('Backward') >= 0) {
|
||||
if (type.indexOf('Line') >= 0) start = 0;
|
||||
else if (type.indexOf('Word') >= 0) start = lower(start);
|
||||
else start = Math.max(0, start - 1);
|
||||
} else if (type.indexOf('Line') >= 0) {
|
||||
end = current.length;
|
||||
} else if (type.indexOf('Word') >= 0) {
|
||||
end = upper(end, current.length);
|
||||
} else {
|
||||
end = Math.min(current.length, end + 1);
|
||||
}
|
||||
}
|
||||
render(current.slice(0, start) + current.slice(end), start);
|
||||
}
|
||||
|
||||
function keepCaret() {
|
||||
setTimeout(function () {
|
||||
if (document.activeElement !== input) return;
|
||||
if (input.value.indexOf(prefix) !== 0) {
|
||||
render(normalize(input.value).slice(0, length));
|
||||
return;
|
||||
}
|
||||
if (input.selectionStart === input.selectionEnd && input.selectionStart < prefix.length) {
|
||||
input.setSelectionRange(prefix.length, prefix.length);
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
input.addEventListener('beforeinput', function (e) {
|
||||
e.preventDefault();
|
||||
if (e.inputType.indexOf('delete') === 0) remove(e.inputType);
|
||||
else if (e.inputType === 'insertFromPaste' || e.inputType === 'insertFromDrop') replace(e.dataTransfer ? e.dataTransfer.getData('text/plain') : (e.data || ''), true);
|
||||
else if (e.inputType.indexOf('insert') === 0 && e.data) replace(e.data, e.inputType === 'insertReplacementText');
|
||||
});
|
||||
|
||||
input.addEventListener('input', function () {
|
||||
render(normalize(input.value.indexOf(prefix) === 0 ? digitsOf(input.value) : input.value).slice(0, length));
|
||||
});
|
||||
|
||||
input.addEventListener('focus', function () {
|
||||
if (!input.value) render('');
|
||||
keepCaret();
|
||||
});
|
||||
|
||||
input.addEventListener('blur', function () {
|
||||
if (!digitsOf(input.value)) input.value = '';
|
||||
validate();
|
||||
});
|
||||
|
||||
['click', 'keyup', 'select'].forEach(function (type) { input.addEventListener(type, keepCaret); });
|
||||
|
||||
validate();
|
||||
|
||||
input.vinoPhone = {
|
||||
value: function () {
|
||||
var digits = digitsOf(input.value);
|
||||
return digits ? '+' + code + digits : '';
|
||||
},
|
||||
filled: function () { return digitsOf(input.value).length > 0; },
|
||||
complete: function () { return digitsOf(input.value).length === length; }
|
||||
};
|
||||
}
|
||||
|
||||
document.querySelectorAll('[data-phone-code]').forEach(setup);
|
||||
})();
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
if ('IntersectionObserver' in window && !window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
||||
document.documentElement.classList.add('vino-reveal');
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
(function (run) {
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
||||
else run();
|
||||
})(function () {
|
||||
(function () {
|
||||
var root = document.documentElement;
|
||||
if (!root.classList.contains('vino-reveal')) return;
|
||||
var els = document.querySelectorAll('.vino-section, .vino-story, .vino-sales, .vino-footer, .vino-pdp, .vino-reviews, .vino-pdp-related, .vino-auth:not(.vino-auth--embed), .vino-cab, .vino-cart, .vino-order, .vino-done, .vino-about__panel, .vino-brand-detail, .vino-article, .vino-news-feed, .vino-offers, .vino-offer, .vino-offers-wines, .vino-showcase, .vino-showcase-shots');
|
||||
els.forEach(function (el) {
|
||||
el.classList.add('vino-reveal__item');
|
||||
if (el.classList.contains('vino-section') || el.classList.contains('vino-news-feed') || el.classList.contains('vino-offers-wines')) el.classList.add('vino-reveal__item--deep');
|
||||
});
|
||||
var seen = false;
|
||||
var io = new IntersectionObserver(function (list) {
|
||||
seen = true;
|
||||
list.forEach(function (e) {
|
||||
if (e.isIntersecting) {
|
||||
e.target.classList.add('vino-reveal__item--visible');
|
||||
io.unobserve(e.target);
|
||||
}
|
||||
});
|
||||
}, { rootMargin: '0px 0px -12% 0px' });
|
||||
els.forEach(function (el) { io.observe(el); });
|
||||
setTimeout(function () {
|
||||
if (!seen) els.forEach(function (el) { el.classList.add('vino-reveal__item--visible'); });
|
||||
}, 3000);
|
||||
})();
|
||||
});
|
||||
@@ -0,0 +1,139 @@
|
||||
(function () {
|
||||
var modal = document.querySelector('[data-review-modal]');
|
||||
if (!modal || !window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var body = modal.querySelector('[data-review-body]');
|
||||
var form = modal.querySelector('[data-review-form]');
|
||||
var done = modal.querySelector('[data-review-done]');
|
||||
var err = modal.querySelector('[data-review-err]');
|
||||
var submit = form.querySelector('[type="submit"]');
|
||||
var guard = form.querySelector('altcha-widget');
|
||||
var started = 0;
|
||||
var codes = { rating: 'rating', author: 'author', text: 'text', consent: 'consent', too_often: 'tooOften', altcha: 'altcha' };
|
||||
|
||||
function visible(el) {
|
||||
return !!el && el.offsetParent !== null;
|
||||
}
|
||||
|
||||
function focusable() {
|
||||
return Array.prototype.filter.call(modal.querySelectorAll('a[href], button:not([disabled]), input:not([disabled]):not([type="hidden"]), textarea:not([disabled]), select:not([disabled])'), function (el) {
|
||||
return el.tabIndex >= 0 && visible(el);
|
||||
});
|
||||
}
|
||||
|
||||
function say(message) {
|
||||
err.textContent = message || '';
|
||||
err.hidden = !message;
|
||||
}
|
||||
|
||||
function mark(field, bad) {
|
||||
if (!field) return;
|
||||
field.classList.toggle('vino-inp--invalid', bad);
|
||||
if (bad) field.setAttribute('aria-invalid', 'true');
|
||||
else field.removeAttribute('aria-invalid');
|
||||
}
|
||||
|
||||
function open() {
|
||||
body.hidden = false;
|
||||
done.hidden = true;
|
||||
say('');
|
||||
started = Date.now();
|
||||
VinoAltcha.arm(guard);
|
||||
modal.showModal();
|
||||
var heading = visible(modal.querySelector('.vino-review-user')) ? modal.querySelector('#vino-review-title') : modal.querySelector('.vino-auth__title');
|
||||
if (heading) {
|
||||
if (!heading.id) heading.id = 'vino-auth-title';
|
||||
modal.setAttribute('aria-labelledby', heading.id);
|
||||
}
|
||||
setTimeout(function () {
|
||||
var list = focusable();
|
||||
var first = form.querySelector('.vino-rate__input:checked');
|
||||
if (!first || !visible(first)) first = list[0];
|
||||
if (first) first.focus();
|
||||
}, 30);
|
||||
}
|
||||
|
||||
function close() {
|
||||
modal.close();
|
||||
}
|
||||
|
||||
function check() {
|
||||
var f = form.elements;
|
||||
var problems = [];
|
||||
var rated = !!form.querySelector('.vino-rate__input:checked');
|
||||
form.querySelector('.vino-rate').classList.toggle('vino-rate--invalid', !rated);
|
||||
if (!rated) problems.push('rating');
|
||||
var text = f.text.value.trim();
|
||||
mark(f.text, text.length < 10);
|
||||
if (text.length < 10) problems.push('text');
|
||||
mark(f.consent, !f.consent.checked);
|
||||
if (!f.consent.checked) problems.push('consent');
|
||||
return problems;
|
||||
}
|
||||
|
||||
function message(code) {
|
||||
var key = codes[code] || 'error';
|
||||
return form.dataset['msg' + key.charAt(0).toUpperCase() + key.slice(1)] || form.dataset.msgError;
|
||||
}
|
||||
|
||||
form.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
var problems = check();
|
||||
if (problems.length) {
|
||||
say(message(problems[0]));
|
||||
return;
|
||||
}
|
||||
say('');
|
||||
submit.disabled = true;
|
||||
var f = form.elements;
|
||||
var data = {
|
||||
productId: form.dataset.product,
|
||||
rating: form.querySelector('.vino-rate__input:checked').value,
|
||||
text: f.text.value.trim(),
|
||||
consent: f.consent.checked ? '1' : '',
|
||||
website: f.website.value,
|
||||
started: String(started),
|
||||
lang: document.documentElement.lang || 'en',
|
||||
page: window.location.href
|
||||
};
|
||||
VinoAltcha.armed(guard)
|
||||
.then(function (ok) {
|
||||
if (!ok) return Promise.reject({ errors: [{ code: 'altcha' }] });
|
||||
data.altcha = f.altcha ? f.altcha.value : '';
|
||||
return BX.ajax.runAction('jinove:vinoteca.review.add', { data: data });
|
||||
})
|
||||
.then(function () {
|
||||
submit.disabled = false;
|
||||
form.reset();
|
||||
if (guard && typeof guard.reset === 'function') guard.reset();
|
||||
body.hidden = true;
|
||||
done.hidden = false;
|
||||
done.focus();
|
||||
}, function (res) {
|
||||
submit.disabled = false;
|
||||
VinoAltcha.reset(guard);
|
||||
var code = res && res.errors && res.errors[0] ? res.errors[0].code : '';
|
||||
say(message(code));
|
||||
});
|
||||
});
|
||||
|
||||
form.addEventListener('input', function (event) {
|
||||
if (event.target.classList.contains('vino-inp--invalid')) mark(event.target, false);
|
||||
if (event.target.classList.contains('vino-rate__input')) form.querySelector('.vino-rate').classList.remove('vino-rate--invalid');
|
||||
});
|
||||
|
||||
document.addEventListener('click', function (event) {
|
||||
if (event.target.closest('[data-review-open]')) {
|
||||
event.preventDefault();
|
||||
open();
|
||||
return;
|
||||
}
|
||||
if (modal.open && (event.target === modal || event.target.closest('[data-review-close]'))) close();
|
||||
});
|
||||
|
||||
try {
|
||||
if (sessionStorage.getItem('vinoOpenReview') === '1') {
|
||||
sessionStorage.removeItem('vinoOpenReview');
|
||||
open();
|
||||
}
|
||||
} catch (e) {}
|
||||
})();
|
||||
@@ -0,0 +1,36 @@
|
||||
(function () {
|
||||
function init() {
|
||||
var list = document.querySelector('[data-shots]');
|
||||
if (!list || !window.PhotoSwipe) return;
|
||||
var texts = {};
|
||||
try { texts = JSON.parse(list.getAttribute('data-viewer') || '{}'); } catch (e) {}
|
||||
var links = Array.prototype.slice.call(list.querySelectorAll('[data-shots-item]'));
|
||||
var items = links.map(function (link) {
|
||||
var img = link.querySelector('img');
|
||||
return {
|
||||
src: link.getAttribute('href'),
|
||||
width: Number(link.getAttribute('data-width')),
|
||||
height: Number(link.getAttribute('data-height')),
|
||||
alt: img ? img.alt : ''
|
||||
};
|
||||
});
|
||||
links.forEach(function (link, index) {
|
||||
link.addEventListener('click', function (event) {
|
||||
if (event.ctrlKey || event.metaKey || event.shiftKey || event.button !== 0) return;
|
||||
event.preventDefault();
|
||||
new window.PhotoSwipe(Object.assign({
|
||||
dataSource: items,
|
||||
index: index,
|
||||
bgOpacity: 0.94,
|
||||
wheelToZoom: true,
|
||||
paddingFn: function (viewport) {
|
||||
var side = viewport.x < 760 ? 0 : 88;
|
||||
return { top: 64, bottom: viewport.x < 760 ? 16 : 40, left: side, right: side };
|
||||
}
|
||||
}, texts)).init();
|
||||
});
|
||||
});
|
||||
}
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
|
||||
else init();
|
||||
})();
|
||||
@@ -0,0 +1,86 @@
|
||||
(function (run) {
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
||||
else run();
|
||||
})(function () {
|
||||
(function () {
|
||||
function dots(track, box) {
|
||||
var items = box.querySelectorAll('.vino-dots__dot'), cur = 0;
|
||||
if (!items.length) return null;
|
||||
items.forEach(function (dot, i) {
|
||||
dot.addEventListener('click', function () {
|
||||
track.scrollTo({ left: i * track.clientWidth, behavior: 'smooth' });
|
||||
});
|
||||
});
|
||||
return function () {
|
||||
var w = track.clientWidth;
|
||||
if (!w) return;
|
||||
var i = Math.max(0, Math.min(items.length - 1, Math.round(track.scrollLeft / w)));
|
||||
if (i === cur) return;
|
||||
items[cur].classList.remove('vino-dots__dot--active');
|
||||
items[cur].removeAttribute('aria-current');
|
||||
items[i].classList.add('vino-dots__dot--active');
|
||||
items[i].setAttribute('aria-current', 'true');
|
||||
cur = i;
|
||||
};
|
||||
}
|
||||
function progress(track, box) {
|
||||
var bar = box.querySelector('.vino-progress__bar');
|
||||
if (!bar) return null;
|
||||
return function () {
|
||||
var total = track.scrollWidth, view = track.clientWidth;
|
||||
if (!total || !view) return;
|
||||
var size = Math.min(1, view / total), max = total - view;
|
||||
bar.style.setProperty('--vino-progress-width', (size * 100) + '%');
|
||||
bar.style.setProperty('--vino-progress-shift', (max > 0 ? (track.scrollLeft / max) * (1 / size - 1) * 100 : 0) + '%');
|
||||
};
|
||||
}
|
||||
function init(track) {
|
||||
var name = track.getAttribute('data-slider'), handlers = [];
|
||||
var dotsBox = document.querySelector('[data-slider-dots="' + name + '"]');
|
||||
var barBox = document.querySelector('[data-slider-progress="' + name + '"]');
|
||||
if (dotsBox) handlers.push(dots(track, dotsBox));
|
||||
if (barBox) handlers.push(progress(track, barBox));
|
||||
handlers = handlers.filter(Boolean);
|
||||
if (!handlers.length) return;
|
||||
function update() { handlers.forEach(function (fn) { fn(); }); }
|
||||
track.addEventListener('scroll', update, { passive: true });
|
||||
window.addEventListener('resize', update);
|
||||
update();
|
||||
}
|
||||
function arrows(viewport, prev, next) {
|
||||
if (!viewport) return;
|
||||
function step() {
|
||||
var card = viewport.querySelector('.vino-card');
|
||||
if (!card) return 300;
|
||||
var gap = parseFloat(getComputedStyle(card.parentElement).columnGap) || 0;
|
||||
return (card.getBoundingClientRect().width + gap) * 2;
|
||||
}
|
||||
function update() {
|
||||
var max = viewport.scrollWidth - viewport.clientWidth - 2;
|
||||
var fits = max <= 0;
|
||||
if (prev) {
|
||||
prev.hidden = fits;
|
||||
prev.disabled = viewport.scrollLeft <= 2;
|
||||
}
|
||||
if (next) {
|
||||
next.hidden = fits;
|
||||
next.disabled = viewport.scrollLeft >= max;
|
||||
}
|
||||
}
|
||||
if (prev) prev.addEventListener('click', function () { viewport.scrollBy({ left: -step(), behavior: 'smooth' }); });
|
||||
if (next) next.addEventListener('click', function () { viewport.scrollBy({ left: step(), behavior: 'smooth' }); });
|
||||
viewport.addEventListener('scroll', update, { passive: true });
|
||||
window.addEventListener('resize', update);
|
||||
update();
|
||||
}
|
||||
document.querySelectorAll('[data-slider]').forEach(init);
|
||||
document.querySelectorAll('[data-slider]').forEach(function (viewport) {
|
||||
var name = viewport.getAttribute('data-slider');
|
||||
var prev = document.querySelector('[data-slider-prev="' + name + '"]');
|
||||
var next = document.querySelector('[data-slider-next="' + name + '"]');
|
||||
if (prev || next) arrows(viewport, prev, next);
|
||||
});
|
||||
arrows(document.querySelector('.vino-bestsellers .vino-slider__viewport'), document.querySelector('.vino-bestsellers [data-slide-prev]'), document.querySelector('.vino-bestsellers [data-slide-next]'));
|
||||
arrows(document.querySelector('.vino-new-arrivals .vino-slider__viewport'), document.querySelector('.vino-new-arrivals [data-arrivals-prev]'), document.querySelector('.vino-new-arrivals [data-arrivals-next]'));
|
||||
})();
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
(function (run) {
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
||||
else run();
|
||||
})(function () {
|
||||
(function () {
|
||||
var m = document.querySelector('[data-sommelier]');
|
||||
if (!m) return;
|
||||
var cfg = {};
|
||||
try { cfg = JSON.parse(document.getElementById('vino-config').textContent); } catch (e) {}
|
||||
var text = cfg.text || {};
|
||||
var body = m.querySelector('[data-sommelier-body]');
|
||||
var form = m.querySelector('[data-sommelier-form]');
|
||||
var done = m.querySelector('[data-sommelier-done]');
|
||||
var err = m.querySelector('[data-sommelier-err]');
|
||||
var guard = form.querySelector('altcha-widget');
|
||||
|
||||
function clear(el) {
|
||||
el.classList.remove('vino-inp--invalid');
|
||||
el.removeAttribute('aria-invalid');
|
||||
}
|
||||
|
||||
function open() {
|
||||
body.hidden = false;
|
||||
done.hidden = true;
|
||||
err.hidden = true;
|
||||
VinoAltcha.arm(guard);
|
||||
var nav = document.querySelector('[data-menu]');
|
||||
if (nav) nav.classList.remove('vino-nav--open');
|
||||
var menuToggle = document.querySelector('[data-menu-toggle]');
|
||||
if (menuToggle) menuToggle.setAttribute('aria-expanded', 'false');
|
||||
form.querySelectorAll('.vino-inp--invalid').forEach(clear);
|
||||
m.showModal();
|
||||
setTimeout(function () {
|
||||
var f = form.querySelector('[name="name"]');
|
||||
if (f) f.focus();
|
||||
}, 30);
|
||||
}
|
||||
|
||||
function close() {
|
||||
m.close();
|
||||
}
|
||||
|
||||
function say(msg) {
|
||||
err.textContent = msg;
|
||||
err.hidden = !msg;
|
||||
}
|
||||
|
||||
function check() {
|
||||
var f = form.elements, first = '', focus = null, mask = 0;
|
||||
function bad(el, msg) {
|
||||
el.classList.add('vino-inp--invalid');
|
||||
el.setAttribute('aria-invalid', 'true');
|
||||
if (!focus) focus = el;
|
||||
if (msg && !first) first = msg;
|
||||
}
|
||||
[f.name, f.phone, f.message, f.consent].forEach(clear);
|
||||
var phone = f.phone.vinoPhone;
|
||||
if (!f.name.value.trim()) { bad(f.name, ''); mask += 1; }
|
||||
if (!phone.filled()) { bad(f.phone, ''); mask += 2; }
|
||||
if (!f.message.value.trim()) { bad(f.message, ''); mask += 4; }
|
||||
if (mask) first = (text.empty || [])[mask] || '';
|
||||
if (phone.filled() && !phone.complete()) bad(f.phone, text.phone);
|
||||
if (!f.consent.checked) bad(f.consent, text.consent);
|
||||
say(first);
|
||||
if (focus) focus.focus();
|
||||
return !focus;
|
||||
}
|
||||
|
||||
document.addEventListener('click', function (e) {
|
||||
var opener = e.target.closest('[data-sommelier-open]');
|
||||
if (opener) {
|
||||
e.preventDefault();
|
||||
open();
|
||||
var note = opener.getAttribute('data-sommelier-note');
|
||||
if (note && !form.elements.message.value.trim()) form.elements.message.value = note;
|
||||
return;
|
||||
}
|
||||
if (m.open && (e.target === m || e.target.closest('[data-sommelier-close]'))) close();
|
||||
});
|
||||
form.addEventListener('input', function (e) { clear(e.target); });
|
||||
form.addEventListener('change', function (e) { clear(e.target); });
|
||||
form.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
if (!check()) return;
|
||||
var b = form.querySelector('[type="submit"]');
|
||||
b.disabled = true;
|
||||
VinoAltcha.armed(guard)
|
||||
.then(function (ok) {
|
||||
if (!ok) throw new Error('guard');
|
||||
var f = form.elements;
|
||||
return BX.ajax.runAction('jinove:vinoteca.lead.question', {
|
||||
data: {
|
||||
name: f.name.value,
|
||||
phone: f.phone.vinoPhone.value(),
|
||||
message: f.message.value,
|
||||
consent: f.consent.checked ? 'Y' : '',
|
||||
website: f.website.value,
|
||||
altcha: f.altcha ? f.altcha.value : '',
|
||||
page: window.location.pathname
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(function (res) {
|
||||
if (res && res.data && res.data.ok) {
|
||||
done.querySelector('[data-sommelier-phone]').textContent = form.elements.phone.value.trim();
|
||||
form.reset();
|
||||
body.hidden = true;
|
||||
done.hidden = false;
|
||||
done.focus();
|
||||
} else {
|
||||
say(text.fail);
|
||||
}
|
||||
})
|
||||
.catch(function () { VinoAltcha.reset(guard); say(text.fail); })
|
||||
.then(function () { b.disabled = false; });
|
||||
});
|
||||
})();
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
(function () {
|
||||
if (!window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var pending = null;
|
||||
window.VinoState = {
|
||||
first: function (name) {
|
||||
if (!pending) {
|
||||
pending = new Promise(function (resolve) {
|
||||
BX.ajax.runAction('jinove:vinoteca.state.all', { method: 'GET' }).then(
|
||||
function (response) { resolve((response && response.data) || {}); },
|
||||
function () { resolve({}); }
|
||||
);
|
||||
});
|
||||
}
|
||||
return pending.then(function (all) {
|
||||
if (!all[name]) throw new Error('state');
|
||||
return { data: all[name] };
|
||||
});
|
||||
}
|
||||
};
|
||||
})();
|
||||
@@ -0,0 +1,86 @@
|
||||
(function (run) {
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
||||
else run();
|
||||
})(function () {
|
||||
(function () {
|
||||
var form = document.querySelector('[data-subscribe]');
|
||||
if (!form || !window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var cfg = {};
|
||||
try { cfg = JSON.parse(document.getElementById('vino-config').textContent); } catch (e) {}
|
||||
var text = cfg.text || {};
|
||||
var done = form.parentNode.querySelector('[data-subscribe-msg]');
|
||||
var err = form.querySelector('[data-subscribe-err]');
|
||||
var guard = form.querySelector('altcha-widget');
|
||||
|
||||
function clear(el) {
|
||||
el.classList.remove('vino-inp--invalid');
|
||||
el.removeAttribute('aria-invalid');
|
||||
}
|
||||
|
||||
function mark(el) {
|
||||
el.classList.add('vino-inp--invalid');
|
||||
el.setAttribute('aria-invalid', 'true');
|
||||
}
|
||||
|
||||
function say(msg) {
|
||||
err.textContent = msg;
|
||||
err.hidden = !msg;
|
||||
}
|
||||
|
||||
form.addEventListener('focusin', function () { VinoAltcha.arm(guard); }, { once: true });
|
||||
form.addEventListener('input', function (e) { clear(e.target); say(''); });
|
||||
form.addEventListener('change', function (e) { clear(e.target); say(''); });
|
||||
|
||||
form.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
var f = form.elements, focus = null, msg = '';
|
||||
[f.email, f.consent].forEach(clear);
|
||||
if (!f.email.value.trim() || !f.email.checkValidity()) {
|
||||
mark(f.email);
|
||||
focus = f.email;
|
||||
msg = form.getAttribute('data-error');
|
||||
}
|
||||
if (!f.consent.checked) {
|
||||
mark(f.consent);
|
||||
if (!focus) focus = f.consent;
|
||||
if (!msg) msg = text.consent;
|
||||
}
|
||||
say(msg);
|
||||
if (focus) {
|
||||
focus.focus();
|
||||
return;
|
||||
}
|
||||
var button = form.querySelector('[type="submit"]');
|
||||
button.disabled = true;
|
||||
VinoAltcha.arm(guard);
|
||||
VinoAltcha.armed(guard)
|
||||
.then(function (verified) {
|
||||
if (!verified) throw new Error('guard');
|
||||
return BX.ajax.runAction('jinove:vinoteca.lead.subscribe', {
|
||||
data: {
|
||||
email: f.email.value,
|
||||
consent: 'Y',
|
||||
website: f.website.value,
|
||||
altcha: f.altcha ? f.altcha.value : '',
|
||||
page: window.location.pathname
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(function (res) {
|
||||
if (res && res.data && res.data.ok) {
|
||||
form.hidden = true;
|
||||
done.textContent = form.getAttribute('data-ok');
|
||||
done.hidden = false;
|
||||
} else {
|
||||
say(text.fail);
|
||||
}
|
||||
})
|
||||
.catch(function (res) {
|
||||
VinoAltcha.reset(guard);
|
||||
var code = res && res.errors && res.errors[0] ? res.errors[0].code : '';
|
||||
say(code === 'email' ? form.getAttribute('data-error') : code === 'consent' ? text.consent : text.fail);
|
||||
})
|
||||
.then(function () { button.disabled = false; });
|
||||
});
|
||||
})();
|
||||
});
|
||||
Reference in new issue
Block a user