loader.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { project } from "./project.js";
  2. import { submission } from "./submission.js";
  3. import { element } from "./element.js";
  4. import { exists, is_string } from "./functions.js";
  5. class loader {
  6. #load_from;
  7. #database;
  8. #content;
  9. constructor(database, project) {
  10. if (typeof(database) !== "string") {
  11. throw "Database URL must be string.";
  12. }
  13. if (typeof(project) !== "string") {
  14. throw "Project file must be string.";
  15. }
  16. this.#database = database;
  17. this.#load_from = database + "/" + project;
  18. this.#content = null;
  19. }
  20. async load() {
  21. const fetched = await fetch(this.#load_from);
  22. if (fetched.status !== 200) {
  23. throw "Can not load database from " + this.#load_from;
  24. }
  25. try {
  26. const result = await fetched.json();
  27. this.#content = result;
  28. } catch (error) {
  29. throw "Database JSON is bad formated.";
  30. }
  31. }
  32. #prepare_image(url) {
  33. return this.#database + "/" + url;
  34. }
  35. #prepare_element(input) {
  36. if (!exists(input.name)) {
  37. return null;
  38. }
  39. const result = new element(input.name);
  40. if (exists(input.description)) {
  41. result.description = input.description;
  42. }
  43. if (exists(input.mesh)) {
  44. result.mesh = this.#prepare_image(input.mesh);
  45. }
  46. if (exists(input.thumbnail)) {
  47. result.thumbnail = this.#prepare_image(input.thumbnail);
  48. }
  49. if (exists(input.pictures)) {
  50. const pictures = Array.from(input.pictures);
  51. pictures.forEach(picture => {
  52. if (!is_string(picture)) {
  53. return;
  54. }
  55. result.pictures.add(this.#prepare_image(picture));
  56. });
  57. }
  58. if (exists(input.params)) {
  59. const names = Object.keys(input.params);
  60. names.forEach(name => {
  61. if (!is_string(name) || !is_string(input.params[name])) {
  62. return;
  63. }
  64. result.params.set(name, input.params[name]);
  65. });
  66. }
  67. if (exists(input.shop)) {
  68. result.shop = input.shop;
  69. }
  70. return result;
  71. }
  72. #prepare_one(input) {
  73. if (typeof(input.name) === "undefined") {
  74. return null;
  75. }
  76. const result = new submission(input.name)
  77. if (typeof(input.description) !== "undefined") {
  78. result.description = input.description;
  79. }
  80. if (typeof(input.thumbnail) !== "undefined") {
  81. result.thumbnail = this.#prepare_image(input.thumbnail);
  82. }
  83. if (typeof(input.elements) === "undefined") {
  84. return result;
  85. }
  86. const elements = Array.from(input.elements);
  87. elements.forEach(data => {
  88. const current = this.#prepare_element(data);
  89. if (current !== null) {
  90. result.add(current);
  91. }
  92. });
  93. return result;
  94. }
  95. get loaded() {
  96. if (this.#content === null) {
  97. throw "Must load database before trying to access it.";
  98. }
  99. const content = this.#content;
  100. if (!exists(content.name)) {
  101. content.name = "Unnamed";
  102. }
  103. const result = new project(content.name);
  104. if (exists(content.description)) {
  105. result.description = content.description;
  106. }
  107. if (!exists(content.submissions)) {
  108. return result;
  109. }
  110. const submissions = Array.from(content.submissions);
  111. submissions.forEach(submission => {
  112. const item = this.#prepare_one(submission);
  113. if (item === null) {
  114. return;
  115. }
  116. result.add(item);
  117. });
  118. return result;
  119. }
  120. }
  121. export { loader };