ingredient.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.addData(this.formatData("ingredient", ingredient.id));
  41. }
  42. },
  43. formatData: function(type, id){
  44. dataList = new Array(365).fill(0);
  45. let today = new Date();
  46. if(type === "ingredient"){
  47. let dataLastDate = new Date(data.transactions[0].date);
  48. let dateRange = Math.floor((Date.UTC(today.getFullYear(), today.getMonth(), today.getDate()) - Date.UTC(dataLastDate.getFullYear(), dataLastDate.getMonth(), dataLastDate.getDate())) / (1000 * 60 * 60 * 24));
  49. for(let transaction of data.transactions){
  50. let transDate = new Date(transaction.date);
  51. let diff = Math.floor((Date.UTC(transDate.getFullYear(), transDate.getMonth(), transDate.getDate()) - Date.UTC(today.getFullYear(), today.getMonth(), today.getDate())) / (1000 * 60 * 60 * 24));
  52. if(diff <= 0){
  53. for(let recipe of transaction.recipes){
  54. for(let merchRecipe of data.merchant.recipes){
  55. if(merchRecipe._id === recipe.recipe){
  56. for(let ingredient of merchRecipe.ingredients){
  57. if(ingredient.ingredient === id){
  58. dataList[dateRange - Math.abs(diff)] += ingredient.quantity;
  59. }
  60. }
  61. break;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. return {id: id, set: dataList};
  68. }
  69. }
  70. }