krz/space-wiki

A fun wiki about space.

clone: git clone https://gitbay.org/krz/space-wiki.git

main: assets/js/scripts.js · raw

 1function fetchAPOD() {
 2  var nasaURL = "https://api.nasa.gov/planetary/apod?api_key=1GOcJRVS7vlt3ePPXPquaxDi8gfduRVeFJwcemIN";
 3
 4  $.ajax({
 5    url: nasaURL,
 6    success: function(result) {
 7      $("#apod-container").css("display", "block");
 8      if ("copyright" in result) {
 9        $("#apod-credit").text("Credit: " + result.copyright);
10      } else {
11        $("#apod-credit").text("Credit: " + "Public Domain");
12      }
13
14      if (result.media_type === "video") {
15        $("#apod-vid").css("display", "block");
16        $("#apod-vid").attr("src", result.url);
17      } else {
18        $("#apod-img").css("display", "block");
19        $("#apod-img").attr("src", result.url);
20      }
21      $("#apod-explanation").text(result.explanation);
22      $("#apod-title").text(result.title);
23    }
24  });
25
26}
27
28// NASA Image API Search
29function search() {
30  $("#temporary-banner").hide();
31  $(".card-columns").empty();
32  var searchTerm = $("#search_input").val();
33  searchAJAX(undefined, searchTerm);
34}
35
36function searchAJAX(searchURL, searchTerm) {
37  if (searchURL === undefined) {
38    searchURL = "https://images-api.nasa.gov/search?q=" + searchTerm;
39  }
40
41  $.ajax({
42    url: searchURL,
43    success: function(result) {
44      console.log(result);
45      var totalHits = result.collection.metadata.total_hits;
46      for (var i = 0; i < totalHits; i++) {
47        if (result.collection.items[i] != undefined && result.collection.items[i].data[0].media_type == "image") {
48          var title = result.collection.items[i].data[0].title;
49          var thumbnail = result.collection.items[i].links[0].href;
50          var card = "<div class='card gallery-card'><a href=" + thumbnail + " target='_blank'><img class='card-img-top' src='" + thumbnail + "' alt='" + title + "' /></a><div class='card-body' style='padding:0.5rem;'><p class='card-title'>" + title + "</p></div></div>";
51          $(".card-columns").append(card);
52        }
53      }
54      /*
55      ** 
56      ** This next piece of code allows the function to continue returning additional sets of API results (100 per API call) until there are no more pages of results.
57      ** Instead of doing this automatically, there should be a button navigation implemented.
58      **
59      if (result.collection.links[0].rel === "next") {
60        searchAJAX(result.collection.links[0].href, searchTerm);
61      } else if (result.collection.links[1].rel === "next") {
62        searchAJAX(result.collection.links[1].href, searchTerm);
63      }
64      */
65    }
66  });
67}
68
69function setSearchInput() {
70  document.getElementById("search_input").onkeypress = function(e) {
71    if (!e) {
72      e = window.event;
73    }
74    if (e.keyCode == "13") {
75      search();
76      return false;
77    }
78  };
79}