-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductsView.js
54 lines (47 loc) · 1.74 KB
/
ProductsView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class ProductsView {
#container;
#showBarcodeInHeading;
constructor(container, showBarcodeInHeading) {
this.#container = container;
this.#showBarcodeInHeading = showBarcodeInHeading;
}
displayProducts(products) {
this
.#sortProductsByName(products)
.forEach(product => this.#displayProduct(product));
}
#sortProductsByName(products) {
return products.toSorted(
(product1, product2) =>
Utils.compareStrsIgnoreCase(
product1.product_name,
product2.product_name));
}
#displayProduct(product) {
this.#container.appendChild(
ProductCardViewFactory.createProductCardView(
{
product_name_html: this.#getHeading(product),
image_url: product.image_url,
unwantedIngredients: Utils.jsonArray2Str(product.unwantedIngredients),
brands: Utils.jsonArray2Str(product.brands),
stores: Utils.jsonArray2Str([...product.stores].sort()),
countries: Utils.jsonArray2Str([...product.countries]),
barcode: product.barcode
}));
}
#getHeading(product) {
let heading = this.#getProductLink(product).outerHTML;
if (this.#showBarcodeInHeading) {
heading = `${heading} (${product.barcode})`;
}
return heading;
}
#getProductLink(product) {
return UIUtils.createLink(
{
href: 'https://world.openfoodfacts.org/product/' + product.barcode,
text: product.product_name !== "" ? product.product_name : "(product name unknown)"
});
}
}