-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
260 lines (209 loc) · 8.32 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
"use strict";
const hamburger = document.getElementById("hamburger");
const navMenu = document.querySelector(".nav-menu");
const bgBlurMobile = document.querySelector(".bgBlur-mobile");
const bgBlurDesktop = document.querySelector(".bgBlur-desktop");
const close = document.getElementById("close");
const mainSlider = document.querySelector(".slider");
const mainSlides = document.querySelectorAll(".slide");
const mainButtonLeft = document.querySelector(".slider .button-left");
const mainButtonRight = document.querySelector(".slider .button-right");
const mainThumbnailContainer = document.querySelector(".thumbnail-container");
const lightboxSlides = document.querySelectorAll(".lightbox-slide");
const lightboxButtonLeft = document.querySelector(".lightbox-button-left");
const lightboxButtonRight = document.querySelector(".lightbox-button-right");
const lightboxThumbnailContainer = document.querySelector(
".lightbox-thumbnail-container"
);
const closeLightbox = document.querySelector(".button-close-lightbox");
const lightboxModal = document.querySelector(".lightbox-modal");
const cartIcon = document.querySelector(".nav-right img");
const basket = document.querySelector("nav .basket");
const incrementButton = document.querySelector(".increment");
const decrementButton = document.querySelector(".decrement");
const shoesUnity = document.querySelector(".show-unity");
const basketContainer = document.querySelector(".basket-container");
const basketShoesUnityIcon = document.querySelector(".cart-icon span");
const addToCartButton = document.querySelector(".input-block > button");
let currentSlide = 0;
/////////////////////////////////////////////////
// Update UI based on local storage
const updateCart = () => {
basketShoesUnityIcon.textContent = +shoesUnity.textContent;
basketShoesUnityIcon.classList.add("show-cart-number");
// new URL("./images/image-product-1.jpg", import.meta.url);
// It's how parcel access images from within JS
// It's necessary so the a build can be done successfully
// for more info, check https://parceljs.org/languages/javascript/#url-dependencies
basketContainer.innerHTML = `
<div class="detail">
<img
src="${new URL("./images/image-product-1.jpg", import.meta.url)}"
alt="Shoes image"
height="60"
width="60"
/>
<div class="shoes-detail-container">
<p>Autumn Limited Edition...</p>
<div class="shoes-detail">
<span>$125.00 x </span>
<span class="shoes-unity">${+shoesUnity.textContent}</span>
<span class="shoes-total-price">$${(+shoesUnity.textContent * 125).toFixed(
2
)}</span>
</div>
</div>
<button type="button" class="erase-basket"></button>
</div>
<button type="button" class="checkout">Checkout</button>`;
const eraseBasket = document.querySelector(".erase-basket");
const checkoutButton = document.querySelector(".checkout");
eraseBasket.addEventListener("click", () => {
const basketHeight = parseFloat(getComputedStyle(basketContainer).height);
basketContainer.innerHTML = "Your Cart is empty";
basketContainer.style.height = `${basketHeight}px`;
basketShoesUnityIcon.classList.remove("show-cart-number");
localStorage.removeItem("shoesToBuy");
});
checkoutButton.addEventListener("click", () => {
document.body.classList.add("checkout-body");
document.body.innerHTML = `
<p>Thank you for trying out this app!</p>
`;
localStorage.removeItem("shoesToBuy");
});
};
if (localStorage.getItem("shoesToBuy")) {
shoesUnity.textContent = +localStorage.getItem("shoesToBuy");
updateCart();
}
/////////////////////////////////////////////////
// handles hamburger toggle
const toggleMenuNav = () => {
navMenu.classList.toggle("nav-menu-active");
bgBlurMobile.classList.toggle("show-bg");
};
hamburger.addEventListener("click", () => {
toggleMenuNav();
});
close.addEventListener("click", () => {
toggleMenuNav();
});
/////////////////////////////////////////////////
// define functions used by the sliders
// slider -> slides parent element, which is the container for the slides
// slides -> elements that should move
// there are two slides: mainSlides and lightboxSlides
const goToSlide = function (slides) {
slides.forEach(
(slide, index) =>
(slide.style.transform = `translateX(${100 * (index - currentSlide)}%)`)
);
};
const showSelectedThumbnail = thumbnailContainer => {
thumbnailContainer
.querySelectorAll(".thumbnail")
.forEach(thumbnail => thumbnail.classList.remove("thumbnail-selected"));
thumbnailContainer
.querySelector(`.thumbnail[data-thumb="${currentSlide}"]`)
.classList.add("thumbnail-selected");
};
const moveSlides = slides => {
goToSlide(slides);
showSelectedThumbnail(
slides[0].parentElement.parentElement.querySelector(".thumb-cont")
);
};
const goToNextSlide = function (slides) {
currentSlide === slides.length - 1 ? (currentSlide = 0) : currentSlide++;
moveSlides(slides);
};
const goToPreviousSlide = function (slides) {
currentSlide === 0 ? (currentSlide = slides.length - 1) : currentSlide--;
moveSlides(slides);
};
/////////////////////////////////////////////////
// initiate main slider
moveSlides(mainSlides);
/////////////////////////////////////////////////
// Event handlers for buttons from main
mainButtonRight.addEventListener("click", () => goToNextSlide(mainSlides));
mainButtonLeft.addEventListener("click", () => goToPreviousSlide(mainSlides));
mainThumbnailContainer.addEventListener("click", event => {
if (!event.target.classList.contains("thumbnail")) return;
currentSlide = +event.target.dataset.thumb;
// currentSlide needs to be updated first
// so that showSelectedThumbnail() works properly
moveSlides(mainSlides);
});
/////////////////////////////////////////////////
// handles correct lightbox slide display after clicking on a slide from main
mainSlider.addEventListener("click", event => {
// prevent lightbox to appear in mobile screen size
if (getComputedStyle(hamburger).display !== "none") return;
bgBlurDesktop.classList.toggle("show-bg");
lightboxModal.classList.toggle("show-lightbox-modal");
currentSlide = +event.target.closest(".slide").dataset.slide;
// update currentSlide to the slide clicked
moveSlides(lightboxSlides);
lightboxButtonRight.addEventListener("click", () =>
goToNextSlide(lightboxSlides)
);
lightboxButtonLeft.addEventListener("click", () =>
goToPreviousSlide(lightboxSlides)
);
lightboxThumbnailContainer.addEventListener("click", event => {
if (!event.target.classList.contains("thumbnail")) return;
currentSlide = +event.target.dataset.thumb;
// currentSlide needs to be updated first
// so that showSelectedThumbnail() works properly
moveSlides(lightboxSlides);
});
});
/////////////////////////////////////////////////
// handles keyboard navigation
const defineSlidesToWork = (slides, event) => {
if (event.key === "ArrowRight") goToNextSlide(slides);
if (event.key === "ArrowLeft") goToPreviousSlide(slides);
};
document.addEventListener("keydown", event => {
if (lightboxModal.classList.contains("show-lightbox-modal")) {
// if the lightbox modal is active, handles keyboard navigation on the lightbox modal
defineSlidesToWork(lightboxSlides, event);
} else {
// else (in this case the lightbox is inactive), handles keyboard navigation on the main slides
defineSlidesToWork(mainSlides, event);
}
});
/////////////////////////////////////////////////
// handles lightbox modal closing
const toggleLightboxModal = () => {
bgBlurDesktop.classList.toggle("show-bg");
lightboxModal.classList.toggle("show-lightbox-modal");
// update main slides
moveSlides(mainSlides);
};
bgBlurDesktop.addEventListener("click", () => toggleLightboxModal());
closeLightbox.addEventListener("click", () => {
toggleLightboxModal();
});
document.addEventListener("keydown", event => {
if (!lightboxModal.classList.contains("show-lightbox-modal")) return;
if (event.key === "Escape") toggleLightboxModal();
});
cartIcon.addEventListener("click", () => {
basket.classList.toggle("basket-active");
});
////////////////////////////////////////////////
// Cart functionality
incrementButton.addEventListener("click", () => {
shoesUnity.textContent = +shoesUnity.textContent + 1;
});
decrementButton.addEventListener("click", () => {
if (+shoesUnity.textContent === 1) return;
shoesUnity.textContent = +shoesUnity.textContent - 1;
});
addToCartButton.addEventListener("click", () => {
updateCart();
localStorage.setItem("shoesToBuy", `${shoesUnity.textContent}`);
});