-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
389 lines (328 loc) · 11.3 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
`use strict`;
const container__upcoming = document.querySelector(".upcoming_container");
const container__topRated = document.querySelector(".toprated_container");
const container__tvSeries = document.querySelector(".series_container");
const topArr__nav = document.querySelector(".top-nav-arrow");
const btn__pricing = document.querySelectorAll(".btn--pricing");
const header = document.querySelector(".header");
const main = document.querySelector(`main`);
const footer = document.querySelector("footer");
const video = document.querySelector("video");
let movies_Array = [];
const section__hero = document.querySelector(".section--hero");
const section__upcoming = document.querySelector(".section-upcoming");
const section__highlight = document.querySelector(".section--movie-highlight");
const section__topRated = document.querySelector(".section--toprated");
const section__tvSeries = document.querySelector(".section--series");
const section__pricing = document.querySelector(".section-pricing");
const nav = document.querySelector(".header--top .nav");
const pricing__mothlyYearly = document.querySelector(".toggle-checkbox");
// HELPER FUNCTIONS
// 1.
const smoothScroll_helper = (clicked, class_list, scroll_to) => {
if (clicked.classList.contains(class_list))
scroll_to.scrollIntoView({
behavior: "smooth",
});
};
const hide_showTopNavArrow = () => {
const heightUsed = 650;
window.addEventListener("scroll", () => {
if (window.pageYOffset > heightUsed) topArr__nav.style.opacity = 1;
else topArr__nav.style.opacity = 0;
});
};
hide_showTopNavArrow();
btn__pricing.forEach((el) =>
el.addEventListener(
"click",
() => (window.location.href = "./login page/login.html")
)
);
const rendering_Movie = (movies, section) => {
section.innerHTML = ``;
movies.map((movie) => {
const movie_imgUrl = movie.imgUrl;
const movie_imgUrl_webp = movie.imgUrl_webp;
const movie_title = movie.title;
const movie_releaseYear = movie.releaseYear;
const movie_cert = movie.quality;
const movie_duration = movie.duration;
const movie_rating = movie.rating;
const movie_id = movie.movie_id;
const html = `
<div class="upcoming--movie movie--id" id='${movie_id}'>
<div role="img" aria-label="movie-cover-photo" class="img-holder">
<picture>
<source srcset="${movie_imgUrl_webp}" type="image/webp" />
<source srcset="${movie_imgUrl}" type="image/png" />
<img src="${movie_imgUrl}" alt="${movie_title} cover photo" />
</picture>
</div>
<div class="upcoming-details">
<div class="name-date">
<h3 class="tetiary-text--1">${movie_title}</h3>
<p class="release-year">${movie_releaseYear}</p>
</div>
<div class="download-duration">
<button class="btn btn--small btn-video-quality">${movie_cert}</button>
<div class="duration-rating">
<p class="upcoming-duration">
<ion-icon name="time-outline"></ion-icon>
<span class="duration-time">${movie_duration} min</span>
</p>
<p class="upcoming-duration">
<ion-icon name="star"></ion-icon>
<span class="upcoming-rating">${movie_rating}</span>
</p>
</div>
</div>
</div>
</div>
`;
section.insertAdjacentHTML("beforeend", html);
});
};
const rendering_section_highlight = async (movie) => {
try {
const html = `
<div role="img" aria-label="highlight-movie-cover" class="img-holder">
<img
src="${movie.imgUrl}"
alt="${movie.title}"
/>
</div>
<div class="movie-hightlight-details">
<p class="h1-intro">New Episodes</p>
<h1 class="primary-text">${movie.title}</h1>
<div class="hero-movie-details">
<div class="btns">
<button class="btn btn--small btn-pg">pg 13</button>
<button class="btn btn--small btn-hd">${movie.quality}</button>
</div>
<p class="hero-movie-type">
${movie.genre.split(",").map((el) => `<span>${el}</span>`)}
</p>
<div class="time-duration">
<p class="hero-movie-date">
<ion-icon name="calendar-outline"></ion-icon>
<span class="release-date">${movie.releaseYear}</span>
</p>
<p class="hero-movie-duration">
<ion-icon name="time-outline"></ion-icon>
<span class="duration">${movie.duration} min</span>
</p>
</div>
</div>
<p class="hightlight-description">
${movie.synopsis}
</p>
<div class="share-watch grid grid-template--3">
<p class="share">
<ion-icon name="share-social"></ion-icon>
<span>Share</span>
</p>
<p class="prime-text">
<span class="text">Prime Video</span>
<span>Streaming Channels</span>
</p>
<button class="btn btn--big btn-watch-now">
<ion-icon name="play-outline"></ion-icon>
<span>watch now</span>
</button>
</div>
</div>
<button class="btn btn--big btn-download">
<span>download</span>
<ion-icon name="download-outline"></ion-icon>
</button>
`;
section__highlight.innerHTML = `
<div class="container grid">
<div class="video">
<video hidden controls id="videoPlayer">
<source src="./video/trailer.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
<div id="player"></div>
</div>
</div>
`;
section__highlight
.querySelector(".container")
.insertAdjacentHTML("afterbegin", html);
vivid = movie.trailer;
} catch (error) {
console.error(error);
}
};
const mappingAPI_Data = async () => {
try {
const response = await fetch("data.json");
const [data, top_rated, best_tv_series] = await response.json();
movies_Array = [...data, ...top_rated, ...best_tv_series];
rendering_Movie(data, container__upcoming);
rendering_Movie(top_rated, container__topRated);
rendering_Movie(best_tv_series, container__tvSeries);
} catch (error) {
console.error(error);
}
};
mappingAPI_Data();
// ////////////////////////////////////
// EVENT LISTENERS
// ////////////////////////////////////
// 1.
topArr__nav.addEventListener("click", (e) => {
header.scrollIntoView({
behavior: "smooth",
});
});
// 2.
header.addEventListener("click", (e) => {
const clicked = e.target;
if (clicked.classList.contains("btn-signin"))
window.location.href = "./login page/login.html";
smoothScroll_helper(clicked, "tv-show", section__topRated);
smoothScroll_helper(clicked, "tv-series", section__tvSeries);
smoothScroll_helper(clicked, "pricing", section__pricing);
smoothScroll_helper(clicked, "movie", section__upcoming);
nav.classList.add("hidden");
if (clicked.closest(".menu-btns")) nav.classList.remove("hidden");
});
// 3.
section__hero.addEventListener("click", (e) => nav.classList.add("hidden"));
// 4.
section__highlight.addEventListener("click", (e) => {
const clicked = e.target;
if (clicked.closest(".btn-watch-now")) playThisVideo(vivid);
// DOWNLOADING VIDEO
// 1.
const getFileNameFromUrl = (url) => {
const parts = url.split("/");
return parts[parts.length - 1];
};
// DOWNLOADING VIDEO
// 2.
if (clicked.closest(".btn-download")) {
const videoUrl = video.currentSrc;
const fileName = getFileNameFromUrl(videoUrl);
const link = document.createElement("a");
link.href = videoUrl;
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
});
// 5.
main.addEventListener("click", (e) => {
const clicked = e.target;
const element = clicked.closest(".movie--id");
if (!element) return;
const movie__id = element.id;
const movieclicked = movies_Array.find(
(movie) => movie.movie_id === movie__id
);
rendering_section_highlight(movieclicked);
section__highlight.classList.remove("hidden");
section__highlight.scrollIntoView({
behavior: "smooth",
});
});
// 6.
footer.addEventListener("click", (e) => {
const clicked = e.target;
smoothScroll_helper(clicked, "tv-show", section__topRated);
smoothScroll_helper(clicked, "tv-series", section__tvSeries);
smoothScroll_helper(clicked, "pricing", section__pricing);
smoothScroll_helper(clicked, "movie", section__upcoming);
});
// 7.
pricing__mothlyYearly.addEventListener("change", () => {
const monthlyYearly = document.querySelectorAll(".prices");
const offer = document.querySelector(".offer");
offer.classList.toggle("visible");
const updatePrices = (newPrices) =>
monthlyYearly.forEach((priceElement, index) => {
priceElement.textContent = newPrices[index];
});
if (pricing__mothlyYearly.checked) updatePrices([999, 2999, 3499]);
else updatePrices([99, 299, 499]);
});
// ////////////////////////////////////
// LOGIC TO PLAYING YOUTUBE VIDEO
// ////////////////////////////////////
let tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
let firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function onYouTubeIframeAPIReady() {
console.log("Youtube is ready");
}
//
function onPlayerReady(event) {
event.target.playVideo();
}
//
let done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
//
function stopVideo() {
player.stopVideo();
}
//
let player;
function playThisVideo(vivid) {
const playerContainer = document.querySelector(".video");
const containerWidth = playerContainer.offsetWidth;
// Adjust dimensions based on container width
const playerHeight = (containerWidth / 16) * 9;
const playerWidth = containerWidth;
if (player) player.destroy();
player = new YT.Player("player", {
height: playerHeight,
width: playerWidth,
videoId: vivid,
playerVars: {
playersinline: 1,
autoplay: 0,
controls: 1,
},
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange,
},
});
}
// RESIZING IFRAME VIDEO --> RESPONSIVENESS
window.addEventListener("resize", () => {
if (player) return;
const playerContainer = document.querySelector(".video");
const containerWidth = playerContainer.offsetWidth;
const playerHeight = (containerWidth / 16) * 9;
const playerWidth = containerWidth;
player.setSize(playerWidth, playerHeight);
});
// HADNINGLIG FORMS
const handleSubmit = (event) => {
event.preventDefault();
const myForm = event.target;
const formData = new FormData(myForm);
fetch("/", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams(formData).toString(),
})
.then(() => alert("Sucessfully subscribed"))
.catch((error) => alert(error));
document.querySelector(".email-input").value = "";
};
document
.querySelector(".email-holder")
.addEventListener("submit", handleSubmit);