history population to handle back events

This commit is contained in:
2026-07-24 08:30:08 +03:00
parent 41c38055a1
commit ecbf7f0f8a
+28
View File
@@ -221,9 +221,15 @@ let game = {
mistakes: [],
};
let popstateHandled = false;
function showScreen(id) {
document.querySelectorAll('.screen').forEach(s => s.classList.remove('active'));
document.getElementById('screen-' + id).classList.add('active');
if (id !== 'menu' && !popstateHandled) {
history.pushState({ screen: id }, '');
}
popstateHandled = false;
}
function shuffle(arr) {
@@ -978,3 +984,25 @@ document.addEventListener('keydown', (e) => {
}
}
});
// Handle mobile back/cancel button
const BACK_MAP = {
'screen-setup-system': 'btn-system-back',
'screen-setup-range': 'btn-range-back',
'screen-setup-options': 'btn-options-back',
'screen-game': 'btn-stop',
'screen-results': 'btn-results-menu',
'screen-theory-system': 'btn-theory-back',
'screen-theory-content': 'btn-theory-content-back',
};
window.addEventListener('popstate', () => {
const active = document.querySelector('.screen.active');
if (!active) return;
const btnId = BACK_MAP[active.id];
if (btnId) {
popstateHandled = true;
document.getElementById(btnId).click();
}
});