LuminaWebsite/assets/js/shop.js
Maaxxs 3c9574c430 Lumishop: categories in their own panel on the left with item counts, modern search bar
- Category list below each other with counts, active category highlighted in gold, sticky while scrolling; on phones a swipeable row
- Search field in the same round style as elsewhere, with a "x of y" count

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 02:30:36 +02:00

217 lines
9.5 KiB
JavaScript

(function () {
'use strict';
function formatPoints(value) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.');
}
// ---- Dialoge ----
function closeModal(modal) { modal.hidden = true; }
document.querySelectorAll('.modal-overlay').forEach(function (modal) {
var close = modal.querySelector('.modal-close');
if (close) close.addEventListener('click', function () { closeModal(modal); });
modal.addEventListener('click', function (event) {
if (event.target === modal) closeModal(modal);
});
});
document.addEventListener('keydown', function (event) {
if (event.key !== 'Escape') return;
document.querySelectorAll('.modal-overlay:not([hidden])').forEach(closeModal);
});
// ---- Shop: Beantragen ----
var buyModal = document.getElementById('shop-buy-modal');
if (buyModal) {
var buyItem = document.getElementById('shop-buy-item');
var buyName = document.getElementById('shop-buy-name');
var buyQuantity = document.getElementById('shop-buy-quantity');
var buyTotal = document.getElementById('shop-buy-total');
var price = 0;
var updateTotal = function () {
var qty = parseInt(buyQuantity.value, 10) || 0;
buyTotal.textContent = formatPoints(Math.max(0, qty) * price);
};
var openBuy = function (button, keepValues) {
buyItem.value = button.dataset.itemId;
buyName.textContent = button.dataset.itemName;
price = parseInt(button.dataset.price, 10) || 0;
buyQuantity.max = button.dataset.max;
if (!keepValues) {
buyQuantity.value = 1;
document.getElementById('shop-buy-note').value = '';
}
updateTotal();
buyModal.hidden = false;
buyQuantity.focus();
buyQuantity.select();
};
document.querySelectorAll('.shop-buy').forEach(function (button) {
button.addEventListener('click', function () { openBuy(button, false); });
});
buyQuantity.addEventListener('input', updateTotal);
// Nach einem Fehler ist der Dialog schon offen - Preis und Hoechstmenge nachladen.
if (!buyModal.hidden) {
var current = document.querySelector('.shop-buy[data-item-id="' + buyItem.value + '"]');
if (current) openBuy(current, true); else buyModal.hidden = true;
}
}
// ---- Shop: Suche und Kategorie ----
var search = document.getElementById('shop-search');
var grid = document.getElementById('shop-items');
if (search && grid) {
var category = document.getElementById('shop-category');
var noMatch = document.getElementById('shop-no-match');
var count = document.getElementById('shop-count');
var total = grid.querySelectorAll('.shop-card').length;
var cat = '';
var filter = function () {
var term = search.value.trim().toLowerCase();
var visible = 0;
grid.querySelectorAll('.shop-card').forEach(function (card) {
var show = (term === '' || card.dataset.name.indexOf(term) !== -1)
&& (cat === '' || card.dataset.category === cat);
card.hidden = !show;
if (show) visible++;
});
if (noMatch) noMatch.hidden = visible > 0;
if (count) {
count.textContent = visible === total
? total + (total === 1 ? ' Gegenstand' : ' Gegenstände')
: visible + ' von ' + total;
}
};
search.addEventListener('input', filter);
// Kategorien im Kasten links: Klick waehlt aus (und markiert den Eintrag)
if (category) {
category.addEventListener('click', function (event) {
var button = event.target.closest('.shop-category');
if (!button) return;
cat = button.dataset.value;
category.querySelectorAll('.shop-category').forEach(function (b) {
var on = b === button;
b.classList.toggle('active', on);
b.setAttribute('aria-pressed', String(on));
});
filter();
});
}
}
// ---- Antraege: Grund abfragen (Ablehnen / Stornieren) ----
document.querySelectorAll('form[data-prompt]').forEach(function (form) {
form.addEventListener('submit', function (event) {
var reason = window.prompt(form.dataset.prompt, '');
if (reason === null) {
event.preventDefault();
return;
}
form.querySelector('input[name="reason"]').value = reason;
});
});
// ---- Antraege: Teilbewilligung - Kosten passend zur eingegebenen Anzahl anzeigen ----
document.querySelectorAll('.shop-approve-quantity').forEach(function (input) {
var form = input.closest('form');
var orderId = form.querySelector('input[name="order_id"]').value;
var price = document.querySelector('[data-order-price="' + orderId + '"]');
var amount = price ? price.lastChild : null;
var unit = parseInt(input.dataset.unitPrice, 10) || 0;
var update = function () {
var qty = Math.min(parseInt(input.value, 10) || 0, parseInt(input.max, 10));
if (amount) amount.textContent = ' ' + formatPoints(Math.max(0, qty) * unit);
};
input.addEventListener('input', update);
update();
});
// ---- Verwaltung: Gegenstand anlegen / bearbeiten ----
var itemModal = document.getElementById('shop-item-modal');
if (itemModal) {
var form = document.getElementById('shop-item-form');
var title = document.getElementById('shop-item-title');
var mode = form.elements.stock_mode;
var applyMode = function () {
itemModal.querySelectorAll('[data-mode-field]').forEach(function (field) {
field.hidden = field.dataset.modeField !== mode.value;
});
var hint = itemModal.querySelector('[data-mode-hint]');
if (hint) hint.textContent = mode.value === 'warehouse' ? '(Pflicht - daraus kommt der Bestand)' : '(optional, für das Bild)';
form.elements.warehouse_item.required = mode.value === 'warehouse';
};
mode.addEventListener('change', applyMode);
var fill = function (data) {
data = data || {};
form.elements.item_id.value = data.id || '';
form.elements.name.value = data.name || '';
form.elements.category_id.value = data.category_id || '';
form.elements.description.value = data.description || '';
form.elements.price.value = data.price !== undefined ? data.price : '';
mode.value = data.stock_mode || 'manual';
form.elements.stock.value = data.stock !== undefined ? data.stock : 0;
form.elements.warehouse_item.value = data.warehouse_item || '';
// Bild in den Auswahlfeldern passend zum eingetragenen Gegenstand
form.querySelectorAll('.item-picker').forEach(function (picker) {
if (picker.itemPicker) picker.itemPicker.refresh();
});
form.elements.active.checked = data.active === undefined ? true : !!data.active;
title.textContent = data.id ? 'Gegenstand bearbeiten' : 'Gegenstand anlegen';
applyMode();
};
document.querySelectorAll('[data-open-modal="shop-item-modal"]').forEach(function (button) {
button.addEventListener('click', function () {
fill(button.dataset.item ? JSON.parse(button.dataset.item) : null);
itemModal.hidden = false;
form.elements.name.focus();
});
});
if (itemModal.dataset.old) {
fill(JSON.parse(itemModal.dataset.old));
} else {
applyMode();
}
}
// ---- Verwaltung: aus dem Lagerhaus uebernehmen ----
var importModal = document.getElementById('shop-import-modal');
if (importModal) {
document.querySelectorAll('[data-open-modal="shop-import-modal"]').forEach(function (button) {
button.addEventListener('click', function () { importModal.hidden = false; });
});
var checks = importModal.querySelectorAll('.shop-import-check');
var all = document.getElementById('shop-import-all');
var priceFor = function (check) {
return importModal.querySelector('input[name="prices[' + check.value + ']"]');
};
// Wer einen Gegenstand auswaehlt, muss einen Preis angeben.
checks.forEach(function (check) {
check.addEventListener('change', function () {
priceFor(check).required = check.checked;
all.checked = Array.prototype.every.call(checks, function (c) { return c.checked; });
});
});
all.addEventListener('change', function () {
checks.forEach(function (check) {
check.checked = all.checked;
priceFor(check).required = all.checked;
});
});
document.getElementById('shop-import-apply').addEventListener('click', function () {
var value = document.getElementById('shop-import-price').value;
if (value === '') return;
checks.forEach(function (check) {
if (check.checked) priceFor(check).value = value;
});
});
}
})();