From 980b3d202b5a53428873feeac2963920d01db8c9 Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Mon, 22 May 2023 15:31:09 -0500 Subject: initial commit --- assets/js/scripts.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 assets/js/scripts.js (limited to 'assets/js') diff --git a/assets/js/scripts.js b/assets/js/scripts.js new file mode 100644 index 0000000..44c264c --- /dev/null +++ b/assets/js/scripts.js @@ -0,0 +1,79 @@ +function fetchAPOD() { + var nasaURL = "https://api.nasa.gov/planetary/apod?api_key=1GOcJRVS7vlt3ePPXPquaxDi8gfduRVeFJwcemIN"; + + $.ajax({ + url: nasaURL, + success: function(result) { + $("#apod-container").css("display", "block"); + if ("copyright" in result) { + $("#apod-credit").text("Credit: " + result.copyright); + } else { + $("#apod-credit").text("Credit: " + "Public Domain"); + } + + if (result.media_type === "video") { + $("#apod-vid").css("display", "block"); + $("#apod-vid").attr("src", result.url); + } else { + $("#apod-img").css("display", "block"); + $("#apod-img").attr("src", result.url); + } + $("#apod-explanation").text(result.explanation); + $("#apod-title").text(result.title); + } + }); + +} + +// NASA Image API Search +function search() { + $("#temporary-banner").hide(); + $(".card-columns").empty(); + var searchTerm = $("#search_input").val(); + searchAJAX(undefined, searchTerm); +} + +function searchAJAX(searchURL, searchTerm) { + if (searchURL === undefined) { + searchURL = "https://images-api.nasa.gov/search?q=" + searchTerm; + } + + $.ajax({ + url: searchURL, + success: function(result) { + console.log(result); + var totalHits = result.collection.metadata.total_hits; + for (var i = 0; i < totalHits; i++) { + if (result.collection.items[i] != undefined && result.collection.items[i].data[0].media_type == "image") { + var title = result.collection.items[i].data[0].title; + var thumbnail = result.collection.items[i].links[0].href; + var card = ""; + $(".card-columns").append(card); + } + } + /* + ** + ** 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. + ** Instead of doing this automatically, there should be a button navigation implemented. + ** + if (result.collection.links[0].rel === "next") { + searchAJAX(result.collection.links[0].href, searchTerm); + } else if (result.collection.links[1].rel === "next") { + searchAJAX(result.collection.links[1].href, searchTerm); + } + */ + } + }); +} + +function setSearchInput() { + document.getElementById("search_input").onkeypress = function(e) { + if (!e) { + e = window.event; + } + if (e.keyCode == "13") { + search(); + return false; + } + }; +} -- cgit v1.2.3-70-g09d2