This commit is contained in:
2025-12-20 03:47:10 +01:00
parent 8428bf9c82
commit dcba044cd6
179 changed files with 10345 additions and 786 deletions

127
frontend/js/utils.js Normal file → Executable file
View File

@@ -40,6 +40,26 @@ function formatFileSize(bytes) {
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i];
}
// Format duration (seconds) into human readable form
function formatDuration(seconds) {
if (seconds === null || seconds === undefined) return 'N/A';
const totalSeconds = Number(seconds);
if (!Number.isFinite(totalSeconds) || totalSeconds < 0) return 'N/A';
const days = Math.floor(totalSeconds / 86400);
const hours = Math.floor((totalSeconds % 86400) / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const secs = Math.floor(totalSeconds % 60);
const parts = [];
if (days > 0) parts.push(`${days} j`);
if (hours > 0) parts.push(`${hours} h`);
if (minutes > 0) parts.push(`${minutes} min`);
if (parts.length === 0) parts.push(`${secs} s`);
return parts.join(' ');
}
// Get score badge class based on value
function getScoreBadgeClass(score) {
if (score === null || score === undefined) return 'score-badge';
@@ -51,7 +71,9 @@ function getScoreBadgeClass(score) {
// Get score badge text
function getScoreBadgeText(score) {
if (score === null || score === undefined) return '--';
return Math.round(score);
const numeric = Number(score);
if (!Number.isFinite(numeric)) return '--';
return Math.ceil(numeric);
}
// Create score badge HTML
@@ -299,8 +321,21 @@ function closeModal(modalId) {
}
}
// Initialize modal close buttons
// Apply icon size preferences from localStorage
function applyIconSizePreferences() {
const sectionIconSize = localStorage.getItem('displayPref_sectionIconSize') || '32';
const buttonIconSize = localStorage.getItem('displayPref_buttonIconSize') || '24';
document.documentElement.style.setProperty('--section-icon-size', `${sectionIconSize}px`);
document.documentElement.style.setProperty('--button-icon-size', `${buttonIconSize}px`);
}
// Initialize modal close buttons and apply icon preferences
document.addEventListener('DOMContentLoaded', () => {
// Apply icon size preferences on all pages
applyIconSizePreferences();
// Initialize modal close buttons
document.querySelectorAll('.modal').forEach(modal => {
modal.addEventListener('click', (e) => {
if (e.target === modal) {
@@ -318,10 +353,90 @@ document.addEventListener('DOMContentLoaded', () => {
});
// Export functions for use in other files
// Unit conversion functions
function getDisplayPreferences() {
return {
memoryUnit: localStorage.getItem('displayPref_memoryUnit') || 'GB',
storageUnit: localStorage.getItem('displayPref_storageUnit') || 'GB',
cacheUnit: localStorage.getItem('displayPref_cacheUnit') || 'KB',
temperatureUnit: localStorage.getItem('displayPref_temperatureUnit') || 'C'
};
}
// Convert memory from MB to preferred unit
function formatMemory(mb, forceUnit = null) {
if (!mb || mb === 0) return '0 MB';
const prefs = getDisplayPreferences();
const unit = forceUnit || prefs.memoryUnit;
if (unit === 'GB') {
return (mb / 1024).toFixed(2) + ' GB';
}
return mb.toFixed(0) + ' MB';
}
// Convert storage from GB to preferred unit
function formatStorage(gb, forceUnit = null) {
if (!gb || gb === 0) return '0 GB';
const prefs = getDisplayPreferences();
const unit = forceUnit || prefs.storageUnit;
if (unit === 'TB') {
return (gb / 1024).toFixed(2) + ' TB';
} else if (unit === 'MB') {
return (gb * 1024).toFixed(0) + ' MB';
}
return gb.toFixed(2) + ' GB';
}
// Convert cache from KB to preferred unit
function formatCache(kb, forceUnit = null) {
if (!kb || kb === 0) return '0 KB';
const prefs = getDisplayPreferences();
const unit = forceUnit || prefs.cacheUnit;
if (unit === 'MB') {
return (kb / 1024).toFixed(2) + ' MB';
}
return kb.toFixed(0) + ' KB';
}
// Convert temperature to preferred unit
function formatTemperature(celsius, forceUnit = null) {
if (celsius === null || celsius === undefined) return 'N/A';
const prefs = getDisplayPreferences();
const unit = forceUnit || prefs.temperatureUnit;
if (unit === 'F') {
const fahrenheit = (celsius * 9/5) + 32;
return fahrenheit.toFixed(1) + '°F';
}
return celsius.toFixed(1) + '°C';
}
function renderMarkdown(text) {
if (!text || !text.trim()) {
return '<div class="markdown-block" style="color: var(--text-secondary);">Aucune note disponible</div>';
}
let html = escapeHtml(text);
html = html.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
html = html.replace(/\*(.+?)\*/g, '<em>$1</em>');
html = html.replace(/`([^`]+)`/g, '<code>$1</code>');
html = html.replace(/\n/g, '<br>');
return `<div class="markdown-block">${html}</div>`;
}
window.BenchUtils = {
formatDate,
formatRelativeTime,
formatFileSize,
formatDuration,
getScoreBadgeClass,
getScoreBadgeText,
createScoreBadge,
@@ -340,5 +455,11 @@ window.BenchUtils = {
formatHardwareInfo,
initTabs,
openModal,
closeModal
closeModal,
getDisplayPreferences,
formatMemory,
formatStorage,
formatCache,
formatTemperature,
renderMarkdown
};