recipeBook.js 2.1 KB

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