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:
@@ -1848,28 +1848,32 @@ function switchTab(tabId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize
|
// ============================================================================
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
// Initialization — all setup in one DOMContentLoaded
|
||||||
// Restore persisted tab from localStorage
|
// ============================================================================
|
||||||
|
|
||||||
|
let statusInterval, logsInterval, argsInterval;
|
||||||
|
|
||||||
|
function startPolling() {
|
||||||
|
if (!statusInterval) statusInterval = setInterval(loadStatus, 10000);
|
||||||
|
if (!logsInterval) logsInterval = setInterval(loadLogs, 5000);
|
||||||
|
if (!argsInterval) argsInterval = setInterval(loadActiveArguments, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopPolling() {
|
||||||
|
clearInterval(statusInterval); statusInterval = null;
|
||||||
|
clearInterval(logsInterval); logsInterval = null;
|
||||||
|
clearInterval(argsInterval); argsInterval = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function initTabState() {
|
||||||
const savedTab = localStorage.getItem('miku-active-tab');
|
const savedTab = localStorage.getItem('miku-active-tab');
|
||||||
if (savedTab && document.getElementById(savedTab)) {
|
if (savedTab && document.getElementById(savedTab)) {
|
||||||
switchTab(savedTab);
|
switchTab(savedTab);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
loadStatus();
|
|
||||||
loadServers();
|
function initTabWheelScroll() {
|
||||||
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');
|
const tabButtonsEl = document.querySelector('.tab-buttons');
|
||||||
if (tabButtonsEl) {
|
if (tabButtonsEl) {
|
||||||
tabButtonsEl.addEventListener('wheel', function(e) {
|
tabButtonsEl.addEventListener('wheel', function(e) {
|
||||||
@@ -1879,22 +1883,9 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
}
|
}
|
||||||
}, { passive: false });
|
}, { passive: false });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Set up periodic updates with visibility-aware polling
|
|
||||||
let statusInterval, logsInterval, argsInterval;
|
function initVisibilityPolling() {
|
||||||
|
|
||||||
function startPolling() {
|
|
||||||
if (!statusInterval) statusInterval = setInterval(loadStatus, 10000);
|
|
||||||
if (!logsInterval) logsInterval = setInterval(loadLogs, 5000);
|
|
||||||
if (!argsInterval) argsInterval = setInterval(loadActiveArguments, 5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
function stopPolling() {
|
|
||||||
clearInterval(statusInterval); statusInterval = null;
|
|
||||||
clearInterval(logsInterval); logsInterval = null;
|
|
||||||
clearInterval(argsInterval); argsInterval = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user