-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
319 lines (267 loc) · 9.86 KB
/
main.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
class Carousel {
/**
* @param {HTMLElement} element
* @param {Object} options
* @param {Object} options.slidesToScroll Nombre d'elements à faire defiler
* @param {Object} options.slidesVisible Nombre d'elements visible dans un slide
*/
constructor (element, options = {}) {
this.element = element;
this.options = Object.assign({}, {
slidesToScroll: 1,
slidesVisible: 1
}, options);
let children = [].slice.call(element.children);
this.currentItem = 0;
this.root = this.createDivWithClass('carousel');
this.container = this.createDivWithClass('carousel__container');
this.root.appendChild(this.container);
this.element.appendChild(this.root);
this.items = children.map((child) => {
let item = this.createDivWithClass('carousel__item');
item.appendChild(child);
this.container.appendChild(item);
return item;
});
this.setStyle();
this.createNavigation();
}
/**
* Applique les bonnes dimensions aux éléments du carousel
*/
setStyle() {
let ratio = (this.items.length / this.options.slidesVisible);
this.container.style.width = (ratio * 100) + "%" ;
this.items.forEach(item => item.style.width = ((100 / this.options.slidesVisible) / ratio) + "%") ;
}
createNavigation () {
let nextButton = this.createDivWithClass('carousel__next');
let prevButton = this.createDivWithClass('carousel__prev');
this.root.appendChild(nextButton);
this.root.appendChild(prevButton);
nextButton.addEventListener('click', this.next.bind(this));
prevButton.addEventListener('click', this.prev.bind(this));
}
next () {
this.gotoItem(this.currentItem + this.options.slidesToScroll)
}
prev () {
this.gotoItem(this.currentItem - this.options.slidesToScroll)
}
/**
* deplace le carousel vers l'element ciblé
* @param {number} index
*/
gotoItem (index) {
if (index < 0) {
index = this.items.length - this.options.slidesVisible
} else if (index >= this.items.length || this.items[this.currentItem + this.options.slidesVisible] === undefined) {
index = 0
}
let translateX = index * -100 / this.items.length
this.container.style.transform = 'translate3d(' + translateX + '%, 0, 0)'
this.currentItem = index;
}
/**
*
* @param {string} className
* @returns {HTMLElement}
*/
createDivWithClass (className) {
//console.log('div created with class');
let div = document.createElement('div');
div.setAttribute('class', className);
return div;
}
}
// create carousel for each element with class name 'carousel'
let carousels = document.getElementsByClassName('carousel');
let nbCarousels = carousels.length;
while ( nbCarousels > 0) {
let test = new Carousel (carousels[nbCarousels-1], {
slidesToScroll: 1,
slidesVisible: 4
});
nbCarousels --;
}
// get the modal
var modal = document.getElementById("myModal");
//get the buttin that opens the modal
var btns = document.getElementsByClassName("myBtn");
// get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// when the user click on buttton, open the modal
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function(event) {
let idEvent = event.target.id;
modal.style.display = "block";
const texteModal = document.getElementById('description_modal');
getInfosMovies(idEvent).then(function (array) {
var html = 'Title : ' + array['title'] + '<br>';
html += 'Genres : ' + array['genres'] + '<br>';
html += 'Date published : ' + array['date_published'] + '<br>';
html += 'Rated : ' + array['rated'] + '<br>';
html += 'Imdb_score : ' + array['imdb_score'] + '<br>';
html += 'Writers : ' + array['writers'] + '<br>';
html += 'Actors : ' + array['actors'] + '<br>';
html += 'Duration : ' + array['duration'] + '<br>';
html += 'Countries : ' + array['countries'] + '<br>';
html += 'Budget : ' + array['budget'] + '<br>';
html += 'Description : ' + array['description'] + '<br>';
texteModal.innerHTML = html;
});
const imageModal = document.getElementById('image_modal');
image_modal.src = event.target.src;
});
}
//when the user clicks on cross, close the modal
span.onclick = function() {
modal.style.display = "none";
}
//when the user clicks anywhere of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
/**
* @param {string} url
* @returns {array} url des Movies
*/
const getUrlImages = async function(urlCategorie) {
let response = await fetch(urlCategorie)
if (response.ok) {
let data = await response.json();
var nbMoviesPage1 = data.results.length;
let nbMovies = 8;
const map = new Map(); //map contains id & url img
var i = 0;
if (nbMoviesPage1 < nbMovies) {
getInfosPage1();
await getInfosPage2();
// situation if nbMoviesPage1 > nbMovies
} else {
var i = 0;
while (nbMovies > 0) {
//array.push(result.image_url);
idMovie = data.results[i].id;
urlImage = data.results[i].image_url;
map.set(idMovie, urlImage);
nbMovies --;
i ++;
}
}
function getInfosPage1 () {
data.results.forEach(function(res){
//array.push(res.image_url);
idMovie = data.results[i].id;
urlImage = data.results[i].image_url;
map.set(idMovie, urlImage);
nbMovies --;
i ++;
});
return map, nbMovies;
}
async function getInfosPage2 () {
let responseNextPage = await fetch(data.next)
if (responseNextPage.ok) {
let dataNextPage = await responseNextPage.json();
let resultsNextPage = dataNextPage.results;
var i = 0;
while (nbMovies > 0) {
//array.push(resultsNextPage[i].image_url);
idMovie = resultsNextPage[i].id;
urlImage = resultsNextPage[i].image_url;
map.set(idMovie, urlImage);
nbMovies --;
i ++;
};
return map;
} else {
console.error('Retour du serveur: ', response.status)
}
}
return map;
} else {
console.error('Retour du serveur: ', response.status)
}
}
/**
* @param {string} url
* @param {string} index
* @returns {array} informations about movie
*/
const getInfosMovies = async function(id_movie) {
const urlSite = 'http://localhost:8000/api/v1/titles/';
var urlMovie = urlSite + id_movie;
let response = await fetch(urlMovie)
if (response.ok) {
let data = await response.json();
var infosArray = {};
infosArray.title = data.title;
infosArray.genres = data.genres;
infosArray.date_published = data.date_published;
infosArray.rated = data.rated;
infosArray.imdb_score = data.imdb_score;
infosArray.writers = data.writers;
infosArray.actors = data.actors;
infosArray.duration = data.duration;
infosArray.countries = data.countries;
infosArray.budget = data.budget;
infosArray.description = data.description;
return infosArray;
} else {
console.error('Retour du serveur: ', response.status)
}
}
// get information about best movie
const linksInfosBestMovie = async function (url) {
let response = await fetch(url)
if (response.ok) {
let data = await response.json();
urlMovie = data.results[0].url;
let res = await fetch(urlMovie)
if (res.ok) {
let dataMovie = await res.json();
var titleBestMovie = document.getElementById('title_best_movie');
var descriptionBestMovie = document.getElementById('description_best_movie');
titleBestMovie.innerHTML = dataMovie.title;
description_best_movie.innerHTML = dataMovie.description;
var elementBM = document.getElementById('best_movie');
elementBM.id = dataMovie.id;
elementBM.src = dataMovie.image_url;
} else {
console.error('Retour du serveur: ', response.status)
}
} else {
console.error('Retour du serveur: ', response.status)
}
}
// link Url & Id Movies to element "img"
function linksUrlToImg (categorie, url) {
const imgs = document.getElementsByClassName(categorie);
getUrlImages(url).then(function (map){
var i = 0;
for (let img of imgs) {
// link url image to img[index]
var urlImg = Array.from(map.values())[i];
img.src = urlImg;
// link id to img[index]
var idImg = Array.from(map.keys())[i];
img.id = idImg;
i ++;
}
});
}
// data
const urlBestMovies = 'http://localhost:8000/api/v1/titles/?year=&min_year=&max_year=&imdb_score=&imdb_score_min=&imdb_score_max=&title=&title_contains=&genre=&genre_contains=&sort_by=-imdb_score&director=&director_contains=&writer=&writer_contains=&actor=&actor_contains=&country=&country_contains=&lang=&lang_contains=&company=&company_contains=&rating=&rating_contains='
const urlCategorie1 = 'http://localhost:8000/api/v1/titles/?year=&min_year=&max_year=&imdb_score=&imdb_score_min=&imdb_score_max=&title=&title_contains=&genre=History&genre_contains=&sort_by=-imdb_score&director=&director_contains=&writer=&writer_contains=&actor=&actor_contains=&country=&country_contains=&lang=&lang_contains=&company=&company_contains=&rating=&rating_contains='
const urlCategorie2 = 'http://localhost:8000/api/v1/titles/?year=&min_year=&max_year=&imdb_score=&imdb_score_min=&imdb_score_max=&title=&title_contains=&genre=Comedy&genre_contains=&sort_by=-imdb_score&director=&director_contains=&writer=&writer_contains=&actor=&actor_contains=&country=&country_contains=&lang=&lang_contains=&company=&company_contains=&rating=&rating_contains='
const urlCategorie3 = 'http://localhost:8000/api/v1/titles/?year=&min_year=&max_year=&imdb_score=&imdb_score_min=&imdb_score_max=&title=&title_contains=&genre=Mystery&genre_contains=&sort_by=-imdb_score&director=&director_contains=&writer=&writer_contains=&actor=&actor_contains=&country=&country_contains=&lang=&lang_contains=&company=&company_contains=&rating=&rating_contains='
// Only for BestMovie, links description & title to element best movie
linksInfosBestMovie(urlBestMovies);
// links img to url test
linksUrlToImg('best_movies', urlBestMovies);
linksUrlToImg('categorie_1', urlCategorie1);
linksUrlToImg('categorie_2', urlCategorie2);
linksUrlToImg('categorie_3', urlCategorie3);