recipeBook.js 2.2 KB

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