2023-08-17 22:03:36 +01:00
|
|
|
let currentSort = {
|
2024-02-16 22:49:06 +01:00
|
|
|
field: null,
|
|
|
|
descending: false,
|
2023-08-17 22:03:36 +01:00
|
|
|
};
|
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
document.getElementById("fileInput-input").addEventListener("change", function () {
|
2024-05-23 19:52:49 +01:00
|
|
|
var files = this.files;
|
2024-02-16 22:49:06 +01:00
|
|
|
displayFiles(files);
|
2023-08-17 22:03:36 +01:00
|
|
|
});
|
|
|
|
|
2024-05-23 19:52:49 +01:00
|
|
|
/**
|
|
|
|
* @param {FileList} files
|
|
|
|
*/
|
2024-10-05 02:44:15 +05:30
|
|
|
async function displayFiles(files) {
|
2024-02-16 22:49:06 +01:00
|
|
|
const list = document.getElementById("selectedFiles");
|
2023-08-17 22:03:36 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
while (list.firstChild) {
|
|
|
|
list.removeChild(list.firstChild);
|
|
|
|
}
|
2024-01-23 16:06:57 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
for (let i = 0; i < files.length; i++) {
|
2024-10-05 02:44:15 +05:30
|
|
|
const pageCount = await getPDFPageCount(files[i]);
|
|
|
|
const pageLabel = pageCount === 1 ? pageTranslation : pagesTranslation;
|
2024-11-15 13:21:23 -07:00
|
|
|
|
2024-11-06 10:44:24 +13:00
|
|
|
// Create list item
|
2024-02-16 22:49:06 +01:00
|
|
|
const item = document.createElement("li");
|
|
|
|
item.className = "list-group-item";
|
2024-11-06 10:44:24 +13:00
|
|
|
|
|
|
|
// Create filename div and set textContent to sanitize
|
|
|
|
const fileNameDiv = document.createElement("div");
|
|
|
|
fileNameDiv.className = "filename";
|
|
|
|
fileNameDiv.textContent = files[i].name;
|
|
|
|
|
|
|
|
// Create page info div and set textContent to sanitize
|
|
|
|
const pageInfoDiv = document.createElement("div");
|
|
|
|
pageInfoDiv.className = "page-info";
|
|
|
|
const pageCountSpan = document.createElement("span");
|
|
|
|
pageCountSpan.className = "page-count";
|
|
|
|
pageCountSpan.textContent = `${pageCount} ${pageLabel}`;
|
|
|
|
pageInfoDiv.appendChild(pageCountSpan);
|
|
|
|
|
|
|
|
// Create arrows div with buttons
|
|
|
|
const arrowsDiv = document.createElement("div");
|
|
|
|
arrowsDiv.className = "arrows d-flex";
|
|
|
|
|
|
|
|
const moveUpButton = document.createElement("button");
|
|
|
|
moveUpButton.className = "btn btn-secondary move-up";
|
|
|
|
moveUpButton.innerHTML = "<span>↑</span>";
|
|
|
|
|
|
|
|
const moveDownButton = document.createElement("button");
|
|
|
|
moveDownButton.className = "btn btn-secondary move-down";
|
|
|
|
moveDownButton.innerHTML = "<span>↓</span>";
|
|
|
|
|
|
|
|
const removeButton = document.createElement("button");
|
|
|
|
removeButton.className = "btn btn-danger remove-file";
|
|
|
|
removeButton.innerHTML = "<span>×</span>";
|
|
|
|
|
|
|
|
arrowsDiv.append(moveUpButton, moveDownButton, removeButton);
|
|
|
|
|
|
|
|
// Append elements to item and then to list
|
|
|
|
const itemContainer = document.createElement("div");
|
|
|
|
itemContainer.className = "d-flex justify-content-between align-items-center w-100";
|
|
|
|
itemContainer.append(fileNameDiv, pageInfoDiv, arrowsDiv);
|
|
|
|
|
|
|
|
item.appendChild(itemContainer);
|
2024-02-16 22:49:06 +01:00
|
|
|
list.appendChild(item);
|
|
|
|
}
|
2024-05-23 19:52:49 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
attachMoveButtons();
|
2023-08-17 22:03:36 +01:00
|
|
|
}
|
|
|
|
|
2024-11-06 10:44:24 +13:00
|
|
|
|
2024-10-05 02:44:15 +05:30
|
|
|
async function getPDFPageCount(file) {
|
|
|
|
const blobUrl = URL.createObjectURL(file);
|
|
|
|
const pdf = await pdfjsLib.getDocument(blobUrl).promise;
|
|
|
|
URL.revokeObjectURL(blobUrl);
|
|
|
|
return pdf.numPages;
|
|
|
|
}
|
|
|
|
|
2023-08-17 22:03:36 +01:00
|
|
|
function attachMoveButtons() {
|
2024-02-16 22:49:06 +01:00
|
|
|
var moveUpButtons = document.querySelectorAll(".move-up");
|
|
|
|
for (var i = 0; i < moveUpButtons.length; i++) {
|
|
|
|
moveUpButtons[i].addEventListener("click", function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var parent = this.closest(".list-group-item");
|
|
|
|
var grandParent = parent.parentNode;
|
|
|
|
if (parent.previousElementSibling) {
|
|
|
|
grandParent.insertBefore(parent, parent.previousElementSibling);
|
|
|
|
updateFiles();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-08-17 22:03:36 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
var moveDownButtons = document.querySelectorAll(".move-down");
|
|
|
|
for (var i = 0; i < moveDownButtons.length; i++) {
|
|
|
|
moveDownButtons[i].addEventListener("click", function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var parent = this.closest(".list-group-item");
|
|
|
|
var grandParent = parent.parentNode;
|
|
|
|
if (parent.nextElementSibling) {
|
|
|
|
grandParent.insertBefore(parent.nextElementSibling, parent);
|
|
|
|
updateFiles();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-12-30 16:42:41 +05:30
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
var removeButtons = document.querySelectorAll(".remove-file");
|
|
|
|
for (var i = 0; i < removeButtons.length; i++) {
|
|
|
|
removeButtons[i].addEventListener("click", function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var parent = this.closest(".list-group-item");
|
2024-05-23 19:52:49 +01:00
|
|
|
//Get name of removed file
|
|
|
|
var fileName = parent.querySelector(".filename").innerText;
|
2024-02-16 22:49:06 +01:00
|
|
|
parent.remove();
|
|
|
|
updateFiles();
|
2024-05-23 19:52:49 +01:00
|
|
|
//Dispatch a custom event with the name of the removed file
|
|
|
|
var event = new CustomEvent("fileRemoved", { detail: fileName });
|
|
|
|
document.dispatchEvent(event);
|
2024-02-16 22:49:06 +01:00
|
|
|
});
|
|
|
|
}
|
2023-08-17 22:03:36 +01:00
|
|
|
}
|
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
document.getElementById("sortByNameBtn").addEventListener("click", function () {
|
|
|
|
if (currentSort.field === "name" && !currentSort.descending) {
|
|
|
|
currentSort.descending = true;
|
|
|
|
sortFiles((a, b) => b.name.localeCompare(a.name));
|
|
|
|
} else {
|
|
|
|
currentSort.field = "name";
|
|
|
|
currentSort.descending = false;
|
|
|
|
sortFiles((a, b) => a.name.localeCompare(b.name));
|
|
|
|
}
|
2023-08-17 22:03:36 +01:00
|
|
|
});
|
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
document.getElementById("sortByDateBtn").addEventListener("click", function () {
|
|
|
|
if (currentSort.field === "lastModified" && !currentSort.descending) {
|
|
|
|
currentSort.descending = true;
|
|
|
|
sortFiles((a, b) => b.lastModified - a.lastModified);
|
|
|
|
} else {
|
|
|
|
currentSort.field = "lastModified";
|
|
|
|
currentSort.descending = false;
|
|
|
|
sortFiles((a, b) => a.lastModified - b.lastModified);
|
|
|
|
}
|
2023-08-17 22:03:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
function sortFiles(comparator) {
|
2024-05-23 19:52:49 +01:00
|
|
|
// Convert FileList to array and sort
|
|
|
|
const sortedFilesArray = Array.from(document.getElementById("fileInput-input").files).sort(comparator);
|
2023-08-17 22:03:36 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
// Refresh displayed list
|
|
|
|
displayFiles(sortedFilesArray);
|
2023-08-17 22:03:36 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
// Update the files property
|
|
|
|
const dataTransfer = new DataTransfer();
|
2024-05-23 19:52:49 +01:00
|
|
|
sortedFilesArray.forEach((file) => dataTransfer.items.add(file));
|
2024-02-16 22:49:06 +01:00
|
|
|
document.getElementById("fileInput-input").files = dataTransfer.files;
|
2023-08-17 22:03:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateFiles() {
|
2024-02-16 22:49:06 +01:00
|
|
|
var dataTransfer = new DataTransfer();
|
|
|
|
var liElements = document.querySelectorAll("#selectedFiles li");
|
2024-05-23 19:52:49 +01:00
|
|
|
const files = document.getElementById("fileInput-input").files;
|
2023-08-17 22:03:36 +01:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
for (var i = 0; i < liElements.length; i++) {
|
2024-05-23 19:52:49 +01:00
|
|
|
var fileNameFromList = liElements[i].querySelector(".filename").innerText;
|
|
|
|
var fileFromFiles;
|
|
|
|
for (var j = 0; j < files.length; j++) {
|
|
|
|
var file = files[j];
|
|
|
|
if (file.name === fileNameFromList) {
|
|
|
|
dataTransfer.items.add(file);
|
2024-02-16 22:49:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-08-17 22:03:36 +01:00
|
|
|
}
|
2024-02-16 22:49:06 +01:00
|
|
|
}
|
|
|
|
document.getElementById("fileInput-input").files = dataTransfer.files;
|
2023-08-17 22:03:36 +01:00
|
|
|
}
|
2024-11-15 13:21:23 -07:00
|
|
|
|
|
|
|
document.querySelector("#resetFileInputBtn").addEventListener("click", ()=>{
|
|
|
|
let formElement = document.querySelector("#fileInput-input");
|
|
|
|
formElement.value = '';
|
|
|
|
clearLiElements();
|
|
|
|
updateFiles();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
function clearLiElements(){
|
|
|
|
let listGroupItemNodeList = document.querySelectorAll(".list-group-item");
|
|
|
|
for (let i = 0; i < listGroupItemNodeList.length; i++) {
|
|
|
|
listGroupItemNodeList[i].remove();
|
|
|
|
};
|
|
|
|
}
|