Compare commits

..

No commits in common. "710080fb899e9f2dbcf056b5be09f1bbf406fae1" and "ec8c086c1454c62956cca493e8720e4b3510d6e0" have entirely different histories.

2 changed files with 33 additions and 42 deletions

71
main.js
View File

@ -30,7 +30,7 @@ function initFoodList() {
function onFoodSearched() { function onFoodSearched() {
const food = foodSearch.value; const food = foodSearch.value;
const foodLC = food.toLowerCase(); const foodLC = food.toLowerCase();
if (food == "") { if (food == "") {
createSearchEntries([]); createSearchEntries([]);
return; return;
@ -114,7 +114,7 @@ async function addMenuItem(food) {
nodeRoot.setAttribute("data-food-name", food.name); nodeRoot.setAttribute("data-food-name", food.name);
itemName.textContent = food.name; itemName.textContent = food.name;
itemKcal.textContent = `${formatValue(nutritions100.kcal)} kcal/100${food.unit}`; itemKcal.textContent = `${nutritions100.kcal} kcal/100${food.unit}`;
itemUnit.textContent = food.unit; itemUnit.textContent = food.unit;
fatAmount.textContent = `${food.fat}g`; fatAmount.textContent = `${food.fat}g`;
@ -163,36 +163,23 @@ async function addMenuItem(food) {
function updateFoodAmount(food, amount, kcalAmount, fatAmount, carbsAmount, sugarAmount, proteinsAmount, alcAmount) { function updateFoodAmount(food, amount, kcalAmount, fatAmount, carbsAmount, sugarAmount, proteinsAmount, alcAmount) {
const {fat, carbs, sugar, proteins, alc, kcal} = calculateKcal(food, amount); const {fat, carbs, sugar, proteins, alc, kcal} = calculateKcal(food, amount);
setAmount(kcalAmount, kcal, "kcal"); kcalAmount.textContent = `${parseInt(kcal)}kcal`;
setAmount(fatAmount, fat, "g"); fatAmount.textContent = `${parseInt(fat)}g`;
setAmount(carbsAmount, carbs, "g"); carbsAmount.textContent = `${parseInt(carbs)}g`;
setAmount(sugarAmount, sugar, "g"); sugarAmount.textContent = `${parseInt(sugar)}g`;
setAmount(proteinsAmount, proteins, "g"); proteinsAmount.textContent = `${parseInt(proteins)}g`;
setAmount(alcAmount, alc, "g"); alcAmount.textContent = `${parseInt(alc)}g`;
console.log(`${food.name} has ${food.kcal}kcal/100${food.unit}`)
updateSummary(); updateSummary();
} }
function setAmount(element, amount, unit) {
element.textContent = `${formatValue(amount)}${unit}`;
element.setAttribute("data-value", amount);
}
function formatValue(value) {
if (value > 0 && value < 1) {
return "<1"
}
return Math.round(value);
}
function calculateKcal(food, amount) { function calculateKcal(food, amount) {
const fat = food.fat / 100 * amount; const fat = Math.round(food.fat / 100 * amount)
const carbs = food.carbs / 100 * amount; const carbs = Math.round(food.carbs / 100 * amount);
const sugar = food.sugar / 100 * amount; const sugar = Math.round(food.sugar / 100 * amount);
const proteins = food.proteins / 100 * amount; const proteins = Math.round(food.proteins / 100 * amount);
const alc = amount * food.alc / 100; const alc = Math.round(amount * food.alc / 100);
const kcal = fat * 9 + carbs * 4 + proteins * 4 + alc * 7; const kcal = Math.round(fat * 9 + carbs * 4 + proteins * 4 + alc * 7);
const score = kcal / amount; const score = kcal / amount;
return {fat, carbs, sugar, proteins, alc, kcal, score} return {fat, carbs, sugar, proteins, alc, kcal, score}
} }
@ -209,20 +196,20 @@ function updateSummary() {
let alc = 0; let alc = 0;
for (let menuItem of menuItems) { for (let menuItem of menuItems) {
energy += parseFloat(menuItem.querySelector(".menu-item-energy").getAttribute("data-value")); energy += parseInt(menuItem.querySelector(".menu-item-energy").textContent);
fat += parseFloat(menuItem.querySelector(".menu-item-fat").getAttribute("data-value")); fat += parseInt(menuItem.querySelector(".menu-item-fat").textContent);
carbs += parseFloat(menuItem.querySelector(".menu-item-carbs").getAttribute("data-value")); carbs += parseInt(menuItem.querySelector(".menu-item-carbs").textContent);
sugar += parseFloat(menuItem.querySelector(".menu-item-sugar").getAttribute("data-value")); sugar += parseInt(menuItem.querySelector(".menu-item-sugar").textContent);
proteins += parseFloat(menuItem.querySelector(".menu-item-proteins").getAttribute("data-value")); proteins += parseInt(menuItem.querySelector(".menu-item-proteins").textContent);
alc += parseFloat(menuItem.querySelector(".menu-item-alc").getAttribute("data-value")); alc += parseInt(menuItem.querySelector(".menu-item-alc").textContent);
} }
summaryEnergy.textContent = `${formatValue(energy)}kcal`; summaryEnergy.textContent = `${energy}kcal`;
summaryFat.textContent = `${formatValue(fat)}g`; summaryFat.textContent = `${fat}g`;
summaryCarbs.textContent = `${formatValue(carbs)}g`; summaryCarbs.textContent = `${carbs}g`;
summarySugar.textContent = `${formatValue(sugar)}g`; summarySugar.textContent = `${sugar}g`;
summaryProteins.textContent = `${formatValue(proteins)}g`; summaryProteins.textContent = `${proteins}g`;
summaryAlc.textContent = `${formatValue(alc)}g`; summaryAlc.textContent = `${alc}g`;
} }
function getScoreColor(density, unit) { function getScoreColor(density, unit) {
@ -236,7 +223,7 @@ function getScoreColor(density, unit) {
return "green"; return "green";
} }
} }
case "ml": { case "ml": {
if (density >= 0.1) { if (density >= 0.1) {
return "red" return "red"
@ -245,4 +232,4 @@ function getScoreColor(density, unit) {
} }
} }
} }
} }

View File

@ -136,6 +136,10 @@ tfoot tr td {
margin: 0 auto; margin: 0 auto;
} }
/* .menu-item:not(:last-child) {
border-bottom: 1px solid lightgray;
} */
.delete-menu-item, .delete-menu-item,
.summary-delete-icon { .summary-delete-icon {
color: #cc3d3d; color: #cc3d3d;