Rework calendar
This commit is contained in:
@@ -6,7 +6,6 @@ import {
|
||||
sameDay,
|
||||
sameMonth,
|
||||
startOfDay,
|
||||
startOfMonth,
|
||||
startOfWeekMonday,
|
||||
} from '../services/dateUtils.js';
|
||||
import {
|
||||
@@ -15,16 +14,23 @@ import {
|
||||
newPlanEntryId,
|
||||
savePlans,
|
||||
} from '../services/planStore.js';
|
||||
import { dayHasAnyMeal } from '../services/planIngredients.js';
|
||||
import { showAppToast } from './toast.js';
|
||||
import {
|
||||
bindCalendarDayClicks,
|
||||
createCalendarTopbarHTML,
|
||||
createCalendarWeekdayHeaderHTML,
|
||||
formatCalendarMonthYear,
|
||||
formatCalendarSelectedDate,
|
||||
isCalendarOnToday,
|
||||
renderCalendarGrid,
|
||||
syncCalendarTodayButton,
|
||||
} from './mealCalendar.js?v=1';
|
||||
|
||||
function esc(s) {
|
||||
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||
}
|
||||
|
||||
const MONTHS_LONG = ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'];
|
||||
const MONTHS_SHORT = ['sty', 'lut', 'mar', 'kwi', 'maj', 'cze', 'lip', 'sie', 'wrz', 'paź', 'lis', 'gru'];
|
||||
const WEEKDAYS_SHORT = ['Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So', 'Nd'];
|
||||
const WEEKDAYS_LONG = ['Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota'];
|
||||
const slotLabel = Object.fromEntries(MEAL_SLOTS.map((s) => [s.id, s.label]));
|
||||
|
||||
/* ── HTML template ──────────────────────────────────── */
|
||||
@@ -47,16 +53,15 @@ export function getMealPlanEditorHTML() {
|
||||
</div>
|
||||
<div id="mpe-body" class="flex-1 min-h-0 overflow-y-auto no-scrollbar px-5 py-3">
|
||||
<div id="mpe-cal-section" class="hidden mb-4">
|
||||
<div class="flex items-center gap-1 mb-2">
|
||||
<button id="mpe-cal-prev" type="button" class="shrink-0 w-8 h-8 flex items-center justify-center rounded-full border border-gray-200 text-gray-700 hover:bg-gray-50 transition-colors"><i class="fas fa-chevron-left text-xs"></i></button>
|
||||
<p id="mpe-cal-title" class="flex-1 min-w-0 text-xs font-medium text-gray-900 text-center tabular-nums leading-none px-1 truncate"></p>
|
||||
<button id="mpe-cal-next" type="button" class="shrink-0 w-8 h-8 flex items-center justify-center rounded-full border border-gray-200 text-gray-700 hover:bg-gray-50 transition-colors"><i class="fas fa-chevron-right text-xs"></i></button>
|
||||
</div>
|
||||
<div class="flex items-center justify-center mb-2">
|
||||
<button id="mpe-cal-today" type="button" class="h-6 shrink-0 inline-flex items-center justify-center gap-1 rounded-md border border-gray-200 bg-white px-2.5 text-[10px] font-semibold text-gray-700 shadow-sm hover:bg-gray-50 hover:text-gray-900 transition-colors hidden"><i class="fas fa-calendar-day text-[9px] opacity-70"></i> Dziś</button>
|
||||
</div>
|
||||
<div class="grid grid-cols-7 gap-0.5 text-center text-[9px] font-medium text-gray-400 uppercase tracking-wide mb-0.5 leading-none">${WEEKDAYS_SHORT.map((d) => `<div>${d}</div>`).join('')}</div>
|
||||
<div id="mpe-cal-grid" class="grid grid-cols-7 gap-0.5"></div>
|
||||
${createCalendarTopbarHTML({
|
||||
titleId: 'mpe-cal-title',
|
||||
prevId: 'mpe-cal-prev',
|
||||
todayId: 'mpe-cal-today',
|
||||
nextId: 'mpe-cal-next',
|
||||
wrapperClass: 'mb-2 flex items-center gap-3',
|
||||
})}
|
||||
${createCalendarWeekdayHeaderHTML()}
|
||||
<div id="mpe-cal-grid" class="grid grid-cols-7 gap-1.5"></div>
|
||||
<button id="mpe-cal-toggle" type="button" class="w-full flex items-center justify-center py-1 mt-1 text-gray-400 hover:text-gray-600 transition-colors"><i id="mpe-cal-toggle-icon" class="fas fa-chevron-down text-[10px]"></i></button>
|
||||
<p class="text-[10px] font-bold text-gray-400 uppercase tracking-wider mt-3 mb-2">Pora posiłku</p>
|
||||
<div id="mpe-slot-chips" class="flex flex-wrap gap-1.5"></div>
|
||||
@@ -142,19 +147,6 @@ export function setupMealPlanEditor() {
|
||||
|
||||
/* ── Calendar ──────────────────────────────────── */
|
||||
|
||||
function calCell(d, today, month) {
|
||||
const sel = S.date && sameDay(d, S.date);
|
||||
const past = d.getTime() < today.getTime();
|
||||
const other = d.getMonth() !== month;
|
||||
let cls = 'flex flex-col items-center justify-center rounded-md text-xs font-medium transition-colors w-full min-h-10 py-1 gap-0.5 leading-tight ';
|
||||
if (sel) cls += 'bg-gray-900 text-white ';
|
||||
else if (past || other) cls += 'text-gray-300 ';
|
||||
else cls += 'text-gray-800 hover:bg-gray-100 ';
|
||||
if (!sel && !past && !other && sameDay(d, today)) cls += 'ring-1 ring-inset ring-gray-900 ';
|
||||
const inner = `<span>${d.getDate()}</span><span class="w-1 h-1"></span>`;
|
||||
return (past && !sel) ? `<div class="${cls}">${inner}</div>` : `<button type="button" class="mpe-cal-day ${cls}" data-ts="${d.getTime()}">${inner}</button>`;
|
||||
}
|
||||
|
||||
function renderCal() {
|
||||
const sec = document.getElementById('mpe-cal-section');
|
||||
if (!sec || !S.showCal) { sec?.classList.add('hidden'); return; }
|
||||
@@ -165,34 +157,34 @@ export function setupMealPlanEditor() {
|
||||
const icon = document.getElementById('mpe-cal-toggle-icon');
|
||||
if (!grid || !title) return;
|
||||
const today = startOfDay(new Date());
|
||||
const plans = loadPlans();
|
||||
const mode = S.calExpanded ? 'month' : 'week';
|
||||
|
||||
if (S.calExpanded) {
|
||||
const ms = startOfMonth(S.calDate);
|
||||
title.textContent = `${MONTHS_LONG[ms.getMonth()]} ${ms.getFullYear()}`;
|
||||
icon.className = 'fas fa-chevron-up text-[10px]';
|
||||
const first = startOfWeekMonday(ms);
|
||||
const cells = [];
|
||||
let d = new Date(first);
|
||||
for (let i = 0; i < 42; i++) { cells.push(new Date(d)); d = addDays(d, 1); }
|
||||
while (cells.length > 7 && cells.slice(-7).every((c) => c.getMonth() !== ms.getMonth())) cells.splice(-7);
|
||||
grid.innerHTML = cells.map((c) => calCell(c, today, ms.getMonth())).join('');
|
||||
todayBtn?.classList.toggle('hidden', sameMonth(today, S.calDate));
|
||||
} else {
|
||||
const ws = startOfWeekMonday(S.calDate);
|
||||
title.textContent = `${MONTHS_LONG[S.calDate.getMonth()]} ${S.calDate.getFullYear()}`;
|
||||
icon.className = 'fas fa-chevron-down text-[10px]';
|
||||
const cells = [];
|
||||
for (let i = 0; i < 7; i++) cells.push(addDays(ws, i));
|
||||
grid.innerHTML = cells.map((c) => calCell(c, today, S.calDate.getMonth())).join('');
|
||||
todayBtn?.classList.toggle('hidden', sameDay(startOfWeekMonday(today), ws));
|
||||
title.textContent = S.calExpanded ? formatCalendarMonthYear(S.calDate) : formatCalendarSelectedDate(S.date);
|
||||
if (icon) {
|
||||
icon.className = S.calExpanded ? 'fas fa-chevron-up text-[10px]' : 'fas fa-chevron-down text-[10px]';
|
||||
}
|
||||
syncCalendarTodayButton(
|
||||
todayBtn,
|
||||
isCalendarOnToday(mode, startOfWeekMonday(S.calDate), S.calDate, S.date),
|
||||
);
|
||||
|
||||
grid.querySelectorAll('.mpe-cal-day').forEach((btn) => {
|
||||
btn.addEventListener('click', () => {
|
||||
S.date = new Date(Number(btn.dataset.ts));
|
||||
S.calDate = new Date(S.date);
|
||||
renderCal();
|
||||
});
|
||||
renderCalendarGrid({
|
||||
gridEl: grid,
|
||||
mode,
|
||||
anchorDate: S.calDate,
|
||||
selectedDate: S.date,
|
||||
resolveDayState: (day, meta) => {
|
||||
const isSelected = S.date && sameDay(day, S.date);
|
||||
const isPast = day.getTime() < today.getTime();
|
||||
return {
|
||||
disabled: isPast && !isSelected,
|
||||
dimmed: (isPast || (meta.mode === 'month' && !meta.inCurrentMonth)) && !isSelected,
|
||||
showIndicator: meta.mode === 'month'
|
||||
? meta.inCurrentMonth && dayHasAnyMeal(plans, day)
|
||||
: dayHasAnyMeal(plans, day),
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -475,6 +467,11 @@ export function setupMealPlanEditor() {
|
||||
document.getElementById('mpe-close-btn')?.addEventListener('click', closeEditor);
|
||||
overlay?.addEventListener('click', (e) => { if (e.target === overlay) closeEditor(); });
|
||||
document.getElementById('mpe-confirm-btn')?.addEventListener('click', handleConfirm);
|
||||
bindCalendarDayClicks(document.getElementById('mpe-cal-grid'), (date) => {
|
||||
S.date = date;
|
||||
S.calDate = new Date(date);
|
||||
renderCal();
|
||||
});
|
||||
|
||||
document.getElementById('mpe-cal-prev')?.addEventListener('click', () => {
|
||||
if (!S.showCal) return;
|
||||
|
||||
Reference in New Issue
Block a user