-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery.js
More file actions
244 lines (216 loc) · 9.32 KB
/
Copy pathgallery.js
File metadata and controls
244 lines (216 loc) · 9.32 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
// YKSINKERTAINEN JA TOIMIVA SCREENSHOT GALLERIA + VIDEO SUPPORT
document.addEventListener('DOMContentLoaded', function() {
// Etsi modal
const modal = document.getElementById('imageModal');
const imageCards = document.querySelectorAll('.image-card');
let currentImageIndex = 0;
if (!modal || imageCards.length === 0) {
console.log('Gallery elements not found');
return;
}
// Prevent scrolling when modal is open on mobile
function preventScroll(e) {
if (modal.classList.contains('active')) {
e.preventDefault();
}
}
// Close-funktio
function closeModal() {
modal.classList.remove('active');
modal.style.display = 'none';
document.body.style.overflow = '';
// Remove scroll prevention
document.removeEventListener('touchmove', preventScroll);
document.removeEventListener('wheel', preventScroll);
}
// Modal opening function
function openImageModal(card, index) {
// Tallenna nykyinen indeksi
if (index !== undefined) {
currentImageIndex = index;
}
// Hae tiedot
const dataImage = card.dataset.image || '';
const imgTitle = card.dataset.title || '';
const imgDesc = card.dataset.desc || '';
// Etsi modal-content
const modalContent = modal.querySelector('.modal-content');
if (!modalContent) return;
// Tarkista onko video vai kuva
if (dataImage === 'youtube-demo' || dataImage.includes('youtube')) {
// VIDEO - etsi iframe kortista
const iframe = card.querySelector('iframe');
if (iframe) {
const videoSrc = iframe.src;
modalContent.innerHTML = `
<span class="close">×</span>
<button class="modal-nav-btn modal-prev" aria-label="Previous">‹</button>
<button class="modal-nav-btn modal-next" aria-label="Next">›</button>
<div style="position: relative; width: 100%; max-width: 1000px; margin: 0 auto;">
<iframe src="${videoSrc}"
style="width: 100%; height: 600px; border: none;"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
</div>
<div class="modal-info" style="${imgTitle || imgDesc ? '' : 'display: none;'}">
<h3></h3>
<p></p>
</div>
`;
// XSS-suojaus: käytä textContent, älä innerHTML
const modalInfo = modalContent.querySelector('.modal-info');
if (modalInfo) {
const h3 = modalInfo.querySelector('h3');
const p = modalInfo.querySelector('p');
if (h3) h3.textContent = imgTitle || '';
if (p) p.textContent = imgDesc || '';
}
}
} else {
// KUVA
const imgElement = card.querySelector('img');
const imgSrc = dataImage || (imgElement ? imgElement.src : '');
if (imgSrc) {
modalContent.innerHTML = `
<span class="close">×</span>
<button class="modal-nav-btn modal-prev" aria-label="Previous">‹</button>
<button class="modal-nav-btn modal-next" aria-label="Next">›</button>
<img src="" alt="" style="width: 100%; height: auto; max-height: 80vh; object-fit: contain; display: block; margin: 0 auto;">
<div class="modal-info" style="${imgTitle || imgDesc ? '' : 'display: none;'}">
<h3></h3>
<p></p>
</div>
`;
const modalImg = modalContent.querySelector('img');
const modalInfo = modalContent.querySelector('.modal-info');
if (modalImg) {
modalImg.src = imgSrc;
modalImg.alt = imgTitle || '';
}
// XSS-suojaus: käytä textContent, älä innerHTML
if (modalInfo) {
const h3 = modalInfo.querySelector('h3');
const p = modalInfo.querySelector('p');
if (h3) h3.textContent = imgTitle || '';
if (p) p.textContent = imgDesc || '';
}
}
}
// Näytä modal
modal.classList.add('active');
modal.style.display = 'block';
document.body.style.overflow = 'hidden';
// Lisää close event uudelle napille
const newCloseBtn = modal.querySelector('.close');
if (newCloseBtn) {
newCloseBtn.addEventListener('click', closeModal);
}
// Lisää navigaatio-napit
const prevBtn = modal.querySelector('.modal-prev');
const nextBtn = modal.querySelector('.modal-next');
if (prevBtn) {
prevBtn.onclick = function(e) {
e.stopPropagation();
navigateImage(-1);
};
// Piilota jos ensimmäinen kuva
prevBtn.style.display = currentImageIndex === 0 ? 'none' : 'block';
}
if (nextBtn) {
nextBtn.onclick = function(e) {
e.stopPropagation();
navigateImage(1);
};
// Piilota jos viimeinen kuva
nextBtn.style.display = currentImageIndex === imageCards.length - 1 ? 'none' : 'block';
}
}
// Navigaatio-funktio
function navigateImage(direction) {
const newIndex = currentImageIndex + direction;
if (newIndex >= 0 && newIndex < imageCards.length) {
currentImageIndex = newIndex;
openImageModal(imageCards[currentImageIndex], currentImageIndex);
}
}
// Lisää click event jokaiselle kortille (toimii myös touch-eventeillä)
imageCards.forEach(function(card, index) {
let touchStartX = 0;
let touchStartY = 0;
let touchStartTime = 0;
let isTouchMoving = false;
// Track touch start position
card.addEventListener('touchstart', function(e) {
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
touchStartTime = Date.now();
isTouchMoving = false;
}, { passive: true });
// Track if user is scrolling
card.addEventListener('touchmove', function(e) {
const touchMoveX = e.touches[0].clientX;
const touchMoveY = e.touches[0].clientY;
const deltaX = Math.abs(touchMoveX - touchStartX);
const deltaY = Math.abs(touchMoveY - touchStartY);
// If moved more than 10px, consider it scrolling
if (deltaX > 10 || deltaY > 10) {
isTouchMoving = true;
}
}, { passive: true });
// Click event
card.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
try {
openImageModal(this, index);
} catch (error) {
console.error('Error opening image modal:', error);
}
});
// Touch event for mobile devices - only trigger if not scrolling
card.addEventListener('touchend', function(e) {
const touchEndTime = Date.now();
const touchDuration = touchEndTime - touchStartTime;
// Only open modal if:
// 1. User didn't scroll (isTouchMoving is false)
// 2. Touch duration was less than 500ms (quick tap)
if (!isTouchMoving && touchDuration < 500) {
e.preventDefault();
e.stopPropagation();
try {
openImageModal(this, index);
} catch (error) {
console.error('Error opening image modal:', error);
}
}
}, { passive: false });
});
// Sulje modal kun klikataan taustaa
modal.addEventListener('click', function(e) {
if (e.target === modal) {
closeModal();
}
});
// Touch event for mobile background close
modal.addEventListener('touchend', function(e) {
if (e.target === modal) {
e.preventDefault();
closeModal();
}
});
// Sulje modal ESC-näppäimellä ja navigoi nuolinäppäimillä
document.addEventListener('keydown', function(e) {
if (!modal.classList.contains('active')) return;
if (e.key === 'Escape') {
closeModal();
} else if (e.key === 'ArrowLeft') {
navigateImage(-1);
} else if (e.key === 'ArrowRight') {
navigateImage(1);
}
});
// Add scroll prevention for mobile
document.addEventListener('touchmove', preventScroll, { passive: false });
document.addEventListener('wheel', preventScroll, { passive: false });
});