dashboard.js 18 KB

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