| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import { project } from "./project.js";
- import { submission } from "./submission.js";
- class loader {
- #load_from;
- #database;
- #content;
- constructor(database, project) {
- if (typeof(database) !== "string") {
- throw "Database URL must be string.";
- }
- if (typeof(project) !== "string") {
- throw "Project file must be string.";
- }
- this.#database = database;
- this.#load_from = database + "/" + project;
- this.#content = null;
- }
- async load() {
- const fetched = await fetch(this.#load_from);
- const result = await fetched.json();
- this.#content = result;
- }
- get loaded() {
- if (this.#content === null) {
- throw "Must load database before trying to access it.";
- }
-
- const content = this.#content;
- const result = new project(content.name);
- if (typeof(content.description) !== "undefined") {
- result.description = content.description;
- }
- if (typeof(content.submissions) === "undefined") {
- return result;
- }
- const submissions = Array.from(content.submissions);
- submissions.forEach(submission => {
- const item = this.#prepare_one(submission);
- if (item === null) {
- return;
- }
- result.add(item);
- });
- }
- #prepare_image(url) {
- return this.#database + "/" + url;
- }
- #prepare_one(input) {
- if (typeof(input.name) === "undefined") {
- return null;
- }
- const submission = new submission(input.name)
-
- if (typeof(input.description) !== "undefined") {
- submission.description = input.description;
- }
- if (typeof(input.thumbnail) !== "undefined") {
- submission.thumbnail = this.#prepare_image(input.thumbnail);
- }
- return submission;
- }
- }
- export { loader };
|