feat: Реализована календарная лента из 9 дней для фильтрации заказов

Основные изменения:

**Компонент date_range_filter.html:**
- Заменены простые кнопки на горизонтальную ленту из 9 дней
- Добавлены стрелки навигации влево/вправо
- Скрытые поля дат для работы с django-filter

**Стили date_filter.css:**
- Дизайн календарной ленты с карточками дней
- Выделение сегодняшнего дня синим цветом
- Выделение выбранной даты зеленым цветом
- Hover-эффекты и анимации
- Адаптивность для мобильных устройств
- Стили для стрелок навигации

**Логика date_filter.js:**
- Класс DateCarousel для управления лентой
- Генерация 9 дней (±4 от центральной даты)
- Определение "Вчера/Сегодня/Завтра" для центральных 3 кнопок
- Отображение числа (01-31) и дня недели (ПН-ВС)
- Навигация стрелками (сдвиг на 1 день)
- Клик по дню устанавливает дату в оба поля фильтра
- Визуальная индикация выбранной даты

**Формат каждой кнопки:**
┌─────────┐
│ Сегодня │  ← Текст (если вчера/сегодня/завтра)
│   07    │  ← Число месяца
│   ЧТ    │  ← День недели
└─────────┘

**Поведение:**
- По умолчанию: сегодня в центре (5-я кнопка)
- Сегодняшний день выделен синим
- Клик по дню фильтрует заказы за эту конкретную дату
- Стрелки сдвигают весь диапазон на 1 день вперед/назад

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-07 18:33:48 +03:00
parent 48021da856
commit 1f0821efbe
3 changed files with 355 additions and 187 deletions

View File

@@ -1,5 +1,5 @@
/** /**
* Стили для календарного фильтра по датам * Стили для календарного фильтра с лентой из 9 дней
* Используется в компоненте date_range_filter.html * Используется в компоненте date_range_filter.html
*/ */
@@ -23,78 +23,185 @@
color: #0d6efd; color: #0d6efd;
} }
.date-range-filter .date-input { /* Календарная лента */
cursor: pointer; .date-carousel {
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
.date-range-filter .date-input:focus {
border-color: #0d6efd;
box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.15);
}
.date-range-filter .text-muted.small {
font-size: 0.75rem;
margin-bottom: 0.25rem;
}
/* Быстрые кнопки фильтров */
.quick-filters {
margin-top: 0.75rem;
}
.quick-filters .btn-group {
display: flex; display: flex;
gap: 0.25rem; align-items: center;
gap: 0.5rem;
} }
.quick-date-btn { .carousel-nav-btn {
font-size: 0.75rem; background: #fff;
padding: 0.375rem 0.5rem; border: 1px solid #dee2e6;
border-radius: 4px !important; border-radius: 50%;
width: 36px;
height: 36px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.2s ease; transition: all 0.2s ease;
flex-shrink: 0;
}
.carousel-nav-btn:hover {
background: #0d6efd;
border-color: #0d6efd;
color: white;
transform: scale(1.1);
}
.carousel-nav-btn:active {
transform: scale(0.95);
}
.carousel-nav-btn i {
font-size: 1.2rem;
}
/* Контейнер с днями */
.date-carousel-container {
display: flex;
gap: 0.5rem;
overflow-x: auto;
flex: 1; flex: 1;
white-space: nowrap; padding: 0.25rem 0;
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE/Edge */
} }
.quick-date-btn:hover { .date-carousel-container::-webkit-scrollbar {
background-color: #0d6efd; display: none; /* Chrome/Safari */
color: white; }
/* Кнопка дня */
.date-btn {
background: #fff;
border: 2px solid #dee2e6;
border-radius: 8px;
padding: 0.5rem;
min-width: 70px;
cursor: pointer;
transition: all 0.2s ease;
text-align: center;
display: flex;
flex-direction: column;
gap: 0.25rem;
flex-shrink: 0;
}
.date-btn:hover {
border-color: #0d6efd; border-color: #0d6efd;
transform: translateY(-1px); background: #f0f7ff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
} }
.quick-date-btn:active { .date-btn-label {
transform: translateY(0); font-size: 0.7rem;
box-shadow: none; font-weight: 600;
color: #6c757d;
text-transform: uppercase;
} }
.quick-date-btn.active { .date-btn-day {
background-color: #0d6efd; font-size: 1.5rem;
color: white; font-weight: 700;
color: #212529;
line-height: 1;
}
.date-btn-weekday {
font-size: 0.75rem;
font-weight: 500;
color: #6c757d;
text-transform: uppercase;
}
/* Сегодняшний день (активный) */
.date-btn.today {
background: #0d6efd;
border-color: #0d6efd; border-color: #0d6efd;
color: white;
}
.date-btn.today .date-btn-label,
.date-btn.today .date-btn-day,
.date-btn.today .date-btn-weekday {
color: white;
}
.date-btn.today:hover {
background: #0b5ed7;
border-color: #0b5ed7;
}
/* Выбранный день */
.date-btn.selected {
background: #198754;
border-color: #198754;
color: white;
}
.date-btn.selected .date-btn-label,
.date-btn.selected .date-btn-day,
.date-btn.selected .date-btn-weekday {
color: white;
} }
/* Адаптивность для мобильных устройств */ /* Адаптивность для мобильных устройств */
@media (max-width: 576px) { @media (max-width: 768px) {
.date-range-filter { .date-range-filter {
padding: 0.75rem; padding: 0.75rem;
} }
.quick-filters .btn-group { .date-carousel {
flex-wrap: wrap; gap: 0.25rem;
} }
.quick-date-btn { .carousel-nav-btn {
flex: 1 1 calc(33.333% - 0.25rem); width: 32px;
min-width: 0; height: 32px;
}
.date-btn {
min-width: 60px;
padding: 0.4rem;
}
.date-btn-label {
font-size: 0.65rem;
}
.date-btn-day {
font-size: 1.3rem;
}
.date-btn-weekday {
font-size: 0.7rem; font-size: 0.7rem;
padding: 0.35rem 0.4rem;
} }
} }
/* Анимация для визуальной обратной связи */ @media (max-width: 576px) {
.date-btn {
min-width: 50px;
padding: 0.3rem;
}
.date-btn-label {
font-size: 0.6rem;
}
.date-btn-day {
font-size: 1.1rem;
}
.date-btn-weekday {
font-size: 0.65rem;
}
}
/* Анимация при клике */
@keyframes pulse { @keyframes pulse {
0% { 0% {
transform: scale(1); transform: scale(1);
@@ -107,6 +214,6 @@
} }
} }
.quick-date-btn.clicked { .date-btn.clicked {
animation: pulse 0.3s ease; animation: pulse 0.3s ease;
} }

