diff --git a/web/static/scripts/gallery.js b/web/static/scripts/gallery.js index cbf224d..ab2406d 100644 --- a/web/static/scripts/gallery.js +++ b/web/static/scripts/gallery.js @@ -1,120 +1,118 @@ -$.ajax({ - url: `https://api.nasa.gov/planetary/apod?api_key=1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et&start_date=${new Date().toLocaleString().slice(6, 10)}-${new Date().toLocaleString().slice(3, 5)}-01&end_date=${new Date().toLocaleString().slice(6,10)}-${new Date().toLocaleString().slice(3, 5)}-${new Date().toLocaleString().slice(0,2)}`, - type: "GET", - success: function(data){ - Wallpaper(data) - }, - error: function(){ - let prev_date = Number(new Date().toLocaleString().slice(0,2)) - this.url = `https://api.nasa.gov/planetary/apod?api_key=1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et&start_date=${new Date().toLocaleString().slice(6, 10)}-${new Date().toLocaleString().slice(3, 5)}-01&end_date=${new Date().toLocaleString().slice(6,10)}-${new Date().toLocaleString().slice(3, 5)}-${prev_date-1}`, - $.ajax({ - url: this.url, - type: "GET", - success: function(data){ - Wallpaper(data) - } - }) - } +$(document).ready(function() { + let today = new Date(); + let date = today.setDate(today.getDate() - 31); + date = new Date(date); + + const apiKEY = "1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et"; + + $.ajax({ + url: "https://api.nasa.gov/planetary/apod", + type: "GET", + data: { + api_key: apiKEY, + start_date: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`, + end_date: `${new Date().toLocaleString().slice(6, 10)}-${new Date().toLocaleString().slice(3, 5)}-${new Date().toLocaleString().slice(0, 2)}`, + }, + success: function(data) { + wallpaper(data); + }, + error: function() { + let prev_date = Number(new Date().toLocaleString().slice(0,2))-1; + $.ajax({ + url: "https://api.nasa.gov/planetary/apod", + data: { + api_key: apiKEY, + start_date: `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`, + end_date: `${new Date().toLocaleString().slice(6,10)}-${new Date().toLocaleString().slice(3, 5)}-${prev_date}`, + }, + success: function (data) { + wallpaper(data); + } + }); + }, + }); }); const myModal = new bootstrap.Modal('#Wallpaper', { keyboard: false -}) -myModal.show() +}); +myModal.show(); + +function wallpaper(data) { + $(".preloader").hide(); + data = data.reverse(); -function Wallpaper(data) { - $(".preloader").fadeOut(0) for (let i = 0; i < data.length; i++) { - if (data[i].media_type === "image") { + if (data[i]['media_type'] === "image") { $(".header-row").append(` -
- - ${data[i].title} - -
- `) +
+ + ${data[i]['title']} + +
+ `); } else { $(".header-row").append(`
- ${data[i].title} + ${data[i]['title']}
- `) + `); } - let buttons = document.querySelector(".header-row") - buttons.addEventListener("click", function (event) { - if (event.target.tagName === "IMG") { - let id = event.target.getAttribute("idi") - let img = document.querySelector(".modal-body") - let title = document.querySelector(".w-modal-title") - - let button = document.querySelector(".modal-footer") - button.innerHTML = ` - - ` - let setWallpaper = document.querySelector("#setWallpaper") - - if(data[id-1].media_type === "image") { - img.innerHTML = ` - ${data[id - 1].title} -

Author: ${data[id - 1].copyright}

-

Date of publication: ${data[id - 1].date}

-

Explanation: ${data[id - 1].explanation}

- `.replace("undefined", "Unknown") - title.innerHTML = ` - - ` - - setWallpaper.addEventListener("click", function () { - $.ajax({ - url: `http://localhost:8080/api/update/wallpaper`, - type: "POST", - data: { - url: data[id - 1].hdurl - }, - success: function () { - $(".toast-body").text("The wallpaper has been installed") - let toastLiveExample = document.getElementById('liveToast') - let toast = new bootstrap.Toast(toastLiveExample) - toast.show() - }, - error: function (err) { - console.error(err); - } - }) - }) - } else { - img.innerHTML = ` - -

Author: ${data[id - 1].copyright}

-

Date of publication: ${data[id - 1].date}

-

Explanation: ${data[id - 1].explanation}

- `.replace("undefined", "Unknown") - title.innerHTML = ` - - ` - - setWallpaper.addEventListener("click", function () { - $.ajax({ - url: `http://localhost:8080/api/update/wallpaper`, - type: "POST", - data: { - url: `https://img.youtube.com/vi/${data[id - 1].url.slice(30,41)}/maxresdefault.jpg` - }, - success: function () { - $(".toast-body").text("The wallpaper has been installed") - let toastLiveExample = document.getElementById('liveToast') - let toast = new bootstrap.Toast(toastLiveExample) - toast.show() - }, - error: function (err) { - console.error(err); - } - }) - }) - } - } - }) } + let button_modal = document.querySelector(".header-row"); + button_modal.addEventListener("click", function (event) { + let id = event.target.getAttribute("idi") - 1; + let img = document.querySelector(".modal-body"); + let title = document.querySelector(".w-modal-title"); + + let button = document.querySelector(".modal-footer"); + button.innerHTML = ``; + let setWallpaper = document.querySelector("#setWallpaper"); + + data[id]['copyright'] = data[id]['copyright'] === undefined ? "NASA" : data[id]['copyright']; + + if (data[id]['media_type'] === "image") { + img.innerHTML = ` + ${data[id]['title']} +

Author: ${data[id]['copyright']}

+

Date of publication: ${data[id]['date']}

+

Explanation: ${data[id]['explanation']}

+ `; + + title.innerHTML = ``; + setWallpaper.addEventListener("click", function () { + wallpaperUpdate(data[id]['hdurl']); + }); + } else { + img.innerHTML = ` + +

Author: ${data[id]['copyright']}

+

Date of publication: ${data[id]['date']}

+

Explanation: ${data[id]['explanation']}

+ `; + + title.innerHTML = ``; + setWallpaper.addEventListener("click", function () { + wallpaperUpdate(`https://img.youtube.com/vi/${data[id]['url'].slice(30,41)}/maxresdefault.jpg`); + }); + } + }); +} + +function wallpaperUpdate(url) { + $.ajax({ + url: "http://localhost:8080/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(); + }, + }); } \ No newline at end of file