ingredient.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. window.ingredientObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(type, ingredient){
  5. clearScreen();
  6. document.querySelector("#ingredientStrand").style.display = "flex";
  7. document.querySelector("strand-selector").setAttribute("strand", "ingredient");
  8. if(!this.isPopulated){
  9. let ingredientsDiv = document.querySelector("#ingredientOptions");
  10. for(let item of data.merchant.inventory){
  11. let label = document.createElement("label");
  12. label.innerText = item.ingredient.name;
  13. ingredientsDiv.appendChild(label);
  14. let checkbox = document.createElement("input");
  15. checkbox.type = "checkbox";
  16. checkbox.onchange = ()=>{
  17. if(checkbox.checked){
  18. this.graph.addData(this.formatData("ingredient", item.ingredient._id));
  19. }else{
  20. this.graph.removeData(item.ingredient._id);
  21. }
  22. };
  23. label.appendChild(checkbox);
  24. }
  25. let startDate = new Date();
  26. startDate.setFullYear(new Date().getFullYear() - 1);
  27. this.graph = new LineGraph(
  28. document.querySelector("#ingredientStrand canvas"),
  29. "Quantity",
  30. "Date",
  31. {
  32. type: "date",
  33. start: startDate,
  34. end: new Date()
  35. }
  36. );
  37. this.isPopulated = true;
  38. }
  39. if(ingredient){
  40. this.graph.clear();
  41. this.graph.addData(this.formatData(type, ingredient.id));
  42. for(let label of document.querySelector("#ingredientOptions").children){
  43. if(label.innerText === ingredient.name){
  44. label.children[0].checked = true;
  45. }else{
  46. label.children[0].checked = false;
  47. }
  48. }
  49. }
  50. },
  51. formatData: function(type, id){
  52. dataList = new Array(365).fill(0);
  53. let today = new Date();
  54. if(type === "ingredient"){
  55. let dataLastDate = new Date(data.transactions[0].date);
  56. let dateRange = Math.floor((Date.UTC(today.getFullYear(), today.getMonth(), today.getDate()) - Date.UTC(dataLastDate.getFullYear(), dataLastDate.getMonth(), dataLastDate.getDate())) / (1000 * 60 * 60 * 24));
  57. for(let transaction of data.transactions){
  58. let transDate = new Date(transaction.date);
  59. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(today.getFullYear(), today.getMonth(), today.getDate())) / (1000 * 60 * 60 * 24));
  60. if(diff <= 0){
  61. for(let recipe of transaction.recipes){
  62. for(let merchRecipe of data.merchant.recipes){
  63. if(merchRecipe._id === recipe.recipe){
  64. for(let ingredient of merchRecipe.ingredients){
  65. if(ingredient.ingredient === id){
  66. dataList[dateRange - Math.abs(diff)] += ingredient.quantity;
  67. }
  68. }
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. return {id: id, set: dataList};
  76. }
  77. }
  78. }