Jake Vanderwerf
2026-01-01 07282da9671de8fb2601e9e641decb2655439ad8
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// GalleryModal.js - Fullscreen gallery viewer component
class GalleryModal {
    constructor(items, initialIndex = 0) {
        this.items = items || [];
        this.currentIndex = initialIndex;
        this.touchStart = null;
        this.touchEnd = null;
        this.minSwipeDistance = 50;
        this.modal = null;
        this.keyHandler = null;
        this.loading = false;
    }
 
    /**
     * Show the gallery modal
     */
    show() {
        // Create modal if not already created
        if (!this.modal) {
            this.modal = this.createModal();
            document.body.appendChild(this.modal);
        }
 
        // Lock body scroll
        document.body.style.overflow = 'hidden';
 
        // Bind event handlers
        this.bindEvents();
 
        // Show current image
        this.updateDisplay();
 
        // Preload adjacent images
        this.preloadImages();
 
        // Announce to screen readers
        this.announceToScreenReaders();
    }
 
    /**
     * Create the modal element
     */
    createModal() {
        const modal = document.createElement('div');
        modal.className = 'gallery-modal';
        modal.setAttribute('role', 'dialog');
        modal.setAttribute('aria-modal', 'true');
        modal.setAttribute('aria-label', 'Image Gallery');
 
        modal.innerHTML = `
      <div class="gallery-overlay">
        <button class="gallery-close" aria-label="Close gallery">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <line x1="18" y1="6" x2="6" y2="18"></line>
            <line x1="6" y1="6" x2="18" y2="18"></line>
          </svg>
        </button>
 
        <button class="gallery-nav gallery-prev" aria-label="Previous image">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polyline points="15 18 9 12 15 6"></polyline>
          </svg>
        </button>
 
        <button class="gallery-nav gallery-next" aria-label="Next image">
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polyline points="9 18 15 12 9 6"></polyline>
          </svg>
        </button>
 
        <div class="gallery-content">
          <img src="" alt="" class="gallery-image">
          <details>
            <summary>DETAILS</summary>
            <div class="item-info"></div>
          </details>
        </div>
 
        <div class="gallery-favourite"></div>
        <div class="gallery-counter"></div>
 
        <div class="live-region" role="status" aria-live="polite" class="screen-reader-text"></div>
      </div>
    `;
 
        // Add styles if they don't exist
        this.ensureGalleryStyles();
 
        return modal;
    }
 
    /**
     * Ensure gallery styles are in the document
     */
    ensureGalleryStyles() {
        if (!document.getElementById('gallery-styles')) {
            const styles = document.createElement('style');
            styles.id = 'gallery-styles';
            styles.textContent = `
        .gallery-modal {
          position: fixed;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          z-index: 9999;
          background: rgba(27, 27, 27, 0.9);
          display: flex;
          align-items: center;
          justify-content: center;
        }
 
        .gallery-overlay {
          position: relative;
          width: 100%;
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
        }
 
        .gallery-content {
          position: relative;
          max-width: 100%;
          max-height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 2rem;
        }
 
        .gallery-favourite button.favourite {
          top: unset;
          bottom: 1rem;
          right: 1rem;
        }
 
        .gallery-image {
          max-width: 100%;
          max-height: calc(100vh - 4rem);
          object-fit: contain;
        }
 
        .gallery-close {
          position: absolute;
          top: 1rem;
          right: 1rem;
          background: none;
          border: none;
          color: white;
          cursor: pointer;
          padding: 0.5rem;
          z-index: 10;
          transition: color 0.3s ease;
        }
 
        .gallery-close:hover {
          color: #FF0080;
        }
 
        .gallery-nav {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          background: none;
          border: none;
          color: white;
          cursor: pointer;
          padding: 1rem;
          transition: color 0.3s ease;
        }
 
        .gallery-nav:hover {
          color: #FF0080;
        }
 
        .gallery-prev {
          left: 1rem;
        }
 
        .gallery-next {
          right: 1rem;
        }
 
        .gallery-counter {
          position: absolute;
          top: 1rem;
          left: 1rem;
          color: white;
          font-size: 0.875rem;
        }
 
        .gallery-content details {
          position: absolute;
          bottom: 1rem;
          left: 2rem;
          width: calc(100% - 4rem);
          padding: 0;
        }
 
        .gallery-content details summary {
          background-color: rgba(249,249,249,.2);
          backdrop-filter: blur(5px);
          border: none;
          cursor: pointer;
        }
 
        .gallery-content details:hover summary,
        .gallery-content details[open] summary {
          background-color: rgba(255,0,128,.4);
          backdrop-filter: blur(5px);
        }
 
        .gallery-content .item-info {
          background-color: rgba(249,249,249,.6);
          backdrop-filter: blur(5px);
        }
      `;
            document.head.appendChild(styles);
        }
    }
 
