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