-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathcode.js
More file actions
122 lines (106 loc) · 2.96 KB
/
code.js
File metadata and controls
122 lines (106 loc) · 2.96 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
//varibles
const myForm = document.querySelector(".Form");
const myTextArea= document.querySelector("#searchfield");
const myButton = document.querySelector(".favButton");
const myDiv= document.querySelector(".movies");
const myDropBox= document.querySelector(".dropBox");
let favMovies=[];
//functions
function fetching()
{
let search=myTextArea.value;
fetch(`http://www.omdbapi.com/?s=${search}&apikey=60ece986`)
.then(function(response){
return response.json();
}).then(function(jsonData){
displayMovies(jsonData);
}).catch(function(error){
alert("error")
});
}
function fetchingDeatils(im)
{
let search=myTextArea.value;
fetch(`http://www.omdbapi.com/?i=${im}&apikey=60ece986`)
.then(function(response){
return response.json();
}).then(function(jsonData){
displayDetails(jsonData, im);
}).catch(function(error){
alert("error")
});
}
function displayDetails(data, im){
const movie=document.querySelector(`#${im}`);
if(movie.childElementCount<9){
let newNode=document.createElement("li");
newNode.classList.add("details");
for(let value in data)
{
if((value=="Poster") || (value=="Response") || (value=="Ratings")) {
// do nothing
}
else{
newNode.innerHTML+=`${value}: ${data[value]} <br>`;
}
}
movie.appendChild(newNode);
}
}
function displayMovies(data){
data.Search.forEach(function(movie){
let newNode=document.createElement("li");
newNode.classList.add("movie");
for(let value in movie)
{
if(value=="imdbID"){
newNode.id=movie[value];
newNode.innerHTML+=`<a href=https://www.imdb.com/title/${movie[value]}/> LINKK </a> <br>`;
}
else if(value=="Poster"){
newNode.innerHTML+=`<img src=${movie[value]}></img> <br>`;
}
else{
newNode.innerHTML+=`${value}: ${movie[value]} <br>`;
}
}
newNode.innerHTML+=`<button class="fav" type="button">add to fav </button>`;
myDiv.appendChild(newNode);
});
}
function submitHandler(event){
event.preventDefault();
myDiv.innerHTML="";
fetching();
}
function resetHandler(){
myDiv.innerHTML="";
}
function favHandler(event)
{
if(event.target.classList.value=="fav"){
let search=favMovies.find(function(item){
return item==event.target.parentElement.id;
});
if(search==event.target.parentElement.id){
alert("movie already in favourite list");
}
else {
favMovies.push(event.target.parentElement.id);
newNode=document.createElement("option");
newNode.id=event.target.parentElement.id;
newNode.textContent=event.target.parentElement.firstChild.data;
myDropBox.appendChild(newNode);
}
}
else if (event.target.classList.value=="movie") {
fetchingDeatils(event.target.id);
}
}
function showFav(event){
}
//body
myForm.addEventListener("submit", submitHandler);
myDiv.addEventListener("click", favHandler);
myForm.addEventListener("reset", resetHandler);
myDropBox.addEventListener("change",showFav);