2026-03-22 12:48:20 +03:00
|
|
|
|
const MOSCOW_TIMEZONE = 'Europe/Moscow';
|
|
|
|
|
|
const WEEKDAYS = ['Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота', 'Воскресенье'];
|
|
|
|
|
|
const MONTHS = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'];
|
2025-12-30 12:23:42 +03:00
|
|
|
|
|
2026-03-22 12:48:20 +03:00
|
|
|
|
function getMoscowDateString() {
|
2025-12-30 12:23:42 +03:00
|
|
|
|
const formatter = new Intl.DateTimeFormat('en-CA', {
|
2026-03-22 12:48:20 +03:00
|
|
|
|
timeZone: MOSCOW_TIMEZONE,
|
2025-12-30 12:23:42 +03:00
|
|
|
|
year: 'numeric',
|
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
|
day: '2-digit'
|
|
|
|
|
|
});
|
2026-03-22 12:48:20 +03:00
|
|
|
|
return formatter.format(new Date());
|
2025-12-30 12:23:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-22 12:48:20 +03:00
|
|
|
|
function parseISODate(value) {
|
|
|
|
|
|
const [year, month, day] = value.split('-').map(Number);
|
|
|
|
|
|
return new Date(year, month - 1, day);
|
2025-12-30 12:23:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-22 12:48:20 +03:00
|
|
|
|
function formatISODate(date) {
|
|
|
|
|
|
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function addDays(dateStr, days) {
|
|
|
|
|
|
const date = parseISODate(dateStr);
|
|
|
|
|
|
date.setDate(date.getDate() + days);
|
|
|
|
|
|
return formatISODate(date);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function formatDate(dateStr) {
|
|
|
|
|
|
const date = parseISODate(dateStr);
|
|
|
|
|
|
return `${WEEKDAYS[(date.getDay() + 6) % 7]}, ${date.getDate()} ${MONTHS[date.getMonth()]}`;
|
2025-12-30 12:23:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function calculateEndTime(startTime, durationMin) {
|
|
|
|
|
|
const [hour, minute] = startTime.split(':').map(Number);
|
|
|
|
|
|
const totalMinutes = hour * 60 + minute + durationMin;
|
|
|
|
|
|
const endHour = Math.floor(totalMinutes / 60);
|
|
|
|
|
|
const endMinute = totalMinutes % 60;
|
|
|
|
|
|
return `${String(endHour).padStart(2, '0')}:${String(endMinute).padStart(2, '0')}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-22 12:48:20 +03:00
|
|
|
|
function renderDay(container, titleEl, dateStr, items, options = {}) {
|
|
|
|
|
|
let titleText = formatDate(dateStr);
|
|
|
|
|
|
if (options.today) {
|
|
|
|
|
|
titleText = `Сегодня, ${titleText.toLowerCase()}`;
|
|
|
|
|
|
} else if (options.tomorrow) {
|
|
|
|
|
|
titleText = `Завтра, ${titleText.toLowerCase()}`;
|
2025-12-30 12:23:42 +03:00
|
|
|
|
}
|
2026-03-22 12:48:20 +03:00
|
|
|
|
titleEl.textContent = titleText;
|
|
|
|
|
|
|
|
|
|
|
|
const tasks = items.filter((item) => item.kind === 'task');
|
|
|
|
|
|
const events = items
|
|
|
|
|
|
.filter((item) => item.kind === 'event')
|
|
|
|
|
|
.sort((a, b) => a.start_time.localeCompare(b.start_time));
|
|
|
|
|
|
|
|
|
|
|
|
if (!tasks.length && !events.length) {
|
|
|
|
|
|
container.innerHTML = '<div class="empty-message">На этот день расписания нет.</div>';
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const tasksHtml = tasks.length ? `
|
|
|
|
|
|
<div class="tasks-list">
|
|
|
|
|
|
${tasks.map((task) => `<div class="task-item">${task.repeat_weekly ? 'Каждую неделю: ' : ''}${escapeHtml(task.title)}</div>`).join('')}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
` : '';
|
|
|
|
|
|
|
|
|
|
|
|
const eventsHtml = events.length ? `
|
|
|
|
|
|
<div class="events-list">
|
|
|
|
|
|
${events.map((event) => `
|
|
|
|
|
|
<div class="event-item">
|
|
|
|
|
|
<span class="event-time">${event.start_time}-${calculateEndTime(event.start_time, event.duration_min)}</span>
|
|
|
|
|
|
<span class="event-title">${escapeHtml(event.title)}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
`).join('')}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
` : '';
|
|
|
|
|
|
|
|
|
|
|
|
container.innerHTML = `${tasksHtml}${eventsHtml}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function escapeHtml(value) {
|
|
|
|
|
|
return String(value)
|
|
|
|
|
|
.replaceAll('&', '&')
|
|
|
|
|
|
.replaceAll('<', '<')
|
|
|
|
|
|
.replaceAll('>', '>')
|
|
|
|
|
|
.replaceAll('"', '"')
|
|
|
|
|
|
.replaceAll("'", ''');
|
2025-12-30 12:23:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-22 12:48:20 +03:00
|
|
|
|
async function loadSchedule() {
|
|
|
|
|
|
const today = getMoscowDateString();
|
|
|
|
|
|
const tomorrow = addDays(today, 1);
|
2025-12-30 12:23:42 +03:00
|
|
|
|
|
2026-03-22 12:48:20 +03:00
|
|
|
|
const response = await fetch(`/api/schedule?from_date=${today}&to_date=${tomorrow}`);
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
throw new Error('Не удалось загрузить расписание');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
renderDay(
|
|
|
|
|
|
document.getElementById('today-content'),
|
|
|
|
|
|
document.getElementById('today-title'),
|
|
|
|
|
|
today,
|
|
|
|
|
|
data.items.filter((item) => item.date === today),
|
|
|
|
|
|
{ today: true }
|
|
|
|
|
|
);
|
|
|
|
|
|
renderDay(
|
|
|
|
|
|
document.getElementById('tomorrow-content'),
|
|
|
|
|
|
document.getElementById('tomorrow-title'),
|
|
|
|
|
|
tomorrow,
|
|
|
|
|
|
data.items.filter((item) => item.date === tomorrow),
|
|
|
|
|
|
{ tomorrow: true }
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-12-30 12:23:42 +03:00
|
|
|
|
|
2026-03-22 12:48:20 +03:00
|
|
|
|
loadSchedule().catch((error) => console.error(error));
|
|
|
|
|
|
setInterval(() => loadSchedule().catch((error) => console.error(error)), 5 * 60 * 1000);
|