website/js/customElements/mainMenu.js

79 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-04-13 10:07:16 +02:00
class MainMenu extends HTMLElement {
constructor() {
super();
this.generateMenu();
}
2021-09-25 23:37:15 +02:00
async generateMenu() {
let menu = await (await fetch("/API/mainMenu.json")).json();
2021-04-13 10:07:16 +02:00
let menuContainer = document.createElement("div");
menuContainer.className = "mainMenuContainer";
2021-04-13 13:16:13 +02:00
let burgerMenu = document.createElement("div");
burgerMenu.id = "burgerMenu";
burgerMenu.onclick = () => {
document.getElementById("burgerMenu").classList.toggle("change");
document.querySelector(".mainMenuContainer").classList.toggle("visible");
}
2021-09-25 23:37:15 +02:00
for (let i = 1; i <= 3; i++) {
2021-04-13 13:16:13 +02:00
let bar = document.createElement("div");
bar.className = "bar" + i;
burgerMenu.appendChild(bar);
}
this.appendChild(burgerMenu);
2021-04-13 10:07:16 +02:00
menu.forEach((element) => {
2021-09-25 23:37:15 +02:00
if (element["type"] === "link") {
2021-04-13 10:07:16 +02:00
let a = document.createElement("a");
a.href = element["url"];
a.innerText = element["name"];
2021-09-25 23:37:15 +02:00
a.classList.add("navLink");
2021-04-13 10:07:16 +02:00
menuContainer.appendChild(a);
2021-09-25 23:37:15 +02:00
if (window.location['pathname'] === element["url"]) {
document.title = element["name"] + " - Jonas Leder";
}
2021-09-25 23:37:15 +02:00
} else if (element["type"] === "dropdown") {
2021-04-13 10:07:16 +02:00
let dropdown = document.createElement("div");
dropdown.className = "dropdown";
let a = document.createElement("a");
a.href = element["url"];
2021-09-25 23:37:15 +02:00
a.classList.add("navLink");
2021-04-13 10:07:16 +02:00
a.innerText = element["name"];
dropdown.appendChild(a)
let dropdownContent = document.createElement("div");
dropdownContent.className = "dropdown-content"
element["childElements"].forEach((childElement) => {
let a = document.createElement("a");
a.href = childElement["url"];
a.innerText = childElement["name"];
dropdownContent.appendChild(a);
let br = document.createElement("br");
dropdownContent.appendChild(br);
2021-09-25 23:37:15 +02:00
if (window.location['pathname'] === childElement["url"]) {
document.title = childElement["name"] + " - Jonas Leder";
}
2021-04-13 10:07:16 +02:00
});
dropdown.appendChild(dropdownContent);
menuContainer.appendChild(dropdown);
}
});
this.appendChild(menuContainer);
}
}
customElements.define("jl-main_menu", MainMenu);