heissepreise/site/main.js

116 lines
3.8 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-16 15:56:46 +02:00
items = await response.json();
2023-05-15 15:18:46 +02:00
for (item of items) {
2023-05-15 15:31:14 +02:00
item.search = item.name + " " + item.unit;
2023-05-16 15:56:46 +02:00
item.search = item.search.toLowerCase();
}
2023-05-15 22:35:36 +02:00
2023-05-15 13:53:34 +02:00
setupUI();
}
function dom(el, html) {
let element = document.createElement(el);
element.innerHTML = html;
return element;
}
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)
hits.push({ doc: 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);
} catch(e) {
return defaultValue;
}
}
2023-05-15 15:18: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-15 22:35:36 +02:00
const spar = document.querySelector("#spar").checked;
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) => {
return a.doc.price - b.doc.price;
})
table.appendChild(dom("tr", `
<th>Kette</th><th>Name</th><th>Menge</th><th>Preis</th>
`));
for (hit of hits) {
const name = hit.doc.name.toLowerCase();
if (hit.doc.store == "billa" && !billa) continue;
if (hit.doc.store == "spar" && !spar) continue;
2023-05-15 22:35:36 +02:00
if (hit.doc.price < minPrice) continue;
if (hit.doc.price > maxPrice) continue;
2023-05-15 13:53:34 +02:00
if (eigenmarken && !(name.indexOf("clever") == 0 || name.indexOf("s-budget") == 0))
continue;
table.appendChild(dom("tr", `
<td>${hit.doc.store}</td>
<td>${hit.doc.name}</td>
<td>${hit.doc.unit ? hit.doc.unit : ""}</td>
2023-05-15 13:53:34 +02:00
<td>${hit.doc.price}</td>
`));
}
}
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));
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();