From ad74948bd210d4d8978e5c433f50458fc28d1515 Mon Sep 17 00:00:00 2001 From: Redume Date: Mon, 19 Sep 2022 02:08:26 +0300 Subject: [PATCH] gallery made --- main.go | 1 + web/src/gallery.html | 29 +++++++- web/static/scripts/gallery.js | 128 ++++++++++++++++++++++++++++++---- 3 files changed, 143 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 0134ec3..d020ded 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ func main() { router.HandleFunc("/api/get/settings", controllers.SettingsGet).Methods("GET") router.HandleFunc("/api/get/version", controllers.Version).Methods("GET") router.HandleFunc("/api/update/settings", controllers.SettingsUpdate).Methods("POST") + router.HandleFunc("/api/update/wallpaper", controllers.WallpaperUpdate).Methods("POST") go func() { err := http.ListenAndServe(":8080", router) diff --git a/web/src/gallery.html b/web/src/gallery.html index 4756e59..9652cb0 100644 --- a/web/src/gallery.html +++ b/web/src/gallery.html @@ -4,8 +4,7 @@ EveryNasa - - + @@ -32,12 +31,36 @@ +
+ +
+ +
- + \ No newline at end of file diff --git a/web/static/scripts/gallery.js b/web/static/scripts/gallery.js index b879e61..20f9c99 100644 --- a/web/static/scripts/gallery.js +++ b/web/static/scripts/gallery.js @@ -1,17 +1,121 @@ $.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(3, 5)}`, + 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){ - for (let i = 0; i < data.length; i++) { - if (data[i].media_type == "image") { - $(".header-row").append(` - -
- ${data[i].title} -
-
- `) + 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) } - } + }) } -}); \ No newline at end of file +}); + +const myModal = new bootstrap.Modal('#Wallpaper', { + keyboard: false +}) +myModal.show() + +function Wallpaper(data) { + for (let i = 0; i < data.length; i++) { + if (data[i].media_type === "image") { + $(".header-row").append(` +
+ + ${data[i].title} + +
+ `) + } else { + $(".header-row").append(` +
+ + ${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(".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 shooting: ${data[id - 1].date}

+

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

+ ` + 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 shooting: ${data[id - 1].date}

+

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

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