-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f70b7c
commit 6cf4079
Showing
9 changed files
with
92 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
customElements.define( | ||
"tree-menu", | ||
class extends HTMLElement { | ||
constructor() { | ||
super(); | ||
const ul = document.createElement("ul"); | ||
this.appendChild(ul); | ||
this.loadMenu(ul); | ||
} | ||
|
||
render(menu, ul, url) { | ||
const fragment = document.createDocumentFragment(); | ||
|
||
for (const { data, children } of menu) { | ||
let li = document.createElement("li"); | ||
fragment.appendChild(li); | ||
if (children) { | ||
li.innerHTML = `<details><summary></summary></details>`; | ||
li = li.querySelector("summary"); | ||
} | ||
|
||
if (data.url) { | ||
const a = document.createElement("a"); | ||
a.href = data.url; | ||
a.textContent = data.title || data.basename; | ||
|
||
if (data.url === url) { | ||
a.setAttribute("aria-current", "page"); | ||
} | ||
li.appendChild(a); | ||
} else { | ||
li.innerHTML = `<strong>${data.title || data.basename}</strong>`; | ||
} | ||
|
||
if (children) { | ||
const ul = document.createElement("ul"); | ||
li.parentElement.appendChild(ul); | ||
this.render(children, ul, url); | ||
} | ||
} | ||
|
||
ul.appendChild(fragment); | ||
} | ||
|
||
async loadMenu(ul) { | ||
const url = this.dataset.url; | ||
const response = await fetch(url); | ||
const data = await response.json(); | ||
this.render(data, ul, decodeURIComponentSafe(location.pathname)); | ||
const current = ul.querySelector("[aria-current]"); | ||
|
||
if (current) { | ||
let parent = current.parentElement; | ||
|
||
while (parent) { | ||
if (parent.tagName === "DETAILS") { | ||
parent.open = true; | ||
parent = parent.parentElement.parentElement; | ||
} else { | ||
parent = parent.parentElement; | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
); | ||
|
||
function decodeURIComponentSafe(path) { | ||
return decodeURIComponent(path.replace(/%(?![0-9a-fA-F]+)/g, "%25")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const url = "/menu.json"; | ||
|
||
export default function ({ nav }: Lume.Data) { | ||
const menu = nav.menu("/", "", "order=asc basename=asc").children; | ||
|
||
return JSON.stringify(menu, null, 2); | ||
} |