screen.js 1.3 KB

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