-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (88 loc) · 3.3 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
import Navbar from './view/Navbar.js';
import Utils from './Utils.js';
import ErrorPage from './view/Error.js';
import SearchPage from './view/searchPage.js';
import MyMovies from './view/MyMovies.js';
let myMovieList = [];
const routes = {
'/': SearchPage,
'/savedmovies': MyMovies
}
const router = async () => {
const header = null || document.getElementById('header_container');
const content = null || document.getElementById('page_container');
header.innerHTML = await Navbar.render();
await Navbar.after_render();
// Get the parsed URl from the addressbar
let request = Utils.parseRequestURL();
let parsedURL = (request.resource ? '/' + request.resource : '/');
// If the parsed URL is not in our list of supported routes, select the 404 page instead
let page = routes[parsedURL] ? routes[parsedURL] : ErrorPage
content.innerHTML = await page.render();
await page.after_render();
}
// Listen on hash change:
window.addEventListener('hashchange', router);
// Listen on page load:
window.addEventListener('load', router);
//return Movie Object template to display
export function getMovieTemplate(movie, cols, button = true) {
return `
<div class="movieCard">
<img class="card-img-top" src="${movie.Poster}" alt="${movie.Title}">
<div class="card-body">
<h5 class="card-title">${movie.Title}</h5>
<p class="card-text">${movie.Year}</p>
${
button ? `<button data-id="${movie.imdbID}" type="button" class="addMyMoviesButton">Add</button>`
: `<button data-id="${movie.imdbID}" type="button" class="deleteMoviesButton">Delete</button>`
}
</div>
</div>
`;
}
// return the Movie list added to My Movies
export function updateMyMovieList(movie) {
let tempList = myMovieList.length > 0 ? myMovieList : [];
tempList.push(movie);
myMovieList = [...new Set(tempList.filter(x => x.imdbID))];
}
export function deleteMovie(movie) {
let tempList = myMovieList.length > 0 ? myMovieList : [];
myMovieList = [...new Set(tempList.filter(x => {
if(x.imdbID != movie.imdbID) {
return x;
}
}))];
return myMovieList;
}
export function getMyMovieList() {
return myMovieList;
}
export function sortMovies(sortByValue, movieObj) {
if(movieObj == null) {
if(sortByValue == 'title') {
myMovieList.sort(function(a, b) {
let textA = a.Title.toUpperCase();
let textB = b.Title.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
return myMovieList;
} else if(sortByValue == 'year'){
myMovieList.sort(function(a, b){return a.Year - b.Year});
return myMovieList;
}
} else if(movieObj.length >= 0) {
if(sortByValue == 'title') {
movieObj.sort(function(a, b) {
let textA = a.Title.toUpperCase();
let textB = b.Title.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
return movieObj;
} else if(sortByValue == 'year'){
movieObj.sort(function(a, b){return a.Year - b.Year});
return movieObj;
}
}
}