ingredients.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. module.exports = {
  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 = merchant.categorizeIngredients();
  14. }else if(property === "unit"){
  15. categories = merchant.unitizeIngredients();
  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.ingredient.name;
  34. ingredientDiv.children[2].innerText = `${ingredient.ingredient.convert(ingredient.quantity).toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  35. ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient)};
  36. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  37. ingredientDiv._unit = ingredient.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. document.querySelector("#ingredientSelect").selectedIndex = 0;
  64. if(input === ""){
  65. this.populateByProperty("category");
  66. document.querySelector("#ingredientClearButton").style.display = "none";
  67. return;
  68. }
  69. let matchingIngredients = [];
  70. for(let i = 0; i < this.ingredients.length; i++){
  71. if(this.ingredients[i]._name.includes(input)){
  72. matchingIngredients.push(this.ingredients[i]);
  73. }
  74. }
  75. document.querySelector("#ingredientClearButton").style.display = "inline";
  76. this.displayIngredientsOnly(matchingIngredients);
  77. },
  78. sort: function(sortType){
  79. if(sortType === ""){
  80. return;
  81. }
  82. document.querySelector("#ingredientSearch").value = "";
  83. if(sortType === "category"){
  84. this.populateByProperty("category");
  85. return;
  86. }
  87. if(sortType === "unit"){
  88. this.populateByProperty("unit");
  89. return;
  90. }
  91. document.querySelector("#ingredientClearButton").style.display = "inline";
  92. let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
  93. this.displayIngredientsOnly(sortedIngredients);
  94. },
  95. clearSorting: function(button){
  96. document.querySelector("#ingredientSearch").value = "";
  97. document.querySelector("#ingredientSelect").selectedIndex = 0;
  98. document.querySelector("#ingredientClearButton").style.display = "none";
  99. this.populateByProperty("category");
  100. }
  101. }