-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
357 lines (311 loc) · 11.2 KB
/
script.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
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
// ——————————————————————————————————————————————————
// VIEW MODE
// ——————————————————————————————————————————————————
let viewModes = ['list', 'detail', 'thumbnail', 'dense'];
let view = 'list';
function changeView(mode) {
let prevView = document.querySelector(`[data-view="${view}"]`);
prevView.dataset.active = 0;
view = mode;
let currView = document.querySelector(`[data-view="${view}"]`);
currView.dataset.active = 1;
let archiveContent = document.querySelector('.archive-content');
archiveContent.dataset.mode = view;
archiveFlash();
scrollArchive();
}
// Change view without scroll/flash
function changeViewAlt(mode) {
let prevView = document.querySelector(`[data-view="${view}"]`);
prevView.dataset.active = 0;
view = mode;
let currView = document.querySelector(`[data-view="${view}"]`);
currView.dataset.active = 1;
let archiveContent = document.querySelector('.archive-content');
archiveContent.dataset.mode = view;
}
// Flash on view change
function archiveFlash() {
let flash = document.querySelector('.archive-flash');
flash.style.transition = 'unset';
flash.style.opacity = 1;
flash.style.backgroundColor = `hsl(${Math.floor(Math.random()*360)}deg, 100%, 50%)`;
setTimeout(() => {
flash.style.transition = 'opacity .5s';
flash.style.opacity = 0;
}, 100)
}
// ——————————————————————————————————————————————————
// SEARCH
// ——————————————————————————————————————————————————
// Add event listener
let searchInput = document.querySelector('.controls-search');
searchInput.addEventListener('input', () => {
searchArchive(searchInput.value);
})
// Search function
function searchArchive(query) {
formattedInput = query.toLowerCase().replace(/[.,\/#!$%\^&*;:{}=\-_`~()‘’“”\?]/g,"");
for (let key of Object.keys(archiveJSON)) {
let entry = archiveJSON[key];
if (entry['active'] == false) {
continue
}
let archiveItemParent = document.querySelector(`.archive-item-parent[data-key="${key}"]`);
// Format title and subtitle
let title = entry['title'];
let subtitle = entry['subtitle'];
let formattedTitle = title.toLowerCase().replace(/[.,\/#!$%\^&*;:{}=\-_`~()‘’“”\?]/g,"");
let formattedSubtitle = subtitle.toLowerCase().replace(/[.,\/#!$%\^&*;:{}=\-_`~()‘’“”\?]/g,"");
if (formattedTitle.includes(formattedInput) || formattedSubtitle.includes(formattedInput)) {
archiveItemParent.dataset.search = 1;
} else {
archiveItemParent.dataset.search = 0;
}
}
checkIfEmpty();
scrollArchive();
}
function clearSearch() {
for (let archiveItemParent of document.querySelectorAll('.archive-item-parent')) {
archiveItemParent.dataset.search = 1;
}
}
// Clear search when resized to mobile
window.addEventListener('resize', () => {
if (window.innerWidth <= 820) {
searchInput.value = "";
clearSearch();
checkIfEmpty();
}
})
// ——————————————————————————————————————————————————
// SORTING AND FILTERS
// ——————————————————————————————————————————————————
// Open/close settings menu
let settings = '';
function toggleSettings(menu) {
for (let i of document.querySelectorAll(".settings-menu")) {
i.dataset.active = 0;
}
for (let i of document.querySelectorAll(".settings-label")) {
i.dataset.active = 0;
}
if (settings == menu) {
settings = '';
} else {
settings = menu;
let currMenu = document.querySelector(`[data-settings="${settings}"]`);
currMenu.dataset.active = 1;
let currLabel = document.querySelector(`[data-settings-label="${settings}"]`);
currLabel.dataset.active = 1;
}
}
function closeSettings() {
settings = '';
for (let i of document.querySelectorAll(".settings-menu")) {
i.dataset.active = 0;
}
for (let i of document.querySelectorAll(".settings-label")) {
i.dataset.active = 0;
}
}
// Close settings if somewhere else clicked
function detectClose(e) {
let controlsSettings = document.querySelector('.controls-settings');
if (!controlsSettings.contains(e.target)) {
closeSettings();
}
}
document.addEventListener('click', (e) => detectClose(e));
// Toggle sorting
let sortingModes = ['curated', 'newest', 'oldest', 'az', 'za', 'random'];
let sorting = 'curated';
function toggleSorting(mode) {
let prevSorting = document.querySelector(`[data-sorting="${sorting}"]`);
prevSorting.dataset.active = 0;
sorting = mode;
let currSorting = document.querySelector(`[data-sorting="${sorting}"]`);
currSorting.dataset.active = 1;
sortArchive();
scrollArchive();
}
// Apply sorting order
function sortArchive() {
if (sorting == "curated") {
for (let archiveItemParent of document.querySelectorAll('.archive-item-parent')) {
archiveItemParent.style.order = archiveJSON[archiveItemParent.dataset.key]['sort-custom'] + Math.floor(Math.random()*90+10).toString();
}
} else if (sorting == "newest") {
for (let archiveItemParent of document.querySelectorAll('.archive-item-parent')) {
let sort = "-" + archiveJSON[archiveItemParent.dataset.key]['sort-chrono'].toString().replaceAll('-','');
archiveItemParent.style.order = sort;
}
} else if (sorting == "oldest") {
for (let archiveItemParent of document.querySelectorAll('.archive-item-parent')) {
let sort = archiveJSON[archiveItemParent.dataset.key]['sort-chrono'].toString().replaceAll('-','');
archiveItemParent.style.order = sort;
}
} else if (sorting == "az") {
let sortOrder = [];
// Build object containing alpha/key pairs
let sortingAlpha = {};
for (let key of Object.keys(archiveJSON)) {
let entry = archiveJSON[key];
if (entry['active'] == false) {
continue
}
sortingAlpha[entry['sort-alpha']] = key;
}
// Sort alpha keys
for (let key of Object.keys(sortingAlpha).sort()) {
sortOrder.push(sortingAlpha[key]);
}
// Apply sorting
let sortCounter = 0;
for (let key of sortOrder) {
let archiveItemParent = document.querySelector(`.archive-item-parent[data-key="${key}"]`);
archiveItemParent.style.order = sortCounter;
sortCounter++;
}
} else if (sorting == "za") {
let sortOrder = [];
// Build object containing alpha/key pairs
let sortingAlpha = {};
for (let key of Object.keys(archiveJSON)) {
let entry = archiveJSON[key];
if (entry['active'] == false) {
continue
}
sortingAlpha[entry['sort-alpha']] = key;
}
// Sort alpha keys
for (let key of Object.keys(sortingAlpha).sort()) {
sortOrder.push(sortingAlpha[key]);
}
// Apply sorting
let sortCounter = 0;
for (let key of sortOrder) {
let archiveItemParent = document.querySelector(`.archive-item-parent[data-key="${key}"]`);
archiveItemParent.style.order = sortCounter;
sortCounter--;
}
} else if (sorting == "random") {
for (let archiveItemParent of document.querySelectorAll('.archive-item-parent')) {
archiveItemParent.style.order = Math.floor(Math.random()*1000);
}
}
}
// Set filters
let filters = [];
function toggleFilter(filter) {
let currFilter = document.querySelector(`[data-filter="${filter}"]`);
if (filters.includes(filter)) {
filters.splice(filters.indexOf(filter), 1);
currFilter.dataset.active = 0;
} else {
filters.push(filter);
currFilter.dataset.active = 1;
}
let clear = document.querySelector(`.settings-clear`);
if (filters.length == 0) {
clear.dataset.active = 0;
} else {
clear.dataset.active = 1;
}
filterArchive();
scrollArchive();
}
// Clear filters
function clearFilters() {
filters = [];
for (let i of document.querySelectorAll(`[data-filter]`)) {
i.dataset.active = 0;
}
let clear = document.querySelector(`.settings-clear`);
clear.dataset.active = 0;
for (let archiveItemParent of document.querySelectorAll('.archive-item-parent')) {
archiveItemParent.dataset.filter = 1;
}
filterArchive();
scrollArchive();
}
// Apply filters
function filterArchive() {
for (let archiveItemParent of document.querySelectorAll('.archive-item-parent')) {
if (filters.length == 0) {
archiveItemParent.dataset.filter = 1;
} else {
archiveItemParent.dataset.filter = 0;
let key = archiveItemParent.dataset.key;
let entryTags = archiveJSON[key]['tags'];
for (let filter of filters) {
if (entryTags.includes(filter)) {
archiveItemParent.dataset.filter = 1;
continue
}
}
}
}
checkIfEmpty();
scrollArchive();
}
// ——————————————————————————————————————————————————
// ARCHIVE HELPER FUNCTIONS
// ——————————————————————————————————————————————————
// Scroll back to top
function scrollArchive() {
let archive = document.querySelector(".archive");
archive.scrollIntoView({ block: "start", inline: "nearest" });
}
// Check if no results are showing
function checkIfEmpty() {
document.querySelector('.archive-content').dataset.hide = 1;
document.querySelector('.archive-empty').dataset.hide = 0;
for (let archiveItemParent of document.querySelectorAll('.archive-item-parent')) {
if (parseInt(archiveItemParent.dataset.search) == 1 && parseInt(archiveItemParent.dataset.filter) == 1) {
document.querySelector('.archive-empty').dataset.hide = 1;
document.querySelector('.archive-content').dataset.hide = 0;
return
}
}
}
// ——————————————————————————————————————————————————
// INITIALIZE
// ——————————————————————————————————————————————————
sortArchive();
// changeViewAlt(viewModes[Math.floor(Math.random()*viewModes.length)]);
setTimeout(() => {
const archive = document.querySelector('.archive');
archive.dataset.initialized = 1;
}, 50)
// OLD CODE: Shuffle archive if visiting the homepage directly
// SCRAPPED: Would need cookies to save portfolio order
// window.addEventListener("pageshow", async (event) => {
// const historyTraversal = event.persisted || performance.getEntriesByType("navigation")[0].type === "back_forward";
// if (!historyTraversal) {
// sortArchive();
// changeViewAlt(viewModes[Math.floor(Math.random()*viewModes.length)]);
// }
// setTimeout(() => {
// const archive = document.querySelector('.archive');
// archive.dataset.initialized = 1;
// }, 50)
// });
// OLD CODE: Fetch JSON and build archive
// let archiveJSON = {};
// fetch('/archive.json')
// .then((response) => response.json())
// .then((json) => {
// // Convert to JSON object format
// for (let i of Object.keys(json)) {
// let entry = json[i];
// let key = entry['key'];
// archiveJSON[key] = entry;
// }
// sortArchive();
// setTimeout(() => {
// let archive = document.querySelector('.archive');
// archive.dataset.initialized = 1;
// }, 50)
// })