Navigator.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import Notifier from "./Notifier.js";
  2. import Elem from "./Elem.js";
  3. export default class Navigator{
  4. constructor(container, options){
  5. const navBox = new Elem("div")
  6. .addClass("navigator")
  7. .appendTo(container);
  8. for(let i = 0; i < options.length; i++){
  9. const option = options[i].split("-");
  10. switch(option[0]){
  11. case "home": navBox.append(this.homeButton()); break;
  12. case "back": navBox.append(this.backButton(option[1])); break;
  13. case "logout": navBox.append(this.logoutButton()); break;
  14. case "viewMenu": navBox.append(this.viewMenuButton()); break;
  15. case "addMenu": navBox.append(this.addMenuButton()); break;
  16. }
  17. }
  18. }
  19. homeButton(){
  20. const homeSvg = '<svg viewBox="0 0 24 24" stroke-width="2.5" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M2 8L11.7317 3.13416C11.9006 3.04971 12.0994 3.0497 12.2683 3.13416L22 8" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M20 11V19C20 20.1046 19.1046 21 18 21H6C4.89543 21 4 20.1046 4 19V11" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>';
  21. return new Elem("button")
  22. .innerHtml(homeSvg)
  23. .addClass("navButton")
  24. .onclick(()=>{changePage("home")});
  25. }
  26. backButton(page){
  27. const backSvg = '<svg viewBox="0 0 24 24" stroke-width="2.5" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M3 12L21 12M21 12L12.5 3.5M21 12L12.5 20.5" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>';
  28. return new Elem("button")
  29. .innerHtml(backSvg)
  30. .addClass("navButton")
  31. .onclick(()=>{changePage(page)});
  32. }
  33. logoutButton(){
  34. const logoutSvg = '<svg stroke-width="2.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M12 12H19M19 12L16 15M19 12L16 9" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M19 6V5C19 3.89543 18.1046 3 17 3H7C5.89543 3 5 3.89543 5 5V19C5 20.1046 5.89543 21 7 21H17C18.1046 21 19 20.1046 19 19V18" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>';
  35. return new Elem("button")
  36. .innerHtml(logoutSvg)
  37. .addClass("navButton")
  38. .onclick(this.logout);
  39. }
  40. viewMenuButton(){
  41. const viewSvg = '<svg viewBox="0 0 24 24" stroke-width="2.5" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M3 13C6.6 5 17.4 5 21 13" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M12 17C10.3431 17 9 15.6569 9 14C9 12.3431 10.3431 11 12 11C13.6569 11 15 12.3431 15 14C15 15.6569 13.6569 17 12 17Z" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>';
  42. return new Elem("button")
  43. .innerHtml(viewSvg)
  44. .addClass("navButton")
  45. .onclick(()=>{changePage("viewMenu")});
  46. }
  47. addMenuButton(){
  48. const addSvg = '<svg stroke-width="2.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M6 12H12M18 12H12M12 12V6M12 12V18" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>';
  49. return new Elem("button")
  50. .innerHtml(addSvg)
  51. .addClass("navButton")
  52. .onclick(()=>{changePage("addMenu")});
  53. }
  54. async logout(){
  55. let response;
  56. try{
  57. response = await fetch("/api/user/logout", {
  58. method: "GET",
  59. headers: {"Content-Type": "application/json"}
  60. });
  61. }catch(e){
  62. new Notifier("error", "Something went wrong, try refreshing the page");
  63. }
  64. if(!response.ok) return new Notifier("error", response.error.message);
  65. changePage("login");
  66. }
  67. }