Tranga-Website/Website/interaction.js

288 lines
9.8 KiB
JavaScript
Raw Normal View History

2023-05-22 23:52:35 +02:00
const slideInRight = [
{ right: "-20rem" },
{ right: "0" }
];
const slideInRightTiming = {
duration: 200,
iterations: 1,
fill: "forwards",
easing: "ease-out"
}
const slideOutRightTiming = {
direction: "reverse",
duration: 200,
iterations: 1,
fill: "forwards",
easing: "ease-in"
}
2023-05-23 17:57:48 +02:00
let publications = [];
let tasks = [];
2023-05-23 18:28:27 +02:00
let toEditId;
2023-05-23 17:57:48 +02:00
2023-05-23 16:27:09 +02:00
const searchPublicationQuery = document.querySelector("#searchPublicationQuery");
const selectPublication = document.querySelector("#taskSelectOutput");
const connectorSelect = document.querySelector("#connectors");
const settingsTab = document.querySelector("#settingstab");
const settingsCog = document.querySelector("#settingscog");
const selectRecurrence = document.querySelector("#selectReccurrence");
const tasksContent = document.querySelector("content");
2023-05-23 18:46:06 +02:00
const addTaskPopup = document.querySelector("#addTaskPopup");
const publicationViewerPopup = document.querySelector("#publicationViewerPopup");
const publicationViewerWindow = document.querySelector("publication-viewer");
2023-05-23 17:57:48 +02:00
const publicationViewerDescription = document.querySelector("#publicationViewerDescription");
const publicationViewerName = document.querySelector("#publicationViewerName");
const publicationViewerAuthor = document.querySelector("#publicationViewerAuthor");
const pubviewcover = document.querySelector("#pubviewcover");
const publicationDelete = document.querySelector("publication-delete");
2023-05-23 18:28:27 +02:00
const publicationAdd = document.querySelector("publication-add");
2023-05-23 16:27:09 +02:00
const closetaskpopup = document.querySelector("#closePopupImg");
2023-05-24 20:57:17 +02:00
const settingDownloadLocation = document.querySelector("#downloadLocation");
2023-05-24 21:48:54 +02:00
const settingKomgaUrl = document.querySelector("#komgaUrl");
2023-05-24 20:57:17 +02:00
const settingKomgaUser = document.querySelector("#komgaUsername");
const settingKomgaPass = document.querySelector("#komgaPassword");
const settingKomgaTime = document.querySelector("#komgaUpdateTime");
2023-05-24 21:48:54 +02:00
const settingKomgaConfigured = document.querySelector("#komgaConfigured");
2023-05-25 10:42:19 +02:00
const settingApiUri = document.querySelector("#settingApiUri");
2023-05-24 20:57:17 +02:00
2023-05-23 16:27:09 +02:00
2023-05-24 21:48:54 +02:00
settingsCog.addEventListener("click", () => OpenSettings());
2023-05-23 18:46:06 +02:00
closetaskpopup.addEventListener("click", () => HideAddTaskPopup());
document.querySelector("#blurBackgroundTaskPopup").addEventListener("click", () => HideAddTaskPopup());
document.querySelector("#blurBackgroundPublicationPopup").addEventListener("click", () => HidePublicationPopup());
2023-05-23 18:07:15 +02:00
publicationDelete.addEventListener("click", () => DeleteTaskClick());
2023-05-23 18:46:06 +02:00
publicationAdd.addEventListener("click", () => AddTaskClick());
2023-05-25 10:42:19 +02:00
settingApiUri.addEventListener("keypress", (event) => {
if(event.key === "Enter"){
apiUri = settingApiUri.value;
setTimeout(() => GetSettingsClick(), 100);
document.cookie = `apiUri=${apiUri};`;
}
});
searchPublicationQuery.addEventListener("keypress", (event) => {
if(event.key === "Enter"){
NewSearch();
}
});
2023-05-23 16:27:09 +02:00
2023-05-23 14:44:59 +02:00
let availableConnectors;
GetAvailableControllers()
.then(json => availableConnectors = json)
2023-05-23 15:15:29 +02:00
.then(json =>
2023-05-23 14:44:59 +02:00
json.forEach(connector => {
2023-05-23 15:15:29 +02:00
var option = document.createElement('option');
option.value = connector;
option.innerText = connector;
connectorSelect.appendChild(option);
})
);
2023-05-23 14:44:59 +02:00
2023-05-24 20:57:41 +02:00
function NewSearch(){
//Disable inputs
selectRecurrence.disabled = true;
connectorSelect.disabled = true;
searchPublicationQuery.disabled = true;
//Empty previous results
selectPublication.replaceChildren();
GetPublication(connectorSelect.value, searchPublicationQuery.value)
.then(json =>
json.forEach(publication => {
var option = CreatePublication(publication, connectorSelect.value);
option.addEventListener("click", (mouseEvent) => {
ShowPublicationViewerWindow(publication.internalId, mouseEvent, true);
});
selectPublication.appendChild(option);
}
))
.then(() => {
//Re-enable inputs
selectRecurrence.disabled = false;
connectorSelect.disabled = false;
searchPublicationQuery.disabled = false;
});
}
2023-05-24 20:17:50 +02:00
//Returns a new "Publication" Item to display in the tasks section
2023-05-23 16:27:09 +02:00
function CreatePublication(publication, connector){
2023-05-23 17:57:48 +02:00
var publicationElement = document.createElement('publication');
publicationElement.setAttribute("id", publication.internalId);
2023-05-23 15:15:29 +02:00
var img = document.createElement('img');
2023-05-25 13:58:10 +02:00
img.src = `data:image;base64,${publication.posterBase64}`;
2023-05-23 17:57:48 +02:00
publicationElement.appendChild(img);
2023-05-23 15:15:29 +02:00
var info = document.createElement('publication-information');
var connectorName = document.createElement('connector-name');
2023-05-23 16:27:09 +02:00
connectorName.innerText = connector;
2023-05-23 15:15:29 +02:00
connectorName.className = "pill";
info.appendChild(connectorName);
var publicationName = document.createElement('publication-name');
publicationName.innerText = publication.sortName;
info.appendChild(publicationName);
2023-05-23 17:57:48 +02:00
publicationElement.appendChild(info);
if(publications.filter(pub => pub.internalId === publication.internalId) < 1)
publications.push(publication);
return publicationElement;
2023-05-23 15:15:29 +02:00
}
2023-05-23 18:07:15 +02:00
function DeleteTaskClick(){
2023-05-23 18:28:27 +02:00
taskToDelete = tasks.filter(tTask => tTask.publication.internalId === toEditId)[0];
DeleteTask("DownloadNewChapters", taskToDelete.connectorName, toEditId);
HidePublicationPopup();
2023-05-23 18:46:06 +02:00
}
function AddTaskClick(){
CreateTask("DownloadNewChapters", selectRecurrence.value, connectorSelect.value, toEditId, "en")
HideAddTaskPopup();
HidePublicationPopup();
2023-05-23 15:15:29 +02:00
}
2023-05-23 14:44:59 +02:00
2023-05-24 20:17:50 +02:00
let slideIn = true;
2023-05-23 17:57:48 +02:00
function slide() {
if (slideIn)
2023-05-22 23:52:35 +02:00
settingsTab.animate(slideInRight, slideInRightTiming);
else
settingsTab.animate(slideInRight, slideOutRightTiming);
slideIn = !slideIn;
}
2023-05-23 16:27:09 +02:00
function ResetContent(){
2023-05-24 20:17:50 +02:00
//Delete everything
2023-05-23 16:27:09 +02:00
tasksContent.replaceChildren();
2023-05-24 20:17:50 +02:00
//Add "Add new Task" Button
2023-05-23 16:27:09 +02:00
var add = document.createElement("div");
add.setAttribute("id", "addPublication")
var plus = document.createElement("p");
plus.innerText = "+";
add.appendChild(plus);
add.addEventListener("click", () => ShowNewTaskWindow());
tasksContent.appendChild(add);
}
2023-05-23 18:28:27 +02:00
function ShowPublicationViewerWindow(publicationId, event, add){
2023-05-24 20:17:50 +02:00
//Set position to mouse-position
2023-05-23 18:46:06 +02:00
publicationViewerWindow.style.top = `${event.clientY - 60}px`;
publicationViewerWindow.style.left = `${event.clientX}px`;
2023-05-23 17:57:48 +02:00
2023-05-24 20:17:50 +02:00
//Edit information inside the window
var publication = publications.filter(pub => pub.internalId === publicationId)[0];
2023-05-23 17:57:48 +02:00
publicationViewerName.innerText = publication.sortName;
publicationViewerDescription.innerText = publication.description;
publicationViewerAuthor.innerText = publication.author;
pubviewcover.src = publication.posterUrl;
2023-05-23 18:28:27 +02:00
toEditId = publicationId;
2023-05-23 17:57:48 +02:00
2023-05-24 20:17:50 +02:00
//Check what action should be listed
2023-05-23 18:28:27 +02:00
if(add){
publicationAdd.style.display = "block";
publicationDelete.style.display = "none";
}
2023-05-23 18:46:06 +02:00
else{
publicationAdd.style.display = "none";
publicationDelete.style.display = "block";
}
2023-05-23 18:28:27 +02:00
2023-05-24 20:17:50 +02:00
//Show popup
2023-05-23 18:46:06 +02:00
publicationViewerPopup.style.display = "block";
2023-05-23 17:57:48 +02:00
}
2023-05-24 20:17:50 +02:00
function HidePublicationPopup(){
publicationViewerPopup.style.display = "none";
}
2023-05-23 16:27:09 +02:00
function ShowNewTaskWindow(){
selectPublication.replaceChildren();
2023-05-23 18:46:06 +02:00
addTaskPopup.style.display = "block";
}
function HideAddTaskPopup(){
addTaskPopup.style.display = "none";
2023-05-23 16:27:09 +02:00
}
2023-05-23 18:46:06 +02:00
2023-05-23 16:27:09 +02:00
const fadeIn = [
{ opacity: "0" },
{ opacity: "1" }
];
const fadeInTiming = {
2023-05-23 17:57:48 +02:00
duration: 50,
2023-05-23 16:27:09 +02:00
iterations: 1,
fill: "forwards"
}
2023-05-24 21:48:54 +02:00
function OpenSettings(){
GetSettingsClick();
slide();
}
2023-05-24 20:57:17 +02:00
function GetSettingsClick(){
2023-05-25 10:42:19 +02:00
settingApiUri.value = "";
2023-05-24 21:48:54 +02:00
settingKomgaUrl.value = "";
settingKomgaUser.value = "";
settingKomgaPass.value = "";
2023-05-25 10:42:19 +02:00
settingApiUri.placeholder = apiUri;
2023-05-24 21:48:54 +02:00
GetSettings().then(json => {
settingDownloadLocation.innerText = json.downloadLocation;
if(json.komga != null)
settingKomgaUrl.placeholder = json.komga.baseUrl;
});
2023-05-24 20:57:17 +02:00
2023-05-24 21:48:54 +02:00
GetKomgaTask().then(json => {
if(json.length > 0)
settingKomgaConfigured.innerText = "✅";
else
settingKomgaConfigured.innerText = "❌";
});
2023-05-24 20:57:17 +02:00
}
2023-05-25 10:42:19 +02:00
function UpdateKomgaSettings(){
2023-05-24 21:48:54 +02:00
var auth = utf8_to_b64(`${settingKomgaUser.value}:${settingKomgaPass.value}`);
console.log(auth);
UpdateSettings("", settingKomgaUrl.value, auth);
2023-05-24 20:57:17 +02:00
CreateTask("UpdateKomgaLibrary", settingKomgaTime.value, "","","");
2023-05-24 21:48:54 +02:00
setTimeout(() => GetSettingsClick(), 500);
}
function utf8_to_b64( str ) {
return window.btoa(unescape(encodeURIComponent( str )));
2023-05-24 20:57:17 +02:00
}
2023-05-24 20:17:50 +02:00
//Resets the tasks shown
2023-05-23 16:27:09 +02:00
ResetContent();
2023-05-24 20:17:50 +02:00
//Get Tasks and show them
2023-05-24 21:48:54 +02:00
GetDownloadTasks()
2023-05-23 16:27:09 +02:00
.then(json => json.forEach(task => {
var publication = CreatePublication(task.publication, task.connectorName);
publication.addEventListener("click", (event) => ShowPublicationViewerWindow(task.publication.internalId, event, false));
2023-05-23 16:27:09 +02:00
tasksContent.appendChild(publication);
tasks.push(task);
2023-05-23 16:27:09 +02:00
}));
2023-05-23 14:44:59 +02:00
setInterval(() => {
2023-05-24 20:17:50 +02:00
//Tasks from API
var cTasks = [];
2023-05-24 21:48:54 +02:00
GetDownloadTasks()
.then(json => json.forEach(task => cTasks.push(task)))
.then(() => {
2023-05-24 20:17:50 +02:00
//Only update view if tasks-amount has changed
if(tasks.length != cTasks.length) {
2023-05-24 20:17:50 +02:00
//Resets the tasks shown
ResetContent();
2023-05-24 20:17:50 +02:00
//Add all currenttasks to view
2023-05-23 18:19:46 +02:00
cTasks.forEach(task => {
var publication = CreatePublication(task.publication, task.connectorName);
publication.addEventListener("click", (event) => ShowPublicationViewerWindow(task.publication.internalId, event, false));
2023-05-23 18:19:46 +02:00
tasksContent.appendChild(publication);
})
tasks = cTasks;
}
}
);
2023-05-23 18:12:45 +02:00
}, 1000);