recipeBook.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. window.recipeBookStrandObj = {
  2. isPopulated: false,
  3. recipeDivList: [],
  4. display: function(){
  5. if(!this.isPopulated){
  6. this.populateRecipes();
  7. this.isPopulated = true;
  8. }
  9. },
  10. populateRecipes: function(){
  11. let recipeList = document.querySelector("#recipeList");
  12. while(recipeList.children.length > 0){
  13. recipeList.removeChild(recipeList.firstChild);
  14. }
  15. for(let recipe of merchant.recipes){
  16. let recipeDiv = document.createElement("div");
  17. recipeDiv.classList = "rowItem";
  18. recipeDiv.onclick = ()=>{recipeDetailsComp.display(recipe)};
  19. recipeDiv._name = recipe.name;
  20. recipeList.appendChild(recipeDiv);
  21. let recipeName = document.createElement("p");
  22. recipeName.innerText = recipe.name;
  23. recipeDiv.appendChild(recipeName);
  24. let recipePrice = document.createElement("p");
  25. recipePrice.innerText = `$${(recipe.price / 100).toFixed(2)}`;
  26. recipeDiv.appendChild(recipePrice);
  27. this.recipeDivList.push(recipeDiv);
  28. }
  29. },
  30. search: function(){
  31. let input = document.getElementById("recipeSearch").value.toLowerCase();
  32. let recipeList = document.getElementById("recipeList");
  33. let clearButton = document.getElementById("recipeClearButton");
  34. let matchingRecipes = [];
  35. for(let i = 0; i < this.recipeDivList.length; i++){
  36. if(this.recipeDivList[i]._name.toLowerCase().includes(input)){
  37. matchingRecipes.push(this.recipeDivList[i]);
  38. }
  39. }
  40. while(recipeList.children.length > 0){
  41. recipeList.removeChild(recipeList.firstChild);
  42. }
  43. for(let i = 0; i < matchingRecipes.length; i++){
  44. recipeList.appendChild(matchingRecipes[i]);
  45. }
  46. if(input === ""){
  47. clearButton.style.display = "none";
  48. }else{
  49. clearButton.style.display = "inline";
  50. }
  51. },
  52. clearSorting: function(){
  53. document.getElementById("recipeSearch").value = "";
  54. this.search();
  55. }
  56. }