-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
451 lines (383 loc) · 15.7 KB
/
script.js
File metadata and controls
451 lines (383 loc) · 15.7 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Theme handling
function initTheme() {
// SVG strings for theme icons
const moonSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"><path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z"/></svg>`;
const sunSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg>`;
const themeToggle = document.getElementById('theme-toggle');
function setTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
themeToggle.innerHTML = theme === 'dark' ? sunSvg : moonSvg;
}
const savedTheme = localStorage.getItem('theme');
const systemThemeDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const initialTheme = savedTheme || (systemThemeDark ? 'dark' : 'light');
setTheme(initialTheme);
themeToggle.addEventListener('click', () => {
const currentTheme = document.documentElement.getAttribute('data-theme');
setTheme(currentTheme === 'dark' ? 'light' : 'dark');
});
}
// Tag filtering functionality
function initTagFiltering() {
const tags = document.querySelectorAll('.tag');
const tableRows = document.querySelectorAll('.full-width-table tbody tr');
const entries = document.querySelectorAll('.entry');
let activeTag = null;
// Get current tag from URL
function getCurrentTag() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('tag');
}
// Update URL and history
function updateHistory(tag) {
const newUrl = tag
? `${window.location.pathname}?tag=${encodeURIComponent(tag)}`
: window.location.pathname;
history.pushState({ tag }, '', newUrl);
}
// Update content visibility based on tag
function updateContent(selectedTag) {
// Update table rows if they exist
if (tableRows.length > 0) {
tableRows.forEach(row => {
const rowTags = Array.from(row.querySelectorAll('.tag'))
.map(tag => tag.textContent.trim());
if (!selectedTag || rowTags.includes(selectedTag)) {
row.style.display = '';
} else {
row.style.display = 'none';
}
});
}
// Update entries if they exist
if (entries.length > 0) {
entries.forEach(entry => {
const entryTags = Array.from(entry.querySelectorAll('.tag'))
.map(tag => tag.textContent.trim());
if (!selectedTag || entryTags.includes(selectedTag)) {
entry.style.display = '';
} else {
entry.style.display = 'none';
}
});
}
}
// Update visual state of tags
function updateTagStates(selectedTag) {
tags.forEach(tag => {
if (tag.textContent.trim() === selectedTag) {
tag.classList.add('active');
} else {
tag.classList.remove('active');
}
});
}
// Handle tag clicks
tags.forEach(tag => {
tag.addEventListener('click', (e) => {
e.preventDefault();
const tagName = tag.textContent.trim();
// If we're in an entry page, navigate to index with tag
if (document.querySelector('.entry-tags')) {
window.location.href = `/updates/index.html?tag=${encodeURIComponent(tagName)}`;
return;
}
// Toggle tag if clicking active tag
if (activeTag === tagName) {
activeTag = null;
tags.forEach(t => t.classList.remove('active'));
updateHistory('');
} else {
activeTag = tagName;
updateTagStates(tagName);
updateHistory(tagName);
}
updateContent(activeTag);
});
});
// Handle browser back/forward
window.addEventListener('popstate', (event) => {
activeTag = event.state?.tag || getCurrentTag();
updateTagStates(activeTag);
updateContent(activeTag);
});
// Initial state from URL
const initialTag = getCurrentTag();
if (initialTag) {
activeTag = decodeURIComponent(initialTag);
updateTagStates(activeTag);
updateContent(activeTag);
}
}
// Lightbox functionality
function initLightbox() {
// Create lightbox elements dynamically
function createLightbox() {
// Check if lightbox already exists
if (document.getElementById('lightbox')) return;
// Create lightbox container
const lightbox = document.createElement('div');
lightbox.id = 'lightbox';
lightbox.className = 'lightbox';
lightbox.setAttribute('aria-hidden', 'true');
lightbox.setAttribute('role', 'dialog');
lightbox.setAttribute('aria-label', 'Image viewer');
// Create inner content wrapper
const lightboxContent = document.createElement('div');
lightboxContent.className = 'lightbox-content';
// Create close button
const closeBtn = document.createElement('button');
closeBtn.className = 'lightbox-close';
closeBtn.setAttribute('aria-label', 'Close image viewer');
closeBtn.innerHTML = '×';
// Create image element
const img = document.createElement('img');
img.className = 'lightbox-image';
img.alt = '';
// Create caption element
const caption = document.createElement('div');
caption.className = 'lightbox-caption';
// Create image counter element
const counter = document.createElement('div');
counter.className = 'lightbox-counter';
counter.setAttribute('aria-live', 'polite');
counter.setAttribute('aria-atomic', 'true');
// Create navigation buttons
const prevBtn = document.createElement('button');
prevBtn.className = 'lightbox-nav lightbox-prev';
prevBtn.setAttribute('aria-label', 'Previous image');
prevBtn.innerHTML = '‹';
const nextBtn = document.createElement('button');
nextBtn.className = 'lightbox-nav lightbox-next';
nextBtn.setAttribute('aria-label', 'Next image');
nextBtn.innerHTML = '›';
// Assemble lightbox
lightboxContent.appendChild(closeBtn);
lightboxContent.appendChild(counter);
lightboxContent.appendChild(prevBtn);
lightboxContent.appendChild(nextBtn);
lightboxContent.appendChild(img);
lightboxContent.appendChild(caption);
lightbox.appendChild(lightboxContent);
document.body.appendChild(lightbox);
return {
container: lightbox,
image: img,
caption: caption,
counter: counter,
close: closeBtn,
prev: prevBtn,
next: nextBtn
};
}
// Initialize lightbox
const lightboxElements = createLightbox();
let currentImageIndex = 0;
let lightboxImages = [];
// Find all images that should trigger lightbox
const images = document.querySelectorAll(
'article img:not(.no-lightbox), ' +
'.grid-item img:not(.no-lightbox), ' +
'.content-with-image img:not(.no-lightbox)'
);
// Process all images in DOM order
images.forEach((img, domIndex) => {
// Skip if explicitly excluded
if (img.classList.contains('no-lightbox')) return;
// Add placeholder to maintain DOM order
const imageIndex = lightboxImages.length;
lightboxImages.push({
src: img.src,
alt: img.alt,
caption: img.getAttribute('title') || img.alt || '',
element: img,
valid: true // Assume valid until proven otherwise
});
// Store the index on the element
img.dataset.lightboxIndex = imageIndex;
// Function to validate and setup image
const validateAndSetup = () => {
// Check if image is too small (likely an icon)
if (img.naturalWidth > 0 && img.naturalHeight > 0) {
if (img.naturalWidth < 100 && img.naturalHeight < 100) {
// Mark as invalid but keep in array to maintain indices
lightboxImages[imageIndex].valid = false;
return;
}
}
// Image is valid, make it clickable
img.style.cursor = 'zoom-in';
img.setAttribute('tabindex', '0');
img.setAttribute('role', 'button');
img.setAttribute('aria-label', 'Click to enlarge image');
// Add click handler
img.addEventListener('click', function(e) {
e.preventDefault();
const index = parseInt(this.dataset.lightboxIndex);
openLightbox(index);
});
// Add keyboard handler
img.addEventListener('keydown', function(e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
const index = parseInt(this.dataset.lightboxIndex);
openLightbox(index);
}
});
};
// Check if image is already loaded
if (img.complete && img.naturalWidth > 0) {
validateAndSetup();
} else {
// Wait for image to load
img.addEventListener('load', validateAndSetup);
img.addEventListener('error', () => {
lightboxImages[imageIndex].valid = false;
});
}
});
// Get valid images only (for navigation)
function getValidImages() {
return lightboxImages.filter(img => img.valid);
}
// Get the valid index from the full array index
function getValidIndex(fullIndex) {
let validCount = 0;
for (let i = 0; i <= fullIndex; i++) {
if (lightboxImages[i] && lightboxImages[i].valid) {
if (i === fullIndex) return validCount;
validCount++;
}
}
return 0;
}
// Get the full index from valid index
function getFullIndex(validIndex) {
let validCount = 0;
for (let i = 0; i < lightboxImages.length; i++) {
if (lightboxImages[i] && lightboxImages[i].valid) {
if (validCount === validIndex) return i;
validCount++;
}
}
return 0;
}
// Open lightbox function
function openLightbox(fullIndex) {
// Check if image is valid
if (!lightboxImages[fullIndex] || !lightboxImages[fullIndex].valid) return;
currentImageIndex = getValidIndex(fullIndex);
updateLightboxImage();
lightboxElements.container.classList.add('active');
lightboxElements.container.setAttribute('aria-hidden', 'false');
document.body.style.overflow = 'hidden';
// Focus management for accessibility
lightboxElements.close.focus();
// Update navigation visibility
updateNavigation();
}
// Close lightbox function
function closeLightbox() {
lightboxElements.container.classList.remove('active');
lightboxElements.container.setAttribute('aria-hidden', 'true');
document.body.style.overflow = '';
// Return focus to the image that opened the lightbox
const fullIndex = getFullIndex(currentImageIndex);
const triggerImage = lightboxImages[fullIndex]?.element;
if (triggerImage) triggerImage.focus();
}
// Update displayed image
function updateLightboxImage() {
const validImages = getValidImages();
const imageData = validImages[currentImageIndex];
if (!imageData) return;
lightboxElements.image.src = imageData.src;
lightboxElements.image.alt = imageData.alt;
lightboxElements.caption.textContent = imageData.caption;
// Update counter
lightboxElements.counter.textContent = `${currentImageIndex + 1}/${validImages.length}`;
}
// Update navigation buttons visibility
function updateNavigation() {
const validImages = getValidImages();
lightboxElements.prev.style.display = currentImageIndex > 0 ? 'block' : 'none';
lightboxElements.next.style.display = currentImageIndex < validImages.length - 1 ? 'block' : 'none';
// Hide counter if only one image
lightboxElements.counter.style.display = validImages.length > 1 ? 'block' : 'none';
}
// Navigate to previous image
function showPrevImage() {
const validImages = getValidImages();
if (currentImageIndex > 0) {
currentImageIndex--;
updateLightboxImage();
updateNavigation();
}
}
// Navigate to next image
function showNextImage() {
const validImages = getValidImages();
if (currentImageIndex < validImages.length - 1) {
currentImageIndex++;
updateLightboxImage();
updateNavigation();
}
}
// Event listeners
lightboxElements.close.addEventListener('click', closeLightbox);
lightboxElements.prev.addEventListener('click', showPrevImage);
lightboxElements.next.addEventListener('click', showNextImage);
// Close on image click
lightboxElements.image.addEventListener('click', closeLightbox);
// Close on background click
lightboxElements.container.addEventListener('click', function(e) {
if (e.target === this) {
closeLightbox();
}
});
// Keyboard navigation
document.addEventListener('keydown', function(e) {
if (!lightboxElements.container.classList.contains('active')) return;
switch(e.key) {
case 'Escape':
closeLightbox();
break;
case 'ArrowLeft':
showPrevImage();
break;
case 'ArrowRight':
showNextImage();
break;
}
});
// Touch gesture support for mobile
let touchStartX = 0;
let touchEndX = 0;
lightboxElements.container.addEventListener('touchstart', function(e) {
touchStartX = e.changedTouches[0].screenX;
});
lightboxElements.container.addEventListener('touchend', function(e) {
touchEndX = e.changedTouches[0].screenX;
handleSwipe();
});
function handleSwipe() {
const swipeThreshold = 50;
const diff = touchStartX - touchEndX;
if (Math.abs(diff) > swipeThreshold) {
if (diff > 0) {
// Swiped left - show next
showNextImage();
} else {
// Swiped right - show previous
showPrevImage();
}
}
}
}
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
initTheme();
initTagFiltering();
initLightbox();
});