-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (75 loc) · 2.49 KB
/
Copy pathscript.js
File metadata and controls
86 lines (75 loc) · 2.49 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
(function () {
'use strict';
var root = document.documentElement;
var toggle = document.getElementById('theme-toggle');
var yearEl = document.getElementById('year');
if (yearEl) yearEl.textContent = new Date().getFullYear();
function currentTheme() {
var stored = localStorage.getItem('theme');
if (stored === 'light' || stored === 'dark') return stored;
return window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
}
function applyTheme(theme) {
root.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
}
if (toggle) {
toggle.addEventListener('click', function () {
var next = currentTheme() === 'dark' ? 'light' : 'dark';
applyTheme(next);
});
}
// Follow the OS if the user has not picked an explicit theme yet.
var mq = window.matchMedia('(prefers-color-scheme: light)');
if (mq.addEventListener) {
mq.addEventListener('change', function () {
if (!localStorage.getItem('theme')) {
root.removeAttribute('data-theme');
}
});
}
// --- Lightbox ---
var dialog = document.getElementById('lightbox');
var dialogImg = document.getElementById('lightbox-img');
var dialogClose = dialog ? dialog.querySelector('.lightbox-close') : null;
function openLightbox(src, alt) {
if (!dialog || !dialogImg || !src) return;
dialogImg.src = src;
dialogImg.alt = alt || '';
if (typeof dialog.showModal === 'function') {
dialog.showModal();
} else {
dialog.setAttribute('open', '');
}
}
function closeLightbox() {
if (!dialog) return;
if (typeof dialog.close === 'function' && dialog.open) {
dialog.close();
} else {
dialog.removeAttribute('open');
}
}
if (dialog) {
dialog.addEventListener('close', function () {
if (dialogImg) dialogImg.removeAttribute('src');
});
dialog.addEventListener('click', function (e) {
// Backdrop click: dialog fills the viewport, so clicks outside the
// image target the dialog itself.
if (e.target === dialog) closeLightbox();
});
}
if (dialogClose) {
dialogClose.addEventListener('click', function (e) {
e.stopPropagation();
closeLightbox();
});
}
document.addEventListener('click', function (e) {
var shot = e.target.closest && e.target.closest('.card-shot');
if (!shot) return;
var img = shot.querySelector('img');
if (img) openLightbox(img.currentSrc || img.src, shot.getAttribute('aria-label') || '');
});
})();