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