1
This commit is contained in:
104
frontend/js/settings.js
Normal file → Executable file
104
frontend/js/settings.js
Normal file → Executable file
@@ -3,14 +3,103 @@
|
||||
const { copyToClipboard, showToast, escapeHtml } = window.BenchUtils;
|
||||
|
||||
let tokenVisible = false;
|
||||
const API_TOKEN = 'YOUR_API_TOKEN_HERE'; // Will be replaced by actual token or fetched from backend
|
||||
let API_TOKEN = 'LOADING...';
|
||||
|
||||
// Load backend config (API token, etc.)
|
||||
async function loadBackendConfig() {
|
||||
try {
|
||||
const backendApiUrl = window.BenchConfig?.backendApiUrl || `${window.location.protocol}//${window.location.hostname}:8007/api`;
|
||||
const response = await fetch(`${backendApiUrl}/config`);
|
||||
if (response.ok) {
|
||||
const config = await response.json();
|
||||
API_TOKEN = config.api_token;
|
||||
document.getElementById('apiToken').value = API_TOKEN;
|
||||
generateBenchCommand();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load backend config:', error);
|
||||
API_TOKEN = 'ERROR_LOADING_TOKEN';
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize settings page
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
loadDisplayPreferences();
|
||||
loadSettings();
|
||||
generateBenchCommand();
|
||||
await loadBackendConfig();
|
||||
});
|
||||
|
||||
// Load display preferences
|
||||
function loadDisplayPreferences() {
|
||||
const memoryUnit = localStorage.getItem('displayPref_memoryUnit') || 'GB';
|
||||
const storageUnit = localStorage.getItem('displayPref_storageUnit') || 'GB';
|
||||
const cacheUnit = localStorage.getItem('displayPref_cacheUnit') || 'KB';
|
||||
const temperatureUnit = localStorage.getItem('displayPref_temperatureUnit') || 'C';
|
||||
const sectionIconSize = localStorage.getItem('displayPref_sectionIconSize') || '32';
|
||||
const buttonIconSize = localStorage.getItem('displayPref_buttonIconSize') || '24';
|
||||
|
||||
document.getElementById('memoryUnit').value = memoryUnit;
|
||||
document.getElementById('storageUnit').value = storageUnit;
|
||||
document.getElementById('cacheUnit').value = cacheUnit;
|
||||
document.getElementById('temperatureUnit').value = temperatureUnit;
|
||||
document.getElementById('sectionIconSize').value = sectionIconSize;
|
||||
document.getElementById('buttonIconSize').value = buttonIconSize;
|
||||
|
||||
// Apply icon sizes
|
||||
applyIconSizes(sectionIconSize, buttonIconSize);
|
||||
}
|
||||
|
||||
// Apply icon sizes dynamically
|
||||
function applyIconSizes(sectionIconSize, buttonIconSize) {
|
||||
document.documentElement.style.setProperty('--section-icon-size', `${sectionIconSize}px`);
|
||||
document.documentElement.style.setProperty('--button-icon-size', `${buttonIconSize}px`);
|
||||
}
|
||||
|
||||
// Save display preferences
|
||||
function saveDisplayPreferences() {
|
||||
const memoryUnit = document.getElementById('memoryUnit').value;
|
||||
const storageUnit = document.getElementById('storageUnit').value;
|
||||
const cacheUnit = document.getElementById('cacheUnit').value;
|
||||
const temperatureUnit = document.getElementById('temperatureUnit').value;
|
||||
const sectionIconSize = document.getElementById('sectionIconSize').value;
|
||||
const buttonIconSize = document.getElementById('buttonIconSize').value;
|
||||
|
||||
localStorage.setItem('displayPref_memoryUnit', memoryUnit);
|
||||
localStorage.setItem('displayPref_storageUnit', storageUnit);
|
||||
localStorage.setItem('displayPref_cacheUnit', cacheUnit);
|
||||
localStorage.setItem('displayPref_temperatureUnit', temperatureUnit);
|
||||
localStorage.setItem('displayPref_sectionIconSize', sectionIconSize);
|
||||
localStorage.setItem('displayPref_buttonIconSize', buttonIconSize);
|
||||
|
||||
// Apply icon sizes immediately
|
||||
applyIconSizes(sectionIconSize, buttonIconSize);
|
||||
|
||||
showToast('Préférences enregistrées ! Rechargez la page pour voir les changements.', 'success');
|
||||
}
|
||||
|
||||
// Reset display preferences to defaults
|
||||
function resetDisplayPreferences() {
|
||||
localStorage.removeItem('displayPref_memoryUnit');
|
||||
localStorage.removeItem('displayPref_storageUnit');
|
||||
localStorage.removeItem('displayPref_cacheUnit');
|
||||
localStorage.removeItem('displayPref_temperatureUnit');
|
||||
localStorage.removeItem('displayPref_sectionIconSize');
|
||||
localStorage.removeItem('displayPref_buttonIconSize');
|
||||
|
||||
// Reset form to defaults
|
||||
document.getElementById('memoryUnit').value = 'GB';
|
||||
document.getElementById('storageUnit').value = 'GB';
|
||||
document.getElementById('cacheUnit').value = 'KB';
|
||||
document.getElementById('temperatureUnit').value = 'C';
|
||||
document.getElementById('sectionIconSize').value = '32';
|
||||
document.getElementById('buttonIconSize').value = '24';
|
||||
|
||||
// Apply default icon sizes
|
||||
applyIconSizes('32', '24');
|
||||
|
||||
showToast('Préférences réinitialisées aux valeurs par défaut', 'success');
|
||||
}
|
||||
|
||||
// Load settings
|
||||
function loadSettings() {
|
||||
// In a real scenario, these would be fetched from backend or localStorage
|
||||
@@ -22,8 +111,7 @@ function loadSettings() {
|
||||
document.getElementById('iperfServer').value = savedIperfServer;
|
||||
document.getElementById('benchMode').value = savedBenchMode;
|
||||
|
||||
// Set API token (in production, this should be fetched securely)
|
||||
document.getElementById('apiToken').value = API_TOKEN;
|
||||
// API token will be loaded asynchronously from backend
|
||||
|
||||
// Add event listeners for auto-generation
|
||||
document.getElementById('backendUrl').addEventListener('input', () => {
|
||||
@@ -74,8 +162,8 @@ function generateBenchCommand() {
|
||||
const scriptUrl = `${backendUrl.replace(':8007', ':8087')}/scripts/bench.sh`;
|
||||
|
||||
// Build command parts
|
||||
let command = `curl -s ${scriptUrl} | bash -s -- \\
|
||||
--server ${backendUrl}/api/benchmark \\
|
||||
let command = `curl -s ${scriptUrl} | sudo bash -s -- \\
|
||||
--server ${backendUrl} \\
|
||||
--token "${API_TOKEN}"`;
|
||||
|
||||
if (iperfServer) {
|
||||
@@ -143,3 +231,5 @@ window.generateBenchCommand = generateBenchCommand;
|
||||
window.copyGeneratedCommand = copyGeneratedCommand;
|
||||
window.toggleTokenVisibility = toggleTokenVisibility;
|
||||
window.copyToken = copyToken;
|
||||
window.saveDisplayPreferences = saveDisplayPreferences;
|
||||
window.resetDisplayPreferences = resetDisplayPreferences;
|
||||
|
||||
Reference in New Issue
Block a user