-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzoomwall.js
More file actions
291 lines (286 loc) · 9.82 KB
/
Copy pathzoomwall.js
File metadata and controls
291 lines (286 loc) · 9.82 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
/*
zoomwall.js
The MIT License (MIT)
Copyright (c) 2014 Eric Leong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* Create a gallery with the provided HTMLElement.
*
* @param blocks contains the images that belong to the gallery
* @param enableKeys enables keyboard navigation
*/
export function create(blocks, enableKeys = false) {
const imgs = blocks.querySelectorAll("img");
resize([...imgs]);
blocks.classList.remove("loading");
// shrink blocks if an empty space is clicked
blocks.addEventListener("click", function () {
const imgs = blocks.getElementsByTagName("img");
if (imgs.length > 0) {
shrink(imgs[0]);
}
});
// add click listeners to blocks
imgs.forEach(function (img) {
img.addEventListener("click", animate);
});
// add key down listener
if (enableKeys) {
keys(blocks);
}
}
function findWall(elem) {
// traverse dom to find gallery root node
let parent = elem;
while (parent.parentElement) {
parent = parent.parentElement;
if (parent instanceof HTMLElement &&
parent.classList.contains("zoomwall")) {
return parent;
}
}
return null;
}
function keys(blocks) {
const keyPager = function (e) {
if (e.defaultPrevented) {
return;
}
if (blocks.classList.contains("lightbox")) {
switch (e.keyCode) {
case 27: {
// escape
const imgs = blocks.getElementsByTagName("img");
if (imgs.length > 0) {
shrink(imgs[0]);
e.preventDefault();
}
break;
}
case 37: // left
page(blocks, false);
e.preventDefault();
break;
case 39: // right
page(blocks, true);
e.preventDefault();
break;
}
}
};
document.addEventListener("keydown", keyPager);
return keyPager;
}
function resizeRow(row, width) {
if (row && row.length > 1) {
row.forEach(function (img) {
img.style.width = `${(parseInt(window.getComputedStyle(img).width, 10) / width) * 100}%`;
img.style.height = "auto";
});
}
}
function calcRowWidth(row) {
return row.reduce((width, img) => width + parseInt(window.getComputedStyle(img).width, 10), 0);
}
function resize(blocks) {
blocks
.reduce(function (rows, block) {
var _a;
const offsetTop = block.offsetTop;
if (!rows.has(offsetTop)) {
rows.set(offsetTop, []);
}
(_a = rows.get(offsetTop)) === null || _a === void 0 ? void 0 : _a.push(block);
return rows;
}, new Map())
.forEach((row) => resizeRow(row, calcRowWidth(row)));
}
function reset(block) {
block.style.transform = "translate(0, 0) scale(1)";
block.classList.remove("active");
// swap images
if (block.dataset.lowres) {
block.src = block.dataset.lowres;
}
if (block.dataset.sizes) {
block.sizes = block.dataset.sizes;
}
}
function shrink(block) {
const blocks = findWall(block);
if (blocks) {
blocks.classList.remove("lightbox");
// reset all blocks
blocks.querySelectorAll("img").forEach(function (img) {
reset(img);
});
}
}
function expand(block) {
const blocks = findWall(block);
if (blocks == null) {
return;
}
block.classList.add("active");
blocks.classList.add("lightbox");
// parent dimensions
const parentStyle = window.getComputedStyle(blocks);
const parentWidth = parseInt(parentStyle.width, 10);
const parentHeight = parseInt(parentStyle.height, 10);
const parentTop = blocks.getBoundingClientRect().top;
// block dimensions
const blockStyle = window.getComputedStyle(block);
const blockWidth = parseInt(blockStyle.width, 10);
const blockHeight = parseInt(blockStyle.height, 10);
// determine maximum height
let targetHeight = window.innerHeight;
if (parentHeight < window.innerHeight) {
targetHeight = parentHeight;
}
else if (parentTop > 0) {
targetHeight -= parentTop;
}
// swap images
if (block.dataset.highres) {
if (block.src != block.dataset.highres &&
block.dataset.lowres === undefined) {
block.dataset.lowres = block.src;
}
block.src = block.dataset.highres;
}
if (block.sizes) {
// responsive images
block.dataset.sizes = block.sizes;
block.sizes = "100vw"; // image is now 100% of the viewport width
}
// determine what blocks are on this row
const imgs = [...blocks.querySelectorAll("img")];
const selectedRow = imgs.filter((img) => img.offsetTop == block.offsetTop);
// calculate scale
let scale = targetHeight / blockHeight;
if (blockWidth * scale > parentWidth) {
scale = parentWidth / blockWidth;
}
// determine offset
let offsetY = parentTop - blocks.offsetTop + block.offsetTop;
if (offsetY > 0) {
if (parentHeight < window.innerHeight) {
offsetY -= targetHeight / 2 - (blockHeight * scale) / 2;
}
if (parentTop > 0) {
offsetY -= parentTop;
}
}
const leftWidth = selectedRow
.slice(0, selectedRow.indexOf(block))
.reduce((offset, img) => offset + parseInt(window.getComputedStyle(img).width, 10) * scale, 0);
const leftOffsetX = parentWidth / 2 - (blockWidth * scale) / 2 - leftWidth;
const rows = imgs.reduce(function (rows, block) {
var _a;
// group rows
const offsetTop = block.offsetTop;
if (!rows.has(offsetTop)) {
rows.set(offsetTop, []);
}
(_a = rows.get(offsetTop)) === null || _a === void 0 ? void 0 : _a.push(block);
return rows;
}, new Map());
const selectedIndex = [...rows.keys()].indexOf(block.offsetTop);
const rowHeights = [...rows.values()].map((r) => parseInt(window.getComputedStyle(r[0]).height, 10));
rows.forEach((row, offsetTop, rows) => {
const rowIndex = [...rows.keys()].indexOf(offsetTop);
// compute the y offset based on the distance from this row to the selected row
const rowOffsetY = Math.sign(rowIndex - selectedIndex) *
(scale - 1) *
rowHeights
.slice(...[selectedIndex, rowIndex].sort((a, b) => a - b))
.reduce((offset, height) => offset + height, 0) -
offsetY;
row
.map((img) => {
return {
img: img,
width: parseInt(window.getComputedStyle(img).width, 10),
};
})
.forEach((item, columnIndex, items) => {
const offsetX = items
.slice(0, columnIndex)
.reduce((offset, elem) => offset + elem.width, 0) *
(scale - 1);
const percentageOffsetX = ((offsetX + leftOffsetX) / item.width) * 100;
const percentageOffsetY = (rowOffsetY /
parseInt(window.getComputedStyle(item.img).height, 10)) *
100;
item.img.style.transformOrigin = "0% 0%";
item.img.style.transform =
"translate(" +
percentageOffsetX.toFixed(8) +
"%, " +
percentageOffsetY.toFixed(8) +
"%) scale(" +
scale.toFixed(8) +
")";
});
});
}
function animate(e) {
if (!(e.target instanceof HTMLImageElement)) {
return;
}
const block = e.target;
const blocks = findWall(block);
if (block.classList.contains("active")) {
shrink(block);
}
else if (blocks instanceof HTMLElement) {
[...blocks.getElementsByClassName("active")].forEach((block) => block.classList.remove("active"));
expand(block);
}
e.stopPropagation();
}
function page(blocks, isNext = true) {
const actives = blocks.querySelectorAll("img.active");
if (actives && actives.length > 0) {
const current = actives[0];
let next;
const wall = findWall(current);
if (wall == null) {
return;
}
const imgs = wall.querySelectorAll("img");
const blockIndex = [...imgs].indexOf(current);
if (isNext) {
next = imgs[blockIndex + 1];
}
else {
next = imgs[blockIndex - 1];
}
if (next) {
current.classList.remove("active");
// swap images
if (current.dataset.lowres) {
current.src = current.dataset.lowres;
}
if (current.dataset.sizes) {
current.sizes = current.dataset.sizes;
}
expand(next);
}
}
}