dashboard.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. const home = require("./strands/home.js");
  2. const ingredients = require("./strands/ingredients.js");
  3. const recipeBook = require("./strands/recipeBook.js");
  4. const analytics = require("./strands/analytics.js");
  5. const orders = require("./strands/orders.js");
  6. const transactions = require("./strands/transactions.js");
  7. const account = require("./strands/account.js");
  8. const ingredientDetails = require("./sidebars/ingredientDetails.js");
  9. const newIngredient = require("./sidebars/newIngredient.js");
  10. const editIngredient = require("./sidebars/editIngredient.js");
  11. const newOrder = require("./sidebars/newOrder.js");
  12. const newRecipe = require("./sidebars/newRecipe.js");
  13. const editRecipe = require("./sidebars/editRecipe.js");
  14. const newTransaction = require("./sidebars/newTransaction.js");
  15. const orderDetails = require("./sidebars/orderDetails.js");
  16. const orderFilter = require("./sidebars/orderFilter.js");
  17. const recipeDetails = require("./sidebars/recipeDetails.js");
  18. const transactionDetails = require("./sidebars/transactionDetails.js");
  19. const transactionFilter = require("./sidebars/transactionFilter.js");
  20. const Merchant = require("./classes/Merchant.js");
  21. merchant = new Merchant(data.merchant, data.transactions);
  22. controller = {
  23. openStrand: function(strand, data = undefined){
  24. this.closeSidebar();
  25. let strands = document.querySelectorAll(".strand");
  26. for(let i = 0; i < strands.length; i++){
  27. strands[i].style.display = "none";
  28. }
  29. let buttons = document.querySelectorAll(".menuButton");
  30. for(let i = 0; i < buttons.length - 1; i++){
  31. buttons[i].classList = "menuButton";
  32. buttons[i].disabled = false;
  33. }
  34. let activeButton = {};
  35. switch(strand){
  36. case "home":
  37. activeButton = document.getElementById("homeBtn");
  38. document.getElementById("homeStrand").style.display = "flex";
  39. home.display();
  40. break;
  41. case "ingredients":
  42. activeButton = document.getElementById("ingredientsBtn");
  43. document.getElementById("ingredientsStrand").style.display = "flex";
  44. ingredients.display();
  45. break;
  46. case "recipeBook":
  47. activeButton = document.getElementById("recipeBookBtn");
  48. document.getElementById("recipeBookStrand").style.display = "flex";
  49. recipeBook.display();
  50. break;
  51. case "analytics":
  52. activeButton = document.getElementById("analyticsBtn");
  53. document.getElementById("analyticsStrand").style.display = "flex";
  54. analytics.display();
  55. break;
  56. case "orders":
  57. activeButton = document.getElementById("ordersBtn");
  58. document.getElementById("ordersStrand").style.display = "flex";
  59. orders.display();
  60. break;
  61. case "transactions":
  62. activeButton = document.getElementById("transactionsBtn");
  63. document.getElementById("transactionsStrand").style.display = "flex";
  64. transactions.transactions = data;
  65. transactions.display();
  66. break;
  67. case "account":
  68. activeButton = document.getElementById("accountBtn");
  69. document.getElementById("accountStrand").style.display = "flex";
  70. account.display();
  71. break;
  72. }
  73. activeButton.classList = "menuButton active";
  74. activeButton.disabled = true;
  75. if(screen.height > screen.width || screen.width < 1200){
  76. this.closeMenu();
  77. }
  78. },
  79. /*
  80. Open a specific sidebar
  81. Input:
  82. sidebar: the outermost element of the sidebar (must contain class sidebar)
  83. */
  84. openSidebar: function(sidebar, data = {}){
  85. this.closeSidebar();
  86. document.getElementById("sidebarDiv").classList = "sidebar";
  87. document.getElementById(sidebar).style.display = "flex";
  88. switch(sidebar){
  89. case "ingredientDetails":
  90. ingredientDetails.display(data);
  91. break;
  92. case "newIngredient":
  93. newIngredient.display();
  94. break;
  95. case "editIngredient":
  96. editIngredient.display(data);
  97. break;
  98. case "recipeDetails":
  99. recipeDetails.display(data);
  100. break;
  101. case "editRecipe":
  102. editRecipe.display(data);
  103. break;
  104. case "addRecipe":
  105. newRecipe.display();
  106. break;
  107. case "orderDetails":
  108. orderDetails.display(data);
  109. break;
  110. case "orderFilter":
  111. orderFilter.display();
  112. break;
  113. case "newOrder":
  114. newOrder.display();
  115. break;
  116. case "transactionDetails":
  117. transactionDetails.display(data);
  118. break;
  119. case "transactionFilter":
  120. transactionFilter.display();
  121. break;
  122. case "newTransaction":
  123. newTransaction.display();
  124. break;
  125. }
  126. if(screen.height > screen.width || screen.width < 1200){
  127. document.querySelector(".contentBlock").style.display = "none";
  128. document.getElementById("mobileMenuSelector").style.display = "none";
  129. document.getElementById("sidebarCloser").style.display = "block";
  130. }
  131. },
  132. closeSidebar: function(){
  133. let sidebar = document.getElementById("sidebarDiv");
  134. for(let i = 0; i < sidebar.children.length; i++){
  135. if(sidebar.children[i].style.display !== "none"){
  136. sidebar.children[i].style.display = "none";
  137. let choosables = [];
  138. switch(sidebar.children[i].id){
  139. case "ingredientDetails":
  140. choosables = document.querySelectorAll(".ingredient");
  141. break;
  142. case "transactionDetails":
  143. choosables = document.getElementById("transactionsList").children;
  144. break;
  145. case "recipeDetails":
  146. choosables = document.getElementById("recipeList").children;
  147. break;
  148. case "orderDetails":
  149. choosables = document.getElementById("orderList").children;
  150. break;
  151. }
  152. for(let i = 0; i < choosables.length; i++){
  153. choosables[i].classList.remove("active");
  154. }
  155. }
  156. }
  157. sidebar.classList = "sidebarHide";
  158. if(screen.height > screen.width || screen.width < 1200){
  159. document.querySelector(".contentBlock").style.display = "flex";
  160. document.getElementById("mobileMenuSelector").style.display = "block";
  161. document.getElementById("sidebarCloser").style.display = "none";
  162. }
  163. },
  164. openModal: function(str){
  165. let modal = document.getElementById("modal");
  166. modal.style.display = "flex";
  167. document.getElementById("modalClose").addEventListener("click", this.closeModal);
  168. let content = {};
  169. switch(str){
  170. case "ingredientSpreadsheet":
  171. content = document.getElementById("modalSpreadsheetUpload");
  172. content.style.display = "flex";
  173. document.getElementById("modalSpreadsheetTitle").innerText = "ingredients";
  174. document.getElementById("spreadsheetDownload").href = "/ingredients/download/spreadsheet";
  175. content.onsubmit = newIngredient.submitSpreadsheet;
  176. break;
  177. case "recipeSpreadsheet":
  178. content = document.getElementById("modalSpreadsheetUpload");
  179. content.style.display = "flex";
  180. document.getElementById("modalSpreadsheetTitle").innerText = "recipes";
  181. document.getElementById("spreadsheetDownload").href = "/recipes/download/spreadsheet";
  182. document.getElementById("spreadsheetRecipeIsSquare").parentElement.style.display = "flex";
  183. content.onsubmit = newRecipe.submitSpreadsheet;
  184. break;
  185. case "orderSpreadsheet":
  186. content = document.getElementById("modalSpreadsheetUpload");
  187. content.style.display = "flex";
  188. document.getElementById("modalSpreadsheetTitle").innerText = "orders";
  189. document.getElementById("spreadsheetDownload").href = "/orders/download/spreadsheet";
  190. content.onsubmit = newOrder.submitSpreadsheet;
  191. break;
  192. case "transactionSpreadsheet":
  193. content = document.getElementById("modalSpreadsheetUpload");
  194. content.style.display = "flex";
  195. document.getElementById("modalSpreadsheetTitle").innerText = "transactions";
  196. document.getElementById("spreadsheetDownload").href = "/transactions/download/spreadsheet";
  197. content.onsubmit = newTransaction.submitSpreadsheet;
  198. break;
  199. case "feedback":
  200. document.getElementById("modalFeedback").style.display = "flex";
  201. break;
  202. }
  203. },
  204. closeModal: function(){
  205. let modal = document.getElementById("modal");
  206. let modalContent = document.getElementById("modalContent");
  207. for(let i = 0; i < modalContent.children.length; i++){
  208. modalContent.children[i].style.display = "none";
  209. }
  210. modal.style.display = "none";
  211. },
  212. createBanner: function(text, status){
  213. let container = document.getElementById("bannerContainer");
  214. let template = document.getElementById("banner").content.children[0];
  215. let banner = template.cloneNode(true);
  216. switch(status){
  217. case "error":
  218. banner.children[0].style.backgroundColor = "rgb(200, 0, 0)";
  219. banner.children[0].children[0].style.display = "block";
  220. break;
  221. case "alert":
  222. banner.children[0].style.backgroundColor = "rgb(230, 210, 0)";
  223. banner.children[0].children[1].style.display = "block";
  224. break;
  225. case "success":
  226. banner.children[0].style.backgroundColor = "rgb(0, 145, 55)";
  227. banner.children[0].children[2].style.display = "block";
  228. break;
  229. }
  230. banner.children[1].innerText = text;
  231. container.appendChild(banner);
  232. let timer = setTimeout(()=>{
  233. container.removeChild(banner);
  234. }, 10000);
  235. banner.children[2].addEventListener("click", ()=>{
  236. container.removeChild(banner);
  237. clearTimeout(timer);
  238. });
  239. },
  240. changeMenu: function(){
  241. let menu = document.querySelector(".menu");
  242. let buttons = document.querySelectorAll(".menuButton");
  243. if(!menu.classList.contains("menuMinimized")){
  244. menu.classList = "menu menuMinimized";
  245. for(let i = 0; i < buttons.length; i++){
  246. buttons[i].children[1].style.display = "none";
  247. }
  248. document.getElementById("max").style.display = "none";
  249. document.getElementById("min").style.display = "flex";
  250. }else if(menu.classList.contains("menuMinimized")){
  251. menu.classList = "menu";
  252. for(let i = 0; i < buttons.length; i++){
  253. buttons[i].children[1].style.display = "block";
  254. }
  255. setTimeout(()=>{
  256. document.getElementById("max").style.display = "flex";
  257. document.getElementById("min").style.display = "none";
  258. }, 150);
  259. }
  260. },
  261. openMenu: function(){
  262. document.getElementById("menu").style.display = "flex";
  263. document.querySelector(".contentBlock").style.display = "none";
  264. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  265. },
  266. closeMenu: function(){
  267. document.getElementById("menu").style.display = "none";
  268. document.querySelector(".contentBlock").style.display = "flex";
  269. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  270. },
  271. updateAnalytics: function(){
  272. analytics.isPopulated = false;
  273. }
  274. }
  275. //Add click listeners for menu buttons
  276. document.getElementById("homeBtn").onclick = ()=>{controller.openStrand("home")};
  277. document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("ingredients")};
  278. document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
  279. document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
  280. document.getElementById("ordersBtn").onclick = async ()=>{controller.openStrand("orders")};
  281. document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
  282. document.getElementById("accountBtn").onclick = ()=>{controller.openStrand("account")};
  283. document.getElementById("feedbackButton").onclick = ()=>{controller.openModal("feedback")};
  284. controller.openStrand("home");