Vinoteca: модуль, шаблон и компоненты интернет-магазина вина на 1С-Битрикс
This commit is contained in:
commit
bb7d8af290
469 files changed
+26426
No files matched your search
@@ -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);
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user