104 lines
3.3 KiB
JavaScript
104 lines
3.3 KiB
JavaScript
(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);
|
|
});
|
|
})();
|