loader.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { project } from "./project.js";
  2. import { submission } from "./submission.js";
  3. class loader {
  4. #load_from;
  5. #database;
  6. #content;
  7. constructor(database, project) {
  8. if (typeof(database) !== "string") {
  9. throw "Database URL must be string.";
  10. }
  11. if (typeof(project) !== "string") {
  12. throw "Project file must be string.";
  13. }
  14. this.#database = database;
  15. this.#load_from = database + "/" + project;
  16. this.#content = null;
  17. }
  18. async load() {
  19. const fetched = await fetch(this.#load_from);
  20. const result = await fetched.json();
  21. this.#content = result;
  22. }
  23. get loaded() {
  24. if (this.#content === null) {
  25. throw "Must load database before trying to access it.";
  26. }
  27. const content = this.#content;
  28. const result = new project(content.name);
  29. if (typeof(content.description) !== "undefined") {
  30. result.description = content.description;
  31. }
  32. if (typeof(content.submissions) === "undefined") {
  33. return result;
  34. }
  35. const submissions = Array.from(content.submissions);
  36. submissions.forEach(submission => {
  37. const item = this.#prepare_one(submission);
  38. if (item === null) {
  39. return;
  40. }
  41. result.add(item);
  42. });
  43. }
  44. #prepare_image(url) {
  45. return this.#database + "/" + url;
  46. }
  47. #prepare_one(input) {
  48. if (typeof(input.name) === "undefined") {
  49. return null;
  50. }
  51. const submission = new submission(input.name)
  52. if (typeof(input.description) !== "undefined") {
  53. submission.description = input.description;
  54. }
  55. if (typeof(input.thumbnail) !== "undefined") {
  56. submission.thumbnail = this.#prepare_image(input.thumbnail);
  57. }
  58. return submission;
  59. }
  60. }
  61. export { loader };