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