heissepreise/site/main.js

118 lines
4.2 KiB
JavaScript
Raw Normal View History

2023-05-15 13:53:34 +02:00
let items = null;
async function load() {
let response = await fetch("api/index")
2023-05-17 16:55:46 +02:00
items = await response.json();
for (item of items) {
2023-05-15 15:31:14 +02:00
item.search = item.name + " " + item.unit;
2023-05-17 16:55:46 +02:00
item.search = item.search.toLowerCase();
}
2023-05-15 13:53:34 +02:00
setupUI();
}
2023-05-15 22:35:36 +02:00
function searchItems(query, exact) {
2023-05-15 13:57:04 +02:00
if (query.length < 3) return [];
2023-05-15 13:53:34 +02:00
const tokens = query.split(/\s+/).map(token => token.toLowerCase());
const hits = [];
for (item of items) {
let allFound = true;
for (token of tokens) {
2023-05-15 22:35:36 +02:00
if (token.length == 0) continue;
const index = item.search.indexOf(token);
if (index < 0) {
2023-05-15 13:53:34 +02:00
allFound = false;
break;
}
2023-05-15 22:35:36 +02:00
if (exact) {
if (index > 0 && (item.search.charAt(index - 1) != " " && item.search.charAt(index - 1) != "-")) {
allFound = false;
break;
}
if (index + token.length < item.search.length && item.search.charAt(index + token.length) != " ") {
allFound = false;
break;
}
}
2023-05-15 13:53:34 +02:00
}
2023-05-15 14:47:38 +02:00
if (allFound)
2023-05-17 16:55:46 +02:00
hits.push(item);
2023-05-15 13:53:34 +02:00
}
return hits;
}
2023-05-15 22:35:36 +02:00
function toNumber(value, defaultValue) {
try {
return Number.parseFloat(value);
2023-05-17 16:55:46 +02:00
} catch (e) {
2023-05-15 22:35:36 +02:00
return defaultValue;
}
}
2023-05-17 16:55:46 +02:00
function search(query) {
2023-05-15 22:35:36 +02:00
const exact = document.querySelector("#exact").checked;
const hits = searchItems(query, exact);
2023-05-15 13:53:34 +02:00
const table = document.querySelector("#result");
const eigenmarken = document.querySelector("#eigenmarken").checked;
const billa = document.querySelector("#billa").checked;
2023-05-17 16:55:46 +02:00
const spar = document.querySelector("#spar").checked;
const hofer = document.querySelector("#hofer").checked;
2023-05-15 22:35:36 +02:00
const minPrice = toNumber(document.querySelector("#minprice").value, 0);
const maxPrice = toNumber(document.querySelector("#maxprice").value, 100);
2023-05-15 13:53:34 +02:00
table.innerHTML = "";
2023-05-15 14:41:02 +02:00
if (hits.length == 0) return;
2023-05-15 13:53:34 +02:00
hits.sort((a, b) => {
2023-05-17 16:55:46 +02:00
return a.price - b.price;
2023-05-15 13:53:34 +02:00
})
table.appendChild(dom("tr", `
<th>Kette</th><th>Name</th><th>Menge</th><th>Preis</th>
`));
2023-05-18 15:56:26 +02:00
const genLink = (store, name) => {
if (store == "spar")
return `<a target="_blank" href="https://www.interspar.at/shop/lebensmittel/search/?q=${encodeURIComponent(name)}">${name}</a>`;
if (store == "billa")
return `<a target="_blank" href="https://shop.billa.at/search/results?category=&searchTerm=${encodeURIComponent(name)}">${name}</a>`;
if (store == "hofer")
return `<a target="_blank" href="https://www.roksh.at/hofer/angebot/suche/${encodeURIComponent(name)}">${name}</a>`;
return name;
}
2023-05-17 18:33:43 +02:00
hits.forEach(hit => {
2023-05-17 16:55:46 +02:00
const name = hit.name.toLowerCase();
2023-05-17 18:33:43 +02:00
if (hit.store == "billa" && !billa) return;
if (hit.store == "spar" && !spar) return;
if (hit.store == "hofer" && !hofer) return;
2023-05-17 18:33:43 +02:00
if (hit.price < minPrice) return;
if (hit.price > maxPrice) return;
2023-05-15 13:53:34 +02:00
if (eigenmarken && !(name.indexOf("clever") == 0 || name.indexOf("s-budget") == 0))
2023-05-17 18:33:43 +02:00
return;
2023-05-15 13:53:34 +02:00
table.appendChild(itemToDOM(hit));
2023-05-17 18:33:43 +02:00
});
2023-05-15 13:53:34 +02:00
}
function setupUI() {
2023-05-15 22:35:36 +02:00
let searchInput = document.querySelector("#search");
2023-05-15 13:53:34 +02:00
searchInput.addEventListener("input", (event) => {
2023-05-15 22:35:36 +02:00
if (searchInput.value.length == 0) {
document.querySelector("#minprice").value = 0;
document.querySelector("#maxprice").value = 100;
}
2023-05-15 13:53:34 +02:00
search(searchInput.value);
});
document.querySelector("#eigenmarken").addEventListener("change", () => search(searchInput.value));
document.querySelector("#billa").addEventListener("change", () => search(searchInput.value));
document.querySelector("#spar").addEventListener("change", () => search(searchInput.value));
document.querySelector("#hofer").addEventListener("change", () => search(searchInput.value));
2023-05-15 22:35:36 +02:00
document.querySelector("#exact").addEventListener("change", () => search(searchInput.value));
document.querySelector("#minprice").addEventListener("change", () => search(searchInput.value));
document.querySelector("#maxprice").addEventListener("change", () => search(searchInput.value));
2023-05-15 13:53:34 +02:00
}
load();