refactor: consolidate 3 DOMContentLoaded listeners into single init block

- Extract initTabState, initTabWheelScroll, initVisibilityPolling,
  initChatImagePreview, initModalAccessibility as named functions
- Move polling interval vars to outer scope for accessibility
- Single DOMContentLoaded calls all init functions in logical order
- Replace scattered listeners with comment markers at original locations
This commit is contained in:
2026-02-28 23:50:40 +02:00
parent e0dc190710
commit 820a226dd9

View File

@@ -1848,39 +1848,10 @@ function switchTab(tabId) {
} }
} }
// Initialize // ============================================================================
document.addEventListener('DOMContentLoaded', function() { // Initialization — all setup in one DOMContentLoaded
// Restore persisted tab from localStorage // ============================================================================
const savedTab = localStorage.getItem('miku-active-tab');
if (savedTab && document.getElementById(savedTab)) {
switchTab(savedTab);
}
loadStatus();
loadServers();
loadLastPrompt();
loadLogs();
checkEvilModeStatus(); // Check evil mode on load
checkBipolarModeStatus(); // Check bipolar mode on load
checkGPUStatus(); // Check GPU selection on load
refreshLanguageStatus(); // Check language mode on load
console.log('🚀 DOMContentLoaded - initializing figurine subscribers list');
refreshFigurineSubscribers();
loadProfilePictureMetadata();
initLogsScrollDetection(); // Set up log auto-scroll tracking
// Enable mouse wheel horizontal scrolling on tab bar
const tabButtonsEl = document.querySelector('.tab-buttons');
if (tabButtonsEl) {
tabButtonsEl.addEventListener('wheel', function(e) {
if (e.deltaY !== 0) {
e.preventDefault();
tabButtonsEl.scrollLeft += e.deltaY;
}
}, { passive: false });
}
// Set up periodic updates with visibility-aware polling
let statusInterval, logsInterval, argsInterval; let statusInterval, logsInterval, argsInterval;
function startPolling() { function startPolling() {
@@ -1895,6 +1866,26 @@ document.addEventListener('DOMContentLoaded', function() {
clearInterval(argsInterval); argsInterval = null; clearInterval(argsInterval); argsInterval = null;
} }
function initTabState() {
const savedTab = localStorage.getItem('miku-active-tab');
if (savedTab && document.getElementById(savedTab)) {
switchTab(savedTab);
}
}
function initTabWheelScroll() {
const tabButtonsEl = document.querySelector('.tab-buttons');
if (tabButtonsEl) {
tabButtonsEl.addEventListener('wheel', function(e) {
if (e.deltaY !== 0) {
e.preventDefault();
tabButtonsEl.scrollLeft += e.deltaY;
}
}, { passive: false });
}
}
function initVisibilityPolling() {
document.addEventListener('visibilitychange', () => { document.addEventListener('visibilitychange', () => {
if (document.hidden) { if (document.hidden) {
stopPolling(); stopPolling();
@@ -1905,7 +1896,71 @@ document.addEventListener('DOMContentLoaded', function() {
console.log('▶️ Tab visible — polling resumed'); console.log('▶️ Tab visible — polling resumed');
} }
}); });
}
function initChatImagePreview() {
const imageInput = document.getElementById('chat-image-file');
if (imageInput) {
imageInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
const preview = document.getElementById('chat-image-preview');
const previewImg = document.getElementById('chat-image-preview-img');
previewImg.src = event.target.result;
preview.style.display = 'block';
};
reader.readAsDataURL(file);
}
});
}
}
function initModalAccessibility() {
const editModal = document.getElementById('edit-memory-modal');
const createModal = document.getElementById('create-memory-modal');
if (editModal) {
editModal.setAttribute('role', 'dialog');
editModal.setAttribute('aria-modal', 'true');
editModal.setAttribute('aria-label', 'Edit Memory');
editModal.addEventListener('click', function(e) {
if (e.target === this) closeEditMemoryModal();
});
}
if (createModal) {
createModal.setAttribute('role', 'dialog');
createModal.setAttribute('aria-modal', 'true');
createModal.setAttribute('aria-label', 'Create Memory');
createModal.addEventListener('click', function(e) {
if (e.target === this) closeCreateMemoryModal();
});
}
}
document.addEventListener('DOMContentLoaded', function() {
// Tab & UI initialization
initTabState();
initTabWheelScroll();
initLogsScrollDetection();
initChatImagePreview();
initModalAccessibility();
// Load initial data
loadStatus();
loadServers();
loadLastPrompt();
loadLogs();
checkEvilModeStatus();
checkBipolarModeStatus();
checkGPUStatus();
refreshLanguageStatus();
refreshFigurineSubscribers();
loadProfilePictureMetadata();
loadVoiceDebugMode();
// Start polling with visibility awareness
initVisibilityPolling();
startPolling(); startPolling();
}); });
@@ -4980,28 +5035,7 @@ function toggleChatImageUpload() {
} }
} }
// Preview uploaded image // (Chat image preview + voice debug mode init moved to consolidated DOMContentLoaded)
document.addEventListener('DOMContentLoaded', function() {
const imageInput = document.getElementById('chat-image-file');
if (imageInput) {
imageInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
const preview = document.getElementById('chat-image-preview');
const previewImg = document.getElementById('chat-image-preview-img');
previewImg.src = event.target.result;
preview.style.display = 'block';
};
reader.readAsDataURL(file);
}
});
}
// Load voice debug mode setting
loadVoiceDebugMode();
});
// Load voice debug mode setting from server // Load voice debug mode setting from server
async function loadVoiceDebugMode() { async function loadVoiceDebugMode() {
@@ -5889,26 +5923,7 @@ document.addEventListener('keydown', function(e) {
} }
}); });
document.addEventListener('DOMContentLoaded', function() { // (Modal accessibility init moved to consolidated DOMContentLoaded)
const editModal = document.getElementById('edit-memory-modal');
const createModal = document.getElementById('create-memory-modal');
if (editModal) {
editModal.setAttribute('role', 'dialog');
editModal.setAttribute('aria-modal', 'true');
editModal.setAttribute('aria-label', 'Edit Memory');
editModal.addEventListener('click', function(e) {
if (e.target === this) closeEditMemoryModal();
});
}
if (createModal) {
createModal.setAttribute('role', 'dialog');
createModal.setAttribute('aria-modal', 'true');
createModal.setAttribute('aria-label', 'Create Memory');
createModal.addEventListener('click', function(e) {
if (e.target === this) closeCreateMemoryModal();
});
}
});
async function saveNewMemory() { async function saveNewMemory() {
const collection = document.getElementById('create-memory-collection').value; const collection = document.getElementById('create-memory-collection').value;