dashboard.js 13 KB

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