-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
241 lines (192 loc) · 8.79 KB
/
script.js
File metadata and controls
241 lines (192 loc) · 8.79 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
const API_LINK = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=0d6116e95246dc97d468c2a1b5b45ce5&page=1';
const IMG_PATH = "https://image.tmdb.org/t/p/w1280";
const SEARCHAPI = "https://api.themoviedb.org/3/search/movie?&api_key=0d6116e95246dc97d468c2a1b5b45ce5&query=";
const GET_DESC = "https://api.themoviedb.org/3/movie/";
const request_DESC = "?api_key=0d6116e95246dc97d468c2a1b5b45ce5&append_to_response=watch/providers,credits";
const main = document.getElementById("section");
const form = document.getElementById("search-form");
const query = document.getElementById("query");
const loadResults = document.getElementById("load-results");
//loadScreen();
returnMovies(API_LINK);
// On clicking `Search Movies` the page should sort by popularity again
const Logo = document.getElementById("movies-site");
Logo.addEventListener('click', function(event){
loadScreen(); // Remove the previous results in the section
returnMovies(API_LINK);
});
const currentPage = 0;
/**
* Gets the movies objects from the url and creates a card for each of
* the films and displays
* @param {*} url - api link to get the movies list
*/
function returnMovies(url){
fetch(url).then(res => res.json())
.then(function(data){
console.log(data.results);
data.results.forEach(element => {
// Create HTML elements and set their attributes
if(element!==null){
const div_card = document.createElement('div');
div_card.setAttribute('class','card');
div_card.setAttribute('movie-id',element.id);
const div_row = document.createElement('div');
div_row.setAttribute('class','row');
const div_column = document.createElement('div');
div_column.setAttribute('class','column');
const image = document.createElement('img');
image.setAttribute('id','image');
image.setAttribute('class','thumbnail');
image.setAttribute('movie-id',element.id);
const title = document.createElement('h3');
title.setAttribute('class','title');
title.setAttribute('movie-id',element.id);
const center = document.createElement('center');
// Setting the movie title and poster and creating the required html structure for them
title.innerHTML = `${element.title}`;
image.src = IMG_PATH + element.poster_path;
//image.setAttribute('src',IMG_PATH+element.poster_path);
center.appendChild(image);
div_card.appendChild(center);
div_card.appendChild(title);
div_column.appendChild(div_card);
div_row.appendChild(div_column);
main.appendChild(div_row); // Appends the newly created movie_card to the section element in HTML.
}
});
});
}
// Event Listener for form. So we can call returnMovies function whenever the search form is submitted
form.addEventListener("submit", (e)=> {
e.preventDefault();
main.innerHTML = ''; // Remove the previous results in the section
console.log("Search Submitted");
const searchItem = query.value;
if(searchItem){ // searchItem is not null
console.log(`URL used is ${SEARCHAPI+searchItem}`);
returnMovies(SEARCHAPI+searchItem);
query.value="";
}
});
// Additional Features
// 1. Dialog box when we click a card and display related things.
const card = document.getElementsByClassName("card");
const dialog_screen = document.getElementById("dialog-screen");
main.addEventListener("click", function(event){
console.log(event.target);
if (event.target && (event.target.classList.contains("card") || event.target.classList.contains("thumbnail") || event.target.classList.contains("title"))) {
// Show the dialog box
console.log("Card is clicked");
//dialog.style.display = 'block';
const movie_id = event.target.getAttribute("movie-id");
console.log(movie_id);
const description = GET_DESC+movie_id+request_DESC; // Get the description of the movie from the api.
console.log(description);
console.log(`Requested URL for movie description is ${description}`);
if(movie_id){
displayPopOver(description);
}
}
});
/**
* This method created a dialog Box that shows the movie poster
* and details of the movie
* Add a clickAway feature so the dialog box closes when the user
* clicks outside the dialog box or a close button
* @param {*} url - api link to the description of the movie
*/
function displayPopOver(url){
console.log(dialog_screen);
dialog_screen.innerHTML = '';
fetch(url).then(res => res.json())
.then(function(data){
console.log(data);
// Create popover screen dynamically
const dialog_box = document.createElement("dialog");
dialog_box.setAttribute('class','dialog-box');
dialog_box.setAttribute('id','dialog-box');
const image = document.createElement("img");
image.setAttribute('class','dialog-img');
const description = document.createElement("div");
description.setAttribute('class','Description');
dialog_box.appendChild(image);
dialog_box.appendChild(description);
dialog_screen.appendChild(dialog_box);
console.log(data.title);
image.src = IMG_PATH + data.poster_path;
image.alt = `${data.title}`;
createDescription(description,data);
//description.innerHTML = `${data.overview}`;
dialog_screen.style.display = 'block';
dialog_box.showModal();
});
}
/**
*
* @param {*} element - HTML element to which data has to be
* appended to - DESCRIPTION element created in @function displayPopOver
* @param {*} data - json file that has details related to the movie
*/
function createDescription(element, data){
const overview = document.createElement('div');
overview.setAttribute('class','description-data');
overview.innerHTML = `Overview: <br> ${data.overview}`;
const releaseDate = document.createElement('div');
releaseDate.setAttribute('class','description-data');
releaseDate.innerHTML = `Release Date: ${data.release_date}`;
const rating = document.createElement('div');
rating.setAttribute('class','description-data');
rating.innerHTML = `Rating: ${Math.floor((data.vote_average)*100)/100}`;
const runtime = document.createElement('div');
runtime.setAttribute('class','description-data');
runtime.innerHTML = `Runtime: ${Math.floor(data.runtime/60)} hr ${data.runtime%60} min`;
element.appendChild(overview);
element.appendChild(releaseDate);
element.appendChild(rating);
element.appendChild(runtime);
}
// ClickAway feature for the dialog Box
document.addEventListener('click', function(event){
console.log(event.target);
if((event.target).classList.contains("dialog-box")){
console.log(true);
document.getElementById('dialog-box').close();
console.log(`box closed`);
}
else{
console.log(false);
}
});
// Loading More Results
loadResults.addEventListener('click',function(event){
// Load More Results
// Use returnMovies if its on homepage or search
});
// Add a loading screen with blank posters but with cards.
function loadScreen(){
for (let index = 0; index < 20; index++) {
const div_card = document.createElement('div');
div_card.setAttribute('class','card');
const div_row = document.createElement('div');
div_row.setAttribute('class','row');
const div_column = document.createElement('div');
div_column.setAttribute('class','column');
const image = document.createElement('img');
image.setAttribute('id','image');
image.setAttribute('class','thumbnail');
const title = document.createElement('h3');
title.setAttribute('class','title');
const center = document.createElement('center');
// Setting the movie title and poster and creating the required html structure for them
title.innerHTML = `Title`;
image.src = "";
//image.setAttribute('src',IMG_PATH+element.poster_path);
center.appendChild(image);
div_card.appendChild(center);
div_card.appendChild(title);
div_column.appendChild(div_card);
div_row.appendChild(div_column);
main.appendChild(div_row); // Appends the newly created movie_card to the section element in HTML.
}
}