Fix 'Today' button functionality to properly set date filters and submit form

This commit is contained in:
2025-11-07 20:54:04 +03:00
parent efeb396204
commit 8e1b6fc157

View File

@@ -124,6 +124,7 @@ class DateCarousel {
this.render();
this.attachNavHandlers();
this.attachTodayHandler();
}
/**
@@ -187,6 +188,7 @@ class DateCarousel {
// Перерисовываем только если количество дней изменилось
if (oldDaysCount !== this.daysCount) {
this.render();
this.attachTodayHandler(); // Восстанавливаем обработчик кнопки "Сегодня" после рендеринга
}
}
@@ -361,7 +363,6 @@ class DateCarousel {
attachNavHandlers() {
const prevBtn = this.container.parentElement.querySelector('.carousel-prev');
const nextBtn = this.container.parentElement.querySelector('.carousel-next');
const todayBtn = this.container.parentElement.querySelector('.today-btn');
if (prevBtn) {
prevBtn.addEventListener('click', () => this.shiftDays(-1));
@@ -370,9 +371,22 @@ class DateCarousel {
if (nextBtn) {
nextBtn.addEventListener('click', () => this.shiftDays(1));
}
}
/**
* Подключение обработчика для кнопки "Сегодня"
* Вынесено в отдельный метод, чтобы можно было вызывать после полной отрисовки компонента
*/
attachTodayHandler() {
const todayBtn = this.container.parentElement.querySelector('.today-btn');
if (todayBtn) {
todayBtn.addEventListener('click', () => this.goToToday());
todayBtn.removeEventListener('click', this.boundGoToToday); // Удаляем предыдущий обработчик, если был
this.boundGoToToday = this.goToToday.bind(this);
todayBtn.addEventListener('click', this.boundGoToToday);
console.log('Today button handler attached');
} else {
console.error('Today button not found when trying to attach handler');
}
}
@@ -380,6 +394,14 @@ class DateCarousel {
* Переход к сегодняшней дате
*/
goToToday() {
console.log('Today button clicked');
// Проверяем существование элементов
if (!this.minInput || !this.maxInput) {
console.error('minInput or maxInput not found!');
return;
}
const today = new Date();
today.setHours(0, 0, 0, 0);
@@ -388,21 +410,30 @@ class DateCarousel {
this.minInput.value = formattedDate;
this.maxInput.value = formattedDate;
console.log(`Set values - Min: ${this.minInput.value}, Max: ${this.maxInput.value}`);
// Устанавливаем сегодняшнюю дату как центральную
this.centerDate = today;
// Сохраняем новую центральную дату без смещения
this.saveCenterDate();
// При нажатии на "Сегодня" устанавливаем центральную дату на сегодня без смещения
// и сохраняем без смещения, чтобы календарь центрировался на сегодня
this.saveCenterDate(0);
// Очищаем выделение с других кнопок и обновляем визуальное состояние
this.render();
// Обновляем обработчик после перерисовки
this.attachTodayHandler();
console.log(`Navigated to today: ${formattedDate}`);
// Автоматическая отправка формы
const form = this.minInput.closest('form');
if (form) {
console.log('Submitting form');
form.submit();
} else {
console.error('Form not found!');
}
}