-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
271 lines (226 loc) · 8.82 KB
/
app.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const closeMenuBtn = document.querySelector('#closeMenu');
const openMenuBtn = document.querySelector('#openMenu');
const navPanelContainer = document.querySelector('.nav__panel-container');
const qty = document.querySelector('#qty');
const btns = document.querySelectorAll('.btn');
const clickCart = document.querySelector('.cart-Icon');
const checkoutBtn = document.querySelector('#checkout');
const cartBody = document.querySelector('#cart__body');
const cartPanel = document.querySelector('#cartPanel');
const trashBtn = document.querySelector('#trash');
const round = document.querySelector('#round');
const gallery = document.querySelectorAll('.pic');
const heroImg = document.querySelector('.hero');
const btnNext = document.querySelector('.next');
const btnPrevious = document.querySelector('.previous');
//set initial counter
let count = 0;
function toggleMenu(){
navPanelContainer.classList.toggle('expanded');
}
// function for increasing and decreasing
btns.forEach(function (btn){
btn.addEventListener('click', function(e){
const style = e.currentTarget.classList;
if (style.contains('decrease')){
if (count > 0){
count--;
}
} else if (style.contains('increase')){
count++;
}
qty.textContent = count;
updateCartState(count);
round.textContent = count;
});
});
//removing the cart barge
if (parseInt(qty.textContent) === 0 && count === 0){
round.style.display = "none";
}
// adding the cart barge
function cartBarge(){
if (parseInt(round.textContent) > 0) {
round.style.display = "block";
round.style.color = "white";
round.style.fontSize = "0.7rem";
round.style.alignContent = "center";
round.style.textAlign = "center";
qty.textContent = 0;
}
}
//Object containing the details about product added to cart
const checkoutState = {
'default' : ` <div></div>
<p class="empty">Your cart is empty.</p>
<div></div>`,
'items' : `
<div class="cart__body--content">
<div class="cart__body-description">
<img class="cart__img" src="/CH1-team-6/ecommerce-product-page-main/images/image-product-1-thumbnail.jpg" alt="shoes">
<div class="text-trash">
<div class="prod-content">
<p class="product--name">Fall Limited Edition Sneakers</p>
<p class="product--amt">
<span>$125</span>
<span>×</span>
<span id="Amt">3</span>
<span id="total" class="total">$375</span>
</p>
</div>
<button class="trash" aria-label="Remove product from cart">
<svg width="14" height="16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<path
d="M0 2.625V1.75C0 1.334.334 1 .75 1h3.5l.294-.584A.741.741 0 0 1 5.213 0h3.571a.75.75 0 0 1 .672.416L9.75 1h3.5c.416 0 .75.334.75.75v.875a.376.376 0 0 1-.375.375H.375A.376.376 0 0 1 0 2.625Zm13 1.75V14.5a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 1 14.5V4.375C1 4.169 1.169 4 1.375 4h11.25c.206 0 .375.169.375.375ZM4.5 6.5c0-.275-.225-.5-.5-.5s-.5.225-.5.5v7c0 .275.225.5.5.5s.5-.225.5-.5v-7Zm3 0c0-.275-.225-.5-.5-.5s-.5.225-.5.5v7c0 .275.225.5.5.5s.5-.225.5-.5v-7Zm3 0c0-.275-.225-.5-.5-.5s-.5.225-.5.5v7c0 .275.225.5.5.5s.5-.225.5-.5v-7Z"
id="a" />
</defs>
<use id="trash" fill="#C3CAD9" fill-rule="nonzero" xlink:href="#a" />
</svg>
</button>
</div>
</div>
</div>
<button class="cart__body--btn" id="cart__checkout" >Checkout
</button>`
}
// function for toggling cart
function togglecart(){
clickCart.getAttribute('aria-expanded') === 'false' ? clickCart.setAttribute('aria-expanded', 'true') : clickCart.setAttribute('aria-expanded', 'false');
clickCart.getAttribute('aria-expanded') === 'false' ? cartPanel.setAttribute('disabled', 'true') : cartPanel.removeAttribute('disabled');
count = 0;
}
//function used to toggle the product added to cart
function updateCartState(num){
if(num === 0){
cartBody.innerHTML = checkoutState.default;
round.style.display = "none";
} else {
cartBody.innerHTML = checkoutState.items;
const PRICE = 125;
const amt = document.querySelector('#Amt');
const total = document.querySelector('#total');
amt.textContent = num;
total.textContent = `$${num * PRICE}.00`;
}
}
function onThumbClick(event) {
gallery.forEach(img => {
img.classList.remove('active');
});
event.target.parentElement.classList.add('active');
heroImg.src = event.target.src.replace('-thumbnail', '');
}
function handleBtnClickNext() {
let imageIndex = getCurrentImageIndex();
imageIndex++;
if (imageIndex > 4) {
imageIndex = 1;
}
setHeroImage(imageIndex);
}
function handleBtnClickPrevious() {
let imageIndex = getCurrentImageIndex();
imageIndex--;
if (imageIndex < 1) {
imageIndex = 4;
}
setHeroImage(imageIndex);
}
function getCurrentImageIndex() {
const imageIndex = parseInt(heroImg.src.split('\\').pop().split('/').pop().replace('.jpg', '').replace('image-product-', ''));
return imageIndex;
}
function setHeroImage(imageIndex) {
heroImg.src = `/CH1-team-6/ecommerce-product-page-main/images/image-product-${imageIndex}.jpg`;
//images are not sync
gallery.forEach(img => {
img.classList.remove('active');
});
//set active thumbnail
gallery[imageIndex-1].classList.add('active');
}
gallery.forEach(img => {
img.addEventListener('click', onThumbClick);
});
/* EVENT LISTNERS */
checkoutBtn.addEventListener('click', () => {
togglecart();
cartBarge();
});
clickCart.addEventListener('click', togglecart);
cartPanel.addEventListener('click', (e) => {
e.currentTarget === e.target && togglecart();
e.target === document.querySelector('#trash') && updateCartState(0);
//checkout button
e.target === document.querySelector('#cart__checkout') && updateCartState(0);
});
openMenuBtn.addEventListener('click',toggleMenu);
closeMenuBtn.addEventListener('click',toggleMenu);
btnNext.addEventListener('click', handleBtnClickNext);
btnPrevious.addEventListener('click', handleBtnClickPrevious);
const overLay = document.querySelector('.overlay');
const lightBox = document.querySelector('.img-text');
const btnclose = document.querySelector('.btnClose');
let lightboxGallery;
let lightboxHero;
heroImg.addEventListener('click', onHeroImgClick);
function onHeroImgClick(){
if (window.innerWidth >= 1200){
if (overLay.childElementCount == 1){
const newNode = lightBox.cloneNode(true);
overLay.appendChild(newNode);
const btnOverlayClose = document.querySelector('#btnOverlayClose');
btnOverlayClose.addEventListener('click', onBtnOverlayClose);
lightboxGallery = overLay.querySelectorAll('.pic')
lightboxHero = overLay.querySelector('.hero');
lightboxGallery.forEach(img => {
img.addEventListener('click', onThumbClickLightbox);
});
const btnOverlayNext = overLay.querySelector('.next');
const btnOverlayPrevious = overLay.querySelector('.previous');
btnOverlayNext.addEventListener('click', handleBtnClickNextOverlay);
btnOverlayPrevious.addEventListener('click', handleBtnClickPreviousOverlay);
}
overLay.classList.remove('hidden');
}
}
function onBtnOverlayClose(){
btnclose.parentElement.classList.add('hidden');
}
function onThumbClickLightbox(event) {
lightboxGallery.forEach(img => {
img.classList.remove('active');
});
event.target.parentElement.classList.add('active');
lightboxHero.src = event.target.src.replace('-thumbnail', '');
}
function handleBtnClickNextOverlay() {
let imageIndex = getOverlayCurrentImageIndex();
imageIndex++;
if (imageIndex > 4) {
imageIndex = 1;
}
setOverlayHeroImage(imageIndex);
}
function handleBtnClickPreviousOverlay() {
let imageIndex = getOverlayCurrentImageIndex();
imageIndex--;
if (imageIndex < 1) {
imageIndex = 4;
}
setOverlayHeroImage(imageIndex);
}
function getOverlayCurrentImageIndex() {
const imageIndex = parseInt(lightboxHero.src.split('\\').pop().split('/').pop().replace('.jpg', '').replace('image-product-', ''));
return imageIndex;
}
function setOverlayHeroImage(imageIndex) {
lightboxHero.src = `/CH1-team-6/ecommerce-product-page-main/images/image-product-${imageIndex}.jpg`;
//images are not sync
lightboxGallery.forEach(img => {
img.classList.remove('active');
});
//set active thumbnail
lightboxGallery[imageIndex-1].classList.add('active');
}