-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (163 loc) · 7.25 KB
/
script.js
File metadata and controls
191 lines (163 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
document.addEventListener('DOMContentLoaded', () => {
const devotionalContent = {
title: document.getElementById('entryTitle'),
displayDateElement: document.getElementById('entryDisplayDate'),
devotional: document.getElementById('entryDevotional'), // KJV
esv: document.getElementById('entryEsv'),
esvBlock: document.getElementById('esvBlock'),
verseRef: document.getElementById('entryVerseRef'),
poem: document.getElementById('entryPoem')
};
const prevDayBtn = document.getElementById('prevDayBtn');
const nextDayBtn = document.getElementById('nextDayBtn');
const currentDisplayDateNav = document.getElementById('currentDisplayDate');
const datePicker = document.getElementById('datePicker');
const datePickerWrap = document.getElementById('datePickerWrap');
let allEntries = [];
let esvCache = {};
let currentDate = new Date();
// --- Data Loading ---
async function loadDevotionals() {
try {
// Fetch both data files in parallel
const [entriesResponse, esvResponse] = await Promise.all([
fetch('data/entries.json'),
fetch('data/esv_cache.json').catch(e => {
console.warn("Could not load ESV cache:", e);
return null;
})
]);
if (!entriesResponse.ok) {
throw new Error(`HTTP error loading entries! status: ${entriesResponse.status}`);
}
allEntries = await entriesResponse.json();
if (esvResponse && esvResponse.ok) {
try {
esvCache = await esvResponse.json();
} catch (e) {
console.error("Error parsing ESV cache:", e);
}
}
// Set date picker to today and load today's entry
const today = new Date();
datePicker.value = today.toLocaleDateString('en-CA');
currentDate = today;
displayEntryForDate(currentDate);
} catch (error) {
console.error("Could not load devotional data:", error);
devotionalContent.title.textContent = "Error loading data.";
devotionalContent.devotional.textContent = error.message; // Show specific error on screen
}
}
// --- Date Formatting and Finding ---
function getMMDD(date) {
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${month}${day}`;
}
// Formats date for display (e.g., "June 2")
function formatDisplayDate(date) {
return date.toLocaleDateString('en-US', { month: 'long', day: 'numeric' });
}
// Formats date for display in nav (e.g., "June 2, 2025")
function formatNavDate(date) {
return date.toLocaleDateString('en-US', { month: 'long', day: 'numeric' });
}
function findEntry(date) {
const mmdd = getMMDD(date);
return allEntries.find(entry => entry.mmdd === mmdd);
}
// --- Display Logic ---
function renderPoemLines(poemText) {
if (!poemText) {
devotionalContent.poem.textContent = '';
return;
}
const fragment = document.createDocumentFragment();
const lines = poemText.split(/\r?\n/);
lines.forEach((line) => {
const lineElement = document.createElement('div');
lineElement.classList.add('poem-line');
const cleanedLine = line.replace(/\r/g, '');
if (!cleanedLine.trim()) {
lineElement.classList.add('poem-line--blank');
}
lineElement.textContent = cleanedLine;
fragment.appendChild(lineElement);
});
devotionalContent.poem.replaceChildren(fragment);
}
function displayEntryForDate(date) {
const entry = findEntry(date);
currentDate = date; // Update the global current date being viewed
currentDisplayDateNav.textContent = formatNavDate(date);
// Update the date picker to reflect the currently viewed date.
// Using locale formatting avoids off-by-one errors from UTC conversion.
datePicker.value = date.toLocaleDateString('en-CA');
if (entry) {
const mmdd = entry.mmdd;
devotionalContent.title.textContent = entry.title || "Title not found";
devotionalContent.displayDateElement.textContent = entry.display_date || formatDisplayDate(date);
// KJV Verse (Always present)
devotionalContent.devotional.textContent = entry.bible_verse || "Verse not found.";
// ESV Verse (Check cache)
if (esvCache && esvCache[mmdd] && esvCache[mmdd].text) {
devotionalContent.esv.textContent = esvCache[mmdd].text;
devotionalContent.esvBlock.classList.remove('hidden');
} else {
devotionalContent.esv.textContent = "";
devotionalContent.esvBlock.classList.add('hidden');
}
devotionalContent.verseRef.textContent = entry.verse_ref || "";
if (entry.poem) {
renderPoemLines(entry.poem);
} else {
devotionalContent.poem.textContent = "Poem not found.";
}
} else {
devotionalContent.title.textContent = "No Entry Available";
devotionalContent.displayDateElement.textContent = formatDisplayDate(date);
devotionalContent.devotional.textContent = "There is no devotional entry for this day.";
devotionalContent.verseRef.textContent = "";
devotionalContent.esvBlock.classList.add('hidden');
devotionalContent.poem.textContent = "";
}
// Removed Reftagger call since it is no longer used
}
// --- Event Listeners ---
prevDayBtn.addEventListener('click', () => {
const newDate = new Date(currentDate);
newDate.setDate(currentDate.getDate() - 1);
displayEntryForDate(newDate);
});
nextDayBtn.addEventListener('click', () => {
const newDate = new Date(currentDate);
newDate.setDate(currentDate.getDate() + 1);
displayEntryForDate(newDate);
});
datePicker.addEventListener('change', () => {
const selectedDateValue = datePicker.value; // YYYY-MM-DD
if (selectedDateValue) {
// Date picker gives date in local timezone based on YYYY-MM-DD.
// new Date('YYYY-MM-DD') can have timezone issues (interprets as UTC midnight).
// Safer:
const parts = selectedDateValue.split('-');
const year = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1; // Month is 0-indexed in JS Date
const day = parseInt(parts[2], 10);
const selectedDate = new Date(year, month, day);
displayEntryForDate(selectedDate);
}
});
if (datePickerWrap) {
datePickerWrap.addEventListener('click', () => {
if (typeof datePicker.showPicker === 'function') {
datePicker.showPicker();
return;
}
datePicker.focus();
});
}
// Initial load
loadDevotionals();
});