Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui/project_form: Improve file handling in UI #1036

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions scancodeio/static/add-inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,42 @@
// Visit https://github.com/nexB/scancode.io for support and download.

const fileInput = document.querySelector('#id_input_files');
let selectedFiles = []; // Store selected files

fileInput.onchange = updateFiles;

// Update the list of files to be uploaded in the UI
function updateFiles() {
if (fileInput.files.length > 0) {
const fileName = document.querySelector('#inputs_file_name');
fileName.innerHTML = "";
for (let file of fileInput.files) {
fileName.innerHTML += `<span class="is-block">${file.name}</span>`;

// Update the selectedFiles array
const newFiles = Array.from(fileInput.files);
// Create a Set to track unique file names
const uniqueFileNames = new Set(selectedFiles.map(file => file.name));
// Filter out files with the same name
const filteredNewFiles = newFiles.filter(file => !uniqueFileNames.has(file.name));
// Concatenate the unique files to the existing selectedFiles array
selectedFiles = selectedFiles.concat(filteredNewFiles);

for (let file of selectedFiles) {
const fileNameWithoutSpaces = file.name.replace(/\s/g, '');
fileName.innerHTML += `
<span class="is-flex is-justify-content-space-between is-block" id="file-name-${fileNameWithoutSpaces}">
<span class="is-block">${file.name}</span>
<a href="#" onclick="removeFile('${fileNameWithoutSpaces}')" class="model-button" id="file-delete-btn-${fileNameWithoutSpaces}">
<i class="fa-solid fa-trash-can mr-2"></i>
</a>
</span>
`;
document.getElementById("file-delete-btn-"+ fileNameWithoutSpaces).addEventListener("click", function(event){
disableEvent(event);
removeFile(fileNameWithoutSpaces);
if(selectedFiles.length == 0){
fileName.innerHTML ="<i>No files selected</i>"
}
});
}
}
}
Expand All @@ -40,15 +67,36 @@ function disableEvent(event) {
event.preventDefault();
}

function removeFile(fileName) {
selectedFiles = selectedFiles.filter(file => {
const fileNameWithoutSpaces = file.name.replace(/\s/g, '');
return fileNameWithoutSpaces !== fileName;
});

const fileNameElement = document.getElementById(`file-name-${fileName}`);
if (fileNameElement) {
fileNameElement.remove();
}

const dataTransfer = new DataTransfer();
for (let file of selectedFiles) {
dataTransfer.items.add(file);
}

fileInput.files = dataTransfer.files;
}

function dropHandler(event) {
disableEvent(event);
const droppedFiles = event.dataTransfer.files;
const updatedFiles = Array.from(fileInput.files);

const updatedFilesSet = new Set(Array.from(fileInput.files));
for (let file of droppedFiles) {
updatedFiles.push(file);
updatedFilesSet.add(file);
}

// Convert the Set back to an array if needed
const updatedFiles = Array.from(updatedFilesSet);
const dataTransfer = new DataTransfer();
for (let file of updatedFiles) {
dataTransfer.items.add(file);
Expand Down
Loading