StarlioX/web/static/scripts/gallery.js

172 lines
6.3 KiB
JavaScript
Raw Normal View History

let today = new Date();
let endDate = new Date();
const ids = [];
let id = 0;
2022-10-02 12:20:26 +03:00
const apiKEY = "1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et";
2022-10-02 12:20:26 +03:00
today.setDate(today.getDate() - 17);
$(document).ready(function() {
2022-10-02 12:20:26 +03:00
$.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()}`,
2022-10-02 12:20:26 +03:00
},
success: function(data) {
wallpaper(data);
},
});
2022-09-19 02:08:26 +03:00
});
2022-10-02 12:20:26 +03:00
function wallpaper(data) {
$(".preloader").hide();
data = data.reverse();
2022-09-19 02:08:26 +03:00
for (let i = 0; i < data.length; i++, id++) {
ids.push(data[i]);
2022-10-08 10:58:23 +03:00
if (ids.filter((item) => item['url'] === data[i]['url']).length > 1) {
continue;
}
2022-10-08 10:58:23 +03:00
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") {
2022-09-19 02:08:26 +03:00
$(".header-row").append(`
2022-10-02 12:20:26 +03:00
<div class="card">
<a data-bs-toggle="modal" href="#WallpaperModal" role="button">
2022-10-08 10:58:23 +03:00
<img src="${ids[id]['url']}" alt="${ids[id]['title']}" class="card-img-top" id="${id}">
2022-10-02 12:20:26 +03:00
</a>
</div>
`);
2022-09-19 02:08:26 +03:00
} else {
$(".header-row").append(`
2022-10-08 10:58:23 +03:00
<div class="card">
<a data-bs-toggle="modal" href="#WallpaperModal" role="button">
<img src="http://localhost:4662/static/image/placeholder.png"
data-src="https://img.youtube.com/vi/${ids[id]['url'].slice(30, 41)}/maxresdefault.jpg"
alt="${ids[id]['title']}"
class="card-img-top"
id="${id}">
</a>
</div>
`);
2022-09-18 19:27:23 +03:00
}
2022-10-02 12:20:26 +03:00
}
2022-10-08 10:58:23 +03:00
2022-10-02 12:20:26 +03:00
let button_modal = document.querySelector(".header-row");
button_modal.addEventListener("click", function (event) {
2022-10-08 10:58:23 +03:00
if (event.target === button_modal) {
return;
}
let id = event.target.getAttribute("id");
2022-10-02 12:20:26 +03:00
let img = document.querySelector(".modal-body");
let title = document.querySelector(".w-modal-title");
2022-09-19 02:08:26 +03:00
2022-10-02 12:20:26 +03:00
let button = document.querySelector(".modal-footer");
if($(`img#${id}.card-img-top`).attr("src") !== "http://localhost:4662/static/image/placeholder.png") {
button.innerHTML = `<button type="button" class="btn btn-primary" id="setWallpaper">Set Wallpaper</button>`;
} else {
button.innerHTML = `<button type="button"
class="btn"
id="setWallpaper"
disabled
style="background-color: grey; color: white;">
Set Wallpaper
</button>`;
}
2022-10-02 12:20:26 +03:00
let setWallpaper = document.querySelector("#setWallpaper");
2022-09-19 02:08:26 +03:00
ids[id]['copyright'] = ids[id]['copyright'] === undefined ? "NASA" : ids[id]['copyright'];
2022-09-19 02:08:26 +03:00
if (ids[id]['media_type'] === "image") {
2022-10-08 10:58:23 +03:00
title.innerHTML = `<h5 class="modal-title">${ids[id]['title']}</h5>`;
2022-10-02 12:20:26 +03:00
img.innerHTML = `
<img src="${ids[id]['url']}" alt="${ids[id]['title']}" class="card-img">
<p><strong>Author:</strong> ${ids[id]['copyright']}</p>
<p><strong>Date of publication:</strong> ${ids[id]['date']}</p>
<p><strong>Explanation:</strong> ${ids[id]['explanation']}</p>
2022-10-02 12:20:26 +03:00
`;
2022-09-19 02:08:26 +03:00
2022-10-02 12:20:26 +03:00
setWallpaper.addEventListener("click", function () {
wallpaperUpdate(ids[id]['hdurl']);
2022-10-02 12:20:26 +03:00
});
} else {
2022-10-08 10:58:23 +03:00
title.innerHTML = `<h5 class="modal-title">${ids[id]['title']}</h5>`;
2022-10-02 12:20:26 +03:00
img.innerHTML = `
2022-10-08 10:58:23 +03:00
<iframe width="460"
height="315"
src="${ids[id]['url']}"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen></iframe>
<p><strong>Author:</strong> ${ids[id]['copyright']}</p>
<p><strong>Date of publication:</strong> ${ids[id]['date']}</p>
<p><strong>Explanation:</strong> ${ids[id]['explanation']}</p>
2022-10-02 12:20:26 +03:00
`;
setWallpaper.addEventListener("click", function () {
2022-10-08 10:58:23 +03:00
wallpaperUpdate(`https://img.youtube.com/vi/${ids[id]['url'].slice(30, 41)}/maxresdefault.jpg`);
});
}
});
$(window).scroll(function () {
2022-10-08 10:58:23 +03:00
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");
},
2022-10-02 12:20:26 +03:00
});
}
});
}
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();
},
});
2022-09-19 02:08:26 +03:00
}