-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_cards.js
89 lines (75 loc) · 2.24 KB
/
product_cards.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// A second program checks the availability of the goods in a stock and changes the status of the goods on the site
var cardsData = [
{
inStock: true,
imgUrl: 'gllacy/choco.jpg',
text: 'Сливочно-кофейное с кусочками шоколада',
price: 310,
isHit: true,
specialOffer: 'Двойная порция сиропа бесплатно!'
},
{
inStock: false,
imgUrl: 'gllacy/lemon.jpg',
text: 'Сливочно-лимонное с карамельной присыпкой',
price: 125,
isHit: false
},
{
inStock: true,
imgUrl: 'gllacy/cowberry.jpg',
text: 'Сливочное с брусничным джемом',
price: 170,
isHit: false
},
{
inStock: true,
imgUrl: 'gllacy/cookie.jpg',
text: 'Сливочное с кусочками печенья',
price: 250,
isHit: false
},
{
inStock: true,
imgUrl: 'gllacy/creme-brulee.jpg',
text: 'Сливочное крем-брюле',
price: 190,
isHit: false
}
];
var makeElement = function (tagName, className, text) {
var element = document.createElement(tagName);
element.classList.add(className);
if (text) {
element.textContent = text;
}
return element;
};
var renderCards = function (good) {
var listItem = makeElement('li', 'good');
var title = makeElement('h2', 'good__description', good.text);
listItem.appendChild(title);
var picture = makeElement('img', 'good__image');
picture.src = good.imgUrl;
picture.alt = good.text;
listItem.appendChild(picture);
var price = makeElement('p', 'good__price', (good.price + '₽/кг'));
listItem.appendChild(price);
var availabilityClass = 'good--available';
if (!good.inStock) {
availabilityClass = 'good--unavailable';
}
listItem.classList.add(availabilityClass);
if (good.isHit) {
listItem.classList.add('good--hit');
var specialPrice = makeElement('p', 'good__special-offer');
specialPrice.textContent = good.specialOffer;
listItem.appendChild(specialPrice);
}
return listItem;
};
var cardList = document.querySelector('.goods');
for (i = 0; i < cardsData.length; i++) {
var cardItem = renderCards(cardsData[i]);
cardList.appendChild(cardItem);
}