Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
108 lines (94 loc) · 3.66 KB
/
Copy pathscript.js
File metadata and controls
108 lines (94 loc) · 3.66 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
const header = document.querySelector('[data-header]');
const menuToggle = document.querySelector('.menu-toggle');
const siteNav = document.querySelector('#site-nav');
const year = document.querySelector('[data-year]');
const currentPage = document.body.dataset.page;
if (year) year.textContent = new Date().getFullYear();
const setHeaderState = () => header?.classList.toggle('is-scrolled', window.scrollY > 12);
setHeaderState();
window.addEventListener('scroll', setHeaderState, { passive: true });
document.querySelectorAll('[data-page-link]').forEach((link) => {
const isCurrent = link.dataset.pageLink === currentPage;
link.classList.toggle('is-current', isCurrent);
if (isCurrent) link.setAttribute('aria-current', 'page');
});
const setMobileNavState = (open) => {
header?.classList.toggle('nav-open', open);
document.body.classList.toggle('nav-is-open', open);
menuToggle?.setAttribute('aria-expanded', String(open));
menuToggle?.setAttribute('aria-label', open ? 'Chiudi navigazione' : 'Apri navigazione');
};
const closeMobileNav = () => {
setMobileNavState(false);
};
menuToggle?.addEventListener('click', () => {
setMobileNavState(!header?.classList.contains('nav-open'));
});
siteNav?.querySelectorAll('a').forEach((link) => link.addEventListener('click', () => {
closeMobileNav();
}));
const registrationNotices = Array.from(document.querySelectorAll('[data-registration-notice]'));
const closeRegistrationNotices = () => {
registrationNotices.forEach((notice) => {
notice.removeAttribute('data-open');
notice.querySelector('[role="tooltip"]').setAttribute('aria-hidden', 'true');
});
};
registrationNotices.forEach((notice) => {
const trigger = notice.querySelector('[data-registration-trigger]');
const tooltip = notice.querySelector('[role="tooltip"]');
const show = () => {
closeRegistrationNotices();
notice.setAttribute('data-open', '');
tooltip.setAttribute('aria-hidden', 'false');
};
notice.addEventListener('pointerenter', (event) => {
if (event.pointerType === 'mouse') show();
});
notice.addEventListener('pointerleave', (event) => {
if (event.pointerType === 'mouse' && !trigger.matches(':focus-visible')) {
notice.removeAttribute('data-open');
tooltip.setAttribute('aria-hidden', 'true');
}
});
trigger.addEventListener('focus', show);
trigger.addEventListener('click', show);
trigger.addEventListener('blur', () => {
notice.removeAttribute('data-open');
tooltip.setAttribute('aria-hidden', 'true');
});
});
document.addEventListener('click', (event) => {
if (!event.target.closest('[data-registration-notice]')) closeRegistrationNotices();
if (header?.classList.contains('nav-open') && !event.target.closest('[data-header]')) closeMobileNav();
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
closeRegistrationNotices();
closeMobileNav();
}
});
window.addEventListener('resize', () => {
closeRegistrationNotices();
if (window.innerWidth > 980) closeMobileNav();
});
menuToggle?.addEventListener('click', closeRegistrationNotices);
const setAccordionIcon = (detail) => {
const icon = detail.querySelector('summary i');
if (icon) icon.textContent = detail.open ? '\u00d7' : '+';
};
document.querySelectorAll('[data-accordion-group] details').forEach((detail) => {
setAccordionIcon(detail);
detail.addEventListener('toggle', () => {
if (detail.open) {
const group = detail.closest('[data-accordion-group]');
group?.querySelectorAll('details').forEach((other) => {
if (other !== detail) {
other.open = false;
setAccordionIcon(other);
}
});
}
setAccordionIcon(detail);
});
});