forked from expressjs/expressjs.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
127 lines (111 loc) · 4.15 KB
/
app.js
File metadata and controls
127 lines (111 loc) · 4.15 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
document.addEventListener("DOMContentLoaded", function () {
const languageElement = document.getElementById('languageData');
const languagesData = languageElement ? JSON.parse(languageElement.dataset.languages) : [];
const langDisplay = document.getElementById('current-lang');
const i18nMsgBox = document.getElementById("i18n-notice-box");
const scrollToTopBtn = document.getElementById("top");
// display current language in language picker component
if (langDisplay) {
const currentLanguage = window.location.pathname.split('/')[1];
const matchedLang = languagesData.find(lang => lang.code === currentLanguage);
langDisplay.textContent = matchedLang ? matchedLang.name : 'English';
}
// scroll to top of the page
if (scrollToTopBtn) {
scrollToTopBtn.addEventListener("click", function (e) {
e.preventDefault();
window.scrollTo({
top: 0,
behavior: "smooth"
})
});
}
// add/remove class 'scroll' on scroll by 5px
const scrollTarget = document.querySelector('.logo-container');
const scrollObserver = new IntersectionObserver(
([entry]) => {
if (!entry.isIntersecting) {
document.body.classList.add('scroll');
} else {
document.body.classList.remove('scroll');
}
},
{
root: null,
threshold: 0,
rootMargin: '0px 0px 0px 0px'
}
);
if (scrollTarget) scrollObserver.observe(scrollTarget);
// heighlight current Menu on scroll
const headings = Array.from(document.querySelectorAll("h2, h3"));
const menuLinks = document.querySelectorAll("#menu li a");
const observerOptions = {
root: null,
rootMargin: "-25% 0px -50px 0px",
threshold: 1,
};
const menuObserver = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const currentApiPrefix = entry.target.id.split(".")[0];
const parentMenuSelector = `#${currentApiPrefix}-menu`;
const parentMenuEl = document.querySelector(parentMenuSelector);
// open submenu on scroll
if (parentMenuEl) parentMenuEl.classList.add("active");
// Remove active class from last menu item
const lastActiveMenu = document.querySelector(".active[id$='-menu']");
if (lastActiveMenu && lastActiveMenu.id !== parentMenuEl.id) {
lastActiveMenu.classList.remove("active");
}
// Update active link
menuLinks.forEach((link) => link.classList.remove("active"));
const activeLink = document.querySelector(`a[href="#${entry.target.id}"]`);
if (activeLink) activeLink.classList.add("active");
}
});
}, observerOptions);
headings.forEach((heading) => menuObserver.observe(heading));
// i18n message box : this box appears hidden for all page.lang != 'en'
const isI18nCookie = readCookie('i18nClose');
if (i18nMsgBox && !isI18nCookie) {
const closeI18nBtn = document.getElementById("close-i18n-notice-box");
// show notice box
i18nMsgBox.hidden = false;
// close notice box
if (closeI18nBtn) {
closeI18nBtn.addEventListener("click", () => {
// hide notice
i18nMsgBox.hidden = true;
// set session cookie
createCookie('i18nClose', 1);
});
// keyboard a11y
closeI18nBtn.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
closeI18nBtn.click();
}
});
}
};
function createCookie(name, value, days) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 864e5));
expires = "; expires=" + date.toUTCString();
}
document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}${expires}; path=/; SameSite=Lax; Secure`;
}
function readCookie(name) {
const nameEQ = encodeURIComponent(name) + "=";
const ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
});