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:
Edouard Vanbelle
2026-04-13 15:09:10 +02:00
parent a2e5cfce24
commit 6f4abfcec4
42 changed files with 1104 additions and 1038 deletions
+2 -2
View File
@@ -13,14 +13,14 @@
* HttpOnly cookies.
*/
// eslint-disable-next-line no-unused-vars
function getCsrfToken() {
const match = document.cookie.split('; ').find((row) => row.startsWith('oxicloud_csrf='));
return match ? match.split('=')[1] : '';
}
// biome-ignore lint/correctness/noUnusedVariables: global function
function getCsrfHeaders() {
const token = getCsrfToken();
return token ? { 'X-CSRF-Token': token } : {};
}
export { getCsrfHeaders, getCsrfToken };
+1 -6
View File
@@ -65,9 +65,4 @@ function isTextViewable(mimeType) {
return textTypes.includes(mimeType);
}
window.escapeHtml = escapeHtml;
window.formatFileSize = formatFileSize;
window.formatQuotaSize = formatQuotaSize;
window.formatDateTime = formatDateTime;
window.formatDateShort = formatDateShort;
window.isTextViewable = isTextViewable;
export { escapeHtml, formatDateShort, formatDateTime, formatFileSize, formatQuotaSize, isTextViewable };
+1 -2
View File
@@ -314,8 +314,7 @@ function safeT(key, params = {}) {
return interpolate(value, params);
}
// Export functions for use in other modules
window.i18n = {
export const i18n = {
t: safeT,
setLocale,
getCurrentLocale,
+2 -3
View File
@@ -483,9 +483,8 @@ function replaceIconsInElement(container) {
}
// ── Expose globally ────────────────────────────────────────────
window.oxiIcon = oxiIcon;
window.replaceIconsInElement = replaceIconsInElement;
window.OxiIcons = _ICONS;
export { oxiIcon, replaceIconsInElement };
export const OxiIcons = _ICONS;
// ── Auto-replace: MutationObserver bridge ──────────────────────
// Watches the DOM for new <i class="fa-..."> elements and converts them
+6 -3
View File
@@ -1,3 +1,6 @@
import { ALL_LANGUAGES } from '../features/auth/auth.js';
import { i18n } from './i18n.js';
/**
* Language Selector Component for OxiCloud
* Custom styled dropdown with flags
@@ -63,7 +66,7 @@ function createLanguageSelector(containerId = 'language-selector') {
// Get current language
const languages = getAvailableLanguages();
const currentLocale = window.i18n ? window.i18n.getCurrentLocale() : 'en';
const currentLocale = i18n ? i18n.getCurrentLocale() : 'en';
const currentLang = languages.find((l) => l.code === currentLocale) || languages[0];
// Set initial HTML attributes
@@ -183,8 +186,8 @@ function closeDropdown(container) {
*/
async function selectLanguage(langCode, container) {
// Update i18n if available
if (window.i18n) {
await window.i18n.setLocale(langCode);
if (i18n) {
await i18n.setLocale(langCode);
}
// Update HTML lang attribute and dir for RTL languages
+12 -9
View File
@@ -1,3 +1,6 @@
import { i18n } from './i18n.js';
import { replaceIconsInElement } from './icons.js';
/**
* Modal System for OxiCloud
* Provides modern, styled modals to replace browser prompts/alerts
@@ -86,8 +89,8 @@ const Modal = {
iconContainer.innerHTML = `<i id="modal-icon" class="fas ${icon}"></i>`;
this.icon = document.getElementById('modal-icon');
// Let icons.js convert it to SVG
if (window.replaceIconsInElement) {
window.replaceIconsInElement(iconContainer);
if (replaceIconsInElement) {
replaceIconsInElement(iconContainer);
this.icon = document.getElementById('modal-icon');
}
}
@@ -99,14 +102,14 @@ const Modal = {
// Set button text (use i18n if available)
if (confirmText) {
this.confirmBtn.textContent = confirmText;
} else if (window.i18n) {
this.confirmBtn.textContent = window.i18n.t('actions.confirm');
} else if (i18n) {
this.confirmBtn.textContent = i18n.t('actions.confirm');
}
if (cancelText) {
this.cancelBtn.textContent = cancelText;
} else if (window.i18n) {
this.cancelBtn.textContent = window.i18n.t('actions.cancel');
} else if (i18n) {
this.cancelBtn.textContent = i18n.t('actions.cancel');
}
// Set callbacks
@@ -126,7 +129,7 @@ const Modal = {
* @returns {Promise<string|null>}
*/
promptNewFolder() {
const t = window.i18n ? window.i18n.t.bind(window.i18n) : (k) => k;
const t = i18n ? i18n.t.bind(i18n) : (k) => k;
return this.prompt({
title: t('dialogs.new_folder_title') || 'New folder',
@@ -144,7 +147,7 @@ const Modal = {
* @returns {Promise<string|null>}
*/
promptRename(currentName, isFolder = false) {
const t = window.i18n ? window.i18n.t.bind(window.i18n) : (k) => k;
const t = i18n ? i18n.t.bind(i18n) : (k) => k;
// For files, we want to select only the name part (without extension)
this._selectNameOnly = !isFolder;
@@ -232,4 +235,4 @@ document.addEventListener('DOMContentLoaded', () => {
});
// Export for use in other modules
window.Modal = Modal;
export { Modal };
+7 -5
View File
@@ -1,3 +1,5 @@
import { i18n } from './i18n.js';
/**
* OxiCloud – Notification Bell Module
*
@@ -5,7 +7,7 @@
* in the top-bar. Upload progress, quota errors, and general messages all
* go through this module.
*
* Public API (on window.notifications):
* Public API (exported as `notifications`):
* addUploadBatch(totalFiles) → batchId
* updateFile(batchId, fileName, pct, status)
* finishBatch(batchId, successCount, totalFiles)
@@ -147,7 +149,7 @@ const notifications = (() => {
item.className = 'notif-item';
item.id = batchId;
const t = window.i18n?.t || ((k) => k);
const t = i18n?.t || ((k) => k);
const uploadingText = folderName ? `📁 ${t('upload.uploading')} ${_esc(folderName)}…` : t('upload.uploading');
const filesLabel = t('upload.files');
@@ -252,7 +254,7 @@ const notifications = (() => {
const pctEl = $(`${batchId}-pct`);
const statsEl = $(`${batchId}-stats`);
const t = window.i18n?.t || ((k) => k);
const t = i18n?.t || ((k) => k);
const filesLabel = t('upload.files');
if (fillEl) fillEl.style.width = `${pctVal}%`;
@@ -280,7 +282,7 @@ const notifications = (() => {
const curEl = $(`${batchId}-current`);
if (curEl) curEl.textContent = '';
const t = window.i18n?.t || ((k) => k);
const t = i18n?.t || ((k) => k);
const completeText = t('upload.complete', {
count: successCount,
total: totalFiles
@@ -351,4 +353,4 @@ const notifications = (() => {
};
})();
window.notifications = notifications;
export { notifications };