addIngredients.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. addIngredientsObj = {
  2. isPopulated: false,
  3. display: function(){
  4. controller.clearScreen();
  5. controller.addIngredientsStrand.style.display = "flex";
  6. if(!this.isPopulated){
  7. this.populate();
  8. this.isPopulated = true;
  9. }
  10. },
  11. populate: function(){
  12. let tbody = document.querySelector("#ingredient-display tbody");
  13. for(let ingredient of ingredients){
  14. let row = document.createElement("tr");
  15. row.id = ingredient._id;
  16. tbody.appendChild(row);
  17. let add = document.createElement("td");
  18. row.appendChild(add);
  19. let checkbox = document.createElement("input");
  20. checkbox.type = "checkbox";
  21. add.appendChild(checkbox);
  22. let name = document.createElement("td");
  23. name.innerText = ingredient.name;
  24. name.classList = "truncateLong";
  25. row.appendChild(name);
  26. let category = document.createElement("td");
  27. category.innerText = ingredient.category;
  28. row.appendChild(category);
  29. let quantity = document.createElement("td");
  30. row.appendChild(quantity);
  31. let quantityInput = document.createElement("input");
  32. quantityInput.type = "number";
  33. quantityInput.step = "0.01";
  34. quantityInput.min = "0";
  35. quantityInput.classList = "inputField";
  36. quantity.appendChild(quantityInput);
  37. let unit = document.createElement("td");
  38. unit.innerText = ingredient.unit;
  39. row.appendChild(unit);
  40. }
  41. },
  42. submit: function(){
  43. controller.data.inventory = [];
  44. let tbody = document.querySelector("#ingredient-display tbody");
  45. let isValid = true;
  46. for(let row of tbody.children){
  47. if(row.children[0].children[0].checked){
  48. let quantity = row.children[3].children[0].value;
  49. if(validator.ingredient.quantity(quantity)){
  50. controller.data.inventory.push({
  51. ingredient: {
  52. id: row.id,
  53. name: row.children[1].innerText,
  54. category: row.children[2].innerText,
  55. unit: row.children[4].innerText
  56. },
  57. quantity: quantity
  58. });
  59. }else{
  60. isValid = false;
  61. break;
  62. }
  63. }
  64. }
  65. if(isValid){
  66. createIngredientsObj.display();
  67. }
  68. }
  69. }