| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 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);
-
- if (fetched.status !== 200) {
- throw "Can not load database from " + this.#load_from;
- }
- try {
- const result = await fetched.json();
- this.#content = result;
- } catch (error) {
- throw "Database JSON is bad formated.";
- }
- }
-
- #prepare_image(url) {
- return this.#database + "/" + url;
- }
- #prepare_element(input) {
- if (!exists(input.name)) {
- return null;
- }
- const result = new element(input.name);
- if (exists(input.description)) {
- result.description = input.description;
- }
- if (exists(input.mesh)) {
- result.mesh = this.#prepare_image(input.mesh);
- }
- if (exists(input.thumbnail)) {
- result.thumbnail = this.#prepare_image(input.thumbnail);
- }
-
- if (exists(input.pictures)) {
- const pictures = Array.from(input.pictures);
-
- pictures.forEach(picture => {
- if (!is_string(picture)) {
- return;
- }
- result.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;
- }
- result.params.set(name, input.params[name]);
- });
- }
- if (exists(input.shop)) {
- result.shop = input.shop;
- }
- return result;
- }
- #prepare_one(input) {
- if (typeof(input.name) === "undefined") {
- return null;
- }
- const result = new submission(input.name)
-
- if (typeof(input.description) !== "undefined") {
- result.description = input.description;
- }
- if (typeof(input.thumbnail) !== "undefined") {
- result.thumbnail = this.#prepare_image(input.thumbnail);
- }
- if (typeof(input.elements) === "undefined") {
- return result;
- }
- const elements = Array.from(input.elements);
- elements.forEach(data => {
- const current = this.#prepare_element(data);
- if (current !== null) {
- result.add(current);
- }
- });
- return 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;
- }
- }
- export { loader };
|