recipeBook.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. window.recipeBookStrandObj = {
  2. isPopulated: false,
  3. display: function(){
  4. if(!this.isPopulated){
  5. let recipeList = document.querySelector("#recipeList");
  6. for(let recipe of merchant.recipes){
  7. let recipeDiv = document.createElement("div");
  8. recipeDiv.classList = "recipeItem";
  9. recipeDiv.onclick = ()=>{this.displayRecipe(recipe)};
  10. recipeList.appendChild(recipeDiv);
  11. let recipeName = document.createElement("p");
  12. recipeName.innerText = recipe.name;
  13. recipeDiv.appendChild(recipeName);
  14. let recipePrice = document.createElement("p");
  15. recipePrice.innerText = `$${(recipe.price / 100).toFixed(2)}`;
  16. recipeDiv.appendChild(recipePrice);
  17. this.isPopulated = true;
  18. }
  19. }
  20. },
  21. displayRecipe: function(recipe){
  22. closeSidebar();
  23. document.querySelector("#recipeDetails").classList = "sidebar";
  24. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  25. let ingredientList = document.querySelector("#recipeIngredients");
  26. while(ingredientList.children.length > 0){
  27. ingredientList.removeChild(ingredientList.firstChild);
  28. }
  29. for(let ingredient of recipe.ingredients){
  30. let ingredientDiv = document.createElement("div");
  31. ingredientDiv.classList = "recipeIngredient";
  32. ingredientList.appendChild(ingredientDiv);
  33. let ingredientName = document.createElement("p");
  34. ingredientName.innerText = ingredient.ingredient.name;
  35. ingredientDiv.appendChild(ingredientName);
  36. let ingredientQuantity = document.createElement("p");
  37. ingredientQuantity.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  38. ingredientDiv.appendChild(ingredientQuantity);
  39. }
  40. document.querySelector("#recipePrice p").innerText = `$${(recipe.price / 100).toFixed(2)}`;
  41. },
  42. displayAddRecipe: function(){
  43. closeSidebar();
  44. document.querySelector("#addRecipe").classList = "sidebar";
  45. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  46. let categories = categorizeIngredients();
  47. for(let category of categories){
  48. let optgroup = document.createElement("optgroup");
  49. optgroup.label = category.name;
  50. ingredientsSelect.appendChild(optgroup);
  51. for(let ingredient of category.ingredients){
  52. let option = document.createElement("option");
  53. option.value = ingredient.id;
  54. option.innerText = ingredient.name;
  55. optgroup.appendChild(option);
  56. }
  57. }
  58. },
  59. changeRecipeCount: function(){
  60. let newCount = document.querySelector("#ingredientCount").value;
  61. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  62. let oldCount = ingredientsDiv.children.length;
  63. if(newCount > oldCount){
  64. let newDivs = newCount - oldCount;
  65. for(let i = 0; i < newDivs; i++){
  66. let newNode = ingredientsDiv.children[0].cloneNode(true);
  67. newNode.children[2].children[0].value = "";
  68. ingredientsDiv.appendChild(newNode);
  69. }
  70. for(let i = 0; i < newCount; i++){
  71. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  72. }
  73. }else if(newCount < oldCount){
  74. let newDivs = oldCount - newCount;
  75. for(let i = 0; i < newDivs; i++){
  76. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  77. }
  78. }
  79. }
  80. }