forked from rakihub/ghost-code-injection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nested-menu.min.js
50 lines (50 loc) · 1.61 KB
/
nested-menu.min.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
!(function () {
let e = document.querySelector("nav"),
n = e.innerHTML;
function t() {
let i = "[has-child]",
s = "[child]";
var e = document.querySelectorAll("nav ul li");
let d = new Map(),
l = null;
e.forEach((n, e) => {
var t;
0 <= n.textContent.indexOf(i)
? (n.classList.add("menu-item-has-child"),
(n.innerHTML = n.innerHTML.replace(i, "")),
(n.innerHTML = n.innerHTML.replace(/href="[^"]*"/, 'href="#"')),
(t = document.createElement("ul")).classList.add("nested-menu"),
(l = t),
n.appendChild(t),
n.addEventListener("click", function (e) {
e.stopPropagation(), n.classList.toggle("is-nested-open");
}),
window.addEventListener("click", function (e) {
n.classList.contains("is-nested-open") &&
n.classList.remove("is-nested-open");
}))
: 0 <= n.textContent.indexOf(s) &&
(n.classList.add("nested-menu-item"),
(n.innerHTML = n.innerHTML.replaceAll(s, "")),
d.set(n, l));
}),
0 < d.size &&
document.querySelector("nav").classList.add("nested-menu-container"),
d.forEach((e, n) => {
e.append(n);
});
}
function i() {
var e = document.querySelector(".gh-dropdown"),
n = e && null !== e.querySelector("li");
e && !n && (n = e.parentElement) && n.remove();
}
window.addEventListener("load", function () {
t(), i();
}),
window.addEventListener("resize", function () {
setTimeout(() => {
(e.innerHTML = n), t(), i();
}, 1);
});
})();