-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
337 lines (331 loc) · 11.1 KB
/
script.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/****** Main Variables ******/
const menuIcon = document.querySelector(".nav__hamburger");
const closeIcon = document.querySelector(".nav__close");
const cartBtn = document.querySelector(".header__cart-icon");
const nextBtnMob = document.querySelector(".slider-mob__next-btn");
const prevBtnMob = document.querySelector(".slider-mob__prev-btn");
const incQtyBtn = document.querySelector(".product-buy__increase-icon");
const decQtyBtn = document.querySelector(".product-buy__decrease-icon");
const inpQty = document.getElementById("product-quantity");
const addToCartBtn = document.querySelector(".product-buy__add-cart");
const cartContent = document.querySelector(".cart-content");
const desktopSliderThumbs = Array.from(
document.querySelectorAll(".slider-desktop__thumb")
);
const desktopSliderImg = document.querySelector(
".slider-desktop__active-photo img"
);
const lightBoxSliderImg = document.querySelector(
".slider-lightbox__active-photo img"
);
const closeLightBox = document.querySelector(".slider-lightbox__close-icon");
const lightBoxSliderThumbs = Array.from(
document.querySelectorAll(".slider-lightbox__thumb")
);
const nextBtnLightBox = document.querySelector(".slider-lightbox__next-btn");
const prevBtnLightBox = document.querySelector(".slider-lightbox__prev-btn");
let emptyCartBtn;
let currentImgIndex = 0;
/******
* Start Events *
******/
// Show Navigation menu When click on Menu Icon
menuIcon.addEventListener("click", () => {
alterClass(document.querySelector(".nav__menu"), "nav__menu--visible", "add");
addOverlay();
});
// Hide menu Icon When click on Close Icon
closeIcon.addEventListener("click", () => {
alterClass(
document.querySelector(".nav__menu"),
"nav__menu--visible",
"remove"
);
removeOverlay();
});
// Show Cart Content When Click on Cart Icon
cartBtn.addEventListener("click", () => {
const cartContent = document.querySelector(".cart-content");
alterClass(cartContent, "cart-content--visible", "toggle");
});
/** Mobile Slider Events **/
// Slide to Next Item in Slider
nextBtnMob.addEventListener("click", () => slide("next"));
// Slide to Previous Item in Slider
prevBtnMob.addEventListener("click", () => slide("previous"));
/** Cusomize Input DOM **/
// Increase Quantity Input Value When Click on plus Icon
incQtyBtn.addEventListener("click", incQty);
// Decrease Quantity Input Value When Click on Minus Icon
decQtyBtn.addEventListener("click", decQty);
/** Cart Processing **/
// Add Selected Product data when Click on button && check if button is there
addToCartBtn.addEventListener("click", addToCart);
// Remove Product data From Cart when click on Bin Icon
if (emptyCartBtn) {
emptyCartBtn.addEventListener("click", emptyCart);
}
/*** Desktop Slider Events ***/
desktopSliderThumbs.forEach((element) => {
element.addEventListener("click", function () {
showClickedPhoto(
element,
desktopSliderThumbs,
desktopSliderImg,
"slider-desktop__thumb--active"
);
sliderTracker(
element,
lightBoxSliderThumbs,
lightBoxSliderImg,
"slider-lightbox__thumb--active"
);
});
});
/*** Lightbox Slider Evens***/
// Show LightBox
desktopSliderImg.addEventListener("click", () => {
alterClass(
document.querySelector(".slider-lightbox"),
"slider-lightbox--visible",
"add"
);
addOverlay();
});
// Hide LightBox
closeLightBox.addEventListener("click", () => {
alterClass(
document.querySelector(".slider-lightbox"),
"slider-lightbox--visible",
"remove"
);
removeOverlay();
});
// Show clicked Photo
lightBoxSliderThumbs.forEach((element) => {
element.addEventListener("click", function () {
showClickedPhoto(
element,
lightBoxSliderThumbs,
lightBoxSliderImg,
"slider-lightbox__thumb--active"
);
sliderTracker(
element,
desktopSliderThumbs,
desktopSliderImg,
"slider-desktop__thumb--active"
);
});
});
// Hide Light Box
document.addEventListener("click", (e) => {
if (e.target.classList.contains("overlay")) {
alterClass(
document.querySelector(".slider-lightbox"),
"slider-lightbox--visible",
"remove"
);
removeOverlay();
alterClass(
document.querySelector(".nav__menu"),
"nav__menu--visible",
"remove"
);
}
});
// Slide by controls
prevBtnLightBox.addEventListener("click", (e) => lightboxSlide("previous"));
nextBtnLightBox.addEventListener("click", (e) => lightboxSlide("next"));
/*****
** Main Functions **
*****/
function alterClass(element, class_name, processName) {
if (processName === "add") {
element.classList.add(class_name);
} else if (processName === "remove") {
if (element.classList.contains(class_name)) {
element.classList.remove(class_name);
}
} else if (processName === "toggle") {
element.classList.toggle(class_name);
}
}
function addOverlay() {
if (!document.body.contains(document.querySelector(".overlay"))) {
const overlayElement = document.createElement("div");
overlayElement.classList.add("overlay");
document.body.append(overlayElement);
}
}
function removeOverlay() {
if (document.body.contains(document.querySelector(".overlay"))) {
document.querySelector(".overlay").remove();
}
}
function slide(slideTo) {
const sliderBar = document.querySelector(".slider-mob__bar");
const slideWidth = document.querySelector(".slider-mob__item").offsetWidth;
const sliderItemsNum = Array.from(
document.querySelectorAll(".slider-mob__item")
);
if (slideTo === "next") {
if (sliderBar.scrollLeft < (sliderItemsNum.length - 1) * slideWidth) {
sliderBar.scrollLeft += slideWidth;
}
} else if (slideTo === "previous") {
if (sliderBar.scrollLeft > 0) {
sliderBar.scrollLeft -= slideWidth;
}
}
}
function incQty() {
inpQty.value++;
}
function decQty() {
if (inpQty.value > 0) {
inpQty.value--;
}
}
function addToCart() {
// Declare cart icon
const cartIcon = document.querySelector(".header__cart-icon");
// Create Span
const qtyDom = document.createElement("span");
// Get User Input Value
const qtyValue = Number(inpQty.value);
if (typeof qtyValue === "number" && qtyValue > 0) {
// Add Quantity Number to Span
qtyDom.innerHTML = qtyValue;
if (cartIcon.contains(cartIcon.querySelector("span"))) {
console.log("it contains");
Array.from(cartIcon.querySelectorAll("span")).map((e) => {
e.remove();
});
}
// Add Quantity Span to Dom
cartIcon.appendChild(qtyDom);
// Add Product to Cart
cartContent.innerHTML = `
<h3 class="cart-content__heading">Cart</h3>
<div class="cart-content__full">
<div class="cart-content__product">
<div class="cart-content__prod-img"><img src="images/image-product-1-thumbnail.jpg" alt="Product"></div>
<div class="cart-content__prod-text">
<h6>Autumn Limited Edition...</h6>
<div class="cart-content__prod-price">$125.00 * ${qtyValue} <span>$${
125.0 * qtyValue
}</span></div>
</div>
<div class="cart-content__bin-icon">
<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 fill-rule="nonzero" xlink:href="#a"/></svg>
</div>
</div>
<button class="cart-content__btn">Checkout</button>
</div>`;
emptyCartBtn = document.querySelector(".cart-content__bin-icon");
if (emptyCartBtn) {
emptyCartBtn.addEventListener("click", emptyCart);
}
} else {
alert(`You can't Buy ${inpQty.value} Products`);
}
}
function emptyCart() {
// Declare cart icon
const cartIcon = document.querySelector(".header__cart-icon");
/** Empty Cart Icon **/
// check if cart icon contains Quantity span
if (cartIcon.contains(cartIcon.querySelector("span"))) {
console.log("it contains");
// Loop through All of Quantity Span elements
Array.from(cartIcon.querySelectorAll("span")).map((e) => {
// Remove Span Element
e.remove();
});
}
cartContent.innerHTML = `
<h3 class="cart-content__heading">Cart</h3>
<div class="cart-content__empty">
Your cart is empty.
</div>
`;
}
function showClickedPhoto(element, siblings, visibleImg, class_name) {
const srcImg = element.getAttribute("data-src");
visibleImg.setAttribute("src", srcImg);
siblings.forEach((e) => {
if (e.classList.contains(class_name)) {
e.classList.remove(class_name);
}
});
element.classList.add(class_name);
}
function addOverlay() {
const overlay = document.createElement("div");
overlay.classList.add("overlay");
document.body.appendChild(overlay);
}
function removeOverlay() {
if (document.body.contains(document.body.querySelector(".overlay"))) {
document.body.querySelector(".overlay").remove();
}
}
function sliderTracker(element, siblings, visibleImg, class_name) {
const srcImg = element.getAttribute("data-src");
visibleImg.setAttribute("src", srcImg);
siblings.forEach((e) => {
alterClass(e, class_name, "remove");
if (element.getAttribute("data-src") === e.getAttribute("data-src")) {
alterClass(e, class_name, "add");
}
});
}
function lightboxSlide(slideTo) {
lightBoxSliderThumbs.forEach((e, i) => {
if (e.classList.contains("slider-lightbox__thumb--active")) {
currentImgIndex = i;
}
});
if (slideTo === "next") {
if (currentImgIndex < lightBoxSliderThumbs.length - 1) {
lightBoxSliderThumbs[currentImgIndex].classList.remove(
"slider-lightbox__thumb--active"
);
currentImgIndex++;
lightBoxSliderImg.setAttribute(
"src",
lightBoxSliderThumbs[currentImgIndex].getAttribute("data-src")
);
lightBoxSliderThumbs[currentImgIndex].classList.add(
"slider-lightbox__thumb--active"
);
sliderTracker(
lightBoxSliderThumbs[currentImgIndex],
desktopSliderThumbs,
desktopSliderImg,
"slider-desktop__thumb--active"
);
}
} else if (slideTo === "previous") {
if (currentImgIndex > 0) {
lightBoxSliderThumbs[currentImgIndex].classList.remove(
"slider-lightbox__thumb--active"
);
currentImgIndex--;
lightBoxSliderImg.setAttribute(
"src",
lightBoxSliderThumbs[currentImgIndex].getAttribute("data-src")
);
lightBoxSliderThumbs[currentImgIndex].classList.add(
"slider-lightbox__thumb--active"
);
sliderTracker(
lightBoxSliderThumbs[currentImgIndex],
desktopSliderThumbs,
desktopSliderImg,
"slider-desktop__thumb--active"
);
}
}
}