refactor(js): avoid use of window.XXX and move to import/export
- change worker: do not cache html pages (not necessary) - remove use of window.XXX and maximize import/export, this will provide more clarety, show circular dependencies + you will benefit IDE help
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -3,6 +3,14 @@
|
||||
* This file handles file and folder operations (create, move, delete, rename, upload)
|
||||
*/
|
||||
|
||||
import { refreshUserData } from '../../app/authSession.js';
|
||||
import { loadFiles } from '../../app/filesView.js';
|
||||
import { app } from '../../app/state.js';
|
||||
import { showConfirmDialog, ui } from '../../app/ui.js';
|
||||
import { getCsrfHeaders, getCsrfToken } from '../../core/csrf.js';
|
||||
import { i18n } from '../../core/i18n.js';
|
||||
import { notifications } from '../../core/notifications.js';
|
||||
|
||||
/**
|
||||
* Get authorization headers for API requests.
|
||||
* Tokens are now in HttpOnly cookies — no explicit Authorization header needed.
|
||||
@@ -22,13 +30,13 @@ const fileOps = {
|
||||
|
||||
/** Start a new upload batch in the notification bell */
|
||||
_initUploadToast(totalFiles, folderName) {
|
||||
this._currentBatchId = window.notifications ? window.notifications.addUploadBatch(totalFiles, folderName) : null;
|
||||
this._currentBatchId = notifications ? notifications.addUploadBatch(totalFiles, folderName) : null;
|
||||
},
|
||||
|
||||
/** Finalise the batch in the notification bell */
|
||||
_finishUploadToast(successCount, totalFiles) {
|
||||
if (window.notifications && this._currentBatchId) {
|
||||
window.notifications.finishBatch(this._currentBatchId, successCount, totalFiles);
|
||||
if (notifications && this._currentBatchId) {
|
||||
notifications.finishBatch(this._currentBatchId, successCount, totalFiles);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -58,7 +66,7 @@ const fileOps = {
|
||||
_uploadFileXHR(formData, batchId, fileName, timeoutMs = 120000) {
|
||||
return new Promise((resolve) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const notif = window.notifications;
|
||||
const notif = notifications;
|
||||
// Do NOT set xhr.timeout — it is a TOTAL deadline from send() to
|
||||
// response and would kill large uploads even while data is flowing.
|
||||
// Instead we rely on the stall timer (no progress for N seconds)
|
||||
@@ -299,13 +307,13 @@ const fileOps = {
|
||||
|
||||
const totalFiles = readableFiles.length;
|
||||
|
||||
if (skippedEntries.length > 0 && window.notifications) {
|
||||
const locale = window.i18n?.getCurrentLocale?.() || 'en';
|
||||
if (skippedEntries.length > 0 && notifications) {
|
||||
const locale = i18n?.getCurrentLocale?.() || 'en';
|
||||
const title = locale.startsWith('es') ? 'Entradas omitidas' : 'Entries skipped';
|
||||
const text = locale.startsWith('es')
|
||||
? `Se omitieron ${skippedEntries.length} carpeta(s)/entrada(s) no legibles. Usa "Subir carpeta".`
|
||||
: `${skippedEntries.length} unreadable folder/entry items were skipped. Use "Upload folder".`;
|
||||
window.notifications.addNotification({
|
||||
notifications.addNotification({
|
||||
icon: 'fa-folder-open',
|
||||
iconClass: 'upload',
|
||||
title,
|
||||
@@ -331,7 +339,7 @@ const fileOps = {
|
||||
|
||||
const formData = new FormData();
|
||||
|
||||
const targetFolderId = window.app.currentPath || window.app.userHomeFolderId;
|
||||
const targetFolderId = app.currentPath || app.userHomeFolderId;
|
||||
if (targetFolderId) formData.append('folder_id', targetFolderId);
|
||||
formData.append('file', file);
|
||||
|
||||
@@ -353,9 +361,9 @@ const fileOps = {
|
||||
progressBar.style.width = `${(uploadedCount / totalFiles) * 100}%`;
|
||||
}
|
||||
// Notify bell of per-file completion
|
||||
if (window.notifications && batchId) {
|
||||
if (notifications && batchId) {
|
||||
try {
|
||||
window.notifications.fileCompleted(batchId, result.ok);
|
||||
notifications.fileCompleted(batchId, result.ok);
|
||||
} catch (e) {
|
||||
console.warn('Batch progress update failed:', e);
|
||||
}
|
||||
@@ -366,8 +374,8 @@ const fileOps = {
|
||||
console.log(`Successfully uploaded ${file.name}`, result.data);
|
||||
} else {
|
||||
console.error(`Upload error for ${file.name}`);
|
||||
if (result.isTimeout && window.notifications) {
|
||||
window.notifications.addNotification({
|
||||
if (result.isTimeout && notifications) {
|
||||
notifications.addNotification({
|
||||
icon: 'fa-clock',
|
||||
iconClass: 'error',
|
||||
title: file.name,
|
||||
@@ -375,9 +383,9 @@ const fileOps = {
|
||||
});
|
||||
}
|
||||
if (result.isQuotaError) {
|
||||
const msg = result.errorMsg || window.i18n?.t('storage_quota_exceeded') || 'Storage quota exceeded';
|
||||
if (window.notifications) {
|
||||
window.notifications.addNotification({
|
||||
const msg = result.errorMsg || i18n?.t('storage_quota_exceeded') || 'Storage quota exceeded';
|
||||
if (notifications) {
|
||||
notifications.addNotification({
|
||||
icon: 'fa-exclamation-triangle',
|
||||
iconClass: 'error',
|
||||
title: file.name,
|
||||
@@ -393,14 +401,12 @@ const fileOps = {
|
||||
this._finishUploadToast(successCount, totalFiles);
|
||||
|
||||
// Refresh storage usage display
|
||||
if (typeof window.refreshUserData === 'function') {
|
||||
try {
|
||||
await window.refreshUserData();
|
||||
} catch (_) {}
|
||||
}
|
||||
try {
|
||||
await refreshUserData();
|
||||
} catch (_) {}
|
||||
|
||||
try {
|
||||
await window.loadFiles({ forceRefresh: true });
|
||||
await loadFiles({ forceRefresh: true });
|
||||
} catch (reloadError) {
|
||||
console.error('Error reloading files:', reloadError);
|
||||
}
|
||||
@@ -466,7 +472,7 @@ const fileOps = {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentFolderId = window.app.currentPath || window.app.userHomeFolderId;
|
||||
const currentFolderId = app.currentPath || app.userHomeFolderId;
|
||||
|
||||
// Build folder structure from relative paths
|
||||
const folderMap = new Map();
|
||||
@@ -528,7 +534,7 @@ const fileOps = {
|
||||
.filter(Boolean)
|
||||
)
|
||||
];
|
||||
const locale = window.i18n?.getCurrentLocale?.() || 'en';
|
||||
const locale = i18n?.getCurrentLocale?.() || 'en';
|
||||
const rootFolderLabel =
|
||||
rootFolderNames.length <= 1
|
||||
? rootFolderNames[0] || ''
|
||||
@@ -585,9 +591,9 @@ const fileOps = {
|
||||
console.warn(`[SKIP] #${idx} ${rel} — cannot read 0-byte file (FIFO/pipe?), skipping`);
|
||||
uploadedCount++;
|
||||
successCount++;
|
||||
if (window.notifications && batchId) {
|
||||
if (notifications && batchId) {
|
||||
try {
|
||||
window.notifications.fileCompleted(batchId, true);
|
||||
notifications.fileCompleted(batchId, true);
|
||||
} catch (_) {}
|
||||
}
|
||||
return;
|
||||
@@ -617,9 +623,9 @@ const fileOps = {
|
||||
|
||||
uploadedCount++;
|
||||
|
||||
if (window.notifications && batchId) {
|
||||
if (notifications && batchId) {
|
||||
try {
|
||||
window.notifications.fileCompleted(batchId, result.ok);
|
||||
notifications.fileCompleted(batchId, result.ok);
|
||||
} catch (_) {}
|
||||
}
|
||||
if (progressBar && uploadedCount % 10 === 0) {
|
||||
@@ -633,8 +639,8 @@ const fileOps = {
|
||||
successCount++;
|
||||
} else if (result.isQuotaError) {
|
||||
quotaStop = true;
|
||||
if (window.notifications) {
|
||||
window.notifications.addNotification({
|
||||
if (notifications) {
|
||||
notifications.addNotification({
|
||||
icon: 'fa-exclamation-triangle',
|
||||
iconClass: 'error',
|
||||
title: file.name,
|
||||
@@ -661,14 +667,12 @@ const fileOps = {
|
||||
|
||||
this._finishUploadToast(successCount, totalFiles);
|
||||
|
||||
if (typeof window.refreshUserData === 'function') {
|
||||
try {
|
||||
await window.refreshUserData();
|
||||
} catch (_) {}
|
||||
}
|
||||
try {
|
||||
await refreshUserData();
|
||||
} catch (_) {}
|
||||
|
||||
try {
|
||||
await window.loadFiles({ forceRefresh: true });
|
||||
await loadFiles({ forceRefresh: true });
|
||||
} catch (reloadError) {
|
||||
console.error('Error reloading files:', reloadError);
|
||||
}
|
||||
@@ -699,7 +703,7 @@ const fileOps = {
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: name,
|
||||
parent_id: window.app.currentPath || window.app.userHomeFolderId || null
|
||||
parent_id: app.currentPath || app.userHomeFolderId || null
|
||||
})
|
||||
});
|
||||
|
||||
@@ -710,17 +714,17 @@ const fileOps = {
|
||||
|
||||
// Optimistic UI: add folder card directly from server response
|
||||
// — no reload needed since the backend already confirmed creation.
|
||||
window.ui.addFolderToView(folder);
|
||||
ui.addFolderToView(folder);
|
||||
|
||||
window.ui.showNotification('Folder created', `"${name}" created successfully`);
|
||||
ui.showNotification('Folder created', `"${name}" created successfully`);
|
||||
} else {
|
||||
const errorData = await response.text();
|
||||
console.error('Create folder error:', errorData);
|
||||
window.ui.showNotification('Error', 'Error creating the folder');
|
||||
ui.showNotification('Error', 'Error creating the folder');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error creating folder:', error);
|
||||
window.ui.showNotification('Error', 'Error creating the folder');
|
||||
ui.showNotification('Error', 'Error creating the folder');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -745,8 +749,8 @@ const fileOps = {
|
||||
|
||||
if (response.ok) {
|
||||
// Reload files after moving
|
||||
await window.loadFiles();
|
||||
window.ui.showNotification('File moved', 'File moved successfully');
|
||||
await loadFiles();
|
||||
ui.showNotification('File moved', 'File moved successfully');
|
||||
return true;
|
||||
} else {
|
||||
let errorMessage = 'Unknown error';
|
||||
@@ -756,12 +760,12 @@ const fileOps = {
|
||||
} catch (_e) {
|
||||
errorMessage = 'Error processing server response';
|
||||
}
|
||||
window.ui.showNotification('Error', `Error moving the file: ${errorMessage}`);
|
||||
ui.showNotification('Error', `Error moving the file: ${errorMessage}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error moving file:', error);
|
||||
window.ui.showNotification('Error', 'Error moving the file');
|
||||
ui.showNotification('Error', 'Error moving the file');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -787,8 +791,8 @@ const fileOps = {
|
||||
|
||||
if (response.ok) {
|
||||
// Reload files after moving
|
||||
await window.loadFiles();
|
||||
window.ui.showNotification('Folder moved', 'Folder moved successfully');
|
||||
await loadFiles();
|
||||
ui.showNotification('Folder moved', 'Folder moved successfully');
|
||||
return true;
|
||||
} else {
|
||||
let errorMessage = 'Unknown error';
|
||||
@@ -798,12 +802,12 @@ const fileOps = {
|
||||
} catch (_e) {
|
||||
errorMessage = 'Error processing server response';
|
||||
}
|
||||
window.ui.showNotification('Error', `Error moving the folder: ${errorMessage}`);
|
||||
ui.showNotification('Error', `Error moving the folder: ${errorMessage}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error moving folder:', error);
|
||||
window.ui.showNotification('Error', 'Error moving the folder');
|
||||
ui.showNotification('Error', 'Error moving the folder');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -890,8 +894,8 @@ const fileOps = {
|
||||
if (response.ok) {
|
||||
await response.json();
|
||||
// Reload files after copying
|
||||
await window.loadFiles();
|
||||
window.ui.showNotification('File copied', 'File copied successfully');
|
||||
await loadFiles();
|
||||
ui.showNotification('File copied', 'File copied successfully');
|
||||
return true;
|
||||
} else {
|
||||
let errorMessage = 'Unknown error';
|
||||
@@ -901,12 +905,12 @@ const fileOps = {
|
||||
} catch (_e) {
|
||||
errorMessage = 'Error processing server response';
|
||||
}
|
||||
window.ui.showNotification('Error', `Error copying the file: ${errorMessage}`);
|
||||
ui.showNotification('Error', `Error copying the file: ${errorMessage}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error copying file:', error);
|
||||
window.ui.showNotification('Error', 'Error copying the file');
|
||||
ui.showNotification('Error', 'Error copying the file');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -920,7 +924,7 @@ const fileOps = {
|
||||
*/
|
||||
async copyFolder(_folderId, _targetFolderId) {
|
||||
// Folder copy is not yet implemented in the backend
|
||||
window.ui.showNotification('Not implemented', 'Folder copy is not yet supported');
|
||||
ui.showNotification('Not implemented', 'Folder copy is not yet supported');
|
||||
return false;
|
||||
},
|
||||
|
||||
@@ -954,7 +958,7 @@ const fileOps = {
|
||||
|
||||
// Note: Folder copy is not yet implemented in batch API
|
||||
if (folderIds.length > 0) {
|
||||
window.ui.showNotification('Info', 'Folder copy is not yet supported in batch mode');
|
||||
ui.showNotification('Info', 'Folder copy is not yet supported in batch mode');
|
||||
errors += folderIds.lenngth;
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -990,9 +994,9 @@ const fileOps = {
|
||||
console.log('Response status:', response.status);
|
||||
|
||||
if (response.ok) {
|
||||
window.ui.showNotification(
|
||||
window.i18n ? window.i18n.t('notifications.file_renamed') : 'File renamed',
|
||||
window.i18n ? window.i18n.t('notifications.file_renamed_to', { name: newName }) : `File renamed to "${newName}"`
|
||||
ui.showNotification(
|
||||
i18n ? i18n.t('notifications.file_renamed') : 'File renamed',
|
||||
i18n ? i18n.t('notifications.file_renamed_to', { name: newName }) : `File renamed to "${newName}"`
|
||||
);
|
||||
return true;
|
||||
} else {
|
||||
@@ -1005,12 +1009,12 @@ const fileOps = {
|
||||
} catch (_e) {
|
||||
errorMessage = errorText || response.statusText;
|
||||
}
|
||||
window.ui.showNotification('Error', `Error renaming the file: ${errorMessage}`);
|
||||
ui.showNotification('Error', `Error renaming the file: ${errorMessage}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error renaming file:', error);
|
||||
window.ui.showNotification('Error', 'Error renaming the file');
|
||||
ui.showNotification('Error', 'Error renaming the file');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -1037,7 +1041,7 @@ const fileOps = {
|
||||
console.log('Response status:', response.status);
|
||||
|
||||
if (response.ok) {
|
||||
window.ui.showNotification('Folder renamed', `Folder renamed to "${newName}"`);
|
||||
ui.showNotification('Folder renamed', `Folder renamed to "${newName}"`);
|
||||
return true;
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
@@ -1053,12 +1057,12 @@ const fileOps = {
|
||||
errorMessage = errorText || response.statusText;
|
||||
}
|
||||
|
||||
window.ui.showNotification('Error', `Error renaming the folder: ${errorMessage}`);
|
||||
ui.showNotification('Error', `Error renaming the folder: ${errorMessage}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error renaming folder:', error);
|
||||
window.ui.showNotification('Error', 'Error renaming the folder');
|
||||
ui.showNotification('Error', 'Error renaming the folder');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -1071,11 +1075,9 @@ const fileOps = {
|
||||
*/
|
||||
async deleteFile(fileId, fileName) {
|
||||
const confirmed = await showConfirmDialog({
|
||||
title: window.i18n ? window.i18n.t('dialogs.confirm_delete') : 'Move to trash',
|
||||
message: window.i18n
|
||||
? window.i18n.t('dialogs.confirm_delete_file', { name: fileName })
|
||||
: `Are you sure you want to move the file "${fileName}" to trash?`,
|
||||
confirmText: window.i18n ? window.i18n.t('actions.delete') : 'Delete'
|
||||
title: i18n ? i18n.t('dialogs.confirm_delete') : 'Move to trash',
|
||||
message: i18n ? i18n.t('dialogs.confirm_delete_file', { name: fileName }) : `Are you sure you want to move the file "${fileName}" to trash?`,
|
||||
confirmText: i18n ? i18n.t('actions.delete') : 'Delete'
|
||||
});
|
||||
if (!confirmed) return false;
|
||||
|
||||
@@ -1087,8 +1089,8 @@ const fileOps = {
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
window.loadFiles();
|
||||
window.ui.showNotification('File moved to trash', `"${fileName}" moved to trash`);
|
||||
loadFiles();
|
||||
ui.showNotification('File moved to trash', `"${fileName}" moved to trash`);
|
||||
return true;
|
||||
} else {
|
||||
// Fallback to direct deletion if trash fails
|
||||
@@ -1098,17 +1100,17 @@ const fileOps = {
|
||||
});
|
||||
|
||||
if (fallbackResponse.ok) {
|
||||
window.loadFiles();
|
||||
window.ui.showNotification('File deleted', `"${fileName}" deleted successfully`);
|
||||
loadFiles();
|
||||
ui.showNotification('File deleted', `"${fileName}" deleted successfully`);
|
||||
return true;
|
||||
} else {
|
||||
window.ui.showNotification('Error', 'Error deleting the file');
|
||||
ui.showNotification('Error', 'Error deleting the file');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting file:', error);
|
||||
window.ui.showNotification('Error', 'Error deleting the file');
|
||||
ui.showNotification('Error', 'Error deleting the file');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -1121,11 +1123,11 @@ const fileOps = {
|
||||
*/
|
||||
async deleteFolder(folderId, folderName) {
|
||||
const confirmed = await showConfirmDialog({
|
||||
title: window.i18n ? window.i18n.t('dialogs.confirm_delete') : 'Move to trash',
|
||||
message: window.i18n
|
||||
? window.i18n.t('dialogs.confirm_delete_folder', { name: folderName })
|
||||
title: i18n ? i18n.t('dialogs.confirm_delete') : 'Move to trash',
|
||||
message: i18n
|
||||
? i18n.t('dialogs.confirm_delete_folder', { name: folderName })
|
||||
: `Are you sure you want to move the folder "${folderName}" and all its contents to trash?`,
|
||||
confirmText: window.i18n ? window.i18n.t('actions.delete') : 'Delete'
|
||||
confirmText: i18n ? i18n.t('actions.delete') : 'Delete'
|
||||
});
|
||||
if (!confirmed) return false;
|
||||
|
||||
@@ -1138,12 +1140,12 @@ const fileOps = {
|
||||
|
||||
if (response.ok) {
|
||||
// If we're inside the folder we just deleted, go back up
|
||||
if (window.app.currentPath === folderId) {
|
||||
window.app.currentPath = '';
|
||||
window.ui.updateBreadcrumb('');
|
||||
if (app.currentPath === folderId) {
|
||||
app.currentPath = '';
|
||||
ui.updateBreadcrumb('');
|
||||
}
|
||||
window.loadFiles();
|
||||
window.ui.showNotification('Folder moved to trash', `"${folderName}" moved to trash`);
|
||||
loadFiles();
|
||||
ui.showNotification('Folder moved to trash', `"${folderName}" moved to trash`);
|
||||
return true;
|
||||
} else {
|
||||
// Fallback to direct deletion if trash fails
|
||||
@@ -1154,21 +1156,21 @@ const fileOps = {
|
||||
|
||||
if (fallbackResponse.ok) {
|
||||
// If we're inside the folder we just deleted, go back up
|
||||
if (window.app.currentPath === folderId) {
|
||||
window.app.currentPath = '';
|
||||
window.ui.updateBreadcrumb('');
|
||||
if (app.currentPath === folderId) {
|
||||
app.currentPath = '';
|
||||
ui.updateBreadcrumb('');
|
||||
}
|
||||
window.loadFiles();
|
||||
window.ui.showNotification('Folder deleted', `"${folderName}" deleted successfully`);
|
||||
loadFiles();
|
||||
ui.showNotification('Folder deleted', `"${folderName}" deleted successfully`);
|
||||
return true;
|
||||
} else {
|
||||
window.ui.showNotification('Error', 'Error deleting the folder');
|
||||
ui.showNotification('Error', 'Error deleting the folder');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting folder:', error);
|
||||
window.ui.showNotification('Error', 'Error deleting the folder');
|
||||
ui.showNotification('Error', 'Error deleting the folder');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -1212,15 +1214,15 @@ const fileOps = {
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
window.ui.showNotification('Item restored', 'Item restored successfully');
|
||||
ui.showNotification('Item restored', 'Item restored successfully');
|
||||
return true;
|
||||
} else {
|
||||
window.ui.showNotification('Error', 'Error restoring the item');
|
||||
ui.showNotification('Error', 'Error restoring the item');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error restoring item from trash:', error);
|
||||
window.ui.showNotification('Error', 'Error restoring the item');
|
||||
ui.showNotification('Error', 'Error restoring the item');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -1232,11 +1234,11 @@ const fileOps = {
|
||||
*/
|
||||
async deletePermanently(trashId) {
|
||||
const confirmed = await showConfirmDialog({
|
||||
title: window.i18n ? window.i18n.t('dialogs.confirm_permanent_delete') : 'Delete permanently',
|
||||
message: window.i18n
|
||||
? window.i18n.t('dialogs.confirm_permanent_delete_msg')
|
||||
title: i18n ? i18n.t('dialogs.confirm_permanent_delete') : 'Delete permanently',
|
||||
message: i18n
|
||||
? i18n.t('dialogs.confirm_permanent_delete_msg')
|
||||
: 'Are you sure you want to permanently delete this item? This action cannot be undone.',
|
||||
confirmText: window.i18n ? window.i18n.t('actions.delete_permanently') : 'Delete permanently'
|
||||
confirmText: i18n ? i18n.t('actions.delete_permanently') : 'Delete permanently'
|
||||
});
|
||||
if (!confirmed) return false;
|
||||
|
||||
@@ -1247,15 +1249,15 @@ const fileOps = {
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
window.ui.showNotification('Item deleted', 'Item permanently deleted');
|
||||
ui.showNotification('Item deleted', 'Item permanently deleted');
|
||||
return true;
|
||||
} else {
|
||||
window.ui.showNotification('Error', 'Error deleting the item');
|
||||
ui.showNotification('Error', 'Error deleting the item');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting item permanently:', error);
|
||||
window.ui.showNotification('Error', 'Error deleting the item');
|
||||
ui.showNotification('Error', 'Error deleting the item');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -1266,11 +1268,9 @@ const fileOps = {
|
||||
*/
|
||||
async emptyTrash() {
|
||||
const confirmed = await showConfirmDialog({
|
||||
title: window.i18n ? window.i18n.t('dialogs.confirm_empty_trash') : 'Empty trash',
|
||||
message: window.i18n
|
||||
? window.i18n.t('trash.empty_confirm')
|
||||
: 'Are you sure you want to empty the trash? This action will permanently delete all items.',
|
||||
confirmText: window.i18n ? window.i18n.t('actions.empty_trash') : 'Empty trash'
|
||||
title: i18n ? i18n.t('dialogs.confirm_empty_trash') : 'Empty trash',
|
||||
message: i18n ? i18n.t('trash.empty_confirm') : 'Are you sure you want to empty the trash? This action will permanently delete all items.',
|
||||
confirmText: i18n ? i18n.t('actions.empty_trash') : 'Empty trash'
|
||||
});
|
||||
if (!confirmed) return false;
|
||||
|
||||
@@ -1281,15 +1281,15 @@ const fileOps = {
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
window.ui.showNotification('Trash emptied', 'The trash has been emptied successfully');
|
||||
ui.showNotification('Trash emptied', 'The trash has been emptied successfully');
|
||||
return true;
|
||||
} else {
|
||||
window.ui.showNotification('Error', 'Error emptying the trash');
|
||||
ui.showNotification('Error', 'Error emptying the trash');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error emptying trash:', error);
|
||||
window.ui.showNotification('Error', 'Error emptying the trash');
|
||||
ui.showNotification('Error', 'Error emptying the trash');
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -1315,11 +1315,11 @@ const fileOps = {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
} else {
|
||||
window.ui.showNotification('Error', 'Error downloading the file');
|
||||
ui.showNotification('Error', 'Error downloading the file');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error downloading file:', error);
|
||||
window.ui.showNotification('Error', 'Error downloading the file');
|
||||
ui.showNotification('Error', 'Error downloading the file');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1331,7 +1331,7 @@ const fileOps = {
|
||||
async downloadFolder(folderId, folderName) {
|
||||
try {
|
||||
// Show notification to user
|
||||
window.ui.showNotification('Preparing download', 'Preparing the folder for download...');
|
||||
ui.showNotification('Preparing download', 'Preparing the folder for download...');
|
||||
|
||||
const response = await fetch(`/api/folders/${folderId}/download?format=zip`, {
|
||||
headers: getAuthHeaders()
|
||||
@@ -1347,14 +1347,13 @@ const fileOps = {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
} else {
|
||||
window.ui.showNotification('Error', 'Error downloading the folder');
|
||||
ui.showNotification('Error', 'Error downloading the folder');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error downloading folder:', error);
|
||||
window.ui.showNotification('Error', 'Error downloading the folder');
|
||||
ui.showNotification('Error', 'Error downloading the folder');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Expose file operations module globally
|
||||
window.fileOps = fileOps;
|
||||
export { fileOps, getAuthHeaders };
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
* A simpler approach to viewing files that doesn't rely on complex DOM manipulation
|
||||
*/
|
||||
|
||||
import { updateHistory } from '../../app/main.js';
|
||||
import { app } from '../../app/state.js';
|
||||
import { isTextViewable } from '../../core/formatters.js';
|
||||
import { wopiEditor } from './wopiEditor.js';
|
||||
|
||||
class InlineViewer {
|
||||
constructor() {
|
||||
this.setupViewer();
|
||||
@@ -97,9 +102,9 @@ class InlineViewer {
|
||||
const ext = (file.name || '').split('.').pop().toLowerCase();
|
||||
const imageExts = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp', 'bmp', 'ico', 'heic', 'heif', 'avif', 'tiff'];
|
||||
const isImage = file.mime_type?.startsWith('image/') || imageExts.includes(ext);
|
||||
if (!isImage && window.wopiEditor && (await window.wopiEditor.canEdit(file.name))) {
|
||||
if (!isImage && wopiEditor && (await wopiEditor.canEdit(file.name))) {
|
||||
try {
|
||||
window.wopiEditor.openInModal(file.id, file.name, 'edit');
|
||||
wopiEditor.openInModal(file.id, file.name, 'edit');
|
||||
return;
|
||||
} catch (e) {
|
||||
console.warn('WOPI editor failed, falling back to inline viewer:', e);
|
||||
@@ -204,9 +209,9 @@ class InlineViewer {
|
||||
modal.classList.add('active');
|
||||
}
|
||||
|
||||
// Check if a MIME type is text-viewable — delegates to window.isTextViewable
|
||||
// Check if a MIME type is text-viewable
|
||||
isTextViewable(mimeType) {
|
||||
return window.isTextViewable ? window.isTextViewable(mimeType) : false;
|
||||
return isTextViewable(mimeType);
|
||||
}
|
||||
|
||||
// Creates a text viewer using authenticated fetch
|
||||
@@ -513,8 +518,8 @@ class InlineViewer {
|
||||
}
|
||||
|
||||
// clear
|
||||
window.app.viewFile = null;
|
||||
window.updateHistory(false);
|
||||
app.viewFile = null;
|
||||
updateHistory(false);
|
||||
|
||||
// Clear references
|
||||
this.currentFile = null;
|
||||
@@ -573,21 +578,4 @@ class InlineViewer {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize viewer when document is ready
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// Check if it's already initialized
|
||||
if (!window.inlineViewer) {
|
||||
console.log('Initializing inline viewer on DOMContentLoaded');
|
||||
window.inlineViewer = new InlineViewer();
|
||||
}
|
||||
});
|
||||
|
||||
// Fallback initialization for cases where DOMContentLoaded already fired
|
||||
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
||||
if (!window.inlineViewer) {
|
||||
console.log('Fallback initialization for inline viewer');
|
||||
setTimeout(() => {
|
||||
window.inlineViewer = new InlineViewer();
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
export const inlineViewer = new InlineViewer();
|
||||
|
||||
@@ -11,6 +11,14 @@
|
||||
|
||||
// @ts-check
|
||||
|
||||
import { loadFiles } from '../../app/filesView.js';
|
||||
import { app } from '../../app/state.js';
|
||||
import { showConfirmDialog, ui } from '../../app/ui.js';
|
||||
import { i18n } from '../../core/i18n.js';
|
||||
import { favorites } from '../library/favorites.js';
|
||||
import { contextMenus } from './contextMenus.js';
|
||||
import { getAuthHeaders } from './fileOperations.js';
|
||||
|
||||
const multiSelect = {
|
||||
/** Currently selected items: Map<id, { id, name, type, parentId }> */
|
||||
_selected: new Map(),
|
||||
@@ -42,8 +50,8 @@ const multiSelect = {
|
||||
// ── Helpers for i18n ────────────────────────────────────
|
||||
|
||||
_t(key, vars) {
|
||||
if (window.i18n && typeof window.i18n.t === 'function') {
|
||||
const val = window.i18n.t(key, vars);
|
||||
if (i18n && typeof i18n.t === 'function') {
|
||||
const val = i18n.t(key, vars);
|
||||
// If i18n returned the key itself, it's missing → fall back
|
||||
if (val && val !== key) return val;
|
||||
}
|
||||
@@ -133,15 +141,15 @@ const multiSelect = {
|
||||
showBatchResult(action, result) {
|
||||
if (action === 'copy') {
|
||||
if (result.errors > 0) {
|
||||
window.ui.showNotification('Batch copy', `${result.success} copied, ${result.errors} failed`);
|
||||
ui.showNotification('Batch copy', `${result.success} copied, ${result.errors} failed`);
|
||||
} else {
|
||||
window.ui.showNotification('Items copied', `${result.success} item${result.success !== 1 ? 's' : ''} copied successfully`);
|
||||
ui.showNotification('Items copied', `${result.success} item${result.success !== 1 ? 's' : ''} copied successfully`);
|
||||
}
|
||||
} else {
|
||||
if (result.errors > 0) {
|
||||
window.ui.showNotification('Batch move', `${result.success} moved, ${result.errors} failed`);
|
||||
ui.showNotification('Batch move', `${result.success} moved, ${result.errors} failed`);
|
||||
} else {
|
||||
window.ui.showNotification('Items moved', `${result.success} item${result.success !== 1 ? 's' : ''} moved successfully`);
|
||||
ui.showNotification('Items moved', `${result.success} item${result.success !== 1 ? 's' : ''} moved successfully`);
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -366,18 +374,18 @@ const multiSelect = {
|
||||
const errors = data.stats?.failed || 0;
|
||||
|
||||
this.clear();
|
||||
window.loadFiles();
|
||||
loadFiles();
|
||||
|
||||
if (errors > 0) {
|
||||
window.ui.showNotification('Batch delete', `${success} moved to trash, ${errors} failed`);
|
||||
ui.showNotification('Batch delete', `${success} moved to trash, ${errors} failed`);
|
||||
} else {
|
||||
window.ui.showNotification('Moved to trash', `${success} item${success !== 1 ? 's' : ''} moved to trash`);
|
||||
ui.showNotification('Moved to trash', `${success} item${success !== 1 ? 's' : ''} moved to trash`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Batch trash error:', e);
|
||||
window.ui.showNotification('Error', 'Could not move items to trash');
|
||||
ui.showNotification('Error', 'Could not move items to trash');
|
||||
this.clear();
|
||||
window.loadFiles();
|
||||
loadFiles();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -386,9 +394,9 @@ const multiSelect = {
|
||||
const items = this.items;
|
||||
if (items.length === 0) return;
|
||||
|
||||
window.app.moveDialogMode = 'batch';
|
||||
window.app.batchMoveItems = items;
|
||||
window.app.selectedTargetFolderId = '';
|
||||
app.moveDialogMode = 'batch';
|
||||
app.batchMoveItems = items;
|
||||
app.selectedTargetFolderId = '';
|
||||
|
||||
const dialog = document.getElementById('move-file-dialog');
|
||||
const dialogHeader = dialog.querySelector('.rename-dialog-header');
|
||||
@@ -406,7 +414,7 @@ const multiSelect = {
|
||||
const items = this.items;
|
||||
if (items.length === 0) return;
|
||||
|
||||
window.ui.showNotification('Preparing download', 'Creating ZIP archive...');
|
||||
ui.showNotification('Preparing download', 'Creating ZIP archive...');
|
||||
|
||||
try {
|
||||
const fileIds = items.filter((i) => i.type === 'file').map((i) => i.id);
|
||||
@@ -431,20 +439,20 @@ const multiSelect = {
|
||||
URL.revokeObjectURL(url);
|
||||
} catch (e) {
|
||||
console.error('Batch download error:', e);
|
||||
window.ui.showNotification('Error', 'Could not download selected items');
|
||||
ui.showNotification('Error', 'Could not download selected items');
|
||||
}
|
||||
},
|
||||
|
||||
/** Batch add to favorites — single API call */
|
||||
async batchFavorites() {
|
||||
const items = this.items;
|
||||
if (items.length === 0 || !window.favorites) return;
|
||||
if (items.length === 0 || !favorites) return;
|
||||
|
||||
// Filter out items already in favourites
|
||||
const toAdd = items.filter((i) => !window.favorites.isFavorite(i.id, i.type));
|
||||
const toAdd = items.filter((i) => !favorites.isFavorite(i.id, i.type));
|
||||
if (toAdd.length === 0) {
|
||||
this.clear();
|
||||
window.ui.showNotification(this._t('favorites.add') || 'Favorites', 'All selected items are already favorites');
|
||||
ui.showNotification(this._t('favorites.add') || 'Favorites', 'All selected items are already favorites');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -463,23 +471,23 @@ const multiSelect = {
|
||||
const inserted = data.stats?.inserted || 0;
|
||||
|
||||
// Replace cache directly from response (no extra GET)
|
||||
if (data.favorites && window.favorites._replaceCacheFromResponse) {
|
||||
window.favorites._replaceCacheFromResponse(data.favorites);
|
||||
if (data.favorites && favorites._replaceCacheFromResponse) {
|
||||
favorites._replaceCacheFromResponse(data.favorites);
|
||||
} else {
|
||||
await window.favorites._fetchFromServer();
|
||||
await favorites._fetchFromServer();
|
||||
}
|
||||
|
||||
this.clear();
|
||||
if (typeof window.loadFiles === 'function') window.loadFiles();
|
||||
loadFiles();
|
||||
|
||||
if (inserted > 0) {
|
||||
window.ui.showNotification(this._t('favorites.add') || 'Added to favorites', `${inserted} item${inserted !== 1 ? 's' : ''} added to favorites`);
|
||||
ui.showNotification(this._t('favorites.add') || 'Added to favorites', `${inserted} item${inserted !== 1 ? 's' : ''} added to favorites`);
|
||||
} else {
|
||||
window.ui.showNotification(this._t('favorites.add') || 'Favorites', 'All selected items are already favorites');
|
||||
ui.showNotification(this._t('favorites.add') || 'Favorites', 'All selected items are already favorites');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Batch favorites error:', e);
|
||||
window.ui.showNotification('Error', 'Could not add items to favorites');
|
||||
ui.showNotification('Error', 'Could not add items to favorites');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -510,8 +518,8 @@ const multiSelect = {
|
||||
const batchSelectionBar = document.getElementById('batch-selection-bar');
|
||||
batchSelectionBar.innerHTML = this._buildSelectionBarHTML();
|
||||
|
||||
if (window.i18n?.translateElement) {
|
||||
window.i18n.translateElement(batchSelectionBar);
|
||||
if (i18n?.translateElement) {
|
||||
i18n.translateElement(batchSelectionBar);
|
||||
}
|
||||
this._wireBarButtons();
|
||||
},
|
||||
@@ -524,5 +532,4 @@ const multiSelect = {
|
||||
}
|
||||
};
|
||||
|
||||
// Expose globally
|
||||
window.multiSelect = multiSelect;
|
||||
export { multiSelect };
|
||||
|
||||
@@ -7,6 +7,11 @@
|
||||
* displays the enriched results returned by the server.
|
||||
*/
|
||||
|
||||
import { loadFiles } from '../../app/filesView.js';
|
||||
import { app } from '../../app/state.js';
|
||||
import { ui } from '../../app/ui.js';
|
||||
import { getAuthHeaders } from './fileOperations.js';
|
||||
|
||||
const search = {
|
||||
/**
|
||||
* Perform a search using query parameters.
|
||||
@@ -55,7 +60,7 @@ const search = {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error performing search:', error);
|
||||
window.ui.showNotification('Error', 'Error performing search');
|
||||
ui.showNotification('Error', 'Error performing search');
|
||||
return {
|
||||
files: [],
|
||||
folders: [],
|
||||
@@ -109,7 +114,7 @@ const search = {
|
||||
* @param {Object} results - Enriched search results from backend
|
||||
*/
|
||||
displaySearchResults(results) {
|
||||
window.ui.resetFilesList(); // ensure also list visible & error hidden
|
||||
ui.resetFilesList(); // ensure also list visible & error hidden
|
||||
//FIXME: move into action rather bage sticky header + hide bread crumb ? + note also that search result does not consider section
|
||||
const pageStickyHeader = document.getElementById('page-sticky-header');
|
||||
|
||||
@@ -163,17 +168,17 @@ const search = {
|
||||
if (clearSearchBtn) {
|
||||
clearSearchBtn.addEventListener('click', () => {
|
||||
document.querySelector('.search-container input').value = '';
|
||||
window.app.currentPath = '';
|
||||
window.app.isSearchMode = false;
|
||||
app.currentPath = '';
|
||||
app.isSearchMode = false;
|
||||
document.querySelector('.search-results-header')?.remove();
|
||||
window.ui.updateBreadcrumb('');
|
||||
window.loadFiles();
|
||||
ui.updateBreadcrumb('');
|
||||
loadFiles();
|
||||
});
|
||||
}
|
||||
|
||||
// Empty state
|
||||
if (results.files.length === 0 && results.folders.length === 0) {
|
||||
window.ui.showError(`
|
||||
ui.showError(`
|
||||
<i class="fas fa-search empty-state-icon"></i>
|
||||
<p class="search-empty-text">No results found for this search</p>
|
||||
`);
|
||||
@@ -182,12 +187,12 @@ const search = {
|
||||
|
||||
// Render folders (server-provided enriched data)
|
||||
results.folders.forEach((folder) => {
|
||||
window.ui.addFolderToView(folder);
|
||||
ui.addFolderToView(folder);
|
||||
});
|
||||
|
||||
// Render files (server-provided enriched data)
|
||||
results.files.forEach((file) => {
|
||||
window.ui.addFileToView(file);
|
||||
ui.addFileToView(file);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -203,19 +208,18 @@ const search = {
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
window.ui.showNotification('Cache cleared', 'Search cache cleared successfully');
|
||||
ui.showNotification('Cache cleared', 'Search cache cleared successfully');
|
||||
return true;
|
||||
} else {
|
||||
window.ui.showNotification('Error', 'Error clearing search cache');
|
||||
ui.showNotification('Error', 'Error clearing search cache');
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error clearing search cache:', error);
|
||||
window.ui.showNotification('Error', 'Error clearing search cache');
|
||||
ui.showNotification('Error', 'Error clearing search cache');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Expose the search module globally
|
||||
window.search = search;
|
||||
export { search };
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
* Opens document files in Collabora Online / OnlyOffice via WOPI protocol.
|
||||
* Supports two modes: in-app modal (default) and new browser tab.
|
||||
*/
|
||||
|
||||
import { loadFiles } from '../../app/filesView.js';
|
||||
|
||||
class WopiEditor {
|
||||
constructor() {
|
||||
this.editorModal = null;
|
||||
@@ -203,10 +206,9 @@ class WopiEditor {
|
||||
this._messageHandler = null;
|
||||
}
|
||||
this.editorModal = null;
|
||||
|
||||
// Refresh file list to pick up any saves
|
||||
if (typeof loadFiles === 'function') {
|
||||
loadFiles();
|
||||
}
|
||||
loadFiles();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,7 +245,7 @@ class WopiEditor {
|
||||
}
|
||||
|
||||
// Global instance
|
||||
window.wopiEditor = new WopiEditor();
|
||||
export const wopiEditor = new WopiEditor();
|
||||
|
||||
// Prefetch supported extensions so canEdit() is fast on first use
|
||||
window.wopiEditor._fetchSupportedExtensions();
|
||||
wopiEditor._fetchSupportedExtensions();
|
||||
|
||||
Reference in New Issue
Block a user