-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
166 lines (131 loc) · 4.4 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
// Initial Values
const INITIAL_SEARCH_VALUE = 'spiderman';
const log = console.log;
// Selecting elements from the DOM
const searchButton = document.querySelector('#search');;
const searchInput = document.querySelector('#exampleInputEmail1');
const moviesContainer = document.querySelector('#movies-container');
const moviesSearchable = document.querySelector('#movies-searchable');
function createImageContainer(imageUrl, id) {
const tempDiv = document.createElement('div');
tempDiv.setAttribute('class', 'imageContainer');
tempDiv.setAttribute('data-id', id);
const movieElement = `
<img src="${imageUrl}" alt="" data-movie-id="${id}">
`;
tempDiv.innerHTML = movieElement;
return tempDiv;
}
function resetInput() {
searchInput.value = '';
}
function handleGeneralError(error) {
log('Error: ', error.message);
alert(error.message || 'Internal Server');
}
function createIframe(video) {
const videoKey = (video && video.key) || 'No key found!!!';
const iframe = document.createElement('iframe');
iframe.src = `https://www.youtube.com/embed/${videoKey}`;
iframe.width = 360;
iframe.height = 315;
iframe.allowFullscreen = true;
return iframe;
}
function insertIframeIntoContent(video, content) {
const videoContent = document.createElement('div');
const iframe = createIframe(video);
videoContent.appendChild(iframe);
content.appendChild(videoContent);
}
function createVideoTemplate(data) {
const content = this.content;
content.innerHTML = '<p id="content-close">X</p>';
const videos = data.results || [];
if (videos.length === 0) {
content.innerHTML = `
<p id="content-close">X</p>
<p>No Trailer found for this video id of ${data.id}</p>
`;
return;
}
for (let i = 0; i < 4; i++) {
const video = videos[i];
insertIframeIntoContent(video, content);
}
}
function createSectionHeader(title) {
const header = document.createElement('h2');
header.innerHTML = title;
return header;
}
function renderMovies(data) {
const moviesBlock = generateMoviesBlock(data);
const header = createSectionHeader(this.title);
moviesBlock.insertBefore(header, moviesBlock.firstChild);
moviesContainer.appendChild(moviesBlock);
}
function renderSearchMovies(data) {
moviesSearchable.innerHTML = '';
const moviesBlock = generateMoviesBlock(data);
moviesSearchable.appendChild(moviesBlock);
}
function generateMoviesBlock(data) {
const movies = data.results;
const section = document.createElement('section');
section.setAttribute('class', 'section');
for (let i = 0; i < movies.length; i++) {
const { poster_path, id } = movies[i];
if (poster_path) {
const imageUrl = MOVIE_DB_IMAGE_ENDPOINT + poster_path;
const imageContainer = createImageContainer(imageUrl, id);
section.appendChild(imageContainer);
}
}
const movieSectionAndContent = createMovieContainer(section);
return movieSectionAndContent;
}
// Inserting section before content element
function createMovieContainer(section) {
const movieElement = document.createElement('div');
movieElement.setAttribute('class', 'movie');
const template = `
<div class="content">
<p id="content-close">X</p>
</div>
`;
movieElement.innerHTML = template;
movieElement.insertBefore(section, movieElement.firstChild);
return movieElement;
}
searchButton.onclick = function (event) {
event.preventDefault();
const value = searchInput.value
if (value) {
searchMovie(value);
}
resetInput();
}
// Click on any movies
// Event Delegation
document.onclick = function (event) {
log('Event: ', event);
const { tagName, id } = event.target;
if (tagName.toLowerCase() === 'img') {
const movieId = event.target.dataset.movieId;
const section = event.target.parentElement.parentElement;
const content = section.nextElementSibling;
content.classList.add('content-display');
getVideosByMovieId(movieId, content);
}
if (id === 'content-close') {
const content = event.target.parentElement;
content.classList.remove('content-display');
}
}
// Initialize the search
searchMovie(INITIAL_SEARCH_VALUE);
searchUpcomingMovies();
getTopRatedMovies();
searchPopularMovie();
getTrendingMovies();