ingredients.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. let ingredients = {
  2. isPopulated: false,
  3. ingredients: [],
  4. display: function(){
  5. if(!this.isPopulated){
  6. document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
  7. this.populateByProperty();
  8. this.isPopulated = true;
  9. }
  10. },
  11. populateByProperty: function(){
  12. let categories;
  13. categories = merchant.categorizeIngredients();
  14. let ingredientStrand = document.getElementById("categoryList");
  15. let categoryTemplate = document.getElementById("categoryDiv").content.children[0];
  16. let ingredientTemplate = document.getElementById("ingredient").content.children[0];
  17. this.ingredients = [];
  18. while(ingredientStrand.children.length > 0){
  19. ingredientStrand.removeChild(ingredientStrand.firstChild);
  20. }
  21. for(let i = 0; i < categories.length; i++){
  22. let categoryDiv = categoryTemplate.cloneNode(true);
  23. categoryDiv.children[0].children[0].innerText = categories[i].name.toUpperCase();
  24. categoryDiv.children[0].onclick = ()=>{
  25. this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1]);
  26. };
  27. categoryDiv.children[1].style.display = "none";
  28. ingredientStrand.appendChild(categoryDiv);
  29. for(let j = 0; j < categories[i].ingredients.length; j++){
  30. let ingredient = categories[i].ingredients[j];
  31. let ingredientDiv = ingredientTemplate.cloneNode(true);
  32. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  33. ingredientDiv.onclick = ()=>{
  34. controller.openSidebar("ingredientDetails", ingredient);
  35. ingredientDiv.classList.add("active");
  36. };
  37. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  38. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  39. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  40. categoryDiv.children[1].appendChild(ingredientDiv);
  41. this.ingredients.push(ingredientDiv);
  42. }
  43. }
  44. },
  45. displayIngredientsOnly: function(ingredients){
  46. let ingredientDiv = document.getElementById("categoryList");
  47. while(ingredientDiv.children.length > 0){
  48. ingredientDiv.removeChild(ingredientDiv.firstChild);
  49. }
  50. for(let i = 0; i < ingredients.length; i++){
  51. ingredientDiv.appendChild(ingredients[i]);
  52. }
  53. },
  54. toggleCategory: function(div, button){
  55. if(div.style.display === "none"){
  56. button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>';
  57. div.style.display = "flex";
  58. }else if(div.style.display === "flex"){
  59. button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
  60. div.style.display = "none";
  61. }
  62. },
  63. search: function(){
  64. let input = document.getElementById("ingredientSearch").value.toLowerCase();
  65. if(input === ""){
  66. this.populateByProperty();
  67. return;
  68. }
  69. let matchingIngredients = [];
  70. for(let i = 0; i < this.ingredients.length; i++){
  71. if(this.ingredients[i]._name.includes(input)){
  72. matchingIngredients.push(this.ingredients[i]);
  73. }
  74. }
  75. this.displayIngredientsOnly(matchingIngredients);
  76. }
  77. }
  78. module.exports = ingredients;