loader.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. const result = await fetched.json();
  23. this.#content = result;
  24. }
  25. get loaded() {
  26. if (this.#content === null) {
  27. throw "Must load database before trying to access it.";
  28. }
  29. const content = this.#content;
  30. if (!exists(content.name)) {
  31. content.name = "Unnamed";
  32. }
  33. const result = new project(content.name);
  34. if (exists(content.description)) {
  35. result.description = content.description;
  36. }
  37. if (!exists(content.submissions)) {
  38. return result;
  39. }
  40. const submissions = Array.from(content.submissions);
  41. submissions.forEach(submission => {
  42. const item = this.#prepare_one(submission);
  43. if (item === null) {
  44. return;
  45. }
  46. result.add(item);
  47. });
  48. return result;
  49. }
  50. #prepare_image(url) {
  51. return this.#database + "/" + url;
  52. }
  53. #prepare_element(input) {
  54. if (exists(input.name)) {
  55. return null;
  56. }
  57. const element = new element(input.name);
  58. if (exists(input.description)) {
  59. element.description = input.description;
  60. }
  61. if (exists(input.mesh)) {
  62. element.mesh = this.#prepare_image(input.mesh);
  63. }
  64. if (exists(input.thumbnail)) {
  65. element.thumbnail = this.#prepare_image(input.thumbnail);
  66. }
  67. if (exists(input.pictures)) {
  68. const pictures = Array.from(input.pictures);
  69. pictures.forEach(picture => {
  70. if (!is_string(picture) {
  71. return;
  72. }
  73. element.pictures.add(this.#prepare_image(picture));
  74. });
  75. }
  76. if (exists(input.params)) {
  77. const names = Object.keys(input.params);
  78. names.forEach(name => {
  79. if (!is_string(name) || !is_string(input.params[name])) {
  80. return;
  81. }
  82. element.params.set(name, input.params[name]);
  83. });
  84. }
  85. if (exists(input.shop)) {
  86. element.shop = input.shop;
  87. }
  88. return element;
  89. }
  90. #prepare_one(input) {
  91. if (typeof(input.name) === "undefined") {
  92. return null;
  93. }
  94. const submission = new submission(input.name)
  95. if (typeof(input.description) !== "undefined") {
  96. submission.description = input.description;
  97. }
  98. if (typeof(input.thumbnail) !== "undefined") {
  99. submission.thumbnail = this.#prepare_image(input.thumbnail);
  100. }
  101. if (typeof(input.elements) === "undefined") {
  102. return submission;
  103. }
  104. const elements = Array.from(input.elements);
  105. elements.forEach(data => {
  106. const element = new this.#prepare_element(data);
  107. if (element !== null) {
  108. submission.add(element);
  109. }
  110. });
  111. return submission;
  112. }
  113. }
  114. export { loader };