-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.js
192 lines (162 loc) · 5.7 KB
/
gallery.js
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
document.addEventListener("DOMContentLoaded", function () {
const gallery = document.getElementById("gallery");
let columns = [];
let columnHeights = [];
let currentColumnsCount;
let currentFolderIndex = 6;
function setupColumns(numColumns) {
currentColumnsCount = numColumns;
columns = [];
columnHeights = new Array(numColumns).fill(0);
gallery.innerHTML = '';
for (let i = 0; i < numColumns; i++) {
let column = document.createElement("div");
column.className = "column";
gallery.appendChild(column);
columns.push(column);
}
}
function calculateColumns() {
const aspectRatio = window.innerWidth / window.innerHeight;
if (window.innerWidth < 800 || aspectRatio < 1) {
return 1;
} else if (window.innerWidth < 1200) {
return 2;
}
return 3;
}
function findShortestColumn() {
return columnHeights.indexOf(Math.min(...columnHeights));
}
function appendImageToShortestColumn(img, imageContainer, imagePath) {
let columnIndex = findShortestColumn();
const projectPath = imagePath.replace('cover.webp', 'project.html');
img.addEventListener('click', () => {
openOverlay(projectPath);
});
if (imageContainer.parentNode) {
imageContainer.replaceChild(img, imageContainer.firstChild);
} else {
imageContainer.appendChild(img);
columns[columnIndex].appendChild(imageContainer);
}
setTimeout(() => {
columnHeights[columnIndex] = calculateColumnHeight(columns[columnIndex]);
}, 0);
}
function calculateColumnHeight(column) {
let height = 0;
Array.from(column.children).forEach(child => {
height += child.offsetHeight;
});
return height;
}
function loadHighResImage(imagePath, folderNumber, imageContainer) {
let highResImage = new XMLHttpRequest();
highResImage.open('GET', imagePath, true);
highResImage.responseType = 'blob';
highResImage.alt = `Cover Project ${folderNumber}`;
highResImage.onload = function () {
if (this.status === 200) {
let blob = this.response;
let img = document.createElement('img');
img.alt = `Cover Project ${folderNumber}`;
img.onload = () => {
window.URL.revokeObjectURL(img.src);
appendImageToShortestColumn(img, imageContainer, imagePath);
};
img.src = window.URL.createObjectURL(blob);
}
};
highResImage.onerror = function () {
console.log(`Error loading high-res image /${folderNumber}/`);
};
highResImage.send();
}
function loadNextFolderImage(folderNumber) {
if (folderNumber < 1) {
return;
}
let lowResImagePath = `pages/${folderNumber}/coverLowRes.webp`;
let highResImagePath = `pages/${folderNumber}/cover.webp`;
let imageContainer = document.createElement("div");
imageContainer.className = 'image-container';
let lowResImg = new Image();
lowResImg.alt = `Cover Project Low Res ${folderNumber}`;
lowResImg.onload = () => {
lowResImg.style.display = "block";
appendImageToShortestColumn(lowResImg, imageContainer, lowResImagePath);
loadHighResImage(highResImagePath, folderNumber, imageContainer);
loadNextFolderImage(folderNumber - 1);
};
lowResImg.onerror = () => {
console.log(`Can't load low-res image /${folderNumber}/`);
};
lowResImg.src = lowResImagePath;
}
function setupAndLoadImages() {
setupColumns(calculateColumns());
loadNextFolderImage(currentFolderIndex);
}
setupAndLoadImages();
window.addEventListener('resize', () => {
let newColumns = calculateColumns();
if (newColumns !== currentColumnsCount) {
setupAndLoadImages();
}
});
});
let overlayOpen = false;
let currentProjectPath = null;
function openOverlay(projectPath) {
if (overlayOpen) {
return;
}
let modifiedProjectPath = projectPath + "?overlay";
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('upwork')) {
modifiedProjectPath += "&upwork";
}
const overlay = document.createElement("div");
overlay.id = "overlay";
overlay.innerHTML = `
<div class="overlay-content">
<iframe src="${modifiedProjectPath}" frameborder="0"></iframe>
<button class="close-btn" onclick="closeOverlay()"></button>
</div>
`;
document.body.appendChild(overlay);
overlay.style.opacity = "0";
overlay.style.visibility = "hidden";
overlay.addEventListener('click', function(e) {
if (e.target.id === "overlay") {
closeOverlay();
}
});
setTimeout(() => {
overlay.style.opacity = "1";
overlay.style.visibility = "visible";
}, 10);
overlayOpen = true;
currentProjectPath = projectPath;
}
function closeOverlay() {
if (!overlayOpen) {
return;
}
const overlay = document.getElementById("overlay");
if (overlay) {
overlay.style.opacity = "0";
overlay.style.visibility = "hidden";
setTimeout(() => {
overlay.remove();
resetOverlayState();
}, 300);
} else {
resetOverlayState();
}
}
function resetOverlayState() {
overlayOpen = false;
currentProjectPath = null;
}