Files
ConvertX/src/public/script.js
T

129 lines
3.4 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-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-05-19 23:51:27 +02:00
const selectElem = document.querySelector("select[name='convert_to']");
2024-05-17 19:23:05 +02:00
2024-05-24 22:58:25 +02:00
const convertFromSelect = document.querySelector("select[name='convert_from']");
2024-04-10 14:42:05 +02:00
// Add a 'change' event listener to the file input element
fileInput.addEventListener("change", (e) => {
2024-05-19 23:51:27 +02:00
console.log(e.target.files);
// 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>
<td><a class="secondary" onclick="deleteRow(this)" style="cursor: pointer">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();
console.log(file.type);
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
for (const option of convertFromSelect.children) {
console.log(option.value);
if (option.value === fileType) {
option.selected = true;
}
}
2024-05-19 23:51:27 +02:00
fetch("/conversions", {
method: "POST",
body: JSON.stringify({ fileType: fileType }),
headers: {
"Content-Type": "application/json",
},
})
.then((res) => res.text()) // Convert the response to text
.then((html) => {
console.log(html);
selectElem.outerHTML = html; // Set the HTML
})
.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
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']");
formConvert.addEventListener("submit", (e) => {
2024-05-19 23:51:27 +02:00
const hiddenInput = document.querySelector("input[name='file_names']");
hiddenInput.value = JSON.stringify(fileNames);
});