    /**
     * Bind event handlers
     */
    bindEvents() {
        // Close button
        this.modal.querySelector('.gallery-close').addEventListener('click', () => this.close());
 
        // Navigation buttons
        const prevBtn = this.modal.querySelector('.gallery-prev');
        const nextBtn = this.modal.querySelector('.gallery-next');
 
        prevBtn.addEventListener('click', () => this.navigate(-1));
        nextBtn.addEventListener('click', () => this.navigate(1));
 
        // Keyboard navigation
        this.keyHandler = (e) => {
            switch (e.key) {
                case 'ArrowLeft':
                    this.navigate(-1);
                    break;
                case 'ArrowRight':
                    this.navigate(1);
                    break;
                case 'Escape':
                    this.close();
                    break;
            }
        };
        document.addEventListener('keydown', this.keyHandler);
 
        // Touch events
        this.modal.addEventListener('touchstart', (e) => {
            this.touchStart = e.touches[0].clientX;
        });
 
        this.modal.addEventListener('touchmove', (e) => {
            this.touchEnd = e.touches[0].clientX;
        });
 
        this.modal.addEventListener('touchend', () => {
            if (!this.touchStart || !this.touchEnd) return;
 
            const distance = this.touchStart - this.touchEnd;
            const isLeftSwipe = distance > this.minSwipeDistance;
            const isRightSwipe = distance < -this.minSwipeDistance;
 
            if (isLeftSwipe) {
                this.navigate(1);
            } else if (isRightSwipe) {
                this.navigate(-1);
            }
 
            this.touchStart = null;
            this.touchEnd = null;
        });
    }
 
    /**
     * Navigate to previous/next image
     */
    async navigate(direction) {
        const newIndex = this.currentIndex + direction;
 
        // Check if out of bounds
        if (newIndex < 0 || newIndex >= this.items.length) {
            this.announceNavigation(direction > 0 ? 'last' : 'first');
            return;
        }
 
        // Update current index
        this.currentIndex = newIndex;
 
        // Update display
        this.updateDisplay();
 
        // Preload adjacent images
        this.preloadImages();
 
        // Announce to screen readers
        this.announceNavigation(direction > 0 ? 'next' : 'previous');
 
        // Trigger onNavigate callback if provided
        if (this.onNavigate) {
            this.onNavigate(this.currentIndex);
        }
 
        // Check if near the end and can load more
        if (direction > 0 && newIndex >= this.items.length - 3 && this.onLoadMore) {
            if (!this.loading) {
                this.loading = true;
                const loadedMore = await this.onLoadMore();
                this.loading = false;
 
                if (loadedMore) {
                    // Update navigation buttons
                    this.updateNavigationButtons();
                }
            }
        }
    }
 
    /**
     * Preload adjacent images
     */
    preloadImages() {
        // Preload current, previous and next images
        [-1, 0, 1].forEach(offset => {
            const index = this.currentIndex + offset;
            if (index >= 0 && index < this.items.length) {
                const img = new Image();
                const item = this.items[index];
 
                if (window.innerWidth < 1000) {
                    img.src = item.large || item.src;
                } else {
                    img.src = item.full || item.src;
                }
            }
        });
    }
 
    /**
     * Update display with current image
     */
    updateDisplay() {
        const item = this.items[this.currentIndex];
        if (!item) return;
 
        // Get elements
        const favourite = this.modal.querySelector('.gallery-favourite');
        const image = this.modal.querySelector('.gallery-image');
        const counter = this.modal.querySelector('.gallery-counter');
        const info = this.modal.querySelector('.item-info');
 
        // Update image
        image.src = window.innerWidth < 1000 ?
            (item.large || item.src) :
            (item.full || item.src);
 
        image.alt = item.alt || '';
 
        // Update favourite button
        if (favourite && item.fav) {
            favourite.innerHTML = '';
            favourite.appendChild(item.fav.cloneNode(true));
        }
 
        // Update info
        if (info && item.info) {
            info.innerHTML = '';
            const clone = item.info.cloneNode(true);
            info.appendChild(clone);
        }
 
        // Update counter
        counter.textContent = `${this.currentIndex + 1} / ${this.items.length}`;
 
        // Update navigation buttons
        this.updateNavigationButtons();
    }
 
    /**
     * Update navigation button visibility
     */
    updateNavigationButtons() {
        const prevBtn = this.modal.querySelector('.gallery-prev');
        const nextBtn = this.modal.querySelector('.gallery-next');
 
        prevBtn.style.display = this.currentIndex > 0 ? '' : 'none';
        nextBtn.style.display = this.currentIndex < this.items.length - 1 ? '' : 'none';
    }
 
