-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (121 loc) · 5.04 KB
/
index.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
const apiKey = '6dc0d2605088c01254ffedbd444bc2e4';
const imageUrl = 'http://image.tmdb.org/t/p/w300/';
const getMovies = () => {
return fetch(`https://api.themoviedb.org/3/movie/popular?api_key=${apiKey}&language=en-US`).then(movies => {
return movies.json();
});
};
const loadMovies = () => {
const createMovieImage = (divImage, element) => {
const image = document.createElement('img');
image.setAttribute('src', `${imageUrl}${element.backdrop_path}`);
divImage.className = 'movie-image';
divImage.appendChild(image);
};
const createMovieContent = (divContent, element) => {
//Title
const divTitle = document.createElement('h2');
const titleText = document.createTextNode(`${element.title} `);
const releaseDateContainer = document.createElement('small');
const relaseDateText = document.createTextNode(`${element.release_date}`);
releaseDateContainer.appendChild(relaseDateText);
divTitle.appendChild(titleText);
divTitle.appendChild(releaseDateContainer);
//Overview
const pOverview = document.createElement('p');
pOverview.className = 'movie-overview';
const overviewText = document.createTextNode(element.overview);
pOverview.appendChild(overviewText);
//Trailer button
const divTrailerButton = document.createElement('div');
divTrailerButton.className = 'btn trailer-button';
divTrailerButton.setAttribute('movie-id', element.id);
const buttonText = document.createTextNode('Trailer!');
divTrailerButton.appendChild(buttonText);
divContent.className = 'movie-content';
//Add elements to divContent
divContent.appendChild(divTitle);
divContent.appendChild(pOverview);
divContent.appendChild(divTrailerButton);
};
const createVideoModal = (divModal, element) => {
divModal.id = `trailer-${element.id}`;
divModal.className = 'modal';
const modalContent = document.createElement('div');
modalContent.className = 'modal-content';
const close = document.createElement('span');
close.className = 'close-modal';
close.innerHTML = '×';
close.setAttribute('movie-id', element.id)
const videoContent = document.createElement('div')
const videoTitle = document.createElement('h3');
videoTitle.innerHTML = element.title;
const video = document.createElement('img');
video.setAttribute('src',`${imageUrl}${element.poster_path}`);
video.className = 'video-movie';
videoContent.appendChild(videoTitle);
videoContent.appendChild(video);
modalContent.appendChild(close);
modalContent.appendChild(videoContent);
divModal.appendChild(modalContent);
};
const openVideo = (button) => {
const movieId = button.getAttribute('movie-id');
const modal = document.getElementById(`trailer-${movieId}`);
modal.style.display = 'block';
};
const closeVideo = (modal) => {
modal.style.display = 'none';
};
const setMovieOrder = (newOrder) => {
};
const movieOrder = 'up';
getMovies().then(response => {
const listContainer = document.getElementById('container');
let currentMovies = null;
listContainer.innerHTML = '';
currentMovies = response.results;
currentMovies.forEach(element => {
const divMovieData = document.createElement('div');
divMovieData.className = 'movie';
const divContent = document.createElement('div');
const divImage = document.createElement('div');
createMovieImage(divImage, element);
createMovieContent(divContent, element);
//Add childs to div
divMovieData.appendChild(divImage);
divMovieData.appendChild(divContent);
// Video modal
const divModal = document.createElement('div');
createVideoModal(divModal, element);
// Add current div to container
listContainer.appendChild(divMovieData);
listContainer.appendChild(divModal);
});
}).then(() => {
const trailerButtons = document.getElementsByClassName('trailer-button');
Array.from(trailerButtons).forEach(button => {
button.onclick = event => {
openVideo(button);
}
});
}).then(() => {
const closeButtons = document.getElementsByClassName('close-modal');
Array.from(closeButtons).forEach(button => {
const movieId = button.getAttribute('movie-id');
const modal = document.getElementById(`trailer-${movieId}`);
button.onclick = event => {
closeVideo(modal);
};
});
}).then(() => {
const orderButton = document.getElementById('release-order');
orderButton.onclick = event => {
setMovieOrder();
};
});
};
const main = () => {
loadMovies();
}
main();