forked from cpp-gamedev/learn-vulkan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang_toggle.js
More file actions
138 lines (118 loc) · 4.47 KB
/
lang_toggle.js
File metadata and controls
138 lines (118 loc) · 4.47 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
128
129
130
131
132
133
134
135
136
137
138
'use strict';
(function languages() {
const langToggleButton = document.getElementById('lang-toggle');
const langPopup = document.getElementById('lang-list');
if (!langToggleButton || !langPopup) {
return; // Safety check: if they're missing from the page, do nothing
}
function showLangs() {
langPopup.style.display = 'block';
langToggleButton.setAttribute('aria-expanded', 'true');
}
function hideLangs() {
langPopup.style.display = 'none';
langToggleButton.setAttribute('aria-expanded', 'false');
langToggleButton.focus();
}
langToggleButton.addEventListener('click', function() {
if (langPopup.style.display === 'block') {
hideLangs();
} else {
showLangs();
}
});
document.addEventListener('click', function (e) {
if (e.target && e.target.matches('button[data-lang]')) {
const chosenLang = e.target.getAttribute('data-lang');
const supportedLangs = ['en', 'zh-TW', 'ko-KR']; // Add translated languages here
let currentPath = window.location.pathname;
// Find "book/" in the path
const bookAnchor = 'book/';
const idx = currentPath.indexOf(bookAnchor);
if (idx === -1) {
// Fallback: If "book/" isn’t in the path
// Just go to the top-level file in the chosen language.
if (chosenLang === 'en')
window.location.href = 'index.html';
else
window.location.href = chosenLang + '/index.html';
return;
}
// Everything up to (and including) "book/"
// e.g. "/C:/Users/.../guide/book/"
const base = currentPath.substring(0, idx + bookAnchor.length);
// The rest "after book/" part
// e.g. "index.html" or "zh-TW/getting_started/foo.html"
let after = currentPath.substring(idx + bookAnchor.length);
// Split into segments and remove any leading lang if it’s known
// e.g. ["index.html"] or ["en", "getting_started", "foo.html"]
let segments = after.split('/').filter(s => s.length > 0);
if (segments.length > 0 && supportedLangs.includes(segments[0])) {
// remove the first segment if it’s a supported lang
// e.g. now ["getting_started", "foo.html"]
segments.shift();
}
// Insert the chosen language as the first path segment
// Also, English has no prefix
let newPath;
if (chosenLang === 'en') {
// e.g. /C:/Users/.../guide/book/getting_started/foo.html
newPath = base + segments.join('/');
} else {
// e.g. /C:/Users/.../guide/book/zh-TW/getting_started/foo.html
newPath = base + chosenLang + '/' + segments.join('/');
}
window.location.href = newPath;
}
if (
langPopup.style.display === 'block' &&
!langToggleButton.contains(e.target) &&
!langPopup.contains(e.target)
) {
hideLangs();
}
});
// Also hide if focus goes elsewhere
langPopup.addEventListener('focusout', function(e) {
// e.relatedTarget can be null in some browsers
if (!!e.relatedTarget &&
!langToggleButton.contains(e.relatedTarget) &&
!langPopup.contains(e.relatedTarget)) {
hideLangs();
}
});
// Optional: Add keyboard navigation (like theme popup)
document.addEventListener('keydown', function(e) {
if (langPopup.style.display !== 'block') return;
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
let li;
switch (e.key) {
case 'Escape':
e.preventDefault();
hideLangs();
break;
case 'ArrowUp':
e.preventDefault();
li = document.activeElement.parentElement;
if (li && li.previousElementSibling) {
li.previousElementSibling.querySelector('a, button').focus();
}
break;
case 'ArrowDown':
e.preventDefault();
li = document.activeElement.parentElement;
if (li && li.nextElementSibling) {
li.nextElementSibling.querySelector('a, button').focus();
}
break;
case 'Home':
e.preventDefault();
langPopup.querySelector('li:first-child a, li:first-child button').focus();
break;
case 'End':
e.preventDefault();
langPopup.querySelector('li:last-child a, li:last-child button').focus();
break;
}
});
})();