Files
ConvertX/public/script.js
T

223 lines
6.3 KiB
JavaScript
Raw Normal View History

2024-04-10 14:42:05 +02:00
// Select the file input element
const fileInput = document.querySelector('input[type="file"]');
2024-09-09 17:38:03 +02:00
const dropZone = document.getElementById("dropzone");
2024-05-19 00:07:56 +02:00
const fileNames = [];
2024-05-19 23:51:27 +02:00
let fileType;
2024-04-10 14:42:05 +02:00
2024-10-05 00:43:24 +02:00
dropZone.addEventListener("dragover", () => {
2024-09-09 17:38:03 +02:00
dropZone.classList.add("dragover");
});
2024-10-05 00:43:24 +02:00
dropZone.addEventListener("dragleave", () => {
2024-09-09 17:38:03 +02:00
dropZone.classList.remove("dragover");
});
2024-10-07 10:47:37 +02:00
dropZone.addEventListener("drop", () => {
dropZone.classList.remove("dragover");
});
2024-08-20 14:59:25 -04:00
const selectContainer = document.querySelector("form .select_container");
const updateSearchBar = () => {
const convertToInput = document.querySelector(
"input[name='convert_to_search']",
);
const convertToPopup = document.querySelector(".convert_to_popup");
const convertToGroupElements = document.querySelectorAll(".convert_to_group");
const convertToGroups = {};
const convertToElement = document.querySelector("select[name='convert_to']");
const convertButton = document.querySelector("input[type='submit']");
2024-08-20 14:59:25 -04:00
const showMatching = (search) => {
for (const [targets, groupElement] of Object.values(convertToGroups)) {
let matchingTargetsFound = 0;
for (const target of targets) {
if (target.dataset.target.includes(search)) {
matchingTargetsFound++;
2024-09-09 17:38:03 +02:00
target.classList.remove("hidden");
target.classList.add("flex");
2024-08-20 14:59:25 -04:00
} else {
2024-09-09 17:38:03 +02:00
target.classList.add("hidden");
target.classList.remove("flex");
2024-08-20 14:59:25 -04:00
}
}
if (matchingTargetsFound === 0) {
2024-09-09 17:38:03 +02:00
groupElement.classList.add("hidden");
groupElement.classList.remove("flex");
2024-08-20 14:59:25 -04:00
} else {
2024-09-09 17:38:03 +02:00
groupElement.classList.remove("hidden");
groupElement.classList.add("flex");
2024-08-20 14:59:25 -04:00
}
}
};
for (const groupElement of convertToGroupElements) {
const groupName = groupElement.dataset.converter;
const targetElements = groupElement.querySelectorAll(".target");
const targets = Array.from(targetElements);
for (const target of targets) {
target.onmousedown = () => {
2024-08-20 14:59:25 -04:00
convertToElement.value = target.dataset.value;
convertToInput.value = `${target.dataset.target} using ${target.dataset.converter}`;
convertButton.disabled = false;
2024-08-20 14:59:25 -04:00
showMatching("");
};
}
convertToGroups[groupName] = [targets, groupElement];
}
convertToInput.addEventListener("input", (e) => {
showMatching(e.target.value.toLowerCase());
});
convertToInput.addEventListener("search", () => {
// when the user clears the search bar using the 'x' button
convertButton.disabled = true;
});
2024-08-20 14:59:25 -04:00
convertToInput.addEventListener("blur", (e) => {
// Keep the popup open even when clicking on a target button
// for a split second to allow the click to go through
if (e?.relatedTarget?.classList?.contains("target")) {
2024-09-09 17:38:03 +02:00
convertToPopup.classList.add("hidden");
convertToPopup.classList.remove("flex");
2024-08-20 14:59:25 -04:00
return;
}
2024-09-09 17:38:03 +02:00
convertToPopup.classList.add("hidden");
convertToPopup.classList.remove("flex");
2024-08-20 14:59:25 -04:00
});
convertToInput.addEventListener("focus", () => {
2024-09-09 17:38:03 +02:00
convertToPopup.classList.remove("hidden");
convertToPopup.classList.add("flex");
2024-08-20 14:59:25 -04:00
});
};
2024-05-17 19:23:05 +02:00
2024-05-25 02:33:24 +02:00
// const convertFromSelect = document.querySelector("select[name='convert_from']");
2024-05-24 22:58:25 +02:00
2024-04-10 14:42:05 +02:00
// Add a 'change' event listener to the file input element
fileInput.addEventListener("change", (e) => {
2024-05-25 02:33:24 +02:00
// console.log(e.target.files);
2024-05-19 23:51:27 +02:00
// Get the selected files from the event target
const files = e.target.files;
// Select the file-list table
const fileList = document.querySelector("#file-list");
// Loop through the selected files
for (const file of files) {
// Create a new table row for each file
const row = document.createElement("tr");
row.innerHTML = `
2024-05-19 00:07:56 +02:00
<td>${file.name}</td>
2024-05-24 22:58:25 +02:00
<td>${(file.size / 1024).toFixed(2)} kB</td>
2024-10-07 10:47:37 +02:00
<td><a onclick="deleteRow(this)">Remove</a></td>
2024-04-10 14:42:05 +02:00
`;
2024-05-19 23:51:27 +02:00
if (!fileType) {
fileType = file.name.split(".").pop();
fileInput.setAttribute("accept", `.${fileType}`);
2024-05-20 00:56:40 +02:00
setTitle();
2024-05-19 23:51:27 +02:00
2024-05-24 22:58:25 +02:00
// choose the option that matches the file type
2024-05-25 02:33:24 +02:00
// for (const option of convertFromSelect.children) {
// console.log(option.value);
// if (option.value === fileType) {
// option.selected = true;
// }
// }
2024-05-24 22:58:25 +02:00
2024-05-19 23:51:27 +02:00
fetch("/conversions", {
method: "POST",
body: JSON.stringify({ fileType: fileType }),
headers: {
"Content-Type": "application/json",
},
})
2024-05-25 18:09:28 +02:00
.then((res) => res.text())
2024-05-19 23:51:27 +02:00
.then((html) => {
2024-05-25 18:09:28 +02:00
selectContainer.innerHTML = html;
2024-08-20 14:59:25 -04:00
updateSearchBar();
2024-05-19 23:51:27 +02:00
})
.catch((err) => console.log(err));
}
// Append the row to the file-list table
fileList.appendChild(row);
// Append the file to the hidden input
fileNames.push(file.name);
}
uploadFiles(files);
2024-04-10 14:42:05 +02:00
});
2024-05-17 19:23:05 +02:00
2024-05-20 00:56:40 +02:00
const setTitle = () => {
const title = document.querySelector("h1");
2024-05-24 22:58:25 +02:00
title.textContent = `Convert ${fileType ? `.${fileType}` : ""}`;
2024-05-20 00:56:40 +02:00
};
2024-05-18 00:16:08 +02:00
// Add a onclick for the delete button
2024-10-05 00:43:24 +02:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2024-05-18 00:16:08 +02:00
const deleteRow = (target) => {
2024-05-19 23:51:27 +02:00
const filename = target.parentElement.parentElement.children[0].textContent;
const row = target.parentElement.parentElement;
row.remove();
// remove from fileNames
const index = fileNames.indexOf(filename);
fileNames.splice(index, 1);
2024-05-20 00:56:40 +02:00
// if fileNames is empty, reset fileType
if (fileNames.length === 0) {
fileType = null;
fileInput.removeAttribute("accept");
setTitle();
}
2024-05-19 23:51:27 +02:00
fetch("/delete", {
method: "POST",
body: JSON.stringify({ filename: filename }),
headers: {
"Content-Type": "application/json",
},
})
.then((res) => res.json())
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
2024-05-18 00:16:08 +02:00
};
2024-05-17 19:23:05 +02:00
const uploadFiles = (files) => {
2024-05-19 23:51:27 +02:00
const formData = new FormData();
for (const file of files) {
formData.append("file", file, file.name);
}
fetch("/upload", {
method: "POST",
body: formData,
})
.then((res) => res.json())
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
2024-05-17 19:23:05 +02:00
};
2024-05-19 00:07:56 +02:00
const formConvert = document.querySelector("form[action='/convert']");
2024-10-05 00:43:24 +02:00
formConvert.addEventListener("submit", () => {
2024-05-19 23:51:27 +02:00
const hiddenInput = document.querySelector("input[name='file_names']");
hiddenInput.value = JSON.stringify(fileNames);
});
2024-08-20 14:59:25 -04:00
updateSearchBar();