2023-05-31 16:42:41 +02:00
|
|
|
const shoppingCarts = new ShoppingCarts();
|
|
|
|
shoppingCarts.load();
|
|
|
|
|
2023-05-19 16:01:43 +02:00
|
|
|
async function load() {
|
2023-05-23 11:21:48 +02:00
|
|
|
const items = await loadItems();
|
|
|
|
const lookup = {};
|
2023-05-19 16:01:43 +02:00
|
|
|
for (item of items) {
|
2023-05-30 20:02:03 +02:00
|
|
|
lookup[item.store + item.id] = item;
|
2023-05-19 16:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let cart = null;
|
2023-05-23 11:21:48 +02:00
|
|
|
const cartName = getQueryParameter("name");
|
2023-05-27 21:44:22 +02:00
|
|
|
if (cartName) {
|
2023-05-29 19:05:02 +02:00
|
|
|
for (c of shoppingCarts.carts) {
|
2023-05-27 21:44:22 +02:00
|
|
|
if (c.name == cartName) {
|
|
|
|
cart = c;
|
|
|
|
break;
|
|
|
|
}
|
2023-05-19 16:01:43 +02:00
|
|
|
}
|
2023-05-27 22:46:59 +02:00
|
|
|
|
|
|
|
// Update cart pricing info
|
|
|
|
let items = [];
|
|
|
|
for (cartItem of cart.items) {
|
2023-05-30 20:02:03 +02:00
|
|
|
const item = lookup[cartItem.store + cartItem.id];
|
2023-05-29 19:05:02 +02:00
|
|
|
if (!item) shoppingCarts.items.push(cartItem);
|
2023-05-27 22:46:59 +02:00
|
|
|
else items.push(item);
|
|
|
|
}
|
|
|
|
cart.items = items;
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.save();
|
2023-05-19 16:01:43 +02:00
|
|
|
}
|
|
|
|
|
2023-05-27 21:44:22 +02:00
|
|
|
const cartDesc = getQueryParameter("cart");
|
|
|
|
if (cartDesc) {
|
|
|
|
let tokens = cartDesc.split(";");
|
|
|
|
cart = {
|
|
|
|
name: tokens[0],
|
|
|
|
items: [],
|
|
|
|
linked: true
|
|
|
|
};
|
|
|
|
for (let i = 1; i < tokens.length; i++) {
|
|
|
|
const item = lookup[tokens[i]];
|
|
|
|
if (item) cart.items.push(item);
|
|
|
|
}
|
|
|
|
let saveButton = document.querySelector("#save");
|
|
|
|
saveButton.classList.remove("hide");
|
|
|
|
saveButton.addEventListener("click", () => {
|
2023-05-29 19:05:02 +02:00
|
|
|
let index = shoppingCarts.carts.findIndex((c) => c.name === cart.name);
|
2023-05-27 21:44:22 +02:00
|
|
|
if (index != -1) {
|
|
|
|
if (confirm("Existierenden Warenkorb '" + cart.name + " überschreiben?")) {
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.carts[index] = cart;
|
2023-05-27 21:44:22 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.carts.push(importedCart);
|
2023-05-27 21:44:22 +02:00
|
|
|
}
|
|
|
|
location.href = "/cart.html?name=" + encodeURIComponent(cart.name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-19 16:01:43 +02:00
|
|
|
if (cart == null) {
|
|
|
|
alert("Warenkorb '" + cartName + "' existiert nicht.");
|
|
|
|
location.href = "carts.html";
|
|
|
|
}
|
|
|
|
|
2023-05-27 22:46:59 +02:00
|
|
|
if (cart.name != "Momentum Eigenmarken Vergleich" && !cart.linked) showSearch(cart, items);
|
|
|
|
|
2023-05-24 16:59:43 +02:00
|
|
|
const canvasDom = document.querySelector("#chart");
|
|
|
|
document.querySelector("#sum").addEventListener("change", () => {
|
2023-05-27 22:46:59 +02:00
|
|
|
showCharts(canvasDom, cart.items);
|
2023-05-24 16:59:43 +02:00
|
|
|
});
|
2023-05-30 20:02:03 +02:00
|
|
|
document.querySelector("#sumchain").addEventListener("change", () => {
|
|
|
|
showCharts(canvasDom, cart.items);
|
|
|
|
});
|
2023-05-30 21:42:47 +02:00
|
|
|
document.querySelector("#todayonly").addEventListener("change", () => {
|
2023-05-30 20:02:03 +02:00
|
|
|
showCharts(canvasDom, cart.items);
|
|
|
|
})
|
2023-05-28 21:26:49 +02:00
|
|
|
const filtersStore = document.querySelector("#filters-store");
|
|
|
|
filtersStore.innerHTML = STORE_KEYS.map(store => `<label><input id="${store}" type="checkbox" checked="true">${stores[store].name}</label>`).join(" ");
|
|
|
|
filtersStore.querySelectorAll("input").forEach(input => {
|
|
|
|
input.addEventListener("change", () => showCart(cart));
|
|
|
|
});
|
2023-05-28 21:37:42 +02:00
|
|
|
document.querySelector("#filter").addEventListener("input", () => showCart(cart));
|
2023-05-28 21:26:49 +02:00
|
|
|
showCart(cart);
|
2023-05-19 16:01:43 +02:00
|
|
|
}
|
|
|
|
|
2023-05-27 22:46:59 +02:00
|
|
|
function filter(cartItems) {
|
|
|
|
const query = document.querySelector("#filter").value.trim();
|
2023-05-28 21:26:49 +02:00
|
|
|
const storeCheckboxes = STORE_KEYS.map(store => document.querySelector(`#${store}`));
|
|
|
|
const checkedStores = STORE_KEYS.filter((store, i) => storeCheckboxes[i].checked)
|
2023-05-27 22:46:59 +02:00
|
|
|
let items = [];
|
|
|
|
if (query.charAt(0) != "!") {
|
|
|
|
for (item of cartItems) {
|
2023-05-28 21:26:49 +02:00
|
|
|
if (!checkedStores.includes(item.store)) continue;
|
2023-05-27 22:46:59 +02:00
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
items = cartItems;
|
|
|
|
}
|
2023-05-28 21:26:49 +02:00
|
|
|
if (query.length >= 3) items = searchItems(items, document.querySelector("#filter").value, checkedStores, false, 0, 10000, false, false);
|
2023-05-27 22:46:59 +02:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showSearch(cart, items) {
|
2023-05-19 16:01:43 +02:00
|
|
|
const searchDom = document.querySelector("#search");
|
|
|
|
searchDom.innerHTML = "";
|
2023-05-23 20:07:26 +02:00
|
|
|
newSearchComponent(searchDom, items, null, (item) => {
|
2023-05-27 22:46:59 +02:00
|
|
|
// This would filter all items in the cart from the search
|
|
|
|
// result.
|
|
|
|
/*for (let i = 0; i < cart.items.length; i++) {
|
2023-05-19 16:01:43 +02:00
|
|
|
const cartItem = cart.items[i];
|
|
|
|
if (cartItem.id == item.id) return false;
|
2023-05-27 22:46:59 +02:00
|
|
|
}*/
|
2023-05-19 16:01:43 +02:00
|
|
|
return true;
|
|
|
|
}, (header) => {
|
|
|
|
header.append(dom("th", ""));
|
|
|
|
return header;
|
|
|
|
}, (item, itemDom) => {
|
|
|
|
const addButton = dom("input");
|
|
|
|
addButton.setAttribute("type", "button");
|
|
|
|
addButton.setAttribute("value", "+");
|
|
|
|
const cell = dom("td", "");
|
|
|
|
cell.appendChild(addButton);
|
|
|
|
itemDom.appendChild(cell);
|
|
|
|
|
|
|
|
addButton.addEventListener("click", () => {
|
|
|
|
cart.items.push(item);
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.save();
|
2023-05-27 22:46:59 +02:00
|
|
|
showCart(cart);
|
2023-05-19 16:01:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return itemDom;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-27 22:46:59 +02:00
|
|
|
function showCharts(canvasDom, items) {
|
2023-05-30 21:42:47 +02:00
|
|
|
const todayOnly = document.querySelector("#todayonly").checked;
|
2023-05-24 16:59:43 +02:00
|
|
|
let itemsToShow = [];
|
2023-05-19 16:01:43 +02:00
|
|
|
|
2023-05-24 16:59:43 +02:00
|
|
|
if (document.querySelector("#sum").checked && items.length > 0) {
|
|
|
|
itemsToShow.push({
|
2023-05-30 20:02:03 +02:00
|
|
|
name: "Preissumme Warenkorb",
|
2023-05-30 21:42:47 +02:00
|
|
|
priceHistory: calculateOverallPriceChanges(items, todayOnly)
|
2023-05-24 16:59:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-30 20:02:03 +02:00
|
|
|
if (document.querySelector("#sumchain").checked && items.length > 0) {
|
|
|
|
const storeCheckboxes = STORE_KEYS.map(store => document.querySelector(`#${store}`));
|
|
|
|
const checkedStores = STORE_KEYS.filter((store, i) => storeCheckboxes[i].checked)
|
|
|
|
|
|
|
|
checkedStores.forEach(store => {
|
|
|
|
const storeItems = items.filter(item => item.store === store);
|
|
|
|
if (storeItems.length > 0) {
|
|
|
|
itemsToShow.push({
|
|
|
|
name: "Preissumme " + store,
|
2023-05-30 21:42:47 +02:00
|
|
|
priceHistory: calculateOverallPriceChanges(storeItems, todayOnly)
|
2023-05-30 20:02:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-27 22:46:59 +02:00
|
|
|
items.forEach((item) => {
|
2023-05-30 21:42:47 +02:00
|
|
|
if (item.chart) itemsToShow.push({ name: item.store + " " + item.name, priceHistory: todayOnly ? [{date: currentDate(), price: item.price}] : item.priceHistory});
|
2023-05-24 16:59:43 +02:00
|
|
|
});
|
|
|
|
|
2023-05-30 21:42:47 +02:00
|
|
|
showChart(canvasDom, itemsToShow, todayOnly ? "bar" : "line"); // document.querySelector("#charttype").value);
|
2023-05-24 16:59:43 +02:00
|
|
|
}
|
|
|
|
|
2023-05-27 22:46:59 +02:00
|
|
|
function showCart(cart) {
|
2023-05-28 21:43:50 +02:00
|
|
|
let link = cart.name + ";"
|
|
|
|
for (cartItem of cart.items) {
|
2023-05-30 20:02:03 +02:00
|
|
|
link += cartItem.store + cartItem.id + ";";
|
2023-05-28 21:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelector("#cartname").innerHTML = "Warenkorb '" + cart.name + `' <a href="cart.html?cart=${link}">Teilen</a>`;
|
2023-05-24 16:59:43 +02:00
|
|
|
const canvasDom = document.querySelector("#chart");
|
2023-05-27 22:46:59 +02:00
|
|
|
let items = filter(cart.items);
|
|
|
|
if (items.length == cart.items.length) {
|
|
|
|
document.querySelector("#numitems").innerText = `${cart.items.length} Artikel`;
|
|
|
|
} else {
|
|
|
|
document.querySelector("#numitems").innerText = `${items.length} / ${cart.items.length} Artikel`;
|
|
|
|
}
|
|
|
|
showCharts(canvasDom, items);
|
2023-05-24 16:59:43 +02:00
|
|
|
|
|
|
|
const itemTable = document.querySelector("#cartitems");
|
|
|
|
itemTable.innerHTML = "";
|
2023-05-26 14:09:15 +02:00
|
|
|
const header = dom("thead", `<tr><th>Kette</th><th>Name</th><th>Menge</th><th>Preis</th><th></th></tr>`);
|
2023-05-19 16:01:43 +02:00
|
|
|
itemTable.append(header);
|
|
|
|
|
2023-05-27 22:46:59 +02:00
|
|
|
items.forEach((cartItem, idx) => {
|
|
|
|
const itemDom = itemToDOM(cartItem)
|
2023-05-26 16:13:15 +02:00
|
|
|
|
|
|
|
const cell = dom("td", "");
|
2023-05-24 16:59:43 +02:00
|
|
|
const showCheckbox = dom("input", "");
|
|
|
|
showCheckbox.setAttribute("type", "checkbox");
|
|
|
|
if (cartItem.chart) showCheckbox.setAttribute("checked", true);
|
|
|
|
itemDom.append(showCheckbox);
|
|
|
|
showCheckbox.addEventListener("change", () => {
|
|
|
|
cartItem.chart = showCheckbox.checked;
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.save();
|
2023-05-27 22:46:59 +02:00
|
|
|
showCharts(canvasDom, cart.items);
|
2023-05-22 16:02:01 +02:00
|
|
|
});
|
2023-05-26 16:13:15 +02:00
|
|
|
cell.append(showCheckbox);
|
2023-05-22 16:02:01 +02:00
|
|
|
|
2023-05-27 21:44:22 +02:00
|
|
|
if (cart.name != "Momentum Eigenmarken Vergleich" && !cart.linked) {
|
2023-05-22 16:02:01 +02:00
|
|
|
const deleteButton = dom("input", "");
|
|
|
|
deleteButton.setAttribute("type", "button");
|
|
|
|
deleteButton.setAttribute("value", "-");
|
|
|
|
itemDom.append(deleteButton);
|
|
|
|
deleteButton.addEventListener("click", () => {
|
|
|
|
cart.items.splice(idx, 1);
|
2023-05-29 19:05:02 +02:00
|
|
|
shoppingCarts.save();
|
2023-05-27 22:46:59 +02:00
|
|
|
showCart(cart)
|
2023-05-22 16:02:01 +02:00
|
|
|
})
|
2023-05-26 16:13:15 +02:00
|
|
|
cell.appendChild(deleteButton);
|
2023-05-30 21:42:47 +02:00
|
|
|
|
|
|
|
const upButton = dom("input", "");
|
|
|
|
upButton.setAttribute("type", "button");
|
|
|
|
upButton.setAttribute("value", "⬆️");
|
|
|
|
itemDom.append(upButton);
|
|
|
|
upButton.addEventListener("click", () => {
|
|
|
|
if (idx == 0) return;
|
|
|
|
let otherItem = cart.items[idx - 1];
|
|
|
|
cart.items[idx - 1] = cartItem;
|
|
|
|
cart.items[idx] = otherItem;
|
|
|
|
shoppingCarts.save();
|
|
|
|
showCart(cart)
|
|
|
|
})
|
|
|
|
cell.appendChild(upButton);
|
|
|
|
|
|
|
|
const downButton = dom("input", "");
|
|
|
|
downButton.setAttribute("type", "button");
|
|
|
|
downButton.setAttribute("value", "⬇️");
|
|
|
|
itemDom.append(downButton);
|
|
|
|
downButton.addEventListener("click", () => {
|
|
|
|
if (idx == cart.items.length - 1) return;
|
|
|
|
let otherItem = cart.items[idx + 1];
|
|
|
|
cart.items[idx + 1] = cartItem;
|
|
|
|
cart.items[idx] = otherItem;
|
|
|
|
shoppingCarts.save();
|
|
|
|
showCart(cart)
|
|
|
|
})
|
|
|
|
cell.appendChild(downButton);
|
2023-05-22 16:02:01 +02:00
|
|
|
}
|
2023-05-26 16:13:15 +02:00
|
|
|
itemDom.append(cell);
|
2023-05-22 16:02:01 +02:00
|
|
|
itemTable.append(itemDom);
|
2023-05-19 16:01:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
load();
|