-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
278 lines (235 loc) · 10.7 KB
/
script.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
document.addEventListener('DOMContentLoaded', () => {
const stormCount = document.getElementById('storm-count');
const hurricaneList = document.getElementById('hurricanes');
const depList = document.getElementById('depressions');
const stormList = document.getElementById('storms');
let hideHurricanes = true;
let hideTS = true;
let hideTD = true;
let currentImageType = 'cone';
const hurricaneButton = document.getElementById('hurricane-label');
hurricaneButton.onclick = toggleHurricanes;
const stormButton = document.getElementById('storm-label');
stormButton.onclick = toggleStorms;
const depButton = document.getElementById('depression-label');
depButton.onclick = toggleDeps;
const hurricaneDropdown = document.getElementById('hurricane-dropdown');
const stormDropdown = document.getElementById('storm-dropdown');
const depDropdown = document.getElementById('dep-dropdown');
function toggleHurricanes() {
if (hideHurricanes) {
hurricaneList.style.display = "block";
hurricaneDropdown.innerHTML = "arrow_drop_down";
hideHurricanes = false;
} else {
hurricaneList.style.display = "none";
hurricaneDropdown.innerHTML = "arrow_right";
hideHurricanes = true;
}
}
function toggleStorms() {
if (hideTS) {
stormList.style.display = "block";
stormDropdown.innerHTML = "arrow_drop_down";
hideTS = false;
} else {
stormList.style.display = "none";
stormDropdown.innerHTML = "arrow_right";
hideTS = true;
}
}
function toggleDeps() {
if (hideTD) {
depList.style.display = "block";
depDropdown.innerHTML = "arrow_drop_down";
hideTD = false;
} else {
depList.style.display = "none";
depDropdown.innerHTML = "arrow_right";
hideTD = true;
}
}
function fetchStormData() {
const currentStormData = {
hurricanes: [],
storms: [],
depressions: []
};
const api = 'https://api.tropitracker.com/index.json'
fetch(api).then(response => response.json())
.then(data => {
data.activeStorms.forEach(cyclone => {
const name = cyclone.name
const type = cyclone.type
const datetime = cyclone.datetime
const movement = cyclone.movement
const pressure = cyclone.pressure
const wind = cyclone.wind
const headline = cyclone.headline
const category = cyclone.category
const coneTrack = cyclone.coneTrack
const satelliteGif = cyclone.satelliteGif
const irSatelliteGif = cyclone.irSatelliteGif
const stormData = {type, name, category, datetime, movement, pressure, wind, headline, coneTrack, satelliteGif, irSatelliteGif};
if (type.toLowerCase() == "hurricane") {
currentStormData.hurricanes.push(stormData);
} else if (type.toLowerCase() == "tropical storm") {
currentStormData.storms.push(stormData);
} else if (type.toLowerCase() == "tropical depression" || type.toLowerCase() == "potential tropical cyclone") {
currentStormData.depressions.push(stormData);
}
});
updateStormLists(currentStormData);
})
.catch(error => {
console.error('Error:', error);
});
}
function updateStormLists(stormData) {
updateStormList(hurricaneList, stormData.hurricanes, "hurricane");
updateStormList(stormList, stormData.storms, "storm");
updateStormList(depList, stormData.depressions, "depression");
const activeHurricanes = hurricaneList.children.length;
const activeTropicalStorms = stormList.children.length;
const activeTropicalDeps = depList.children.length;
let hurricaneProperGrammar = activeHurricanes === 1 ? "Hurricane" : "Hurricanes";
let stormProperGrammar = activeTropicalStorms === 1 ? "Tropical Storm" : "Tropical Storms";
let depProperGrammar = activeTropicalDeps === 1 ? "Tropical Depression" : "Tropical Depressions";
stormCount.textContent = `Found ${activeHurricanes} ${hurricaneProperGrammar}, ${activeTropicalStorms} ${stormProperGrammar}, and ${activeTropicalDeps} ${depProperGrammar}.`;
hurricaneButton.style.display = activeHurricanes === 0 ? "none" : "block";
stormButton.style.display = activeTropicalStorms === 0 ? "none" : "block";
depButton.style.display = activeTropicalDeps === 0 ? "none" : "block";
}
function updateStormList(listElement, storms) {
listElement.innerHTML = '';
storms.forEach(storm => {
const stormListItem = createStormListItem(
storm.type, storm.name, storm.category, storm.datetime,
storm.movement, storm.pressure, storm.wind, storm.headline, storm.coneTrack, storm.satelliteGif, storm.irSatelliteGif
);
listElement.appendChild(stormListItem);
const imageToShow = stormListItem.querySelector(`#${currentImageType}Image`);
if (imageToShow) {
changeImageDisplay(imageToShow, ...stormListItem.querySelectorAll('.storm-image:not(#' + currentImageType + 'Image)'));
}
});
}
function createStormListItem(type, name, category, datetime, movement, pressure, wind, headline, coneTrack, satelliteGif, irSatelliteGif) {
const stormListItem = document.createElement('div');
const stormText = document.createElement('span');
stormListItem.appendChild(stormText);
if (type.toLowerCase() === "hurricane") {
const hurricaneIcon = document.createElement('img');
hurricaneIcon.src = '/images/hurricane.png';
hurricaneIcon.id = 'hurricane-icon';
hurricaneIcon.style.marginRight = "5px";
stormListItem.className = "hurricane-list-item";
stormListItem.appendChild(hurricaneIcon);
if (category >= 3) {
type = "Major Hurricane";
} else {
type = "Hurricane";
}
stormListItem.innerHTML += `${type} ${name}: Category ${category}`;
} else if (type.toLowerCase() == "tropical storm") {
const tsIcon = document.createElement('img');
tsIcon.src = '/images/tropical-storm.png';
tsIcon.id = 'hurricane-icon';
stormListItem.className = "storm-list-item";
stormListItem.appendChild(tsIcon);
stormListItem.innerHTML += `${type} ${name}`;
} else if (type.toLowerCase().includes("depression")) {
stormListItem.className = "depression-list-item";
stormListItem.innerHTML = `${type} ${name}`;
} else if (type.toLowerCase().includes("potential")) {
stormListItem.className = "depression-list-item";
stormListItem.innerHTML = `Potential TC ${name}`;
}
const update = document.createElement('div');
update.className = "hurricane-update";
update.textContent = `Last updated: ${datetime}`;
stormListItem.appendChild(update);
const headlineElement = document.createElement('div');
headlineElement.className = "hurricane-headline";
headlineElement.textContent = `${headline}`;
stormListItem.appendChild(headlineElement);
const details = document.createElement('div');
details.className = "hurricane-headline";
details.id = "details";
details.style.marginTop = "10px";
details.style.textDecoration = "underline";
details.innerHTML = `Winds: ${wind} MPH<br>Pressure: ${pressure}<br>Movement: ${movement}`;
stormListItem.appendChild(details);
const imgButtonDiv = document.createElement('div');
imgButtonDiv.className = 'img-button-div';
stormListItem.appendChild(imgButtonDiv);
const coneButton = createImgButton('Cone Tracks', () => {
currentImageType = 'cone';
changeImageDisplay(coneImage, satImage, irImage);
});
const satelliteButton = createImgButton('Satellite', () => {
currentImageType = 'sat';
changeImageDisplay(satImage, coneImage, irImage);
});
const IrSatButton = createImgButton('Infrared Satellite', () => {
currentImageType = 'ir';
changeImageDisplay(irImage, coneImage, satImage);
});
imgButtonDiv.appendChild(coneButton);
imgButtonDiv.appendChild(satelliteButton);
imgButtonDiv.appendChild(IrSatButton);
const coneImage = document.createElement('img');
coneImage.className = `storm-image`;
coneImage.src = coneTrack;
coneImage.id = "coneImage";
coneImage.style.display = "block";
stormListItem.appendChild(coneImage);
const satImage = document.createElement('img');
satImage.className = `storm-image`;
satImage.src = satelliteGif;
satImage.id = "satImage";
satImage.style.display = "none";
stormListItem.appendChild(satImage);
const irImage = document.createElement('img');
irImage.className = `storm-image`;
irImage.src = irSatelliteGif;
irImage.id = "irImage";
irImage.style.display = "none";
stormListItem.appendChild(irImage);
return stormListItem;
}
function createImgButton(label, onClick) {
const button = document.createElement('button');
button.className = "img-button";
button.innerHTML = label;
button.onclick = onClick;
return button;
}
function changeImageDisplay(showImage, ...hideImages) {
showImage.style.display = "block";
hideImages.forEach(image => image.style.display = "none");
}
function getHurricaneCategory(winds) {
if (winds >= 157) {
return "5";
} else if (winds >= 130) {
return "4";
} else if (winds >= 111) {
return "3";
} else if (winds >= 96) {
return "2";
} else {
return "1";
}
}
fetchStormData();
setInterval(fetchStormData, 60000);
});
function openNav() {
const nav = document.getElementById('fullscreenNavbar');
nav.style.display = 'block';
}
function closeNav() {
const nav = document.getElementById('fullscreenNavbar');
nav.style.display = 'none';
}