documentation for all JS files
This commit is contained in:
@@ -1,4 +1,24 @@
|
||||
/**
|
||||
* @fileoverview Manages file upload functionality for the room interface.
|
||||
* This file handles:
|
||||
* - File upload initialization and configuration
|
||||
* - Drag and drop file handling
|
||||
* - Upload progress tracking
|
||||
* - File type validation
|
||||
* - Overwrite handling
|
||||
* - Batch upload management
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class UploadManager
|
||||
* @classdesc Manages file upload operations including drag-and-drop, progress tracking,
|
||||
* and handling of file conflicts and validations.
|
||||
*/
|
||||
export class UploadManager {
|
||||
/**
|
||||
* Creates a new UploadManager instance.
|
||||
* @param {RoomManager} roomManager - The parent RoomManager instance
|
||||
*/
|
||||
constructor(roomManager) {
|
||||
this.roomManager = roomManager;
|
||||
this.pendingUploads = [];
|
||||
@@ -19,6 +39,10 @@ export class UploadManager {
|
||||
this.initializeUploadHandlers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes event handlers for file upload functionality.
|
||||
* Sets up drag and drop handlers and file input change handlers.
|
||||
*/
|
||||
initializeUploadHandlers() {
|
||||
if (!this.roomManager.canUpload) return;
|
||||
|
||||
@@ -44,19 +68,33 @@ export class UploadManager {
|
||||
this.fileInput.addEventListener('change', this.handleFileSelect.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents default browser behavior for drag and drop events.
|
||||
* @param {Event} e - The event object
|
||||
*/
|
||||
preventDefaults(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlights the drop zone when files are dragged over it.
|
||||
*/
|
||||
highlight() {
|
||||
this.dropZoneOverlay.style.display = 'block';
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes highlight from the drop zone.
|
||||
*/
|
||||
unhighlight() {
|
||||
this.dropZoneOverlay.style.display = 'none';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles files dropped onto the drop zone.
|
||||
* @param {DragEvent} e - The drop event object
|
||||
*/
|
||||
async handleDrop(e) {
|
||||
const dt = e.dataTransfer;
|
||||
const files = dt.files;
|
||||
@@ -66,11 +104,18 @@ export class UploadManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles files selected through the file input.
|
||||
*/
|
||||
async handleFileSelect() {
|
||||
if (!this.fileInput.files.length) return;
|
||||
await this.startUpload(Array.from(this.fileInput.files));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates the upload process for a set of files.
|
||||
* @param {Array<File>} files - Array of files to upload
|
||||
*/
|
||||
async startUpload(files) {
|
||||
this.uploadProgressContainer.style.display = 'block';
|
||||
this.uploadProgressBar.style.width = '0%';
|
||||
@@ -85,6 +130,10 @@ export class UploadManager {
|
||||
await this.uploadFilesSequentially();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads files one at a time, handling progress and errors.
|
||||
* @async
|
||||
*/
|
||||
async uploadFilesSequentially() {
|
||||
let completedFiles = 0;
|
||||
let currentFileIndex = 0;
|
||||
@@ -182,6 +231,11 @@ export class UploadManager {
|
||||
await processNextFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads a single file to the server.
|
||||
* @param {FormData} formData - Form data containing the file and upload parameters
|
||||
* @returns {Promise<Object>} Response object containing success status and error message if any
|
||||
*/
|
||||
async uploadFile(formData) {
|
||||
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
||||
const response = await fetch(`/api/rooms/${this.roomManager.roomId}/files/upload`, {
|
||||
@@ -197,6 +251,11 @@ export class UploadManager {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles file type validation errors.
|
||||
* Displays error message with allowed file types.
|
||||
* @param {File} file - The file that failed validation
|
||||
*/
|
||||
handleFileTypeError(file) {
|
||||
const allowedTypes = [
|
||||
'Documents: PDF, DOCX, DOC, TXT, RTF, ODT, MD, CSV',
|
||||
@@ -225,6 +284,12 @@ export class UploadManager {
|
||||
this.uploadProgressBar.className = 'progress-bar bg-danger-opacity-15 text-danger';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles file existence conflicts during upload.
|
||||
* @param {File} file - The file being uploaded
|
||||
* @param {FormData} formData - Form data for the upload
|
||||
* @returns {Promise<Object>} Object containing continue status
|
||||
*/
|
||||
async handleFileExists(file, formData) {
|
||||
if (this.overwriteAll) {
|
||||
formData.append('overwrite', 'true');
|
||||
|
||||
Reference in New Issue
Block a user