tehnicheskoe-zadanie-veb-pri/script.js

90 lines
2.7 KiB
JavaScript

// Мой бюджет - JavaScript логика (Фаза 1: навигация и модалки)
// Навигация между экранами
function switchScreen(screenId) {
// Скрываем все экраны
document.querySelectorAll('.screen').forEach(function(el) {
el.classList.remove('active');
});
// Показываем нужный
const target = document.getElementById(screenId);
if (target) {
target.classList.add('active');
}
// Обновляем активные ссылки
document.querySelectorAll('.nav-link, .mobile-nav-item').forEach(function(el) {
el.classList.remove('active');
if (el.dataset.screen === screenId) {
el.classList.add('active');
}
});
// Скролл вверх
window.scrollTo(0, 0);
}
// Модальные окна
function openModal(modalId) {
document.getElementById(modalId).classList.add('active');
}
function closeModal(modalId) {
document.getElementById(modalId).classList.remove('active');
}
// Уведомления
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add('show');
setTimeout(function() {
toast.classList.remove('show');
}, 3000);
}
// Инициализация
document.addEventListener('DOMContentLoaded', function() {
// Обработка кликов по навигации
document.querySelectorAll('[data-screen]').forEach(function(link) {
link.addEventListener('click', function(e) {
e.preventDefault();
const screenId = this.dataset.screen;
switchScreen(screenId);
// Обновляем URL без перезагрузки
history.pushState(null, '', '#' + screenId);
});
});
// Обработка форм
const forms = ['expense-form', 'budget-form', 'product-form', 'goal-form'];
forms.forEach(function(formId) {
const form = document.getElementById(formId);
if (form) {
form.addEventListener('submit', function(e) {
e.preventDefault();
// Здесь будет логика сохранения (Фаза 2)
closeModal(formId.replace('-form', '-modal'));
showToast('✓ Успешно добавлено!');
});
}
});
// Закрытие модальных окон по клику на фон
document.querySelectorAll('.modal-overlay').forEach(function(overlay) {
overlay.addEventListener('click', function(e) {
if (e.target === overlay) {
overlay.classList.remove('active');
}
});
});
// Проверка URL при загрузке
const hash = window.location.hash.slice(1);
if (hash && document.getElementById(hash)) {
switchScreen(hash);
}
});