let today = new Date(); let endDate = new Date(); const ids = []; let id = 0; const apiKEY = "1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et"; today.setDate(today.getDate() - 17); $(document).ready(function() { $.ajax({ url: "https://api.nasa.gov/planetary/apod", type: "GET", data: { api_key: apiKEY, start_date: `${today.getUTCFullYear()}-${today.getUTCMonth() + 1}-${today.getUTCDate()}`, end_date: `${endDate.getUTCFullYear()}-${endDate.getUTCMonth() + 1}-${endDate.getUTCDate()}`, }, success: function(data) { wallpaper(data); }, }); }); function wallpaper(data) { $(".preloader").hide(); data = data.reverse(); for (let i = 0; i < data.length; i++, id++) { ids.push(data[i]); if (ids.filter((item) => item['url'] === data[i]['url']).length > 1) { continue; } let image = new Image(); image.src = ids[id]['media_type'] === "video" ? `https://img.youtube.com/vi/${ids[id]['url'].slice(30, 41)}/maxresdefault.jpg` : ids[id]['url']; image.onload = function() { if(image.width+image.height !== 210) { $(`img[data-src="${image.src}"]`).attr("src", `${image.src}`); } } if (ids[id]['media_type'] === "image") { $(".header-row").append(`
`); } else { $(".header-row").append(` `); } } $(".card-img-top").on("load", function() { $(this).removeClass("shimmer"); }); let button_modal = document.querySelector(".header-row"); button_modal.addEventListener("click", function (event) { if (event.target === button_modal) { return; } let id = event.target.getAttribute("id"); let img = document.querySelector(".modal-body"); let title = document.querySelector(".w-modal-title"); let button = document.querySelector(".modal-footer"); if($(`img#${id}.card-img-top`).attr("src") !== "http://localhost:3000/static/image/placeholder.png") { button.innerHTML = ``; } else { button.innerHTML = ``; } let setWallpaper = document.querySelector("#setWallpaper"); ids[id]['copyright'] = ids[id]['copyright'] === undefined ? "NASA" : ids[id]['copyright']; let explanation = ids[id]['explanation'].length > 200 ? ids[id]['explanation'].slice(0, 200) + "..." : ids[id]['explanation']; if (ids[id]['media_type'] === "image") { title.innerHTML = `Author: ${ids[id]['copyright']}
Date of publication: ${ids[id]['date']}
Explanation: ${explanation}
`; setWallpaper.addEventListener("click", function () { wallpaperUpdate(ids[id]['hdurl']); }); } else { title.innerHTML = `Author: ${ids[id]['copyright']}
Date of publication: ${ids[id]['date']}
Explanation: ${explanation}
`; setWallpaper.addEventListener("click", function () { wallpaperUpdate(`https://img.youtube.com/vi/${ids[id]['url'].slice(30, 41)}/maxresdefault.jpg`); }); } let showMore = document.querySelector("#show-more"); showMore.addEventListener("click", function () { let explanation = document.querySelector(".modal-body p:nth-child(4)"); if(showMore.innerHTML === "Show more") { explanation.innerHTML = `Explanation: ${ids[id]['explanation']}`; showMore.innerHTML = "Show less"; } else { explanation.innerHTML = explanation.innerHTML.slice(0, 200) + "..."; showMore.innerHTML = "Show more"; } }); }); $(window).scroll(function () { if (($(window).scrollTop() > $(document).height() - $(window).height() - 100)) { $(".preloader").show(); $(window).off("scroll"); today.setDate(today.getDate() - 1); endDate.setDate(endDate.getDate() - 15); endDate.setMonth(endDate.getMonth() - 1); $.ajax({ url: "https://api.nasa.gov/planetary/apod", data: { api_key: apiKEY, start_date: `${endDate.getUTCFullYear()}-${endDate.getUTCMonth() + 1}-${endDate.getUTCDate()}`, end_date: `${today.getUTCFullYear()}-${today.getUTCMonth() + 1}-${today.getUTCDate()}`, }, success: function (data) { wallpaper(data); $(window).on("scroll"); }, }); } }); } function wallpaperUpdate(url) { $.ajax({ url: "http://localhost:3000/api/update/wallpaper", type: "POST", data: { url: url, }, success: function (data) { $(".toast-body").text(data.message); let toastLiveExample = document.getElementById('liveToast'); let toast = new bootstrap.Toast(toastLiveExample); toast.show(); }, }); }