Tranga-Website/Website/interaction.js

366 lines
13 KiB
JavaScript
Raw Normal View History

let publications = [];
2023-05-23 17:57:48 +02:00
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 settingsPopup = document.querySelector("#settingsPopup");
2023-05-23 16:27:09 +02:00
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");
const tagTasksRunning = document.querySelector("#tasksRunningTag");
const tagTasksQueued = document.querySelector("#tasksQueuedTag");
const tagTasksTotal = document.querySelector("#totalTasksTag");
2023-05-25 21:58:45 +02:00
const tagTaskPopup = document.querySelector("footer-tag-popup");
const tagTasksPopupContent = document.querySelector("footer-tag-content");
2023-05-23 16:27:09 +02:00
2023-05-24 21:48:54 +02:00
settingsCog.addEventListener("click", () => OpenSettings());
document.querySelector("#blurBackgroundSettingsPopup").addEventListener("click", () => HideSettings());
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-25 21:58:45 +02:00
tagTasksRunning.addEventListener("mouseover", (event) => ShowRunningTasks(event));
tagTasksRunning.addEventListener("mouseout", () => CloseTasksPopup());
tagTasksQueued.addEventListener("mouseover", (event) => ShowQueuedTasks(event));
tagTasksQueued.addEventListener("mouseout", () => CloseTasksPopup());
tagTasksTotal.addEventListener("mouseover", (event) => ShowAllTasks(event));
tagTasksTotal.addEventListener("mouseout", () => CloseTasksPopup());
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 14:28:56 +02:00
img.src = `imageCache/${publication.coverFileNameInCache}`;
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-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){
//Show popup
publicationViewerPopup.style.display = "block";
2023-05-24 20:17:50 +02:00
//Set position to mouse-position
if(event.clientY < window.innerHeight - publicationViewerWindow.offsetHeight)
publicationViewerWindow.style.top = `${event.clientY}px`;
else
publicationViewerWindow.style.top = `${event.clientY - publicationViewerWindow.offsetHeight}px`;
if(event.clientX < window.innerWidth - publicationViewerWindow.offsetWidth)
publicationViewerWindow.style.left = `${event.clientX}px`;
else
publicationViewerWindow.style.left = `${event.clientX - publicationViewerWindow.offsetWidth}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 = `imageCache/${publication.coverFileNameInCache}`;
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 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();
settingsPopup.style.display = "flex";
}
function HideSettings(){
settingsPopup.style.display = "none";
2023-05-24 21:48:54 +02:00
}
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-25 21:58:45 +02:00
function ShowRunningTasks(event){
GetRunningTasks()
.then(json => {
tagTasksPopupContent.replaceChildren();
json.forEach(task => {
var taskname = document.createElement("footer-tag-task-name");
taskname.innerText = task.publication.sortName;
tagTasksPopupContent.appendChild(taskname);
});
if(json.length > 0){
tagTaskPopup.style.display = "block";
tagTaskPopup.style.left = `${event.client - 20}px`;
}
});
}
function ShowQueuedTasks(event){
GetQueue()
.then(json => {
tagTasksPopupContent.replaceChildren();
json.forEach(task => {
var taskname = document.createElement("footer-tag-task-name");
taskname.innerText = task.publication.sortName;
tagTasksPopupContent.appendChild(taskname);
});
if(json.length > 0){
tagTaskPopup.style.display = "block";
tagTaskPopup.style.left = `${event.clientX - 20}px`;
}
});
}
function ShowAllTasks(event){
GetDownloadTasks()
.then(json => {
tagTasksPopupContent.replaceChildren();
json.forEach(task => {
var taskname = document.createElement("footer-tag-task-name");
taskname.innerText = task.publication.sortName;
tagTasksPopupContent.appendChild(taskname);
});
if(json.length > 0){
tagTaskPopup.style.display = "block";
tagTaskPopup.style.left = `${event.clientX - 20}px`;
}
});
}
function CloseTasksPopup(){
tagTaskPopup.style.display = "none";
}
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
GetRunningTasks()
.then(json => {
tagTasksRunning.innerText = json.length;
});
GetDownloadTasks()
.then(json => {
tagTasksTotal.innerText = json.length;
});
GetQueue()
.then(json => {
tagTasksQueued.innerText = json.length;
})
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;
}
}
);
GetRunningTasks()
.then(json => {
tagTasksRunning.innerText = json.length;
});
GetDownloadTasks()
.then(json => {
tagTasksTotal.innerText = json.length;
});
GetQueue()
.then(json => {
tagTasksQueued.innerText = json.length;
})
2023-05-23 18:12:45 +02:00
}, 1000);