ingredients.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. window.ingredientsStrandObj = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. let categories = this.categorizeIngredients();
  6. let ingredientStrand = document.querySelector("#ingredientsStrand");
  7. for(let category of categories){
  8. let categoryDiv = document.createElement("div");
  9. categoryDiv.classList = "categoryDiv"
  10. ingredientStrand.appendChild(categoryDiv);
  11. let headerDiv = document.createElement("div");
  12. categoryDiv.appendChild(headerDiv);
  13. let headerTitle = document.createElement("p");
  14. headerTitle.innerText = category.name;
  15. headerDiv.appendChild(headerTitle);
  16. let ingredientsDiv = document.createElement("div");
  17. ingredientsDiv.classList = "ingredientsDiv";
  18. ingredientsDiv.style.display = "none";
  19. categoryDiv.appendChild(ingredientsDiv);
  20. let headerButton = document.createElement("button");
  21. headerButton.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
  22. headerButton.onclick = ()=>{this.toggleCategory(ingredientsDiv, headerButton)};
  23. headerDiv.appendChild(headerButton);
  24. for(let ingredient of category.ingredients){
  25. let ingredientDiv = document.createElement("div");
  26. ingredientDiv.classList = "ingredient";
  27. ingredientsDiv.appendChild(ingredientDiv);
  28. let ingredientName = document.createElement("p");
  29. ingredientName.innerText = ingredient.name;
  30. ingredientDiv.appendChild(ingredientName);
  31. let spacer = document.createElement("p");
  32. spacer.innerText = "-".repeat(25);
  33. ingredientDiv.appendChild(spacer);
  34. let ingredientData = document.createElement("p");
  35. ingredientData.innerText = `${ingredient.quantity} ${ingredient.unit}`;
  36. ingredientDiv.appendChild(ingredientData);
  37. }
  38. }
  39. this.isPopulated = true;
  40. }
  41. },
  42. categorizeIngredients: function(){
  43. let ingredientsByCategory = [];
  44. for(let item of merchant.inventory){
  45. let categoryExists = false;
  46. for(let category of ingredientsByCategory){
  47. if(item.ingredient.category === category.name){
  48. category.ingredients.push({
  49. id: item.ingredient._id,
  50. name: item.ingredient.name,
  51. quantity: item.quantity,
  52. unit: item.ingredient.unit
  53. });
  54. categoryExists = true;
  55. break;
  56. }
  57. }
  58. if(!categoryExists){
  59. ingredientsByCategory.push({
  60. name: item.ingredient.category,
  61. ingredients: [{
  62. id: item.ingredient._id,
  63. name: item.ingredient.name,
  64. quantity: item.quantity,
  65. unit: item.ingredient.unit
  66. }]
  67. });
  68. }
  69. }
  70. return ingredientsByCategory;
  71. },
  72. toggleCategory: function(div, button){
  73. if(div.style.display === "none"){
  74. button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>';
  75. div.style.display = "flex";
  76. }else if(div.style.display === "flex"){
  77. button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
  78. div.style.display = "none";
  79. }
  80. },
  81. toggleAddIngredient: function(){
  82. let sidebar = document.querySelector("#addIngredient");
  83. if(sidebar.classList.value === "sidebarHide"){
  84. sidebar.classList = "sidebar";
  85. }else{
  86. sidebar.classList = "sidebarHide";
  87. }
  88. }
  89. }