Browse Source
feat: Isolate modal and theme logic into modules
feat: Isolate modal and theme logic into modules
Refactored the modal and theme-switching functionality out of the main script. - Created src/modal.js to handle all logic for the confirmation modal. - Created src/theme.js to manage the dark/light theme and canvas redraws. - The main script now imports and initializes these modules, cleaning up the global scope.refactor/modularize
3 changed files with 91 additions and 68 deletions
@ -0,0 +1,33 @@ |
|||||
|
|
||||
|
import { modalText, modalCancelBtn, modalContainer, modalOverlay, modalContent, modalOkBtn } from './dom.js'; |
||||
|
|
||||
|
// --- Custom Modal Logic --- //
|
||||
|
let modalResolve = null; |
||||
|
export function showModal(message, isConfirm = false) { |
||||
|
return new Promise(resolve => { |
||||
|
modalText.textContent = message; |
||||
|
modalCancelBtn.classList.toggle('hidden', !isConfirm); |
||||
|
modalContainer.classList.remove('hidden'); |
||||
|
setTimeout(() => { |
||||
|
modalOverlay.classList.remove('opacity-0'); |
||||
|
modalContent.classList.remove('scale-95'); |
||||
|
} |
||||
|
, 10); |
||||
|
modalResolve = resolve; |
||||
|
}); |
||||
|
} |
||||
|
function hideModal(value) { |
||||
|
modalOverlay.classList.add('opacity-0'); |
||||
|
modalContent.classList.add('scale-95'); |
||||
|
setTimeout(() => { |
||||
|
modalContainer.classList.add('hidden'); |
||||
|
if (modalResolve) modalResolve(value); |
||||
|
}, 200); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
//----------------------Modal Event Listeners----------------------//
|
||||
|
|
||||
|
modalOkBtn.addEventListener('click', () => hideModal(true)); |
||||
|
modalCancelBtn.addEventListener('click', () => hideModal(false)); |
||||
|
modalOverlay.addEventListener('click', () => hideModal(false)); |
||||
@ -0,0 +1,48 @@ |
|||||
|
import { appState } from './state.js'; |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// --- DARK MODE: Step 3 - Add the JavaScript Logic ---
|
||||
|
const themeToggleBtn = document.getElementById('theme-toggle'); |
||||
|
const darkIcon = document.getElementById('theme-toggle-dark-icon'); |
||||
|
const lightIcon = document.getElementById('theme-toggle-light-icon'); |
||||
|
|
||||
|
function setTheme(theme) { |
||||
|
if (theme === 'dark') { |
||||
|
document.documentElement.classList.add('dark'); |
||||
|
lightIcon.classList.remove('hidden'); |
||||
|
darkIcon.classList.add('hidden'); |
||||
|
localStorage.setItem('color-theme', 'dark'); |
||||
|
} else { |
||||
|
document.documentElement.classList.remove('dark'); |
||||
|
darkIcon.classList.remove('hidden'); |
||||
|
lightIcon.classList.add('hidden'); |
||||
|
localStorage.setItem('color-theme', 'light'); |
||||
|
} |
||||
|
if (appState.p5_instance) appState.p5_instance.redraw(); |
||||
|
if (appState.speedGraphInstance) { |
||||
|
if ((appState.canData.length > 0 || appState.vizData) && videoPlayer.duration) { |
||||
|
appState.speedGraphInstance.setData(appState.canData, appState.vizData, videoPlayer.duration); |
||||
|
} |
||||
|
appState.speedGraphInstance.redraw(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export function initializeTheme() { |
||||
|
const savedTheme = localStorage.getItem('color-theme'); |
||||
|
if (savedTheme) { |
||||
|
setTheme(savedTheme); |
||||
|
} else { |
||||
|
// Default to light mode if no theme is saved
|
||||
|
setTheme('light'); |
||||
|
} |
||||
|
|
||||
|
themeToggleBtn.addEventListener('click', () => { |
||||
|
if (document.documentElement.classList.contains('dark')) { |
||||
|
setTheme('light'); |
||||
|
} else { |
||||
|
setTheme('dark'); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue