|
7 | 7 | let calendar; |
8 | 8 | let currentEvents = []; |
9 | 9 | let currentBooks = {}; |
| 10 | +let monthlyDataCache = new Map(); // Cache for monthly data: month -> {events, books} |
| 11 | +let availableMonths = []; // List of months that have data |
| 12 | +let currentDisplayedMonth = null; |
10 | 13 |
|
11 | 14 | // Exported entry point |
12 | 15 | export function initializeCalendar() { |
13 | | - // Load calendar data from JSON |
14 | | - loadCalendarData().then(calendarData => { |
15 | | - if (calendarData) { |
16 | | - currentEvents = calendarData.events || []; |
17 | | - currentBooks = calendarData.books || {}; |
| 16 | + // First, load the list of available months |
| 17 | + loadAvailableMonths().then(() => { |
| 18 | + // Load calendar data for current month |
| 19 | + const now = new Date(); |
| 20 | + const currentMonth = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`; |
| 21 | + |
| 22 | + loadCalendarData(currentMonth).then(calendarData => { |
| 23 | + if (calendarData) { |
| 24 | + currentEvents = calendarData.events || []; |
| 25 | + currentBooks = calendarData.books || {}; |
| 26 | + currentDisplayedMonth = currentMonth; |
18 | 27 |
|
19 | | - initializeEventCalendar(currentEvents); |
| 28 | + initializeEventCalendar(currentEvents); |
20 | 29 |
|
21 | | - // Populate statistics widgets |
22 | | - updateCalendarStats(currentEvents); |
| 30 | + // Populate statistics widgets |
| 31 | + updateCalendarStats(currentEvents); |
23 | 32 |
|
24 | | - // Wire up DOM interaction handlers (today / prev / next / modal) |
25 | | - setupEventHandlers(); |
26 | | - } |
| 33 | + // Wire up DOM interaction handlers (today / prev / next / modal) |
| 34 | + setupEventHandlers(); |
| 35 | + } |
| 36 | + }); |
27 | 37 | }); |
28 | 38 | } |
29 | 39 |
|
30 | | -// Load calendar data from JSON file |
31 | | -async function loadCalendarData() { |
| 40 | +// Load the list of available months |
| 41 | +async function loadAvailableMonths() { |
| 42 | + try { |
| 43 | + const response = await fetch('/assets/json/calendar/available_months.json'); |
| 44 | + if (response.ok) { |
| 45 | + availableMonths = await response.json(); |
| 46 | + } else { |
| 47 | + console.warn('Could not load available months list'); |
| 48 | + availableMonths = []; |
| 49 | + } |
| 50 | + } catch (error) { |
| 51 | + console.warn('Error loading available months:', error); |
| 52 | + availableMonths = []; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Load and update calendar for a specific month |
| 57 | +async function loadAndUpdateCalendarForMonth(targetMonth) { |
| 58 | + // Skip if this month is already displayed |
| 59 | + if (currentDisplayedMonth === targetMonth) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + // Load the target month's data (will use cache if available) |
| 65 | + await loadCalendarData(targetMonth); |
| 66 | + currentDisplayedMonth = targetMonth; |
| 67 | + |
| 68 | + // Combine all cached events and books to show events from adjacent months |
| 69 | + currentEvents = []; |
| 70 | + currentBooks = {}; |
| 71 | + |
| 72 | + for (const [month, monthData] of monthlyDataCache) { |
| 73 | + // Add all events from all cached months |
| 74 | + currentEvents.push(...(monthData.events || [])); |
| 75 | + |
| 76 | + // Merge all books from all cached months |
| 77 | + Object.assign(currentBooks, monthData.books || {}); |
| 78 | + } |
| 79 | + |
| 80 | + // Update calendar events with all cached data |
| 81 | + if (calendar) { |
| 82 | + const mapEvents = evts => evts.map(ev => { |
| 83 | + const book = currentBooks[ev.book_id] || {}; |
| 84 | + return { |
| 85 | + id: ev.book_id, |
| 86 | + title: book.title || 'Unknown Book', |
| 87 | + start: ev.start, |
| 88 | + end: ev.end || ev.start, |
| 89 | + allDay: true, |
| 90 | + backgroundColor: book.color || getEventColor(ev), |
| 91 | + borderColor: book.color || getEventColor(ev), |
| 92 | + textColor: '#ffffff', |
| 93 | + extendedProps: { |
| 94 | + ...ev, |
| 95 | + book_title: book.title || 'Unknown Book', |
| 96 | + authors: book.authors || [], |
| 97 | + book_path: book.book_path, |
| 98 | + book_cover: book.book_cover, |
| 99 | + color: book.color, |
| 100 | + md5: ev.book_id |
| 101 | + } |
| 102 | + }; |
| 103 | + }); |
| 104 | + |
| 105 | + calendar.setOption('events', mapEvents(currentEvents)); |
| 106 | + } |
| 107 | + } catch (error) { |
| 108 | + console.error(`Failed to load calendar data for ${targetMonth}:`, error); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Load calendar data from monthly JSON files |
| 113 | +async function loadCalendarData(targetMonth = null) { |
32 | 114 | try { |
33 | | - const response = await fetch('/assets/json/calendar_data.json'); |
| 115 | + // If no target month specified, use current month |
| 116 | + if (!targetMonth) { |
| 117 | + const now = new Date(); |
| 118 | + targetMonth = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`; |
| 119 | + } |
| 120 | + |
| 121 | + // Check if month data is already cached |
| 122 | + if (monthlyDataCache.has(targetMonth)) { |
| 123 | + return monthlyDataCache.get(targetMonth); |
| 124 | + } |
| 125 | + |
| 126 | + // Check if this month has data available |
| 127 | + if (availableMonths.length > 0 && !availableMonths.includes(targetMonth)) { |
| 128 | + console.info(`No calendar data available for ${targetMonth}`); |
| 129 | + return { events: [], books: {} }; // Return empty data instead of null |
| 130 | + } |
| 131 | + |
| 132 | + const response = await fetch(`/assets/json/calendar/${targetMonth}.json`); |
34 | 133 | if (!response.ok) { |
35 | | - console.error('Failed to load calendar data:', response.status); |
36 | | - return null; |
| 134 | + console.error(`Failed to load calendar data for ${targetMonth}:`, response.status); |
| 135 | + return { events: [], books: {} }; |
37 | 136 | } |
38 | | - return await response.json(); |
| 137 | + |
| 138 | + const calendarData = await response.json(); |
| 139 | + |
| 140 | + // Cache the loaded data |
| 141 | + monthlyDataCache.set(targetMonth, calendarData); |
| 142 | + |
| 143 | + return calendarData; |
39 | 144 | } catch (error) { |
40 | | - console.error('Error loading calendar data:', error); |
41 | | - return null; |
| 145 | + console.error(`Error loading calendar data for ${targetMonth}:`, error); |
| 146 | + return { events: [], books: {} }; |
42 | 147 | } |
43 | 148 | } |
44 | 149 |
|
@@ -104,6 +209,10 @@ function initializeEventCalendar(events) { |
104 | 209 | updateCalendarTitleDirect(viewTitle); |
105 | 210 | updateMonthlyStats(currentMonthDate); |
106 | 211 |
|
| 212 | + // Load data for the new month if it's different from current data |
| 213 | + const newMonth = `${currentMonthDate.getFullYear()}-${String(currentMonthDate.getMonth() + 1).padStart(2, '0')}`; |
| 214 | + loadAndUpdateCalendarForMonth(newMonth); |
| 215 | + |
107 | 216 | // Scroll current day into view if needed |
108 | 217 | setTimeout(() => scrollCurrentDayIntoView(), 100); |
109 | 218 | } |
@@ -302,10 +411,25 @@ function updateMonthlyStats(currentDate) { |
302 | 411 | // Count unique books (using book_id) |
303 | 412 | uniqueBooks.add(event.book_id); |
304 | 413 |
|
305 | | - // Count unique dates (format as YYYY-MM-DD) |
306 | | - const eventDate = new Date(event.start); |
307 | | - const dateString = eventDate.toISOString().split('T')[0]; |
308 | | - uniqueDates.add(dateString); |
| 414 | + // Count all dates within the event's date range |
| 415 | + const startDate = new Date(event.start); |
| 416 | + const endDate = event.end ? new Date(event.end) : new Date(event.start); |
| 417 | + |
| 418 | + // If end date exists, it's exclusive, so we subtract 1 day |
| 419 | + if (event.end) { |
| 420 | + endDate.setDate(endDate.getDate() - 1); |
| 421 | + } |
| 422 | + |
| 423 | + // Iterate through all dates from start to end (inclusive) |
| 424 | + const currentDate = new Date(startDate); |
| 425 | + while (currentDate <= endDate) { |
| 426 | + // Check if this date is in the target month/year |
| 427 | + if (currentDate.getMonth() === targetMonth && currentDate.getFullYear() === targetYear) { |
| 428 | + const dateString = currentDate.toISOString().split('T')[0]; |
| 429 | + uniqueDates.add(dateString); |
| 430 | + } |
| 431 | + currentDate.setDate(currentDate.getDate() + 1); |
| 432 | + } |
309 | 433 |
|
310 | 434 | // Sum pages and time |
311 | 435 | totalPages += event.total_pages_read || 0; |
|
0 commit comments