import { project } from "./project.js"; import { submission } from "./submission.js"; import { element } from "./element.js"; import { exists, is_string } from "./functions.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; if (!exists(content.name)) { content.name = "Unnamed"; } const result = new project(content.name); if (exists(content.description)) { result.description = content.description; } if (!exists(content.submissions)) { return result; } const submissions = Array.from(content.submissions); submissions.forEach(submission => { const item = this.#prepare_one(submission); if (item === null) { return; } result.add(item); }); return result; } #prepare_image(url) { return this.#database + "/" + url; } #prepare_element(input) { if (exists(input.name)) { return null; } const element = new element(input.name); if (exists(input.description)) { element.description = input.description; } if (exists(input.mesh)) { element.mesh = this.#prepare_image(input.mesh); } if (exists(input.thumbnail)) { element.thumbnail = this.#prepare_image(input.thumbnail); } if (exists(input.pictures)) { const pictures = Array.from(input.pictures); pictures.forEach(picture => { if (!is_string(picture) { return; } element.pictures.add(this.#prepare_image(picture)); }); } if (exists(input.params)) { const names = Object.keys(input.params); names.forEach(name => { if (!is_string(name) || !is_string(input.params[name])) { return; } element.params.set(name, input.params[name]); }); } if (exists(input.shop)) { element.shop = input.shop; } return element; } #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); } if (typeof(input.elements) === "undefined") { return submission; } const elements = Array.from(input.elements); elements.forEach(data => { const element = new this.#prepare_element(data); if (element !== null) { submission.add(element); } }); return submission; } } export { loader };