const HabitTrackerApp = { elements: { habitGrid: null, addHabitForm: null, addHabbitButton: null, filterButtons: [] }, currentFilter: 'all', init: function() { this.loadElements(); this.render(); this.setupEventListeners(); }, setupEventListeners: function() { // Обработчики для кнопок фильтров this.elements.filterButtons.forEach(button => { button.addEventListener('click', (event) => { const filter = event.target.dataset.filter; this.applyFilter(filter); }); }); // Делегирование событий для кнопок в карточках привычек this.elements.habitGrid.addEventListener('click', (event) => { const habitCard = event.target.closest('.habit-card'); if (!habitCard) return; const habitId = parseInt(habitCard.dataset.habitId); if (event.target.classList.contains('js-habit-done')) { HabitManager.markHabitDone(habitId); HabitManager.saveToLocalStorage(); this.render(); } else if (event.target.classList.contains('js-habit-delete')) { HabitManager.deleteHabit(habitId); HabitManager.saveToLocalStorage(); this.render(); } }); }, loadElements: function() { this.elements.habitGrid = document.getElementById('habits-grid'); this.elements.filterButtons = document.querySelectorAll('.filters__button'); }, applyFilter: function(filter) { this.currentFilter = filter; // Обновление активной кнопки фильтра this.elements.filterButtons.forEach(button => { if (button.dataset.filter === filter) { button.classList.remove('button--secondary'); button.classList.add('button--primary'); } else { button.classList.remove('button--primary'); button.classList.add('button--secondary'); } }); this.render(); }, render: function() { const filteredHabits = HabitManager.getFilteredHabits(this.currentFilter); const habitsHTML = filteredHabits.map(habit => { const progress = (habit.currentCount / habit.targetCount) * 100; // Определение классов для карточки const cardClasses = ['habit-card']; if (habit.isCompleted) cardClasses.push('habit-card--completed'); if (habit.isImportant) cardClasses.push('habit-card--important'); if (habit.currentStreak >= 3) cardClasses.push('habit-card--streak'); if (habit.currentCount === 0) cardClasses.push('habit-card--new'); // Выбор иконки в зависимости от названия привычки let icon = '⭐'; if (habit.name.includes('Медитация')) icon = '🧘'; if (habit.name.includes('Учить')) icon = '💻'; if (habit.name.includes('Спорт')) icon = '💪'; if (habit.name.includes('Чтение')) icon = '📚'; return `
${habit.description}