dashboard.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. console.log(data);
  23. window.merchant = new Merchant(
  24. data.merchant.name,
  25. data.merchant.pos,
  26. data.merchant.inventory,
  27. data.merchant.recipes,
  28. data.transactions,
  29. data.merchant.address.full,
  30. data.owner
  31. );
  32. controller = {
  33. openStrand: function(strand, data = undefined){
  34. this.closeSidebar();
  35. let strands = document.querySelectorAll(".strand");
  36. for(let i = 0; i < strands.length; i++){
  37. strands[i].style.display = "none";
  38. }
  39. let buttons = document.querySelectorAll(".menuButton");
  40. for(let i = 0; i < buttons.length - 1; i++){
  41. buttons[i].classList = "menuButton";
  42. buttons[i].disabled = false;
  43. }
  44. let activeButton = {};
  45. switch(strand){
  46. case "home":
  47. activeButton = document.getElementById("homeBtn");
  48. document.getElementById("homeStrand").style.display = "flex";
  49. home.display();
  50. break;
  51. case "ingredients":
  52. activeButton = document.getElementById("ingredientsBtn");
  53. document.getElementById("ingredientsStrand").style.display = "flex";
  54. ingredients.display();
  55. break;
  56. case "recipeBook":
  57. activeButton = document.getElementById("recipeBookBtn");
  58. document.getElementById("recipeBookStrand").style.display = "flex";
  59. recipeBook.display();
  60. break;
  61. case "analytics":
  62. activeButton = document.getElementById("analyticsBtn");
  63. document.getElementById("analyticsStrand").style.display = "flex";
  64. analytics.display();
  65. break;
  66. case "orders":
  67. activeButton = document.getElementById("ordersBtn");
  68. document.getElementById("ordersStrand").style.display = "flex";
  69. orders.display();
  70. break;
  71. case "transactions":
  72. activeButton = document.getElementById("transactionsBtn");
  73. document.getElementById("transactionsStrand").style.display = "flex";
  74. transactions.transactions = data;
  75. transactions.display();
  76. break;
  77. case "account":
  78. activeButton = document.getElementById("accountBtn");
  79. document.getElementById("accountStrand").style.display = "flex";
  80. account.display();
  81. break;
  82. }
  83. activeButton.classList = "menuButton active";
  84. activeButton.disabled = true;
  85. if(screen.height > screen.width || screen.width < 1200){
  86. this.closeMenu();
  87. }
  88. },
  89. /*
  90. Open a specific sidebar
  91. Input:
  92. sidebar: the outermost element of the sidebar (must contain class sidebar)
  93. */
  94. openSidebar: function(sidebar, data = {}){
  95. this.closeSidebar();
  96. document.getElementById("sidebarDiv").classList = "sidebar";
  97. document.getElementById(sidebar).style.display = "flex";
  98. switch(sidebar){
  99. case "ingredientDetails":
  100. ingredientDetails.display(data);
  101. break;
  102. case "newIngredient":
  103. newIngredient.display();
  104. break;
  105. case "editIngredient":
  106. editIngredient.display(data);
  107. break;
  108. case "recipeDetails":
  109. recipeDetails.display(data);
  110. break;
  111. case "editRecipe":
  112. editRecipe.display(data);
  113. break;
  114. case "addRecipe":
  115. newRecipe.display();
  116. break;
  117. case "orderDetails":
  118. orderDetails.display(data);
  119. break;
  120. case "orderFilter":
  121. orderFilter.display();
  122. break;
  123. case "newOrder":
  124. newOrder.display();
  125. break;
  126. case "transactionDetails":
  127. transactionDetails.display(data);
  128. break;
  129. case "transactionFilter":
  130. transactionFilter.display();
  131. break;
  132. case "newTransaction":
  133. newTransaction.display();
  134. break;
  135. }
  136. if(screen.height > screen.width || screen.width < 1200){
  137. document.querySelector(".contentBlock").style.display = "none";
  138. document.getElementById("mobileMenuSelector").style.display = "none";
  139. document.getElementById("sidebarCloser").style.display = "block";
  140. }
  141. },
  142. closeSidebar: function(){
  143. let sidebar = document.getElementById("sidebarDiv");
  144. for(let i = 0; i < sidebar.children.length; i++){
  145. if(sidebar.children[i].style.display !== "none"){
  146. sidebar.children[i].style.display = "none";
  147. let choosables = [];
  148. switch(sidebar.children[i].id){
  149. case "ingredientDetails":
  150. choosables = document.querySelectorAll(".ingredient");
  151. break;
  152. case "transactionDetails":
  153. choosables = document.getElementById("transactionsList").children;
  154. break;
  155. case "recipeDetails":
  156. choosables = document.getElementById("recipeList").children;
  157. break;
  158. case "orderDetails":
  159. choosables = document.getElementById("orderList").children;
  160. break;
  161. }
  162. for(let i = 0; i < choosables.length; i++){
  163. choosables[i].classList.remove("active");
  164. }
  165. }
  166. }
  167. sidebar.classList = "sidebarHide";
  168. if(screen.height > screen.width || screen.width < 1200){
  169. document.querySelector(".contentBlock").style.display = "flex";
  170. document.getElementById("mobileMenuSelector").style.display = "block";
  171. document.getElementById("sidebarCloser").style.display = "none";
  172. }
  173. },
  174. openModal: function(str, data){
  175. let modal = document.getElementById("modal");
  176. modal.style.display = "flex";
  177. document.getElementById("modalClose").addEventListener("click", this.closeModal);
  178. let content = {};
  179. switch(str){
  180. case "ingredientSpreadsheet":
  181. content = document.getElementById("modalSpreadsheetUpload");
  182. content.style.display = "flex";
  183. document.getElementById("modalSpreadsheetTitle").innerText = "ingredients";
  184. document.getElementById("spreadsheetDownload").href = "/ingredients/download/spreadsheet";
  185. content.onsubmit = newIngredient.submitSpreadsheet;
  186. break;
  187. case "recipeSpreadsheet":
  188. content = document.getElementById("modalSpreadsheetUpload");
  189. content.style.display = "flex";
  190. document.getElementById("modalSpreadsheetTitle").innerText = "recipes";
  191. document.getElementById("spreadsheetDownload").href = "/recipes/download/spreadsheet";
  192. content.onsubmit = newRecipe.submitSpreadsheet;
  193. break;
  194. case "orderSpreadsheet":
  195. content = document.getElementById("modalSpreadsheetUpload");
  196. content.style.display = "flex";
  197. document.getElementById("modalSpreadsheetTitle").innerText = "orders";
  198. document.getElementById("spreadsheetDownload").href = "/orders/download/spreadsheet";
  199. content.onsubmit = newOrder.submitSpreadsheet;
  200. break;
  201. case "transactionSpreadsheet":
  202. content = document.getElementById("modalSpreadsheetUpload");
  203. content.style.display = "flex";
  204. document.getElementById("modalSpreadsheetTitle").innerText = "transactions";
  205. document.getElementById("spreadsheetDownload").href = "/transactions/download/spreadsheet";
  206. content.onsubmit = newTransaction.submitSpreadsheet;
  207. break;
  208. case "feedback":
  209. modalScript.feedback();
  210. break;
  211. case "newMerchant":
  212. modalScript.newMerchant();
  213. break;
  214. case "confirmDeleteMerchant":
  215. content = document.getElementById("modalConfirm");
  216. content.style.display = "flex";
  217. content.children[1].innerText = "Are you sure you want to delete this merchant?";
  218. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  219. content.children[2].children[1].onclick = ()=>{account.deleteMerchant()};
  220. break;
  221. case "confirmDeleteIngredient":
  222. content = document.getElementById("modalConfirm");
  223. content.style.display = "flex";
  224. content.children[1].innerText = `Are you sure you want to delete ingredient: ${data.ingredient.name}?`;
  225. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  226. content.children[2].children[1].onclick = ()=>{ingredientDetails.remove(data)};
  227. break;
  228. case "confirmDeleteRecipe":
  229. content = document.getElementById("modalConfirm");
  230. content.style.display = "flex";
  231. content.children[1].innerText = `Are you sure you want to delete recipe: ${data.name}?`;
  232. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  233. content.children[2].children[1].onclick = ()=>{recipeDetails.remove(data)};
  234. break;
  235. case "confirmDeleteOrder":
  236. content = document.getElementById("modalConfirm");
  237. content.style.display = "flex";
  238. content.children[1].innerText = `Are you sure you want to delete order: ${data.name}`;
  239. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  240. content.children[2].children[1].onclick = ()=>{orderDetails.remove(data)};
  241. break;
  242. case "confirmDeleteTransaction":
  243. content = document.getElementById("modalConfirm");
  244. content.style.display = "flex";
  245. content.children[1].innerText = `Are you sure you want to delete this transaction?`;
  246. content.children[2].children[0].onclick = ()=>{controller.closeModal()};
  247. content.children[2].children[1].onclick = ()=>{transactionDetails.remove(data)};
  248. break;
  249. case "squareLocations":
  250. modalScript.squareLocations(data);
  251. break;
  252. case "editSubIngredients":
  253. modalScript.editSubIngredients(data);
  254. break;
  255. case "circularReference":
  256. modalScript.circularReference(data);
  257. break;
  258. }
  259. },
  260. closeModal: function(){
  261. let modal = document.getElementById("modal");
  262. let modalContent = document.getElementById("modalContent");
  263. for(let i = 0; i < modalContent.children.length; i++){
  264. modalContent.children[i].style.display = "none";
  265. }
  266. modal.style.display = "none";
  267. },
  268. createBanner: function(text, status){
  269. let container = document.getElementById("bannerContainer");
  270. let template = document.getElementById("banner").content.children[0];
  271. let banner = template.cloneNode(true);
  272. switch(status){
  273. case "error":
  274. banner.children[0].style.backgroundColor = "rgb(200, 0, 0)";
  275. banner.children[0].children[0].style.display = "block";
  276. break;
  277. case "alert":
  278. banner.children[0].style.backgroundColor = "rgb(230, 210, 0)";
  279. banner.children[0].children[1].style.display = "block";
  280. break;
  281. case "success":
  282. banner.children[0].style.backgroundColor = "rgb(0, 145, 55)";
  283. banner.children[0].children[2].style.display = "block";
  284. break;
  285. }
  286. banner.children[1].innerText = text;
  287. container.appendChild(banner);
  288. let timer = setTimeout(()=>{
  289. container.removeChild(banner);
  290. }, 10000);
  291. banner.children[2].addEventListener("click", ()=>{
  292. container.removeChild(banner);
  293. clearTimeout(timer);
  294. });
  295. },
  296. changeMenu: function(){
  297. let menu = document.querySelector(".menu");
  298. let buttons = document.querySelectorAll(".menuButton");
  299. let links = document.getElementById("menuLinks");
  300. let merchantName = document.getElementById("menuLocationName");
  301. if(!menu.classList.contains("menuMinimized")){
  302. merchantName.style.display = "none";
  303. menu.classList = "menu menuMinimized";
  304. for(let i = 0; i < buttons.length; i++){
  305. buttons[i].children[1].style.display = "none";
  306. }
  307. document.getElementById("max").style.display = "none";
  308. document.getElementById("min").style.display = "flex";
  309. links.children[0].style.fontSize = "12px";
  310. links.children[1].style.display = "none";
  311. }else if(menu.classList.contains("menuMinimized")){
  312. merchantName.style.display = "block";
  313. menu.classList = "menu";
  314. for(let i = 0; i < buttons.length; i++){
  315. buttons[i].children[1].style.display = "block";
  316. }
  317. document.getElementById("max").style.display = "flex";
  318. document.getElementById("min").style.display = "none";
  319. links.children[0].style.fontSize = "16px";
  320. links.children[1].style.display = "block";
  321. setTimeout(()=>{
  322. document.getElementById("max").style.display = "flex";
  323. document.getElementById("min").style.display = "none";
  324. }, 150);
  325. }
  326. },
  327. openMenu: function(){
  328. document.getElementById("menu").style.display = "flex";
  329. document.querySelector(".contentBlock").style.display = "none";
  330. document.getElementById("mobileMenuSelector").onclick = ()=>{this.closeMenu()};
  331. },
  332. closeMenu: function(){
  333. document.getElementById("menu").style.display = "none";
  334. document.querySelector(".contentBlock").style.display = "flex";
  335. document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
  336. },
  337. baseUnit(quantity, unit){
  338. switch(unit){
  339. case "g": return quantity;
  340. case "kg": return quantity * 1000;
  341. case "oz": return quantity * 28.3495;
  342. case "lb": return quantity * 453.5924;
  343. case "ml": return quantity / 1000;
  344. case "l": return quantity;
  345. case "tsp": return quantity / 202.8842;
  346. case "tbsp": return quantity / 67.6278;
  347. case "ozfl": return quantity / 33.8141;
  348. case "cup": return quantity / 4.1667;
  349. case "pt": return quantity / 2.1134;
  350. case "qt": return quantity / 1.0567;
  351. case "gal": return quantity * 3.7854;
  352. case "mm": return quantity / 1000;
  353. case "cm": return quantity / 100;
  354. case "m": return quantity;
  355. case "in": return quantity / 39.3701;
  356. case "ft": return quantity / 3.2808;
  357. default: return quantity;
  358. }
  359. }
  360. }
  361. window.state = {
  362. updateIngredients: function(){
  363. ingredients.populateByProperty();
  364. analytics.populateButtons();
  365. home.drawInventoryCheckCard();
  366. },
  367. updateRecipes: function(){
  368. recipeBook.populateRecipes();
  369. analytics.populateButtons();
  370. },
  371. updateTransactions: function(){
  372. home.isPopulated = false;
  373. ingredients.populateByProperty();
  374. analytics.displayIngredient();
  375. analytics.displayRecipe();
  376. home.drawRevenueGraph();
  377. },
  378. updateOrders: function(){
  379. ingredients.isPopulated = false;
  380. ordersStrand.isPopulated = false;
  381. },
  382. updateMerchant(){
  383. this.updateIngredients();
  384. this.updateRecipes();
  385. this.updateTransactions();
  386. this.updateOrders();
  387. document.getElementById("menuLocationName").innerText = merchant.name;
  388. recipeBook.isPopulated = false;
  389. }
  390. }
  391. //Add click listeners for menu buttons
  392. document.getElementById("menuShifter").onclick = ()=>{controller.changeMenu()}
  393. document.getElementById("menuShifter2").onclick = ()=>{controller.changeMenu()}
  394. document.getElementById("homeBtn").onclick = ()=>{controller.openStrand("home")};
  395. document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("ingredients")};
  396. document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
  397. document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
  398. document.getElementById("ordersBtn").onclick = async ()=>{controller.openStrand("orders")};
  399. document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
  400. document.getElementById("accountBtn").onclick = ()=>{controller.openStrand("account")};
  401. document.getElementById("feedbackButton").onclick = ()=>{controller.openModal("feedback")};
  402. document.getElementById("menuLocationName").innerText = merchant.name;
  403. controller.openStrand("home");