Starlio-web/web/static/scripts/gallery.js

120 lines
5.9 KiB
JavaScript
Raw Normal View History

2022-09-18 19:27:23 +03:00
$.ajax({
2022-09-19 02:08:26 +03:00
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)}`,
2022-09-18 19:27:23 +03:00
type: "GET",
success: function(data){
2022-09-19 02:08:26 +03:00
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)
}
})
}
});
const myModal = new bootstrap.Modal('#Wallpaper', {
keyboard: false
})
myModal.show()
function Wallpaper(data) {
2022-09-19 23:40:49 +03:00
$(".preloader").fadeOut(0)
2022-09-19 02:08:26 +03:00
for (let i = 0; i < data.length; i++) {
if (data[i].media_type === "image") {
$(".header-row").append(`
2022-09-18 19:27:23 +03:00
<div class="card">
2022-09-19 02:08:26 +03:00
<a data-bs-toggle="modal" href="#WallpaperModal" role="button">
<img src="${data[i].url}" alt="${data[i].title}" class="card-img-top" idi="${i+1}">
</a>
</div>
`)
} else {
$(".header-row").append(`
<div class="card">
<a data-bs-toggle="modal" href="#WallpaperModal" role="button">
<img src="https://img.youtube.com/vi/${data[i].url.slice(30,41)}/maxresdefault.jpg" alt="${data[i].title}" class="card-img-top" idi="${i+1}">
</a>
2022-09-18 19:27:23 +03:00
</div>
`)
}
2022-09-19 02:08:26 +03:00
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")
2022-09-21 10:46:02 +03:00
let title = document.querySelector(".w-modal-title")
2022-09-19 02:08:26 +03:00
let button = document.querySelector(".modal-footer")
button.innerHTML = `
2022-09-19 13:25:20 +03:00
<button type="button" class="btn btn-primary" id="setWallpaper">Set Wallpaper</button>
`
2022-09-19 02:08:26 +03:00
let setWallpaper = document.querySelector("#setWallpaper")
if(data[id-1].media_type === "image") {
img.innerHTML = `
2022-09-19 13:25:20 +03:00
<img src="${data[id - 1].url}" alt="${data[id - 1].title}" class="card-img">
<p><strong>Author:</strong> ${data[id - 1].copyright}</p>
<p><strong>Date of publication:</strong> ${data[id - 1].date}</p>
<p><strong>Explanation:</strong> ${data[id - 1].explanation}</p>
2022-09-19 23:40:49 +03:00
`.replace("undefined", "Unknown")
2022-09-19 02:08:26 +03:00
title.innerHTML = `
<h5 class="modal-title">${data[id - 1].title}</h5>
`
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 = `
<iframe width="460" height="315" src="${data[id - 1].url}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<p><strong>Author:</strong> ${data[id - 1].copyright}</p>
<p><strong>Date of publication:</strong> ${data[id - 1].date}</p>
<p><strong>Explanation:</strong> ${data[id - 1].explanation}</p>
2022-09-19 23:40:49 +03:00
`.replace("undefined", "Unknown")
2022-09-19 02:08:26 +03:00
title.innerHTML = `
<h5 class="modal-title">${data[id].title}</h5>
`
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);
}
})
})
}
}
})
2022-09-18 19:27:23 +03:00
}
2022-09-19 02:08:26 +03:00
}