-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework7.js
152 lines (129 loc) · 3.99 KB
/
homework7.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var cart = [];
var products = [
{
id: 1,
name: 'Фубтолка',
price: 1000,
color: 'Желтый',
},
{
id: 2,
name: 'Джинсы',
price: 3000,
color: 'Синий',
},
{
id: 3,
name: 'Носки',
price: 300,
color: 'Черный',
},
];
var $cart = document.getElementById('cart');
var $catalog = document.getElementById('catalog');
var $template = document.getElementById('template').children[0];
$catalog.addEventListener('click', handleBuyClick);
$cart.addEventListener('click', handleRemoveClick);
function indexOf(item) {
for (var i = 0; i < cart.length; i++) {
if (cart[i].id === item.id) {
return i;
}
}
return -1;
}
function handleRemoveClick(event) {
if (event.target.classList.contains('remove')) {
var idx = indexOf({id: +event.target.dataset.id});
if (cart[idx].quantity > 1) {
cart[idx].quantity--;
} else {
cart.splice(idx, 1);
}
countBasketPrice(cart);
}
}
function handleBuyClick(event) {
if (event.target.classList.contains('buy')) {
var cartItem = {
id: +event.target.dataset.id,
name: event.target.dataset.name,
price: event.target.dataset.price,
};
var idx = indexOf(cartItem);
if (idx === -1) {
// товара в корзине еще нет
cartItem.quantity = 1;
cart.push(cartItem);
} else {
cart[idx].quantity++;
}
countBasketPrice(cart);
}
event.preventDefault();
}
function countBasketPrice(cart) {
$cart.innerHTML = '';
if (cart && cart.length > 0) {
var $ul = document.createElement('ul');
var sum = 0;
var count = 0;
for (var i = 0; i < cart.length; i++) {
var $li = document.createElement('li');
var $span = document.createElement('span');
var $remove = document.createElement('button');
// var $edit
$span.textContent = cart[i].name + ' (' + cart[i].quantity + ')';
$remove.textContent = 'x';
$remove.dataset.id = cart[i].id;
$remove.classList.add('remove');
$li.appendChild($span);
$li.appendChild($remove);
$ul.appendChild($li);
sum += cart[i].price * cart[i].quantity;
count += cart[i].quantity;
}
var $total = document.createElement('div');
$total.textContent = 'В корзине: ' + count + ' товаров на сумму ' + sum + ' рублей';
$cart.appendChild($ul);
$cart.appendChild($total);
} else {
$cart.textContent = 'Корзина пуста';
}
}
function buildCatalog(products) {
for (var i = 0; i < products.length; i++) {
var $item = $template.cloneNode(true);
var keys = Object.keys(products[i]);
var $button = $item.querySelector('.buy');
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var $element = $item.querySelector('.' + key);
if ($element) {
$element.textContent = products[i][key];
}
$button.dataset[key] = products[i][key];
}
$catalog.appendChild($item);
}
}
buildCatalog(products);
countBasketPrice(cart);
var $nav = document.querySelector(".nav");
$nav.addEventListener("click", handleNavigation);
var $delivery = document.querySelector(".delivery");
var $comment = document.querySelector(".comment");
var $structure = [$cart, $catalog, $delivery, $comment];
function handleNavigation() {
for(var i = 0; i <= $structure.length-1; i++) {
var j = i + 1;
$structure[i].classList.toggle("hide");
$structure[j].classList.toggle("hide");
if( i > $structure.length) {
i = 0;
}
if( j > $structure.length) {
j =1;
}
}
}