-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
118 lines (100 loc) · 4.01 KB
/
script.js
File metadata and controls
118 lines (100 loc) · 4.01 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
const apiUrlFilms = "https://swapi.dev/api/films/";
let filmsData = [];
async function getFilms(url) {
try {
const response = await fetch(url);
const data = await response.json();
return data.results;
} catch (error) {
console.log("Error fetching films:", error);
}
}
// Call for getFilms async function
getFilms(apiUrlFilms)
.then((fetchedFilms) => {
filmsData = fetchedFilms.map((film) => ({
title: film.title,
director: film.director,
producer: film.producer,
opening: film.opening_crawl,
}));
})
.catch((error) => {
console.log("Error fetching films:", error);
});
/*search-function---------------------------------------------------------*/
function searchTitle() {
const input = document.getElementById("input").value.toLowerCase().trim();
const errorElement = document.getElementById("error");
errorElement.innerHTML = "";
// Only show results if input has 3 characters or more
if (input === "") {
const filmListContainer = document.getElementById("filmList");
filmListContainer.innerHTML = "";
errorElement.style.display = "block";
errorElement.innerHTML = "Please insert text";
} else if (input.length < 3) {
errorElement.style.display = "block";
errorElement.innerHTML = "Min 3 characters";
} else {
// Variable to store results in if match is found
const searchResult = filmsData.filter((film) =>
film.title.toLowerCase().includes(input)
);
// Show results if there is any
if (searchResult.length > 0) {
console.log(searchResult);
// Hide search when showing results
const searchBar = document.querySelector(".input-group");
searchBar.style.animation = "hide 1s";
setTimeout(function () {
searchBar.style.display = "none";
}, 900);
// Display refresh button when showing results
const refreshBtn = document.querySelector(".btn-refresh");
refreshBtn.style.display = "block";
// Display search results in vp
searchResult.forEach((film) => {
const filmListContainer = document.getElementById("filmList");
const filmTitleElement = document.createElement("h2");
filmTitleElement.classList.add("filmTitle");
filmTitleElement.textContent = film.title;
filmListContainer.appendChild(filmTitleElement);
const filmDirectorElement = document.createElement("p");
filmDirectorElement.classList.add("filmDirector");
filmDirectorElement.textContent = `Directed by \r\n`;
filmDirectorElement.textContent += film.director;
filmListContainer.appendChild(filmDirectorElement);
const filmProducerElement = document.createElement("p");
filmProducerElement.classList.add("filmProducer");
filmProducerElement.textContent = `Produced by \r\n`;
filmProducerElement.textContent += film.producer;
filmListContainer.appendChild(filmProducerElement);
const filmOpeningElement = document.createElement("p");
filmOpeningElement.classList.add("filmOpener");
filmOpeningElement.textContent = film.opening;
filmListContainer.appendChild(filmOpeningElement);
});
} else {
console.log("Value not found");
errorElement.innerHTML = "Could not find";
}
}
}
/*Add stars---------------------------------------------------*/
const body = document.querySelector("body");
// Create star divs and randomly position them inside vp
for (let stars = 0; stars < 300; stars++) {
let starDiv = document.createElement("div");
starDiv.className = "star" + " " + stars;
body.appendChild(starDiv);
}
const allStars = document.querySelectorAll(".star");
const colors = ["white", "white", "white", "red", "orange"];
allStars.forEach((star) => {
let randomColors = Math.floor(Math.random() * colors.length);
let randomColor = colors[randomColors];
star.style.left = Math.floor(Math.random() * window.innerWidth) + "px";
star.style.bottom = Math.floor(Math.random() * window.innerHeight) + "px";
star.style.backgroundColor = randomColor;
});