ingredients.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. window.ingredientsStrandObj = {
  2. isPopulated: false,
  3. addIngredientsDiv: [],
  4. display: function(){
  5. if(!this.isPopulated){
  6. this.populateIngredients();
  7. this.isPopulated = true;
  8. }
  9. },
  10. populateIngredients: function(){
  11. let categories = categorizeIngredients(merchant.inventory);
  12. let ingredientStrand = document.querySelector("#categoryList");
  13. while(ingredientStrand.children.length > 0){
  14. ingredientStrand.removeChild(ingredientStrand.firstChild);
  15. }
  16. for(let category of categories){
  17. let categoryDiv = document.createElement("div");
  18. categoryDiv.classList = "categoryDiv"
  19. ingredientStrand.appendChild(categoryDiv);
  20. let headerDiv = document.createElement("div");
  21. categoryDiv.appendChild(headerDiv);
  22. let headerTitle = document.createElement("p");
  23. headerTitle.innerText = category.name;
  24. headerDiv.appendChild(headerTitle);
  25. let ingredientsDiv = document.createElement("div");
  26. ingredientsDiv.classList = "ingredientsDiv";
  27. ingredientsDiv.style.display = "none";
  28. categoryDiv.appendChild(ingredientsDiv);
  29. let headerButton = document.createElement("button");
  30. headerButton.innerHTML = '<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>';
  31. headerButton.onclick = ()=>{this.toggleCategory(ingredientsDiv, headerButton)};
  32. headerDiv.appendChild(headerButton);
  33. for(let ingredient of category.ingredients){
  34. let ingredientDiv = document.createElement("div");
  35. ingredientDiv.classList = "ingredient";
  36. ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient, category)};
  37. ingredientsDiv.appendChild(ingredientDiv);
  38. let ingredientName = document.createElement("p");
  39. ingredientName.innerText = ingredient.name;
  40. ingredientDiv.appendChild(ingredientName);
  41. let spacer = document.createElement("hr");
  42. spacer.classList = "ingredientSpacer";
  43. ingredientDiv.appendChild(spacer);
  44. let ingredientData = document.createElement("p");
  45. ingredientData.innerText = `${ingredient.quantity} ${ingredient.unit}`;
  46. ingredientDiv.appendChild(ingredientData);
  47. }
  48. }
  49. },
  50. //Open or close the list of ingredients for a category
  51. toggleCategory: function(div, button){
  52. if(div.style.display === "none"){
  53. button.innerHTML = '<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>';
  54. div.style.display = "flex";
  55. }else if(div.style.display === "flex"){
  56. button.innerHTML = '<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>';
  57. div.style.display = "none";
  58. }
  59. }
  60. }