-
Notifications
You must be signed in to change notification settings - Fork 0
/
rover.html
354 lines (289 loc) · 14.2 KB
/
rover.html
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="rover.css">
<title>Document</title>
</head>
<body>
<h1>Rover Photo Explorer</h1>
<div id="errorPanel">
<img src="spirit.png" alt="">
<div>This is the error message.</div>
</div>
<!-- Generic Container -->
<div id="container">
<div id="card" class="">
<div id="rovers" class="face front">
<div id="loader">
<img src="marsspinning.gif" alt="" height=30 width=30>
</div>
<!-- <button onclick="toggleCard(null)">Go to Rover</button> -->
<div class="rover" id="opportunity" data-img="rover/opportunity.jpg"></div>
<div class="rover" id="spirit" data-img="rover/spirit.jpg"></div>
<div class="rover" id="curiosity" data-img="rover/curiosity.jpg"></div>
<div id="introduction">
Did you know that Mars is a planet populated entirely by robots? It's true.
Humanity has been sending robotic probes to Mars since 1975 with the Viking 1.
In the last couple of decades we've put a few <em>mobile science labs</em>
on the red planet. NASA has kindly <a href="https://api.nasa.gov/api.html" target="_blank">provided an API</a>
to view images taken by these robots.
Click on a Rover above for more information and to browse images.
Please note that the Curiosity rover has a LOT of data, it will take some time to load.
<br/>Currently getting Sol: <input id="sol_select" type="text" maxlength="4" class="disabled" value="500" />
</div>
</div>
<div id="rover_details" class="face back">
<div class="title_bar">
<span id="back_button" onclick="toggleCard()">Back</span>
<span id="title"></span>
<span id="current_sol"></span>
</div>
<div id="layout">
<div id="rover_info">
<table>
<tr><th>Launch Date: </th> <td id="launch_date"></td></tr>
<tr><th>Landing Date: </th> <td id="land_date"></td></tr>
<tr><th>Status: </th><td id="status"></td></tr>
<tr><th>Total Photos: </th><td id="total_photos"></td></tr>
<tr><th colspan="2">Cameras: </th></tr>
<tr>
<td id="camera_list" colspan="2"></td>
</tr>
</table>
</div>
<div id="rover_pic">
<div>Select a camera on the left to view images </div>
<select name="pictures" id="pictures" multiple>
</select>
</div>
</div>
</div>
</div>
</div>
<div id="photo" class="hidden">
<div id="photo_details">
ID: <span id="photo_id"></span>
Date: <span id="photo_date"></span>
Camera: <span id="photo_camera"></span>
</div>
<img id="roverPhoto" src="" alt="">
<div id="photo_instructions">Use left and right keys to move through images in the set.</div>
</div>
<script>
var data = {};
let rover = "curiosity"; //Set a default
/*
Normally API keys are secret. Fortuantely, NASA hands these out freely. If you want your own key, please go get one:
at https://api.nasa.gov/index.html#apply-for-an-api-key
*/
var nak = "kxaDQyDbQNFu6BqiblPVt0ojjqa8J4ZGJB4UUWyJ";
//Handle on page load.
document.addEventListener('DOMContentLoaded',e=>{
document.querySelector("#photo").addEventListener("click",e=>{
document.querySelector("#photo").classList.add("hidden");
document.querySelector("#roverPhoto").classList.remove("image_load");
})
document.querySelector("#introduction input").addEventListener("click",e=>{
e.target.select();
e.target.classList.remove("disabled");
})
document.querySelector("#errorPanel").addEventListener("click", e=>{
e.target.closest("#errorPanel").classList.remove("show");
})
document.querySelector("#introduction input").addEventListener("blur",e=>{
e.target.classList.add("disabled");
})
document.addEventListener("keydown",e => {
if(e.code == "ArrowLeft") {
prevImage();
} else if (e.code == "ArrowRight"){
nextImage();
}else if (e.keyCode == 27){
document.getElementById("photo").classList.add("hidden");
}
})
var rovers = document.querySelectorAll(".rover");
rovers.forEach(e=>{
e.addEventListener("click",r=>{
getRoverSummary(r.target.id);
});
})
});
function nextImage() {
let ops = document.querySelectorAll("#pictures option");
let c = document.getElementById("pictures").selectedIndex;
if(c < ops.length - 1) {
let src = document.querySelectorAll("#pictures option")[c + 1];
let details = getInfoFromElement(src);
document.getElementById("roverPhoto").src = details.img;
setPhotoDetails(details.id, details.date, details.cam);
document.getElementById("pictures").selectedIndex = c + 1;
}
}
function prevImage() {
let c = document.getElementById("pictures").selectedIndex;
if(c > 0) {
let src = document.querySelectorAll("#pictures option")[c - 1];
let details = getInfoFromElement(src);
document.getElementById("roverPhoto").src = details.img;
setPhotoDetails(details.id, details.date, details.cam);
document.getElementById("pictures").selectedIndex = c - 1;
}
}
function getInfoFromElement(element) {
return {
"img": element.getAttribute("data-img"),
"id": element.getAttribute("data-id"),
"date": element.getAttribute("data-date"),
"cam": element.getAttribute("data-cam")
}
}
function setPhotoDetails(id, date, camera) {
document.getElementById("photo_id").innerHTML = id;
document.getElementById("photo_date").innerHTML = date;
document.getElementById("photo_camera").innerHTML = camera;
}
function toggleCard() {
document.getElementById("card").classList.toggle("flipped");
}
function displayRoverDetails(rover) {
document.querySelector("#pictures").innerHTML = "";
let sol = document.querySelector("#introduction input").value;
document.querySelector("span#current_sol").innerHTML = `Sol: ${sol}`;
document.querySelector("#rover_details span#title").innerHTML = rover.name;
document.querySelector("td#launch_date").innerHTML = rover.launch_date;
document.querySelector("td#land_date").innerHTML = rover.landing_date;
document.querySelector("td#status").innerHTML = rover.status;
document.querySelector("td#total_photos").innerHTML = rover.total_photos;
// if(!rover.cameras) rover.cameras = [];
let solData = rover.photos.filter(s=> s.sol == sol);
rover.cameras = solData[0].cameras;
// rover.cameras.push({"name":"all", "full_name":"All Cameras"});
rover.cameras.push("ALL");
//Clear the camera list.
document.querySelector("td#camera_list").innerHTML = "";
rover.cameras.forEach(c=>{
let cam = document.createElement("li");
cam.setAttribute("data-cam",c);
cam.innerHTML = c;
cam.addEventListener("click", ev => {
listImages(ev.target.getAttribute("data-cam"));
})
document.querySelector("td#camera_list").appendChild(cam);
})
}
function getRoverSummary(rover) {
var roverUrl = `https://api.nasa.gov/mars-photos/api/v1/manifests/${rover}?api_key=${nak}`;
var r = new XMLHttpRequest();
r.open("GET", roverUrl);
document.querySelector("#loader").style.left = document.querySelector("#" + rover).offsetLeft + "px";
document.querySelector("#loader").style.top = document.querySelector("#" + rover).offsetTop + "px";
document.querySelector("#loader").style.visibility = "visible";
document.querySelector("#loader").style.opacity = "1";
r.onload = () => {
if(r.status == 200) {
data = JSON.parse(r.responseText);
var sol = document.getElementById("sol_select").value;
let photosForSol = data.photo_manifest.photos.filter(s=> s.sol == sol);
if(photosForSol.length == 0) {
showError(`No photos for ${rover} on sol ${sol}`);
clearLoading();
return;
}
displayRoverDetails(data.photo_manifest);
toggleCard();
getRoverDetails(rover);
clearLoading();
console.log(data);
}else{
showError("Error communicating with NASA");
}
}
r.send();
}
function getRoverDetails(rover) {
// var url = "https://api.nasa.gov/mars-photos/api/v1/rovers/" + rover + "/photos?sol=1000&api_key=" + nak;
var sol = document.getElementById("sol_select").value;
var url = `https://api.nasa.gov/mars-photos/api/v1/rovers/${rover}/photos?sol=${sol}&api_key=${nak}`;
//Create an XMLHttpRequest.
var r = new XMLHttpRequest();
r.open("GET",url);
//Set a loading spinner.
document.querySelector("#loader").style.left = document.querySelector("#" + rover).offsetLeft + "px";
document.querySelector("#loader").style.top = document.querySelector("#" + rover).offsetTop + "px";
document.querySelector("#loader").style.visibility = "visible";
document.querySelector("#loader").style.opacity = "1";
//ES2015 approach. You could easily do:
// r.onload = function (){ ...
r.onload = () => {
if(r.status == 200) {
data = JSON.parse(r.responseText);
if(data.photos.length === 0) {
showError(`No photos for ${rover} on sol ${sol}`);
clearLoading();
return;
}
listImages("ALL");
}else{
//probably an error - possibly a 3xx status code.
showError("Error communicating with NASA");
}
}
r.send();
}
function listImages(camera) {
let photoSet = data.photos.filter(p=>{
if(camera == "ALL"){
return true;
}else{
return p.camera.name == camera;
}
});
document.querySelector("#pictures").innerHTML = "";
if(photoSet.length > 0 ) {
photoSet.forEach(p=>{
let op = document.createElement("option");
op.innerHTML = `ID:${p.id} Date:${p.earth_date} Cam:${p.camera.name}`
op.setAttribute("data-img",p.img_src);
op.setAttribute("data-id",p.id);
op.setAttribute("data-date",p.earth_date);
op.setAttribute("data-cam",p.camera.full_name);
op.addEventListener("click", e=>{
let details = getInfoFromElement(e.target);
setPhotoDetails(details.id, details.date, details.cam);
showPicture(details.img);
})
document.querySelector("#pictures").appendChild(op);
});
}else{
let op = document.createElement("option");
op.innerHTML = "No images for this Camera";
document.querySelector("#pictures").appendChild(op);
}
}
function showPicture(imageUrl) {
var img = document.getElementById("roverPhoto");
img.src = imageUrl;
img.onload = e => {
document.getElementById("photo").classList.remove("hidden");
img.classList.add("image_load");
}
}
function setLoading() {
document.querySelector("#rovers.front div#loader").style.visibility = "visible";
document.querySelector("#rovers.front div#loader").style.opacity = "1";
}
function clearLoading() {
document.querySelector("#rovers.front div#loader").style.visibility = "hidden";
document.querySelector("#rovers.front div#loader").style.opacity = "0";
}
function showError(errorMessage) {
document.querySelector("#errorPanel div").innerHTML = errorMessage;
document.querySelector("#errorPanel").classList.add("show");
}
</script>
</body>
</html>