-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterior.js
83 lines (73 loc) · 2.64 KB
/
interior.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
const images = document.querySelectorAll('.content__images img');
const TocLinks = document.querySelectorAll('.content__nav a');
TocLinks.forEach(a => {
a.addEventListener('click', e => {
gtag('event', 'toc_click', {
page_title: document.title + ' / ' + e.target.innerHTML,
page_location: window.location + e.target.getAttribute('href'),
send_to: 'G-GWPPZWQTT3'
});
})
});
images.forEach(img => {
img.setAttribute('tabIndex', 0);
img.addEventListener('click', e => enlargeImage(e));
img.addEventListener('keyup', e => {
if (e.key === 'Enter')
enlargeImage(e) ;
});
});
document.addEventListener('keydown', e => {
if (e.key === 'Escape' || e.key === 'Tab')
closeLightbox();
})
function enlargeImage(e) {
const lightbox = document.createElement('div');
const exButton = document.createElement('button');
const ex = document.createElementNS("http://www.w3.org/2000/svg", 'svg');
const path = document.createElementNS("http://www.w3.org/2000/svg", 'path');
exButton.classList.add('lightbox__ex');
path.setAttribute('d', 'm2.784 2.761 14.85 14.849M17.669 2.707 2.82 17.556');
ex.appendChild(path);
exButton.appendChild(ex);
lightbox.addEventListener('click', closeLightbox);
lightbox.classList.add('lightbox');
lightbox.appendChild(exButton);
lightbox.appendChild(e.target.cloneNode());
document.body.appendChild(lightbox);
exButton.focus();
const src = e.target.getAttribute('src');
gtag('event', 'image_view', {
page_title: 'Image: ' + src.replace('/images/', ''),
page_location: 'https://ttrotter.com/' + src,
send_to: 'G-GWPPZWQTT3'
});
}
function closeLightbox() {
const lightbox = document.querySelector('.lightbox');
const activeImage = lightbox.querySelector('img').getAttribute('src');
lightbox.classList.add('is-closing');
setTimeout(() => {
lightbox.remove();
document.querySelector(`[src="${activeImage}"]`).focus();
}, 200);
}
const IO = new IntersectionObserver(IOCallback, {
rootMargin: `0px 0px ${window.innerHeight * -.66}px 0px`,
threshold: 1.0
})
const headings = document.querySelectorAll('h3[id],h4[id]');
headings.forEach(heading => {
IO.observe(heading);
});
let activeHeading;
function IOCallback(entries, observer) {
if (entries[0].isIntersecting)
activeHeading = entries[0].target.getAttribute('id');
else if (window.scrollY < 100)
activeHeading = null;
if (document.querySelector('.content__nav .is-active'))
document.querySelector('.content__nav .is-active').classList.remove('is-active');
if (activeHeading)
document.querySelector(`[href="#${activeHeading}"]`).parentElement.classList.add('is-active');
}