2020-12-07 03:02:50 +01:00
|
|
|
/**
|
|
|
|
* Shows the reviews and scoring base on mangrove.reviesw
|
|
|
|
*/
|
2020-12-08 23:44:34 +01:00
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
|
|
|
import {Review} from "../../Logic/Web/Review";
|
|
|
|
import {UIElement} from "../UIElement";
|
|
|
|
import {Utils} from "../../Utils";
|
|
|
|
import Combine from "../Base/Combine";
|
|
|
|
import {FixedUiElement} from "../Base/FixedUiElement";
|
|
|
|
import Translations from "../i18n/Translations";
|
|
|
|
|
2020-12-07 03:02:50 +01:00
|
|
|
export default class ReviewElement extends UIElement {
|
2020-12-08 23:44:34 +01:00
|
|
|
private readonly _reviews: UIEventSource<Review[]>;
|
|
|
|
private readonly _subject: string;
|
|
|
|
private _middleElement: UIElement;
|
2020-12-07 03:02:50 +01:00
|
|
|
|
2020-12-08 23:44:34 +01:00
|
|
|
constructor(subject: string, reviews: UIEventSource<Review[]>, middleElement: UIElement) {
|
2020-12-07 03:02:50 +01:00
|
|
|
super(reviews);
|
2020-12-08 23:44:34 +01:00
|
|
|
this._middleElement = middleElement;
|
|
|
|
if(reviews === undefined){
|
|
|
|
throw "No reviews UIEVentsource Given!"
|
|
|
|
}
|
2020-12-07 03:02:50 +01:00
|
|
|
this._reviews = reviews;
|
2020-12-08 23:44:34 +01:00
|
|
|
this._subject = subject;
|
2020-12-07 03:02:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
InnerRender(): string {
|
|
|
|
|
2020-12-07 03:31:13 +01:00
|
|
|
function genStars(rating: number) {
|
2020-12-08 23:44:34 +01:00
|
|
|
if(rating === undefined){
|
|
|
|
return Translations.t.reviews.no_rating;
|
|
|
|
}
|
|
|
|
if(rating < 10){
|
|
|
|
rating = 10;
|
|
|
|
}
|
2020-12-07 03:31:13 +01:00
|
|
|
const scoreTen = Math.round(rating / 10);
|
|
|
|
return new Combine([
|
|
|
|
"<img src='./assets/svg/star.svg' />".repeat(Math.floor(scoreTen / 2)),
|
|
|
|
scoreTen % 2 == 1 ? "<img src='./assets/svg/star_half.svg' />" : ""
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2020-12-07 03:02:50 +01:00
|
|
|
const elements = [];
|
2020-12-07 03:31:13 +01:00
|
|
|
const revs = this._reviews.data;
|
|
|
|
revs.sort((a,b) => (b.date.getTime() - a.date.getTime())); // Sort with most recent first
|
|
|
|
const avg = (revs.map(review => review.rating).reduce((a, b) => a + b, 0) / revs.length);
|
|
|
|
elements.push(
|
|
|
|
new Combine([
|
|
|
|
genStars(avg).SetClass("stars"),
|
2020-12-08 23:44:34 +01:00
|
|
|
`<a href='https://mangrove.reviews/search?sub=${this._subject}'>`,
|
2020-12-07 03:31:13 +01:00
|
|
|
Translations.t.reviews.title
|
2020-12-08 23:44:34 +01:00
|
|
|
.Subs({count: "" + revs.length}),
|
|
|
|
"</a>"
|
2020-12-07 03:31:13 +01:00
|
|
|
])
|
2020-12-07 03:02:50 +01:00
|
|
|
|
2020-12-07 03:31:13 +01:00
|
|
|
.SetClass("review-title"));
|
2020-12-08 23:44:34 +01:00
|
|
|
|
|
|
|
elements.push(this._middleElement);
|
2020-12-07 03:02:50 +01:00
|
|
|
|
2020-12-07 03:31:13 +01:00
|
|
|
elements.push(...revs.map(review => {
|
2020-12-07 03:02:50 +01:00
|
|
|
const d = review.date;
|
|
|
|
return new Combine(
|
|
|
|
[
|
|
|
|
new Combine([
|
2020-12-07 03:31:13 +01:00
|
|
|
genStars(review.rating)
|
|
|
|
.SetClass("review-rating"),
|
|
|
|
new FixedUiElement(review.comment).SetClass("review-comment")
|
|
|
|
]).SetClass("review-stars-comment"),
|
2020-12-07 03:02:50 +01:00
|
|
|
|
2020-12-07 03:31:13 +01:00
|
|
|
new Combine([
|
2020-12-08 23:44:34 +01:00
|
|
|
new Combine([
|
2020-12-07 03:02:50 +01:00
|
|
|
|
2020-12-08 23:44:34 +01:00
|
|
|
new FixedUiElement(review.author).SetClass("review-author"),
|
|
|
|
review.affiliated ? Translations.t.reviews.affiliated_reviewer_warning : "",
|
|
|
|
]).SetStyle("margin-right: 0.5em"),
|
2020-12-07 03:31:13 +01:00
|
|
|
new FixedUiElement(`${d.getFullYear()}-${Utils.TwoDigits(d.getMonth() + 1)}-${Utils.TwoDigits(d.getDate())} ${Utils.TwoDigits(d.getHours())}:${Utils.TwoDigits(d.getMinutes())}`)
|
|
|
|
.SetClass("review-date")
|
|
|
|
]).SetClass("review-author-date")
|
2020-12-07 03:02:50 +01:00
|
|
|
|
|
|
|
]
|
|
|
|
).SetClass("review-element")
|
|
|
|
}));
|
|
|
|
elements.push(
|
|
|
|
new Combine([
|
|
|
|
Translations.t.reviews.attribution,
|
|
|
|
"<img src='./assets/mangrove_logo.png'>"
|
|
|
|
])
|
|
|
|
|
|
|
|
.SetClass("review-attribution"))
|
|
|
|
|
|
|
|
return new Combine(elements).SetClass("review").Render();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|