screen.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { view } from "./view.js";
  2. import { loading_screen } from "./loading-screen.js";
  3. export class screen {
  4. #root;
  5. #view;
  6. constructor(root) {
  7. this.#root = root;
  8. }
  9. get root() {
  10. return this.#root;
  11. }
  12. get view() {
  13. return this.#view;
  14. }
  15. get #loading() {
  16. return new loading_screen();
  17. }
  18. #change_view(target) {
  19. if (this.#view !== undefined) {
  20. this.#view.destroy();
  21. }
  22. this.#view = target;
  23. const content = this.#view.show();
  24. if (!(content instanceof HTMLElement)) {
  25. throw new TypeError("View show not return HTML Element.");
  26. }
  27. while (this.root.lastChild) {
  28. this.root.lastChild.remove();
  29. }
  30. this.root.appendChild(content);
  31. this.#loading.hide();
  32. }
  33. set view(target) {
  34. if (!(target instanceof view)) {
  35. throw new Error("Target must be instance of view.");
  36. }
  37. if (this.#loading.visible) {
  38. this.#change_view(target)
  39. return;
  40. }
  41. this.#loading.show();
  42. setTimeout(() => {
  43. this.#change_view(target);
  44. }, this.#loading.time);
  45. }
  46. }