recipeBook.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. document.querySelector("#ingredientCount").value = 1;
  46. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  47. let categories = categorizeIngredients();
  48. for(let category of categories){
  49. let optgroup = document.createElement("optgroup");
  50. optgroup.label = category.name;
  51. ingredientsSelect.appendChild(optgroup);
  52. for(let ingredient of category.ingredients){
  53. let option = document.createElement("option");
  54. option.value = ingredient.id;
  55. option.innerText = ingredient.name;
  56. optgroup.appendChild(option);
  57. }
  58. }
  59. },
  60. changeRecipeCount: function(){
  61. let newCount = document.querySelector("#ingredientCount").value;
  62. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  63. let oldCount = ingredientsDiv.children.length;
  64. if(newCount > oldCount){
  65. let newDivs = newCount - oldCount;
  66. for(let i = 0; i < newDivs; i++){
  67. let newNode = ingredientsDiv.children[0].cloneNode(true);
  68. newNode.children[1].children[0].value = "";
  69. ingredientsDiv.appendChild(newNode);
  70. }
  71. }else if(newCount < oldCount){
  72. let newDivs = oldCount - newCount;
  73. for(let i = 0; i < newDivs; i++){
  74. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  75. }
  76. }
  77. }
  78. }