-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
333 lines (281 loc) · 10.9 KB
/
script.js
File metadata and controls
333 lines (281 loc) · 10.9 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
let journalData = [];
// Theme management
function initTheme() {
const savedTheme = localStorage.getItem("journal-theme") || "dark";
setTheme(savedTheme);
}
function setTheme(theme) {
const body = document.body;
const themeToggle = document.getElementById("themeToggle");
body.className = theme;
if (theme === "dark") {
themeToggle.innerHTML = "🌙";
themeToggle.title = "Switch to light theme";
} else {
themeToggle.innerHTML = "☀️";
themeToggle.title = "Switch to dark theme";
}
localStorage.setItem("journal-theme", theme);
}
function toggleTheme() {
const currentTheme = document.body.className;
const newTheme = currentTheme === "dark" ? "light" : "dark";
setTheme(newTheme);
}
// Function to display messages to the user
function showMessage(message, type = "success") {
const messageBox = document.getElementById("messageBox");
messageBox.textContent = message;
messageBox.className = "message-box"; // Reset classes
if (type === "error") {
messageBox.style.backgroundColor = "#f44336"; // Red for error
} else {
messageBox.style.backgroundColor = "#4CAF50"; // Green for success
}
messageBox.classList.add("show");
setTimeout(() => {
messageBox.classList.remove("show");
}, 3000); // Hide after 3 seconds
}
// Function to extract date from timestamp
function extractDate(timestamp) {
return timestamp.split(" ")[0]; // Assumes format like "2024-01-15 10:30:00"
}
// Function to format date for display
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString("en-US", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
});
}
// Function to extract time from timestamp
function extractTime(timestamp) {
const timePart = timestamp.split(" ")[1];
if (timePart) {
return timePart.substring(0, 5); // Return HH:MM format
}
return "";
}
// Function to render main journal entries
function renderMainEntries() {
const journalEntriesContainer =
document.getElementById("journalEntries");
journalEntriesContainer.innerHTML = "";
if (journalData.length === 0) {
journalEntriesContainer.innerHTML =
'<p class="text-center text-secondary text-lg mt-4">No journal entries found yet.</p>';
return;
}
// Sort entries in reverse chronological order (most recent first)
const sortedData = [...journalData].sort((a, b) => {
const dateA = new Date(a.timestamp);
const dateB = new Date(b.timestamp);
return dateB - dateA; // Sort descending
});
// Render each journal entry
sortedData.forEach((entry) => {
const entryDiv = document.createElement("div");
entryDiv.className = "journal-entry";
const timestampDiv = document.createElement("div");
timestampDiv.className = "entry-timestamp";
timestampDiv.textContent = entry.timestamp;
const contentDiv = document.createElement("div");
contentDiv.className = "entry-content";
contentDiv.innerHTML = entry.entry;
entryDiv.appendChild(timestampDiv);
entryDiv.appendChild(contentDiv);
journalEntriesContainer.appendChild(entryDiv);
// Enhance iframes in this entry
enhanceIframes(contentDiv);
});
}
// Function to render date list
function renderDateList() {
const datesList = document.getElementById("datesList");
datesList.innerHTML = "";
// Group entries by date
const entriesByDate = {};
journalData.forEach((entry) => {
const date = extractDate(entry.timestamp);
if (!entriesByDate[date]) {
entriesByDate[date] = [];
}
entriesByDate[date].push(entry);
});
// Sort dates in reverse chronological order
const sortedDates = Object.keys(entriesByDate).sort(
(a, b) => new Date(b) - new Date(a)
);
sortedDates.forEach((date) => {
const dateItem = document.createElement("div");
dateItem.className = "date-item";
dateItem.onclick = () => showDayView(date, entriesByDate[date]);
const dateInfo = document.createElement("div");
dateInfo.innerHTML = `
<div class="text-lg font-semibold text-primary">${formatDate(
date
)}</div>
<div class="text-sm text-secondary">${date}</div>
`;
const entryCount = document.createElement("div");
entryCount.className = "text-sm text-muted font-medium";
entryCount.textContent = `${entriesByDate[date].length} ${entriesByDate[date].length === 1 ? "entry" : "entries"
}`;
dateItem.appendChild(dateInfo);
dateItem.appendChild(entryCount);
datesList.appendChild(dateItem);
});
}
// Function to show day view
function showDayView(date, entries) {
const fullScreenView = document.getElementById("fullScreenView");
const dayContent = document.getElementById("dayContent");
// Sort entries for this day in chronological order
const sortedEntries = entries.sort(
(a, b) => new Date(a.timestamp) - new Date(b.timestamp)
);
dayContent.innerHTML = `
<div class="day-header">
<h1 class="text-3xl font-bold mb-2 responsive-subtitle">${formatDate(
date
)}</h1>
<p class="text-lg opacity-90">${sortedEntries.length} ${sortedEntries.length === 1 ? "entry" : "entries"
} for this day</p>
</div>
`;
sortedEntries.forEach((entry) => {
const entryDiv = document.createElement("div");
entryDiv.className = "day-entry";
const timeDiv = document.createElement("div");
timeDiv.className = "day-entry-time";
timeDiv.textContent =
extractTime(entry.timestamp) || "No time specified";
const contentDiv = document.createElement("div");
contentDiv.className = "entry-content";
contentDiv.innerHTML = entry.entry;
entryDiv.appendChild(timeDiv);
entryDiv.appendChild(contentDiv);
dayContent.appendChild(entryDiv);
// Enhance iframes in this entry
enhanceIframes(contentDiv);
});
fullScreenView.style.display = "block";
document.body.style.overflow = "hidden"; // Prevent background scrolling
}
// Function to hide day view
function hideDayView() {
const fullScreenView = document.getElementById("fullScreenView");
fullScreenView.style.display = "none";
document.body.style.overflow = "auto"; // Restore background scrolling
}
// Function to show date list view
function showDateListView() {
document.getElementById("mainView").style.display = "none";
document.getElementById("dateListView").style.display = "block";
renderDateList();
}
// Function to show main view
function showMainView() {
document.getElementById("mainView").style.display = "block";
document.getElementById("dateListView").style.display = "none";
hideDayView();
}
document.addEventListener("DOMContentLoaded", () => {
// Initialize theme
initTheme();
const loadingMessage = document.getElementById("loadingMessage");
const jsonFilePath = "journal.json";
// Event listeners
document
.getElementById("themeToggle")
.addEventListener("click", toggleTheme);
document
.getElementById("viewByDateBtn")
.addEventListener("click", showDateListView);
document
.getElementById("allEntriesBtn")
.addEventListener("click", showMainView);
document
.getElementById("backToMainBtn")
.addEventListener("click", showMainView);
document
.getElementById("backToDateListBtn")
.addEventListener("click", () => {
hideDayView();
showDateListView();
});
// Load journal data
fetch(jsonFilePath)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then((data) => {
loadingMessage.style.display = "none";
journalData = data;
renderMainEntries();
showMessage("Journal entries loaded successfully!");
})
.catch((error) => {
loadingMessage.textContent = "Failed to load journal entries.";
loadingMessage.className = "text-center text-red-600 text-lg mt-4";
console.error("Error fetching or parsing journal.json:", error);
showMessage(
`Error: ${error.message}. Make sure 'journal.json' is accessible and valid JSON, and you are running a local server.`,
"error"
);
});
});
// Add this function to your <script> section
function enhanceIframes(container) {
const iframes = container.querySelectorAll("iframe");
iframes.forEach((iframe) => {
// Skip if already wrapped
if (iframe.parentElement.classList.contains("iframe-preview")) return;
// Create preview wrapper
const preview = document.createElement("div");
preview.className = "iframe-preview";
preview.title = "Click to enlarge preview";
// Clone the iframe for preview (smaller size)
const previewIframe = iframe.cloneNode(true);
previewIframe.style.width = "100%";
previewIframe.style.height = "200px";
previewIframe.style.pointerEvents = "none"; // Prevent interaction in preview
preview.appendChild(previewIframe);
// Replace original iframe with preview
iframe.parentElement.replaceChild(preview, iframe);
// On click, show modal
preview.addEventListener("click", () => {
showIframeModal(iframe);
});
});
}
function showIframeModal(iframe) {
const modal = document.getElementById("iframeModal");
modal.innerHTML = `
<div class="iframe-modal-overlay">
<div class="iframe-modal-content">
<button class="iframe-modal-close" title="Close">×</button>
<iframe src="${iframe.src}" allowfullscreen></iframe>
</div>
</div>
`;
modal.style.display = "block";
// Close button
modal.querySelector(".iframe-modal-close").onclick = () => {
modal.style.display = "none";
modal.innerHTML = "";
};
// Close on overlay click
modal.querySelector(".iframe-modal-overlay").onclick = (e) => {
if (e.target === modal.querySelector(".iframe-modal-overlay")) {
modal.style.display = "none";
modal.innerHTML = "";
}
};
}