recipe.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. window.recipeObj = {
  2. isPopulated: false,
  3. graph: {},
  4. display: function(recipe){
  5. clearScreen();
  6. document.querySelector("#recipeStrand").style.display = "flex";
  7. document.querySelector("strand-selector").setAttribute("strand", "recipe");
  8. if(!this.isPopulated){
  9. let date = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
  10. document.querySelector("#recipeFrom").valueAsDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12);
  11. document.querySelector("#recipeTo").valueAsDate = new Date();
  12. let recipesDiv = document.querySelector("#recipeOptions");
  13. for(let recipe of data.merchant.recipes){
  14. let checkDiv = document.createElement("div");
  15. checkDiv.classList = "checkboxDiv";
  16. recipesDiv.appendChild(checkDiv);
  17. let checkbox = document.createElement("input");
  18. checkbox.type = "checkbox";
  19. checkbox.id = `${recipe.name}Checkbox`;
  20. checkbox._id = recipe._id;
  21. checkbox.name = recipe.name;
  22. checkbox.onchange = ()=>{
  23. if(checkbox.checked){
  24. let from, to;
  25. [from, to] = getInputDates("recipe");
  26. this.graph.addData(this.formatData(recipe._id, from, to), [from, to], recipe.name);
  27. }else{
  28. this.graph.removeData(recipe.name);
  29. }
  30. };
  31. checkDiv.appendChild(checkbox);
  32. let label = document.createElement("label");
  33. label.innerText = recipe.name;
  34. label.setAttribute("for", `${recipe.name}Checkbox`);
  35. checkDiv.appendChild(label);
  36. }
  37. this.graph = new LineGraph(
  38. document.querySelector("#recipeStrand canvas"),
  39. "Quantity",
  40. "Date"
  41. );
  42. this.isPopulated = true;
  43. }
  44. if(recipe){
  45. this.graph.clearData();
  46. let from, to;
  47. [from, to] = getInputDates("recipe");
  48. this.graph.addData(this.formatData(recipe.id, from, to), [from, to], recipe.name);
  49. for(let label of document.querySelector("#recipeOptions").children){
  50. if(label.innerText === recipe.name){
  51. label.children[0].checked = true;
  52. }else{
  53. label.children[0].checked = false;
  54. }
  55. }
  56. }
  57. },
  58. //TODO This can be made to be faster, no need to search full data list
  59. formatData: function(id, from, to){
  60. let dateRange = Math.floor((Date.UTC(to.getFullYear(), to.getMonth(), to.getDate()) - Date.UTC(from.getFullYear(), from.getMonth(), from.getDate())) / (1000 * 60 * 60 * 24)) + 1;
  61. let dataList = new Array(Math.abs(dateRange)).fill(0);
  62. for(let transaction of data.transactions){
  63. let diff = Math.floor((Date.UTC(transaction.date.getFullYear(), transaction.date.getMonth(), transaction.date.getDate()) - Date.UTC(to.getFullYear(), to.getMonth(), to.getDate())) / (1000 * 60 * 60 * 24));
  64. if(transaction.date > from && diff <= 0){
  65. for(let recipe of transaction.recipes){
  66. if(recipe.recipe === id){
  67. dataList[dateRange - Math.abs(diff) - 1] += recipe.quantity;
  68. }
  69. }
  70. }
  71. }
  72. return dataList;
  73. },
  74. newDates: function(){
  75. let from, to;
  76. [from, to] = getInputDates("recipe");
  77. if(validator.transaction.date(from, to)){
  78. window.fetchData(from, to, ()=>{
  79. this.graph.clearData();
  80. let recipesDiv = document.querySelector("#recipeOptions");
  81. for(let div of recipesDiv.children){
  82. let checkbox = div.children[0];
  83. if(checkbox.checked){
  84. this.graph.addData(this.formatData(checkbox._id, from, to), [from, to], checkbox.name);
  85. }
  86. }
  87. })
  88. }
  89. }
  90. }