-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
375 lines (340 loc) · 11.9 KB
/
Copy pathapp.js
File metadata and controls
375 lines (340 loc) · 11.9 KB
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
export const apiKey = "api_key=7f77f1bc80c6fe267d4de7f850631d9d"
export const baseUrl = "https://api.themoviedb.org/3/"
const apiUrl = baseUrl + "discover/movie?sort_by=popularity.desc&" + apiKey + '&include_adult=true'
export const imageUrl = "https://image.tmdb.org/t/p/w500"
const section = document.querySelector('.movieSection')
const search = document.querySelector('#search')
export const form = document.querySelector('#form')
const searchUrl = baseUrl + 'search/movie?' + apiKey
export const submit = document.querySelector('#submit')
const overlay = document.querySelector('.overlay')
const other = document.querySelector('#other')
const before = window.getComputedStyle(other,'::before')
const imgDetail = document.querySelector('#other img')
import { convertMinutesToHours,getGenresString,covertDate} from "./other.js"
export const prev = document.querySelector('#prev')
export const next = document.querySelector('#next')
export const current = document.querySelector('#current')
export var currentPage = 1
export var prevPage = 2
export var nextPage = 3
export var totalPage = 200
export var lastUrl = ''
getMovie(apiUrl)
/***
* @param {string} url
*/
export function getMovie(url) {
lastUrl = url
fetch(url)
.then(res => res.json())
.then(data => {
console.log(data);
if(data.results.length !== 0){
showMovie(data.results)
currentPage = data.page
prevPage = currentPage - 1
nextPage = currentPage + 1
totalPage = data.total_pages
if (totalPage === 1) {
prev.classList.add('disable')
next.classList.add('disable')
}else if (currentPage <= 1) {
prev.classList.add('disable')
next.classList.remove('disable')
} else if(currentPage >= totalPage){
prev.classList.remove('disable')
next.classList.add('disable')
}else{
prev.classList.remove('disable')
next.classList.remove('disable')
}
current.textContent = currentPage + ' /' + ` ${totalPage}`
function scrollToTop() {
window.scrollTo(
{
top: 0,
behavior: "smooth"
}
)
}
window.addEventListener('load',scrollToTop())
}else{
section.innerHTML = `
<ion-icon name="warning-outline" class='iconFound'></ion-icon>
<h1 class="noFound" >no results found </h1>
`
}
}).catch(e=>{
const errorPage = `
<div class="error">
<h1>impossible de charger la page !!</h1>
<ion-icon name="warning-outline"></ion-icon>
</div>
`
document.body.innerHTML = errorPage;
})
}
export function showMovie(data) {
section.innerHTML = ""
data.forEach(movie => {
const film = document.createElement('div')
film.classList.add('movieBloc')
film.innerHTML = `
<a href="#"><img id='${movie.id}' src="${movie.poster_path?imageUrl + movie.poster_path: "https://via.placeholder.com/150/65ad4f"}" alt="image"></a>
<div class="movieInfo">
<h1 id="movieName">${movie.title || movie.name}</h1><span class="${getColors(movie.vote_average)}">${movie.vote_average}</span>
</div>
<div class="overview">
<h3>overview</h3>
${movie.overview}
</div>
`
section.appendChild(film)
const img = document.getElementById(movie.id);
const overlayIcon = document.querySelector('#closeOverlay')
img.addEventListener('click',()=>{
overlay.style.width = '100vw'
overlay.style.opacity = 1
getMovieDetail(movie.id)
getActors(movie.id)
getVideo(movie.id)
other.style.setProperty('--before',`url(https://image.tmdb.org/t/p/original${movie.backdrop_path})`)
imgDetail.setAttribute('src',movie.poster_path?imageUrl + movie.poster_path: "https://via.placeholder.com/150/65ad4f")
})
overlayIcon.addEventListener('click',()=>{
overlay.style.width = 0
overlay.style.opacity = 0
})
});
}
const moviePLus = document.querySelector('#moviePlus')
function getMovieDetail(id) {
fetch(baseUrl + `movie/${id}?` + apiKey)
.then(res => res.json())
.then(detail => {
console.log(detail);
const {title,budget,genres,original_language,overview,runtime,release_date,tagline,status,revenue} = detail
moviePLus.innerHTML = ""
const info = `
<div id="first">
<h1>${title}</h1>
<ul>
<li>${covertDate(release_date)}</li>
<li>${getGenresString(genres)}</li>
<li>${convertMinutesToHours(runtime)}</li>
</ul>
</div>
<div id="second">
<p><em>${tagline}</em></p>
<div>
<h3>overview</h3>
<p>
${overview}
</p>
</div>
</div>
<div id="third">
<div class="thirdDet">
<h4>status</h4>
<div>${status}</div>
</div>
<div class="thirdDet">
<h4>original language</h4>
<div>${original_language}</div>
</div>
<div class="thirdDet">
<h4>budget</h4>
<div>${budget}$</div>
</div>
<div class="thirdDet">
<h4>revenue</h4>
<div>${revenue}$</div>
</div>
</div>
`
moviePLus.innerHTML = info
})
}
function getActors(id) {
fetch(`${baseUrl}movie/${id}/credits?${apiKey}`)
.then(response => response.json())
.then(data => {
console.log(data.cast);
showMovieActor(data.cast);
});
}
const movieActor = document.querySelector('#movieActor')
function showMovieActor(cast) {
movieActor.innerHTML = ""
cast.forEach(person => {
const actorBloc = document.createElement('div')
actorBloc.classList.add('actorBloc')
actorBloc.innerHTML = `
<img src=${person.profile_path? imageUrl + person.profile_path:"https://via.placeholder.com/150/65ad4f"} alt="actorImage">
<div><h2>${person.original_name}</h2><span><i>${person.character?person.character:"????"}</i></span></div>
`
movieActor.appendChild(actorBloc)
})
}
/**
* @param {number} vote
* @returns {string}
*/
export function getColors(vote) {
if (vote >= 8) {
return 'green'
} else if(vote >= 5){
return 'orange'
}else{
return 'red'
}
}
form.addEventListener('submit',(e)=>{
e.preventDefault()
const searchTerm = search.value
if (searchTerm) {
getMovie(searchUrl + '&query=' + searchTerm + '&include_adult=true')
}
})
submit.addEventListener('click',()=>{
const searchTerm = search.value
if (searchTerm) {
getMovie(searchUrl + '&query=' + searchTerm)
}
})
const genreUrl = baseUrl + 'genre/movie/list?' + apiKey
export const filter = document.querySelector('#filter')
getGenre(genreUrl)
function getGenre(url) {
fetch(url)
.then(res => res.json())
.then(data => {
showGenre(data.genres)
})
}
// cette fontion affiche le genre sous forme de bouton
function showGenre(data) {
filter.innerHTML = ''
data.forEach(genres => {
const genre = document.createElement('button')
genre.classList.add('filterB')
genre.textContent = genres.name
filter.appendChild(genre)
genre.addEventListener('click',()=>{
toggleGenreSelection(genres.id)
genre.classList.toggle('active')
getFilteredMovie()
})
})
}
const genreSelected = []
function toggleGenreSelection(genreId) {
const index = genreSelected.indexOf(genreId)
if (index > -1) {
genreSelected.splice(index,1)
}else{
genreSelected.push(genreId)
}
getFilteredMovie()
}
function getFilteredMovie() {
const selectedGenreString = genreSelected.join(',')
getMovie(baseUrl +'discover/movie?' + apiKey + `&with_genres=${selectedGenreString}` + '&include_adult=true' )
}
prev.addEventListener('click',()=>{
if (prevPage > 0) {
callPage(prevPage)
}
})
next.addEventListener('click',()=>{
if (nextPage <= totalPage) {
callPage(nextPage)
}
})
// cette fonction gère la pagination
function callPage(page) {
let separator = lastUrl.includes('?') ? '&' : '?'
const pagination = `page=${page}`
return `${getMovie(`${lastUrl}${separator}${pagination}`)}`
}
const searchIcons = document.querySelector('.icon')
const appNAme = document.querySelector('.appName')
const closer = document.querySelector('#close')
searchIcons.addEventListener('click',()=>{
form.style.display = 'block'
searchIcons.style.display = 'none'
appNAme.style.display = 'none'
form.style.width = '100%'
search.style.width = '100%'
closer.style.display = 'block'
submit.style.display = 'none'
})
closer.addEventListener('click',()=>{
form.style.display = 'none'
searchIcons.style.display = 'block'
appNAme.style.display = 'block'
closer.style.display = 'none'
})
const topRated = document.querySelector('#topRated')
topRated.addEventListener('click',()=>{
getMovie('https://api.themoviedb.org/3/movie/top_rated?' + apiKey)
})
const trending = document.querySelector('#trending')
trending.addEventListener('click',()=>{
getMovie('https://api.themoviedb.org/3/trending/movie/day?' + apiKey)
})
const upcoming = document.querySelector('#upcoming')
upcoming.addEventListener('click',()=>{
getMovie('https://api.themoviedb.org/3/movie/upcoming?' + apiKey)
})
//scroll des acteurs
const container = document.querySelector('#movieActor');
const scrollLeftButton = document.querySelector('#left');
const scrollRightButton = document.querySelector('#rigth');
scrollLeftButton.addEventListener('click', () => {
container.scrollBy({
left: -100,
behavior: 'smooth'
}) // Défilement horizontal vers la gauche de 100 pixels
});
scrollRightButton.addEventListener('click', () => {
container.scrollBy({
left: 100,
behavior: 'smooth'
}) // Défilement horizontal vers la droite de 100 pixels
});
function getVideo(id) {
fetch(`https://api.themoviedb.org/3/movie/${id}/videos?${apiKey}`)
.then(res => res.json())
.then(vData => {
console.log(vData.results.filter(video => video.type === 'Trailer' || video.type === 'Teaser'));
showVideo(vData.results.filter(video => video.type === 'Trailer' || video.type === 'Teaser'));
})
.catch(e => console.log('Une erreur s\'est produite :', error))
}
const vBloc = document.querySelector('#vBloc')
function showVideo(dataVideo) {
vBloc.innerHTML = ''
dataVideo.forEach(video =>{
const iframe = document.createElement('iframe')
iframe.width = '650'
iframe.height = '450'
iframe.src = `https://www.youtube.com/embed/${video.key}`
iframe.title = video.name
vBloc.appendChild(iframe)
})
}
/*
const movieId = 928833
// recuperer les bandes annonces
const url = `https://api.themoviedb.org/3/movie/${movieId}/videos?${apiKey}`;
fetch(url)
.then(response => response.json())
.then(data => {
console.log(data.results.filter(video => video.type === 'Trailer' || video.type === 'Teaser'));
})
.catch(error => {
console.log('Une erreur s\'est produite :', error);
});
*/