addIngredients.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. addIngredientsObj = {
  2. isPopulated: false,
  3. rows: [],
  4. displayedRows: [],
  5. currentSort: "",
  6. display: function(){
  7. controller.clearScreen();
  8. controller.addIngredientsStrand.style.display = "flex";
  9. if(!this.isPopulated){
  10. this.createRows();
  11. this.filter();
  12. this.isPopulated = true;
  13. }
  14. },
  15. createRows: function(){
  16. for(let ingredient of ingredients){
  17. let row = document.createElement("tr");
  18. row.id = ingredient._id;
  19. row.sortOptions = {
  20. name: ingredient.name.toLowerCase(),
  21. category: ingredient.category.toLowerCase(),
  22. unit: ingredient.unit.toLowerCase()
  23. };
  24. let add = document.createElement("td");
  25. row.appendChild(add);
  26. let checkbox = document.createElement("input");
  27. checkbox.type = "checkbox";
  28. add.appendChild(checkbox);
  29. let name = document.createElement("td");
  30. name.innerText = ingredient.name;
  31. name.classList = "truncateLong";
  32. row.appendChild(name);
  33. let category = document.createElement("td");
  34. category.innerText = ingredient.category;
  35. row.appendChild(category);
  36. let quantity = document.createElement("td");
  37. row.appendChild(quantity);
  38. let quantityInput = document.createElement("input");
  39. quantityInput.type = "number";
  40. quantityInput.step = "0.01";
  41. quantityInput.min = "0";
  42. quantityInput.classList = "inputField";
  43. quantity.appendChild(quantityInput);
  44. let unit = document.createElement("td");
  45. unit.innerText = ingredient.unit;
  46. row.appendChild(unit);
  47. this.rows.push(row);
  48. }
  49. },
  50. filter: function(){
  51. let searchString = document.querySelector("#filter").value.toLowerCase();
  52. this.displayedRows = [];
  53. for(let row of this.rows){
  54. if(row.sortOptions.name.includes(searchString)){
  55. this.displayedRows.push(row);
  56. }
  57. }
  58. this.currentSort = "";
  59. this.sortIngredients("name");
  60. },
  61. sortIngredients: function(property){
  62. if(this.currentSort === property){
  63. this.displayedRows.sort((a, b)=>(a.sortOptions[property] > b.sortOptions[property]) ? -1 : 1);
  64. this.currentSort = "";
  65. }else{
  66. this.displayedRows.sort((a, b)=>(a.sortOptions[property] > b.sortOptions[property]) ? 1 : -1);
  67. this.currentSort = property;
  68. }
  69. this.populate();
  70. },
  71. populate: function(){
  72. let tbody = document.querySelector("#addIngredientsStrand tbody");
  73. while(tbody.children.length > 0){
  74. tbody.removeChild(tbody.firstChild);
  75. }
  76. for(let row of this.displayedRows){
  77. tbody.appendChild(row);
  78. }
  79. },
  80. submit: function(){
  81. controller.data.inventory = [];
  82. let tbody = document.querySelector("#ingredient-display tbody");
  83. let isValid = true;
  84. for(let row of tbody.children){
  85. if(row.children[0].children[0].checked){
  86. let quantity = row.children[3].children[0].value;
  87. if(validator.ingredient.quantity(quantity)){
  88. controller.data.inventory.push({
  89. ingredient: {
  90. id: row.id,
  91. name: row.children[1].innerText,
  92. category: row.children[2].innerText,
  93. unit: row.children[4].innerText
  94. },
  95. quantity: quantity
  96. });
  97. }else{
  98. isValid = false;
  99. break;
  100. }
  101. }
  102. }
  103. if(isValid){
  104. createIngredientsObj.display();
  105. }
  106. }
  107. }