-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.js
206 lines (180 loc) · 9.34 KB
/
script2.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
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
window.addEventListener('load', function () {
const requestURL = 'https://swapi.dev/api/people/';
const buttons = document.querySelectorAll('#btn');
const infoDiv = document.querySelectorAll('#info-divs');
const infoDivClassName = document.querySelectorAll('.info-divs');
const up = document.querySelector('#up');
const down = document.querySelector('#down');
const popup = document.querySelector('.popup');
const preloader = document.querySelector('.preloader')
const body = document.body;
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
function createSound() {
const hoverSound = new Audio();
hoverSound.src = 'sound/hover-sound.mp3';
hoverSound.autoplay = true;
hoverSound.volume = 0.05;
}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
function setClickSound() {
body.addEventListener('click', (e) => {
let target = e.target;
if (target.localName === 'input') {
createSound();
}
})
}
setClickSound()
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
function slideButtons(index) {
for (let i = 0; i < buttons.length; i++) {
if (i > 4) {
buttons[i].style.display = 'none';
}
}
down.addEventListener('click', () => {
if (index <= 4) {
buttons[index].style.display = 'none';
buttons[index + 5].style.display = 'flex';
index++;
}
createSound();
})
up.addEventListener('click', () => {
if (index > 0) {
buttons[index + 4].style.display = 'none';
buttons[index - 1].style.display = 'flex';
index--;
}
createSound();
})
}
slideButtons(0)
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
function getAllInfo() {
return new Promise((resolve, reject) => {
fetch(requestURL)
.then((response) => response.json())
.then((data) => {
let resArray = data.results;
for (let [index, item] of resArray.entries()) {
let name = item.name;
let planetURL = item.homeworld;
let filmURL = item.films;
let speciesURL = item.species;
let newButtonsArr = Array.from(buttons);
newButtonsArr[index].value = name;
newButtonsArr[index].addEventListener('click', (e) => {
let target = e.target;
switch (target.value) {
case 'Luke Skywalker':
body.style.backgroundImage = 'url(img/luke_skywalker.jpg)';
break;
case 'C-3PO':
body.style.backgroundImage = 'url(img/C-3PO.jpg)';
break;
case 'R2-D2':
body.style.backgroundImage = 'url(img/R2-D2.jpg)';
break;
case 'Darth Vader':
body.style.backgroundImage = 'url(img/darth_vader.jpg)';
break;
case 'Leia Organa':
body.style.backgroundImage = 'url(img/leia_organa.jpg)';
break;
case 'Owen Lars':
body.style.backgroundImage = 'url(img/owen_lars.jpg)';
break;
case 'Beru Whitesun lars':
body.style.backgroundImage = 'url(img/beru_whitesun_lars.jpg)';
break;
case 'R5-D4':
body.style.backgroundImage = 'url(img/R5-D4.jpg)';
break;
case 'Biggs Darklighter':
body.style.backgroundImage = 'url(img/biggs_darklighter.jpg)';
break;
case 'Obi-Wan Kenobi':
body.style.backgroundImage = 'url(img/obi-wan_kenobi.jpg)';
break;
}
if (name == resArray[index].name) {
infoDiv[0].innerHTML = `Name: \b<span style = "color: rgb(0, 255, 179)">${resArray[index].name}</span>`;
infoDiv[1].innerHTML = `Birthday year: \b<span style = "color: rgb(0, 255, 179)">${resArray[index].birth_year}</span>`;
infoDiv[2].innerHTML = `Gender: \b<span style = "color: rgb(0, 255, 179)">${resArray[index].gender}</span>`;
let filmsStr = '';
for (let [index, item] of filmURL.entries()) {
new Promise((resolve, reject) => {
fetch(item)
.then(response => response.json())
.then(data => {
filmsStr += data.title + ' || ';
infoDiv[3].innerHTML = `Films: <span style = "color: rgb(0, 255, 179)">${filmsStr}</span>`;
resolve();
})
.catch(error => reject(error));
})
}
getPlanet('GET', planetURL)
.then(planet => { infoDiv[4].innerHTML = `Planet: \b<span style = "color: rgb(0, 255, 179)">${planet}</span>`; })
getSpecies('GET', speciesURL)
.then($species => { infoDiv[5].innerHTML = `Species: \b<span style = "color: rgb(0, 255, 179)">${$species}</span>`; })
.catch(() => {
infoDiv[5].innerHTML = `Species: \b<span style = "color: rgb(0, 255, 179)">Human</span>`;
})
}
for (let item of infoDivClassName) {
item.classList.toggle('info-divs-visible');
setTimeout(() => {
item.classList.add('info-divs-visible')
}, 500);
}
})
}
if (data) {
preloader.classList.add('preloader-unvisible');
function showPopup() {
setTimeout(() => {
popup.classList.add('popup-visible')
}, 5000)
popup.addEventListener('click', () => {
popup.classList.remove('popup-visible')
})
}
showPopup()
}
resolve();
},
(error) => {
reject(error);
})
})
}
getAllInfo()
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
function getPlanet(method, url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(response => response.json())
.then(data => {
resolve(data.name);
},
(error) => {
reject(error);
})
})
}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
function getSpecies(method, url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(response => response.json())
.then(data => {
resolve(data.name);
},
(error) => {
reject(error);
})
})
}
})