ingredients.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. let ingredients = {
  2. isPopulated: false,
  3. ingredients: [],
  4. display: function(){
  5. if(!this.isPopulated){
  6. document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
  7. this.populateByProperty();
  8. this.isPopulated = true;
  9. }
  10. },
  11. populateByProperty: function(){
  12. let categories;
  13. categories = merchant.categorizeIngredients();
  14. let ingredientStrand = document.getElementById("categoryList");
  15. let categoryTemplate = document.getElementById("categoryDiv").content.children[0];
  16. let ingredientTemplate = document.getElementById("ingredient").content.children[0];
  17. this.ingredients = [];
  18. while(ingredientStrand.children.length > 0){
  19. ingredientStrand.removeChild(ingredientStrand.firstChild);
  20. }
  21. for(let i = 0; i < categories.length; i++){
  22. let categoryDiv = categoryTemplate.cloneNode(true);
  23. categoryDiv.children[0].children[0].innerText = categories[i].name.toUpperCase();
  24. categoryDiv.children[0].onclick = ()=>{
  25. this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1]);
  26. };
  27. categoryDiv.children[1].style.display = "none";
  28. ingredientStrand.appendChild(categoryDiv);
  29. for(let j = 0; j < categories[i].ingredients.length; j++){
  30. let ingredient = categories[i].ingredients[j];
  31. let ingredientDiv = ingredientTemplate.cloneNode(true);
  32. ingredientDiv.children[0].innerText = ingredient.ingredient.name;
  33. ingredientDiv.onclick = ()=>{
  34. controller.openSidebar("ingredientDetails", ingredient);
  35. ingredientDiv.classList.add("active");
  36. };
  37. ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
  38. ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
  39. if(ingredient.ingredient.specialUnit === "bottle"){
  40. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} BOTTLES`
  41. }else{
  42. ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
  43. }
  44. categoryDiv.children[1].appendChild(ingredientDiv);
  45. this.ingredients.push(ingredientDiv);
  46. }
  47. }
  48. },
  49. displayIngredientsOnly: function(ingredients){
  50. let ingredientDiv = document.getElementById("categoryList");
  51. while(ingredientDiv.children.length > 0){
  52. ingredientDiv.removeChild(ingredientDiv.firstChild);
  53. }
  54. for(let i = 0; i < ingredients.length; i++){
  55. ingredientDiv.appendChild(ingredients[i]);
  56. }
  57. },
  58. toggleCategory: function(div, button){
  59. if(div.style.display === "none"){
  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="18 15 12 9 6 15"></polyline></svg>';
  61. div.style.display = "flex";
  62. }else if(div.style.display === "flex"){
  63. 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>';
  64. div.style.display = "none";
  65. }
  66. },
  67. search: function(){
  68. let input = document.getElementById("ingredientSearch").value.toLowerCase();
  69. if(input === ""){
  70. this.populateByProperty();
  71. return;
  72. }
  73. let matchingIngredients = [];
  74. for(let i = 0; i < this.ingredients.length; i++){
  75. if(this.ingredients[i]._name.includes(input)){
  76. matchingIngredients.push(this.ingredients[i]);
  77. }
  78. }
  79. this.displayIngredientsOnly(matchingIngredients);
  80. }
  81. }
  82. module.exports = ingredients;