Vinoteca: модуль, шаблон и компоненты интернет-магазина вина на 1С-Битрикс
This commit is contained in:
commit
f6ba0d2174
470 files changed
+26454
No files matched your search
@@ -0,0 +1,387 @@
|
||||
(function () {
|
||||
var root = document.querySelector('[data-cart]');
|
||||
if (!root || !window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var max = parseInt(root.getAttribute('data-max'), 10);
|
||||
var layout = root.querySelector('[data-cart-layout]');
|
||||
var empty = root.querySelector('[data-cart-empty]');
|
||||
var emptyTitle = root.querySelector('[data-cart-empty-title]');
|
||||
var fail = root.querySelector('[data-cart-fail]');
|
||||
var confirmBox = root.querySelector('[data-cart-confirm]');
|
||||
var clearButton = root.querySelector('[data-cart-clear]');
|
||||
var actions = root.querySelector('[data-cart-actions]');
|
||||
var shareButton = root.querySelector('[data-cart-share]');
|
||||
var shareStatus = root.querySelector('[data-cart-share-status]');
|
||||
var shareFallback = root.querySelector('[data-cart-share-fallback]');
|
||||
var couponForm = root.querySelector('[data-cart-coupon]');
|
||||
var couponInput = couponForm.elements.code;
|
||||
var couponError = root.querySelector('[data-cart-coupon-error]');
|
||||
var couponList = root.querySelector('[data-cart-coupons]');
|
||||
var couponRowBox = couponForm.querySelector('.vino-cart__coupon-row');
|
||||
var couponMore = couponForm.querySelector('[data-cart-coupon-more]');
|
||||
var couponHint = couponForm.querySelector('[data-cart-coupon-hint]');
|
||||
var couponExpanded = false;
|
||||
var timers = {};
|
||||
var queue = Promise.resolve();
|
||||
var version = 0;
|
||||
var inflight = 0;
|
||||
var clearing = false;
|
||||
var shareTimer = 0;
|
||||
|
||||
function noop() {}
|
||||
|
||||
function text(name) {
|
||||
return root.getAttribute('data-text-' + name) || '';
|
||||
}
|
||||
|
||||
function item(id) {
|
||||
return root.querySelector('[data-cart-item="' + id + '"]');
|
||||
}
|
||||
|
||||
function field(li, name) {
|
||||
return li.querySelector('[data-cart-' + name + ']');
|
||||
}
|
||||
|
||||
function showFail(on) {
|
||||
fail.textContent = on ? text('fail') : '';
|
||||
fail.hidden = !on;
|
||||
}
|
||||
|
||||
function couponRow(code) {
|
||||
var li = document.createElement('li');
|
||||
li.className = 'vino-cart__coupon-item';
|
||||
var span = document.createElement('span');
|
||||
span.className = 'vino-cart__coupon-code';
|
||||
span.textContent = code;
|
||||
var button = document.createElement('button');
|
||||
button.type = 'button';
|
||||
button.className = 'vino-cart__coupon-remove';
|
||||
button.setAttribute('data-cart-coupon-remove', code);
|
||||
button.setAttribute('aria-label', text('coupon-remove').replace('#CODE#', code));
|
||||
button.innerHTML = '<svg class="vino-cart__remove-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"></path></svg>';
|
||||
li.appendChild(span);
|
||||
li.appendChild(button);
|
||||
return li;
|
||||
}
|
||||
|
||||
function lock(li, on) {
|
||||
var removed = field(li, 'removed');
|
||||
Array.prototype.forEach.call(li.children, function (child) {
|
||||
if (child !== removed) child.inert = on;
|
||||
});
|
||||
li.classList.toggle('vino-cart__item--removed', on);
|
||||
removed.hidden = !on;
|
||||
}
|
||||
|
||||
function patch(cart) {
|
||||
var seen = {};
|
||||
cart.items.forEach(function (data) {
|
||||
var li = item(data.id);
|
||||
if (!li) return;
|
||||
seen[data.id] = true;
|
||||
if (!timers[data.id]) {
|
||||
if (li.classList.contains('vino-cart__item--removed')) lock(li, false);
|
||||
field(li, 'value').textContent = data.quantity;
|
||||
}
|
||||
li.classList.toggle('vino-cart__item--unavailable', !data.available);
|
||||
field(li, 'unavailable').hidden = data.available;
|
||||
field(li, 'price').textContent = data.price;
|
||||
var old = field(li, 'old');
|
||||
old.textContent = data.basePrice;
|
||||
old.hidden = !data.basePrice;
|
||||
field(li, 'sum').textContent = data.sum;
|
||||
});
|
||||
root.querySelectorAll('[data-cart-item]').forEach(function (li) {
|
||||
var id = li.getAttribute('data-cart-item');
|
||||
var removed = li.classList.contains('vino-cart__item--removed');
|
||||
if (!seen[id] && !timers[id] && (clearing || !removed)) li.remove();
|
||||
});
|
||||
root.querySelector('[data-cart-count-text]').textContent = cart.summary;
|
||||
root.querySelector('[data-cart-base]').textContent = cart.base;
|
||||
root.querySelector('[data-cart-total]').textContent = cart.total;
|
||||
root.querySelector('[data-cart-discount]').textContent = cart.discount || '—';
|
||||
root.querySelector('[data-cart-discount-line]').classList.toggle('vino-cart__line--none', !cart.discount);
|
||||
couponList.textContent = '';
|
||||
var applied = 0;
|
||||
cart.coupons.forEach(function (coupon) {
|
||||
if (!coupon.applied) return;
|
||||
couponList.appendChild(couponRow(coupon.code));
|
||||
applied++;
|
||||
});
|
||||
if (!applied) couponExpanded = false;
|
||||
couponRowBox.hidden = applied > 0 && !couponExpanded;
|
||||
couponHint.hidden = couponRowBox.hidden;
|
||||
couponMore.hidden = applied === 0 || couponExpanded;
|
||||
var left = root.querySelectorAll('[data-cart-item]').length;
|
||||
layout.hidden = !left;
|
||||
empty.hidden = !!left;
|
||||
}
|
||||
|
||||
function apply(res, sent) {
|
||||
var data = res && res.data;
|
||||
if (!data || !data.cart || sent !== version) return;
|
||||
if (data.state) document.dispatchEvent(new CustomEvent('vino:basket', { detail: data.state }));
|
||||
showFail(!data.ok);
|
||||
patch(data.cart);
|
||||
}
|
||||
|
||||
function recover(sent) {
|
||||
if (sent !== version) return;
|
||||
showFail(true);
|
||||
BX.ajax.runAction('jinove:vinoteca.basket.cart', { method: 'GET' }).then(function (res) {
|
||||
if (sent === version && res.data && res.data.cart) {
|
||||
if (res.data.state) document.dispatchEvent(new CustomEvent('vino:basket', { detail: res.data.state }));
|
||||
patch(res.data.cart);
|
||||
}
|
||||
}, noop);
|
||||
}
|
||||
|
||||
function busy() {
|
||||
root.classList.toggle('vino-cart--busy', inflight > 0 || Object.keys(timers).length > 0);
|
||||
}
|
||||
|
||||
function request(action, data) {
|
||||
var run = function () {
|
||||
return BX.ajax.runAction('jinove:vinoteca.basket.' + action, { data: data || {} });
|
||||
};
|
||||
inflight++;
|
||||
busy();
|
||||
var result = queue.then(run);
|
||||
queue = result.then(noop, noop);
|
||||
queue.then(function () {
|
||||
inflight--;
|
||||
busy();
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
function call(action, data) {
|
||||
version++;
|
||||
var sent = version;
|
||||
return request(action, data).then(function (res) {
|
||||
apply(res, sent);
|
||||
return res;
|
||||
}, function (res) {
|
||||
recover(sent);
|
||||
throw res;
|
||||
});
|
||||
}
|
||||
|
||||
function send(id, qty) {
|
||||
clearTimeout(timers[id]);
|
||||
timers[id] = setTimeout(function () {
|
||||
delete timers[id];
|
||||
call('update', { id: id, qty: qty }).then(noop, noop);
|
||||
}, 350);
|
||||
busy();
|
||||
}
|
||||
|
||||
function cancel(id) {
|
||||
clearTimeout(timers[id]);
|
||||
delete timers[id];
|
||||
busy();
|
||||
}
|
||||
|
||||
function step(li, delta) {
|
||||
var id = li.getAttribute('data-cart-item');
|
||||
var value = field(li, 'value');
|
||||
var qty = Math.max(1, Math.min(max, (parseInt(value.textContent, 10) || 1) + delta));
|
||||
if (String(qty) === value.textContent) return;
|
||||
value.textContent = qty;
|
||||
send(id, qty);
|
||||
}
|
||||
|
||||
function remove(li) {
|
||||
var id = li.getAttribute('data-cart-item');
|
||||
li.setAttribute('data-cart-was', field(li, 'value').textContent);
|
||||
cancel(id);
|
||||
lock(li, true);
|
||||
field(li, 'restore').focus();
|
||||
call('update', { id: id, qty: 0 }).then(noop, noop);
|
||||
}
|
||||
|
||||
function restore(li) {
|
||||
var id = li.getAttribute('data-cart-item');
|
||||
var qty = parseInt(li.getAttribute('data-cart-was'), 10) || 1;
|
||||
cancel(id);
|
||||
lock(li, false);
|
||||
field(li, 'value').textContent = qty;
|
||||
field(li, 'remove').focus();
|
||||
call('update', { id: id, qty: qty }).then(noop, noop);
|
||||
}
|
||||
|
||||
function flush() {
|
||||
Object.keys(timers).forEach(function (id) {
|
||||
var li = item(id);
|
||||
cancel(id);
|
||||
if (li) call('update', { id: id, qty: parseInt(field(li, 'value').textContent, 10) || 1 }).then(noop, noop);
|
||||
});
|
||||
}
|
||||
|
||||
function checkout(link, e) {
|
||||
if (!root.classList.contains('vino-cart--busy')) return;
|
||||
e.preventDefault();
|
||||
if (link.getAttribute('aria-busy') === 'true') return;
|
||||
link.setAttribute('aria-busy', 'true');
|
||||
flush();
|
||||
queue.then(function () {
|
||||
link.removeAttribute('aria-busy');
|
||||
if (fail.hidden) location.href = link.href;
|
||||
});
|
||||
}
|
||||
|
||||
function toggleConfirm(on) {
|
||||
confirmBox.hidden = !on;
|
||||
actions.hidden = on;
|
||||
(on ? root.querySelector('[data-cart-clear-no]') : clearButton).focus();
|
||||
}
|
||||
|
||||
function clearCart() {
|
||||
confirmBox.hidden = true;
|
||||
actions.hidden = false;
|
||||
Object.keys(timers).forEach(cancel);
|
||||
clearing = true;
|
||||
layout.hidden = true;
|
||||
empty.hidden = false;
|
||||
emptyTitle.focus();
|
||||
call('clear').then(noop, noop).then(function () { clearing = false; });
|
||||
}
|
||||
|
||||
function shareLink() {
|
||||
var pairs = [];
|
||||
root.querySelectorAll('[data-cart-item]:not(.vino-cart__item--removed)').forEach(function (li) {
|
||||
pairs.push(li.getAttribute('data-cart-item') + '-' + (parseInt(field(li, 'value').textContent, 10) || 1));
|
||||
});
|
||||
return location.origin + location.pathname + '?share=' + pairs.join('.');
|
||||
}
|
||||
|
||||
function copied() {
|
||||
var done = root.querySelector('[data-cart-share-done]').textContent;
|
||||
shareButton.classList.add('vino-cart__share--done');
|
||||
shareStatus.textContent = done;
|
||||
clearTimeout(shareTimer);
|
||||
shareTimer = setTimeout(function () {
|
||||
shareButton.classList.remove('vino-cart__share--done');
|
||||
shareStatus.textContent = '';
|
||||
}, 2500);
|
||||
}
|
||||
|
||||
function showLink(link) {
|
||||
var input = shareFallback.querySelector('[data-cart-share-input]');
|
||||
input.value = link;
|
||||
shareFallback.hidden = false;
|
||||
input.focus();
|
||||
input.select();
|
||||
}
|
||||
|
||||
function share() {
|
||||
var link = shareLink();
|
||||
shareFallback.hidden = true;
|
||||
if (navigator.share && window.matchMedia('(pointer: coarse)').matches) {
|
||||
navigator.share({ title: shareButton.getAttribute('data-title'), url: link }).catch(noop);
|
||||
return;
|
||||
}
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
navigator.clipboard.writeText(link).then(copied, function () { showLink(link); });
|
||||
return;
|
||||
}
|
||||
showLink(link);
|
||||
}
|
||||
|
||||
function addShared(button) {
|
||||
var box = button.closest('[data-cart-shared]');
|
||||
button.disabled = true;
|
||||
BX.ajax.runAction('jinove:vinoteca.basket.addShared', { data: { code: box.getAttribute('data-cart-shared') } }).then(function () {
|
||||
location.replace(box.getAttribute('data-cart-url'));
|
||||
}, function () {
|
||||
button.disabled = false;
|
||||
showFail(true);
|
||||
});
|
||||
}
|
||||
|
||||
function expandCoupon() {
|
||||
couponExpanded = true;
|
||||
couponRowBox.hidden = false;
|
||||
couponMore.hidden = true;
|
||||
couponInput.focus();
|
||||
}
|
||||
|
||||
function couponFail(message) {
|
||||
couponError.textContent = message;
|
||||
couponError.hidden = !message;
|
||||
couponForm.classList.toggle('vino-cart__coupon--error', !!message);
|
||||
couponInput.setAttribute('aria-invalid', message ? 'true' : 'false');
|
||||
}
|
||||
|
||||
root.addEventListener('click', function (e) {
|
||||
var target = e.target.closest('button');
|
||||
if (!target || !root.contains(target)) return;
|
||||
var li = target.closest('[data-cart-item]');
|
||||
if (target.hasAttribute('data-cart-step') && li) step(li, parseInt(target.getAttribute('data-cart-step'), 10));
|
||||
else if (target.hasAttribute('data-cart-remove') && li) remove(li);
|
||||
else if (target.hasAttribute('data-cart-restore') && li) restore(li);
|
||||
else if (target.hasAttribute('data-cart-share')) share();
|
||||
else if (target.hasAttribute('data-cart-shared-add')) addShared(target);
|
||||
else if (target.hasAttribute('data-cart-coupon-more')) expandCoupon();
|
||||
else if (target.hasAttribute('data-cart-clear')) toggleConfirm(true);
|
||||
else if (target.hasAttribute('data-cart-clear-no')) toggleConfirm(false);
|
||||
else if (target.hasAttribute('data-cart-clear-yes')) clearCart();
|
||||
else if (target.hasAttribute('data-cart-coupon-remove')) {
|
||||
call('couponRemove', { code: target.getAttribute('data-cart-coupon-remove') }).then(function () { couponInput.focus(); }, noop);
|
||||
}
|
||||
});
|
||||
|
||||
root.addEventListener('click', function (e) {
|
||||
var link = e.target.closest('[data-cart-checkout]');
|
||||
if (link) checkout(link, e);
|
||||
});
|
||||
|
||||
root.addEventListener('keydown', function (e) {
|
||||
if (e.key !== 'Escape') return;
|
||||
if (!confirmBox.hidden) toggleConfirm(false);
|
||||
else if (!shareFallback.hidden) {
|
||||
shareFallback.hidden = true;
|
||||
shareButton.focus();
|
||||
}
|
||||
});
|
||||
|
||||
couponForm.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
var code = couponInput.value.trim();
|
||||
if (!code) {
|
||||
couponFail(text('coupon-empty'));
|
||||
couponInput.focus();
|
||||
return;
|
||||
}
|
||||
couponFail('');
|
||||
var button = couponForm.querySelector('[type="submit"]');
|
||||
button.disabled = true;
|
||||
version++;
|
||||
var sent = version;
|
||||
request('coupon', { code: code }).then(function (res) {
|
||||
button.disabled = false;
|
||||
couponInput.value = '';
|
||||
couponExpanded = false;
|
||||
apply(res, sent);
|
||||
var chip = couponList.querySelector('button');
|
||||
if (chip) chip.focus();
|
||||
}, function (res) {
|
||||
var error = res && res.errors && res.errors[0];
|
||||
button.disabled = false;
|
||||
if (error && error.code === 'coupon') couponFail(text('coupon-error'));
|
||||
else showFail(true);
|
||||
couponInput.focus();
|
||||
});
|
||||
});
|
||||
|
||||
couponHint.addEventListener('click', function (e) {
|
||||
var fill = e.target.closest('[data-cart-coupon-fill]');
|
||||
if (!fill) return;
|
||||
couponInput.value = fill.getAttribute('data-cart-coupon-fill');
|
||||
couponInput.dispatchEvent(new Event('input'));
|
||||
couponInput.focus();
|
||||
});
|
||||
|
||||
couponInput.addEventListener('input', function () {
|
||||
if (!couponError.hidden) couponFail('');
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user