ingredients.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. window.ingredientsStrandObj = {
  2. isPopulated: false,
  3. ingredients: [],
  4. display: function(){
  5. if(!this.isPopulated){
  6. this.populateByProperty("category");
  7. this.isPopulated = true;
  8. }
  9. },
  10. populateByProperty: function(property){
  11. let categories;
  12. if(property === "category"){
  13. categories = categorizeIngredients(merchant.inventory);
  14. }else if(property === "unit"){
  15. categories = unitizeIngredients(merchant.inventory);
  16. }
  17. let ingredientStrand = document.querySelector("#categoryList");
  18. let categoryTemplate = document.querySelector("#categoryDiv").content.children[0];
  19. let ingredientTemplate = document.querySelector("#ingredient").content.children[0];
  20. this.ingredients = [];
  21. while(ingredientStrand.children.length > 0){
  22. ingredientStrand.removeChild(ingredientStrand.firstChild);
  23. }
  24. for(let i = 0; i < categories.length; i++){
  25. let categoryDiv = categoryTemplate.cloneNode(true);
  26. categoryDiv.children[0].children[0].innerText = categories[i].name;
  27. categoryDiv.children[0].children[1].onclick = ()=>{this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1])};
  28. categoryDiv.children[1].style.display = "none";
  29. ingredientStrand.appendChild(categoryDiv);
  30. for(let j = 0; j < categories[i].ingredients.length; j++){
  31. let ingredient = categories[i].ingredients[j];
  32. let ingredientDiv = ingredientTemplate.cloneNode(true);
  33. ingredientDiv.children[0].innerText = ingredient.name;
  34. ingredientDiv.children[2].innerText = `${ingredient.quantity} ${ingredient.unit}`;
  35. ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient, categories[i])};
  36. ingredientDiv._name = ingredient.name.toLowerCase();
  37. ingredientDiv._unit = ingredient.unit.toLowerCase();
  38. categoryDiv.children[1].appendChild(ingredientDiv);
  39. this.ingredients.push(ingredientDiv);
  40. }
  41. }
  42. },
  43. displayIngredientsOnly: function(ingredients){
  44. let ingredientDiv = document.querySelector("#categoryList");
  45. while(ingredientDiv.children.length > 0){
  46. ingredientDiv.removeChild(ingredientDiv.firstChild);
  47. }
  48. for(let i = 0; i < ingredients.length; i++){
  49. ingredientDiv.appendChild(ingredients[i]);
  50. }
  51. },
  52. toggleCategory: function(div, button){
  53. if(div.style.display === "none"){
  54. button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>';
  55. div.style.display = "flex";
  56. }else if(div.style.display === "flex"){
  57. button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
  58. div.style.display = "none";
  59. }
  60. },
  61. search: function(){
  62. let input = document.querySelector("#ingredientSearch").value.toLowerCase();
  63. if(input === ""){
  64. this.populateByProperty("category");
  65. return;
  66. }
  67. let matchingIngredients = [];
  68. for(let i = 0; i < this.ingredients.length; i++){
  69. if(this.ingredients[i]._name.includes(input)){
  70. matchingIngredients.push(this.ingredients[i]);
  71. }
  72. }
  73. this.displayIngredientsOnly(matchingIngredients);
  74. },
  75. sort: function(sortType){
  76. if(sortType === ""){
  77. return;
  78. }
  79. document.querySelector("#ingredientSearch").value = "";
  80. if(sortType === "category"){
  81. this.populateByProperty("category");
  82. return;
  83. }
  84. if(sortType === "unit"){
  85. this.populateByProperty("unit");
  86. return;
  87. }
  88. let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
  89. this.displayIngredientsOnly(sortedIngredients);
  90. }
  91. }