-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathmenu.js
83 lines (77 loc) · 2.5 KB
/
menu.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
// Improve interactions with dropdown menus.
(function() {
const OPEN_MENU_SELECTOR = ".nav-container details[open]";
function updateMenuPositionForSubMenu() {
const currentMenu = document.querySelector(OPEN_MENU_SELECTOR);
const subMenu = currentMenu?.getElementsByClassName('pure-menu-children')?.[0];
subMenu?.style.setProperty('--menu-x', `${currentMenu.getBoundingClientRect().x}px`);
}
addEventListener('resize', updateMenuPositionForSubMenu);
function previous(allItems, item) {
var i = 1;
var l = allItems.length;
while (i < l) {
if (allItems[i] == item) {
return allItems[i - 1];
}
i += 1;
}
}
function next(allItems, item) {
var i = 0;
var l = allItems.length - 1;
while (i < l) {
if (allItems[i] == item) {
return allItems[i + 1];
}
i += 1;
}
}
function last(allItems) {
return allItems[allItems.length - 1];
}
function closeMenu(ignore) {
const menus = Array.prototype.slice.call(
document.querySelectorAll(OPEN_MENU_SELECTOR));
for (const menu of menus) {
if (menu !== ignore) {
menu.open = false;
}
}
}
function menuOnClick(e) {
if (!this.open) {
this.focus();
} else {
closeMenu(this);
updateMenuPositionForSubMenu();
}
};
function menuKeyDown(e) {
const key = e.key.toLowerCase();
if ((key === "escape" || key === "esc") &&
document.querySelector(OPEN_MENU_SELECTOR) !== null)
{
closeMenu();
e.preventDefault();
e.stopPropagation();
}
}
const setEvents = (menus) => {
menus = Array.prototype.slice.call(menus);
for (const menu of menus) {
menu.addEventListener("toggle", menuOnClick);
menu.addEventListener("keydown", menuKeyDown);
}
};
setEvents(document.querySelectorAll(".nav-container details"));
document.documentElement.addEventListener("keydown", function(ev) {
if (ev.key == "y" && ev.target.tagName != "INPUT") {
let permalink = document.getElementById("permalink");
if (document.location.hash != "") {
permalink.href += document.location.hash;
}
history.replaceState({}, null, permalink.href);
}
});
})();