/** * Duty list (timeline) rendering. */ import { dutyListEl, state } from "./dom.js"; import { t } from "./i18n.js"; import { escapeHtml } from "./utils.js"; import { localDateString, firstDayOfMonth, lastDayOfMonth, dateKeyToDDMM, formatHHMM, formatDateKey } from "./dateUtils.js"; /** * Build HTML for one timeline duty card: one-day "DD.MM, HH:MM – HH:MM" or multi-day. * @param {object} d - Duty * @param {boolean} isCurrent - Whether this is "current" duty * @returns {string} */ export function dutyTimelineCardHtml(d, isCurrent) { const startLocal = localDateString(new Date(d.start_at)); const endLocal = localDateString(new Date(d.end_at)); const startDDMM = dateKeyToDDMM(startLocal); const endDDMM = dateKeyToDDMM(endLocal); const startTime = formatHHMM(d.start_at); const endTime = formatHHMM(d.end_at); let timeStr; if (startLocal === endLocal) { timeStr = startDDMM + ", " + startTime + " – " + endTime; } else { timeStr = startDDMM + " " + startTime + " – " + endDDMM + " " + endTime; } const lang = state.lang; const typeLabel = isCurrent ? t(lang, "duty.now_on_duty") : (t(lang, "event_type." + (d.event_type || "duty"))); const extraClass = isCurrent ? " duty-item--current" : ""; return ( '
' + t(state.lang, "duty.none_this_month") + "
"; return; } dutyListEl.classList.add("duty-timeline"); const current = state.current; const todayKey = localDateString(new Date()); const firstKey = localDateString(firstDayOfMonth(current)); const lastKey = localDateString(lastDayOfMonth(current)); const showTodayInMonth = todayKey >= firstKey && todayKey <= lastKey; const dateSet = new Set(); filtered.forEach((d) => { dateSet.add(localDateString(new Date(d.start_at))); }); if (showTodayInMonth) dateSet.add(todayKey); const dates = Array.from(dateSet).sort(); const now = new Date(); let fullHtml = ""; dates.forEach((date) => { const isToday = date === todayKey; const dayClass = "duty-timeline-day" + (isToday ? " duty-timeline-day--today" : ""); const dateLabel = dateKeyToDDMM(date); const dateCellHtml = isToday ? '' + escapeHtml(t(state.lang, "duty.today")) + '' + escapeHtml(dateLabel) + '' : '' + escapeHtml(dateLabel) + ""; const dayDuties = filtered .filter((d) => localDateString(new Date(d.start_at)) === date) .sort((a, b) => new Date(a.start_at) - new Date(b.start_at)); let dayHtml = ""; dayDuties.forEach((d) => { const start = new Date(d.start_at); const end = new Date(d.end_at); const isCurrent = start <= now && now < end; dayHtml += '