-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecommerce_goods.js
More file actions
80 lines (67 loc) · 2.01 KB
/
ecommerce_goods.js
File metadata and controls
80 lines (67 loc) · 2.01 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// A program checks the availability of the goods in a stock and changes the status of the goods on the site
var cardsData = [
{
isAvailable: true,
imgUrl: 'device/item-1.jpg',
text: 'Селфи-палка для начинающих',
price: 200,
isSpecial: false
},
{
isAvailable: false,
imgUrl: 'device/item-2.jpg',
text: 'Профессиональная селфи-палка',
price: 1500,
isSpecial: false
},
{
isAvailable: true,
imgUrl: 'device/item-3.jpg',
text: 'Непотопляемая селфи-палка',
price: 2500,
isSpecial: false
},
{
isAvailable: true,
imgUrl: 'device/item-4.jpg',
text: 'Селфи-палка «Следуй за мной»',
price: 4900,
isSpecial: true,
specialPrice: 100
}
];
var makeElement = function (tagName, className, text) {
var element = document.createElement(tagName);
element.classList.add(className);
if (text) {
element.textContent = text;
}
return element;
};
var createCard = function (product) {
var listItem = makeElement('li', 'product');
var title = makeElement('h2', 'product__title', product.text);
listItem.appendChild(title);
var picture = makeElement('img', 'product__image');
picture.src = product.imgUrl;
picture.alt = product.text;
listItem.appendChild(picture);
var price = makeElement('p', 'product__price', product.price);
listItem.appendChild(price);
var availabilityClass = 'product--available';
if (!product.isAvailable) {
availabilityClass = 'product--unavailable';
}
listItem.classList.add(availabilityClass);
if (product.isSpecial) {
listItem.classList.add('product--special');
var specialPrice = makeElement('p', 'product__special-price', product.specialPrice);
listItem.appendChild(specialPrice);
}
return listItem;
};
var cardList = document.querySelector('.products');
for (var i = 0; i < cardsData.length; i++) {
var cardItem = createCard(cardsData[i]);
cardList.appendChild(cardItem);
}