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
|
/* eslint-env browser */
import {cssProp} from './client-util.js';
import {fetchWithProgress} from './xhr-util.js';
// TODO: Update to clientSteps style.
function addImageOverlayClickHandlers() {
const container = document.getElementById('image-overlay-container');
if (!container) {
console.warn(`#image-overlay-container missing, image overlay module disabled.`);
return;
}
for (const link of document.querySelectorAll('.image-link')) {
if (link.closest('.no-image-preview')) {
continue;
}
link.addEventListener('click', handleImageLinkClicked);
}
const actionContainer = document.getElementById('image-overlay-action-container');
container.addEventListener('click', handleContainerClicked);
document.body.addEventListener('keydown', handleKeyDown);
function handleContainerClicked(evt) {
// Only hide the image overlay if actually clicking the background.
if (evt.target !== container) {
return;
}
// If you clicked anything close to or beneath the action bar, don't hide
// the image overlay.
const rect = actionContainer.getBoundingClientRect();
if (evt.clientY >= rect.top - 40) {
return;
}
container.classList.remove('visible');
}
function handleKeyDown(evt) {
if (evt.key === 'Escape' || evt.key === 'Esc' || evt.keyCode === 27) {
container.classList.remove('visible');
}
}
}
async function handleImageLinkClicked(evt) {
if (evt.metaKey || evt.shiftKey || evt.altKey) {
return;
}
evt.preventDefault();
// Don't show the overlay if the image still needs to be revealed.
if (evt.target.closest('.reveal:not(.revealed)')) {
return;
}
const container = document.getElementById('image-overlay-container');
container.classList.add('visible');
container.classList.remove('loaded');
container.classList.remove('errored');
const allViewOriginal = document.getElementsByClassName('image-overlay-view-original');
const mainImage = document.getElementById('image-overlay-image');
const thumbImage = document.getElementById('image-overlay-image-thumb');
const {href: originalSrc} = evt.target.closest('a');
const {
src: embeddedSrc,
dataset: {
originalSize: originalFileSize,
thumbs: availableThumbList,
},
} = evt.target.closest('a').querySelector('img');
updateFileSizeInformation(originalFileSize);
let mainSrc = null;
let thumbSrc = null;
if (availableThumbList) {
const {thumb: mainThumb, length: mainLength} = getPreferredThumbSize(availableThumbList);
const {thumb: smallThumb, length: smallLength} = getSmallestThumbSize(availableThumbList);
mainSrc = embeddedSrc.replace(/\.[a-z]+\.(jpg|png)$/, `.${mainThumb}.jpg`);
thumbSrc = embeddedSrc.replace(/\.[a-z]+\.(jpg|png)$/, `.${smallThumb}.jpg`);
// Show the thumbnail size on each <img> element's data attributes.
// Y'know, just for debugging convenience.
mainImage.dataset.displayingThumb = `${mainThumb}:${mainLength}`;
thumbImage.dataset.displayingThumb = `${smallThumb}:${smallLength}`;
} else {
mainSrc = originalSrc;
thumbSrc = null;
mainImage.dataset.displayingThumb = '';
thumbImage.dataset.displayingThumb = '';
}
if (thumbSrc) {
thumbImage.src = thumbSrc;
thumbImage.style.display = null;
} else {
thumbImage.src = '';
thumbImage.style.display = 'none';
}
for (const viewOriginal of allViewOriginal) {
viewOriginal.href = originalSrc;
}
mainImage.addEventListener('load', handleMainImageLoaded);
mainImage.addEventListener('error', handleMainImageErrored);
const showProgress = amount => {
cssProp(container, '--download-progress', `${amount * 100}%`);
};
showProgress(0.00);
const response =
await fetchWithProgress(mainSrc, progress => {
if (progress === -1) {
// TODO: Indeterminate response progress cue
showProgress(0.00);
} else {
showProgress(0.20 + 0.80 * progress);
}
});
if (!response.status.toString().startsWith('2')) {
handleMainImageErrored();
return;
}
const blob = await response.blob();
const blobSrc = URL.createObjectURL(blob);
mainImage.src = blobSrc;
showProgress(1.00);
function handleMainImageLoaded() {
container.classList.add('loaded');
removeEventListeners();
}
function handleMainImageErrored() {
container.classList.add('errored');
removeEventListeners();
}
function removeEventListeners() {
mainImage.removeEventListener('load', handleMainImageLoaded);
mainImage.removeEventListener('error', handleMainImageErrored);
}
}
function parseThumbList(availableThumbList) {
// Parse all the available thumbnail sizes! These are provided by the actual
// content generation on each image.
const defaultThumbList = 'huge:1400 semihuge:1200 large:800 medium:400 small:250'
const availableSizes =
(availableThumbList || defaultThumbList)
.split(' ')
.map(part => part.split(':'))
.map(([thumb, length]) => ({thumb, length: parseInt(length)}))
.sort((a, b) => a.length - b.length);
return availableSizes;
}
function getPreferredThumbSize(availableThumbList) {
// Assuming a square, the image will be constrained to the lesser window
// dimension. Coefficient here matches CSS dimensions for image overlay.
const constrainedLength = Math.floor(Math.min(
0.80 * window.innerWidth,
0.80 * window.innerHeight));
// Match device pixel ratio, which is 2x for "retina" displays and certain
// device configurations.
const visualLength = window.devicePixelRatio * constrainedLength;
const availableSizes = parseThumbList(availableThumbList);
// Starting from the smallest dimensions, find (and return) the first
// available length which hits a "good enough" threshold - it's got to be
// at least that percent of the way to the actual displayed dimensions.
const goodEnoughThreshold = 0.90;
// (The last item is skipped since we'd be falling back to it anyway.)
for (const {thumb, length} of availableSizes.slice(0, -1)) {
if (Math.floor(visualLength * goodEnoughThreshold) <= length) {
return {thumb, length};
}
}
// If none of the items in the list were big enough to hit the "good enough"
// threshold, just use the largest size available.
return availableSizes[availableSizes.length - 1];
}
function getSmallestThumbSize(availableThumbList) {
// Just snag the smallest size. This'll be used for displaying the "preview"
// as the bigger one is loading.
const availableSizes = parseThumbList(availableThumbList);
return availableSizes[0];
}
function updateFileSizeInformation(fileSize) {
const fileSizeWarningThreshold = 8 * 10 ** 6;
const actionContentWithoutSize = document.getElementById('image-overlay-action-content-without-size');
const actionContentWithSize = document.getElementById('image-overlay-action-content-with-size');
if (!fileSize) {
actionContentWithSize.classList.remove('visible');
actionContentWithoutSize.classList.add('visible');
return;
}
actionContentWithoutSize.classList.remove('visible');
actionContentWithSize.classList.add('visible');
const megabytesContainer = document.getElementById('image-overlay-file-size-megabytes');
const kilobytesContainer = document.getElementById('image-overlay-file-size-kilobytes');
const megabytesContent = megabytesContainer.querySelector('.image-overlay-file-size-count');
const kilobytesContent = kilobytesContainer.querySelector('.image-overlay-file-size-count');
const fileSizeWarning = document.getElementById('image-overlay-file-size-warning');
fileSize = parseInt(fileSize);
const round = (exp) => Math.round(fileSize / 10 ** (exp - 1)) / 10;
if (fileSize > fileSizeWarningThreshold) {
fileSizeWarning.classList.add('visible');
} else {
fileSizeWarning.classList.remove('visible');
}
if (fileSize > 10 ** 6) {
megabytesContainer.classList.add('visible');
kilobytesContainer.classList.remove('visible');
megabytesContent.innerText = round(6);
} else {
megabytesContainer.classList.remove('visible');
kilobytesContainer.classList.add('visible');
kilobytesContent.innerText = round(3);
}
void fileSizeWarning;
}
addImageOverlayClickHandlers();
|