ingredients.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. console.log("populate by property");
  12. let categories;
  13. if(property === "category"){
  14. console.log("categorizing");
  15. categories = merchant.categorizeIngredients();
  16. }else if(property === "unit"){
  17. categories = unitizeIngredients(merchant.inventory);
  18. }
  19. let ingredientStrand = document.querySelector("#categoryList");
  20. let categoryTemplate = document.querySelector("#categoryDiv").content.children[0];
  21. let ingredientTemplate = document.querySelector("#ingredient").content.children[0];
  22. this.ingredients = [];
  23. while(ingredientStrand.children.length > 0){
  24. ingredientStrand.removeChild(ingredientStrand.firstChild);
  25. }
  26. for(let i = 0; i < categories.length; i++){
  27. let categoryDiv = categoryTemplate.cloneNode(true);
  28. categoryDiv.children[0].children[0].innerText = categories[i].name;
  29. categoryDiv.children[0].children[1].onclick = ()=>{this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1])};
  30. categoryDiv.children[1].style.display = "none";
  31. ingredientStrand.appendChild(categoryDiv);
  32. for(let j = 0; j < categories[i].ingredients.length; j++){
  33. let ingredient = categories[i].ingredients[j];
  34. let ingredientDiv = ingredientTemplate.cloneNode(true);
  35. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  36. ingredientDiv.children[2].innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  37. ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient, categories[i])};
  38. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  39. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  40. categoryDiv.children[1].appendChild(ingredientDiv);
  41. this.ingredients.push(ingredientDiv);
  42. }
  43. }
  44. console.log("done");
  45. },
  46. displayIngredientsOnly: function(ingredients){
  47. let ingredientDiv = document.querySelector("#categoryList");
  48. while(ingredientDiv.children.length > 0){
  49. ingredientDiv.removeChild(ingredientDiv.firstChild);
  50. }
  51. for(let i = 0; i < ingredients.length; i++){
  52. ingredientDiv.appendChild(ingredients[i]);
  53. }
  54. },
  55. toggleCategory: function(div, button){
  56. if(div.style.display === "none"){
  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="18 15 12 9 6 15"></polyline></svg>';
  58. div.style.display = "flex";
  59. }else if(div.style.display === "flex"){
  60. 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>';
  61. div.style.display = "none";
  62. }
  63. },
  64. search: function(){
  65. let input = document.querySelector("#ingredientSearch").value.toLowerCase();
  66. document.querySelector("#ingredientSelect").selectedIndex = 0;
  67. if(input === ""){
  68. this.populateByProperty("category");
  69. document.querySelector("#ingredientClearButton").style.display = "none";
  70. return;
  71. }
  72. let matchingIngredients = [];
  73. for(let i = 0; i < this.ingredients.length; i++){
  74. if(this.ingredients[i]._name.includes(input)){
  75. matchingIngredients.push(this.ingredients[i]);
  76. }
  77. }
  78. document.querySelector("#ingredientClearButton").style.display = "inline";
  79. this.displayIngredientsOnly(matchingIngredients);
  80. },
  81. sort: function(sortType){
  82. if(sortType === ""){
  83. return;
  84. }
  85. document.querySelector("#ingredientSearch").value = "";
  86. if(sortType === "category"){
  87. this.populateByProperty("category");
  88. return;
  89. }
  90. if(sortType === "unit"){
  91. this.populateByProperty("unit");
  92. return;
  93. }
  94. document.querySelector("#ingredientClearButton").style.display = "inline";
  95. let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
  96. this.displayIngredientsOnly(sortedIngredients);
  97. },
  98. clearSorting: function(button){
  99. document.querySelector("#ingredientSearch").value = "";
  100. document.querySelector("#ingredientSelect").selectedIndex = 0;
  101. document.querySelector("#ingredientClearButton").style.display = "none";
  102. this.populateByProperty("category");
  103. }
  104. }