page.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import menu from "./menu";
  2. import templateManager from "./templateManager";
  3. export abstract class Page {
  4. protected rootSection: HTMLElement;
  5. protected pageName: string;
  6. protected abstract load(): Promise<void>;
  7. protected constructor(pageName: string) {
  8. let rootSection = document.getElementById("page-"+pageName);
  9. if (!rootSection)
  10. throw new Error("Missing section for page " +typeof this);
  11. this.rootSection = rootSection;
  12. this.pageName = pageName;
  13. this.hide();
  14. }
  15. protected hide() {
  16. this.rootSection.classList.add("hidden");
  17. }
  18. protected showWithoutHistory() {
  19. if (templateManager.currentSection === this)
  20. return;
  21. templateManager.currentSection?.hide();
  22. templateManager.currentSection = this;
  23. this.rootSection.classList.remove("hidden");
  24. }
  25. public getPageName(): string {
  26. return this.pageName;
  27. }
  28. public async show(): Promise<void> {
  29. if (templateManager.currentSection === this)
  30. return Promise.resolve();
  31. this.showWithoutHistory();
  32. await menu.init();
  33. history.pushState("", "", `#${this.pageName}`)
  34. return this.load();
  35. }
  36. }