    /**
     * Close the gallery
     */
    close() {
        // Remove event listeners
        document.removeEventListener('keydown', this.keyHandler);
 
        // Remove modal from DOM
        if (this.modal && this.modal.parentNode) {
            document.body.removeChild(this.modal);
        }
 
        // Restore body scroll
        document.body.style.overflow = '';
 
        // Reset state
        this.modal = null;
        this.keyHandler = null;
 
        // Dispatch close event
        document.dispatchEvent(new CustomEvent('galleryClose'));
 
        // Call onClose callback if provided
        if (this.onClose) {
            this.onClose();
        }
    }
 
    /**
     * Announce to screen readers
     */
    announceToScreenReaders() {
        const liveRegion = this.modal.querySelector('.live-region');
        if (liveRegion) {
            liveRegion.textContent = `Image ${this.currentIndex + 1} of ${this.items.length}. Use arrow keys to navigate.`;
        }
    }
 
 
    /**
     * Update gallery items
     * @param {Array} newItems - New gallery items
     */
    updateItems(newItems) {
        // Store original current index and item
        const currentItem = this.items[this.currentIndex];
 
        // Update items array
        this.items = newItems;
 
        // Try to keep the same item selected
        if (currentItem) {
            // Find the same item in the new array by matching source
            const newIndex = this.items.findIndex(item =>
                item.full === currentItem.full ||
                item.large === currentItem.large
            );
 
            if (newIndex !== -1) {
                this.currentIndex = newIndex;
            }
        }
 
        // Update navigation buttons
        this.updateNavigationButtons();
    }
 
 
    /**
     * Set callbacks
     * @param {Object} callbacks - Callback functions
     */
    setCallbacks(callbacks = {}) {
        const { onClose, onLoadMore, onNavigate } = callbacks;
 
        this.onClose = onClose;
        this.onLoadMore = onLoadMore;
        this.onNavigate = onNavigate;
    }
 
 
    /**
     * Ensure gallery is accessible
     */
    setupAccessibility() {
        if (!this.modal) return;
 
        // Add ARIA attributes
        this.modal.setAttribute('role', 'dialog');
        this.modal.setAttribute('aria-modal', 'true');
        this.modal.setAttribute('aria-label', 'Image Gallery');
 
        // Create live region for announcements
        this.liveRegion = document.createElement('div');
        this.liveRegion.setAttribute('aria-live', 'polite');
        this.liveRegion.setAttribute('role', 'status');
        this.liveRegion.className = 'screen-reader-text';
        this.modal.querySelector('.gallery-overlay').appendChild(this.liveRegion);
 
        // Announce initial state
        this.announceToScreenReader(`Image ${this.currentIndex + 1} of ${this.items.length}. Use arrow keys to navigate.`);
 
        // Set up focus trap
        const focusableElements = this.modal.querySelectorAll(
            'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
        );
        if (focusableElements.length) {
            focusableElements[0].focus();
            this.trapFocus(this.modal);
        }
    }
 
    /**
     * Announce to screen readers
     */
    announceToScreenReader(message) {
        if (!this.liveRegion) return;
        this.liveRegion.textContent = message;
    }
 
    /**
     * Trap focus within gallery modal
     */
    trapFocus(element) {
        const focusableElements = element.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');
        const firstFocusable = focusableElements[0];
        const lastFocusable = focusableElements[focusableElements.length - 1];
 
        element.addEventListener('keydown', function(e) {
            if (e.key === 'Tab') {
                // Shift+Tab on first element focuses last element
                if (e.shiftKey && document.activeElement === firstFocusable) {
                    lastFocusable.focus();
                    e.preventDefault();
                }
                // Tab on last element focuses first element
                else if (!e.shiftKey && document.activeElement === lastFocusable) {
                    firstFocusable.focus();
                    e.preventDefault();
                }
            }
        });
    }
    /**
     * Announce navigation to screen readers
     */
    announceNavigation(direction) {
        if (!this.liveRegion) return;
 
        if (direction === 'first') {
            this.liveRegion.textContent = 'At first image';
        } else if (direction === 'last') {
            this.liveRegion.textContent = 'At last image';
        } else {
            this.liveRegion.textContent = `Image ${this.currentIndex + 1} of ${this.items.length}`;
        }
    }
 
    /**
     * Set callbacks
     */
    setCallbacks(callbacks = {}) {
        const { onClose, onLoadMore, onNavigate } = callbacks;
 
        if (onClose) this.onClose = onClose;
        if (onLoadMore) this.onLoadMore = onLoadMore;
        if (onNavigate) this.onNavigate = onNavigate;
    }
}
 
export default GalleryModal;