View File

@@ -1,89 +1,144 @@
/** /**
* Календарный фильтр для выбора диапазона дат * Календарная лента с 9 днями для фильтрации заказов
* Поддерживает быстрые фильтры (сегодня, завтра, неделя) * Сегодня в центре, навигация стрелками
*
* Использование:
* Подключить этот файл в шаблоне после компонента date_range_filter.html
*/ */
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
console.log('Date filter initialized'); console.log('Date carousel initialized');
const quickDateButtons = document.querySelectorAll('.quick-date-btn'); // Инициализация всех календарных лент на странице
const carousels = document.querySelectorAll('.date-carousel-container');
quickDateButtons.forEach(button => { carousels.forEach(container => {
button.addEventListener('click', function(e) { const minInputId = container.getAttribute('data-min-input');
e.preventDefault(); const maxInputId = container.getAttribute('data-max-input');
const period = this.getAttribute('data-period'); // Инициализация с сегодняшней датой в центре
const minInputId = this.getAttribute('data-min-input'); const carousel = new DateCarousel(container, minInputId, maxInputId);
const maxInputId = this.getAttribute('data-max-input'); carousel.init();
const minInput = document.getElementById(minInputId);
const maxInput = document.getElementById(maxInputId);
if (!minInput || !maxInput) {
console.error('Date inputs not found:', minInputId, maxInputId);
return;
}
const dates = getDateRange(period);
minInput.value = dates.min;
maxInput.value = dates.max;
// Визуальная обратная связь
this.classList.add('clicked');
setTimeout(() => this.classList.remove('clicked'), 300);
console.log(`Set date range: ${dates.min} - ${dates.max}`);
});
}); });
});
/** /**
* Вычисляет диапазон дат для выбранного периода * Класс для управления календарной лентой
* @param {string} period - период (today, tomorrow, week)
* @returns {Object} объект с min и max датами в формате YYYY-MM-DD
*/ */
function getDateRange(period) { class DateCarousel {
const today = new Date(); constructor(container, minInputId, maxInputId) {
const tomorrow = new Date(today); this.container = container;
tomorrow.setDate(tomorrow.getDate() + 1); this.minInputId = minInputId;
this.maxInputId = maxInputId;
let minDate, maxDate; this.minInput = document.getElementById(minInputId);
this.maxInput = document.getElementById(maxInputId);
switch(period) { this.centerDate = new Date(); // Центральная дата (по умолчанию сегодня)
case 'today': this.today = new Date();
minDate = maxDate = formatDate(today); this.today.setHours(0, 0, 0, 0);
break;
case 'tomorrow':
minDate = maxDate = formatDate(tomorrow);
break;
case 'week':
minDate = formatDate(today);
const weekEnd = new Date(today);
weekEnd.setDate(weekEnd.getDate() + 6);
maxDate = formatDate(weekEnd);
break;
case 'month':
minDate = formatDate(today);
const monthEnd = new Date(today);
monthEnd.setMonth(monthEnd.getMonth() + 1);
maxDate = formatDate(monthEnd);
break;
default:
minDate = maxDate = '';
}
return { min: minDate, max: maxDate };
} }
/** /**
* Форматирует дату в формат YYYY-MM-DD для input[type="date"] * Инициализация календарной ленты
* @param {Date} date - объект даты
* @returns {string} дата в формате YYYY-MM-DD
*/ */
function formatDate(date) { init() {
this.render();
this.attachNavHandlers();
}
/**
* Генерация и отображение 9 дней
*/
render() {
this.container.innerHTML = '';
const days = this.generateDays();
days.forEach(dayData => {
const btn = this.createDayButton(dayData);
this.container.appendChild(btn);
});
}
/**
* Генерация массива из 9 дней (±4 от центральной даты)
*/
generateDays() {
const days = [];
for (let i = -4; i <= 4; i++) {
const date = new Date(this.centerDate);
date.setDate(date.getDate() + i);
date.setHours(0, 0, 0, 0);
days.push({
date: date,
label: this.getDateLabel(date, i),
isToday: date.getTime() === this.today.getTime(),
isCenter: i === 0
});
}
return days;
}
/**
* Определение текстовой метки для даты
*/
getDateLabel(date, offset) {
if (offset === -1) return 'Вчера';
if (offset === 0 && date.getTime() === this.today.getTime()) return 'Сегодня';
if (offset === 1 && date.getTime() === new Date(this.today.getTime() + 86400000).getTime()) return 'Завтра';
return '';
}
/**
* Создание кнопки дня
*/
createDayButton(dayData) {
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'date-btn';
if (dayData.isToday) {
btn.classList.add('today');
}
// Проверка, выбрана ли эта дата
if (this.isDateSelected(dayData.date)) {
btn.classList.add('selected');
}
// Структура кнопки
const label = document.createElement('div');
label.className = 'date-btn-label';
label.textContent = dayData.label;
const day = document.createElement('div');
day.className = 'date-btn-day';
day.textContent = String(dayData.date.getDate()).padStart(2, '0');
const weekday = document.createElement('div');
weekday.className = 'date-btn-weekday';
weekday.textContent = this.getWeekdayShort(dayData.date);
btn.appendChild(label);
btn.appendChild(day);
btn.appendChild(weekday);
// Обработчик клика
btn.addEventListener('click', () => this.selectDate(dayData.date, btn));
return btn;
}
/**
* Получить короткое название дня недели (ПН, ВТ, СР, ЧТ, ПТ, СБ, ВС)
*/
getWeekdayShort(date) {
const weekdays = ['ВС', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ'];
return weekdays[date.getDay()];
}
/**
* Форматирование даты в YYYY-MM-DD
*/
formatDate(date) {
const year = date.getFullYear(); const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0');
@@ -91,41 +146,53 @@ document.addEventListener('DOMContentLoaded', function() {
} }
/** /**
* Валидация диапазона дат (начало <= конец) * Проверка, выбрана ли дата
*/ */
const dateInputs = document.querySelectorAll('.date-input'); isDateSelected(date) {
dateInputs.forEach(input => { const formattedDate = this.formatDate(date);
input.addEventListener('change', function() { return this.minInput.value === formattedDate && this.maxInput.value === formattedDate;
const container = this.closest('.date-range-filter');
if (!container) return;
const minInput = container.querySelector('.date-input[id$="_after"]');
const maxInput = container.querySelector('.date-input[id$="_before"]');
if (!minInput || !maxInput) return;
if (minInput.value && maxInput.value) {
const minDate = new Date(minInput.value);
const maxDate = new Date(maxInput.value);
if (minDate > maxDate) {
alert('Дата начала не может быть позже даты окончания');
this.value = '';
} }
}
});
});
/** /**
* Сброс дат при клике на кнопку "Сбросить" формы * Выбор даты (установка в оба поля фильтра)
*/ */
const resetButtons = document.querySelectorAll('a[href*="order-list"]:not([href*="?"])'); selectDate(date, btn) {
resetButtons.forEach(button => { const formattedDate = this.formatDate(date);
button.addEventListener('click', function() {
// Очищаем все date inputs this.minInput.value = formattedDate;
dateInputs.forEach(input => { this.maxInput.value = formattedDate;
input.value = '';
}); // Визуальная обратная связь
}); btn.classList.add('clicked');
}); setTimeout(() => btn.classList.remove('clicked'), 300);
});
// Обновление визуального состояния
this.render();
console.log(`Selected date: ${formattedDate}`);
}
/**
* Подключение обработчиков для стрелок навигации
*/
attachNavHandlers() {
const prevBtn = this.container.parentElement.querySelector('.carousel-prev');
const nextBtn = this.container.parentElement.querySelector('.carousel-next');
if (prevBtn) {
prevBtn.addEventListener('click', () => this.shiftDays(-1));
}
if (nextBtn) {
nextBtn.addEventListener('click', () => this.shiftDays(1));
}
}
/**
* Сдвиг диапазона дней на N дней
*/
shiftDays(offset) {
this.centerDate.setDate(this.centerDate.getDate() + offset);
this.render();
}
}

View File

@@ -1,5 +1,5 @@
{% comment %} {% comment %}
Переиспользуемый компонент для фильтрации по диапазону дат Переиспользуемый компонент для фильтрации по диапазону дат с календарной лентой
Параметры: Параметры:
- field_after: поле фильтра "от" (например, filter.form.delivery_date_after) - field_after: поле фильтра "от" (например, filter.form.delivery_date_after)
@@ -19,39 +19,33 @@
{{ label }} {{ label }}
</label> </label>
<div class="row g-2"> <!-- Скрытые поля для хранения дат -->
<div class="col-6"> <div class="d-none">
<label for="{{ field_after.id_for_label }}" class="form-label text-muted small">От</label>
{{ field_after }} {{ field_after }}
</div>
<div class="col-6">
<label for="{{ field_before.id_for_label }}" class="form-label text-muted small">До</label>
{{ field_before }} {{ field_before }}
</div> </div>
<!-- Календарная лента с 9 днями -->
<div class="date-carousel">
<button type="button" class="carousel-nav-btn carousel-prev"
data-min-input="{{ field_after.id_for_label }}"
data-max-input="{{ field_before.id_for_label }}"
title="Предыдущий день">
<i class="bi bi-chevron-left"></i>
</button>
<div class="date-carousel-container"
id="dateCarousel_{{ field_after.id_for_label }}"
data-min-input="{{ field_after.id_for_label }}"
data-max-input="{{ field_before.id_for_label }}">
<!-- Дни генерируются через JavaScript -->
</div> </div>
<!-- Быстрые фильтры --> <button type="button" class="carousel-nav-btn carousel-next"
<div class="quick-filters mt-2">
<small class="text-muted d-block mb-1">Быстрый выбор:</small>
<div class="btn-group btn-group-sm w-100" role="group">
<button type="button" class="btn btn-outline-secondary quick-date-btn"
data-min-input="{{ field_after.id_for_label }}" data-min-input="{{ field_after.id_for_label }}"
data-max-input="{{ field_before.id_for_label }}" data-max-input="{{ field_before.id_for_label }}"
data-period="today"> title="Следующий день">
Сегодня <i class="bi bi-chevron-right"></i>
</button> </button>
<button type="button" class="btn btn-outline-secondary quick-date-btn"
data-min-input="{{ field_after.id_for_label }}"
data-max-input="{{ field_before.id_for_label }}"
data-period="tomorrow">
Завтра
</button>
<button type="button" class="btn btn-outline-secondary quick-date-btn"
data-min-input="{{ field_after.id_for_label }}"
data-max-input="{{ field_before.id_for_label }}"
data-period="week">
Неделя
</button>
</div>
</div> </div>
</div> </div>