Skip to content

Commit

Permalink
feat(navigation): add simple anchor mecanism for views
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Nov 28, 2023
1 parent a38a550 commit 7bff7ab
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion public/js/components/navigation.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
// Import Internal Dependencies
import { PackageInfo } from "./package/package.js";

// CONSTANTS
const kAvailableView = new Set([
"network--view",
"home--view",
"settings--view"
]);

export class ViewNavigation {
static DefaultActiveMenu = "network--view";

constructor() {
this.activeMenu = null;
this.menus = new Map();

const defaultMenu = this.getAnchor();
const navElements = document.querySelectorAll("#view-navigation li");
for (const navigationMenu of navElements) {
const menuName = navigationMenu.getAttribute("data-menu");
this.menus.set(menuName, navigationMenu);

if (menuName === ViewNavigation.DefaultActiveMenu) {
if (menuName === defaultMenu) {
this.setNewActiveMenu(navigationMenu);
}

Expand Down Expand Up @@ -54,6 +62,7 @@ export class ViewNavigation {

document.getElementById(menuName).classList.remove("hidden");
selectedNav.classList.add("active");
this.setAnchor(menuName);

this.activeMenu = selectedNav;
}
Expand All @@ -66,6 +75,20 @@ export class ViewNavigation {
view.classList.add("hidden");
}

getAnchor() {
const documentURL = new URL(document.URL);
const anchorName = documentURL.searchParams.get("view") ?? ViewNavigation.DefaultActiveMenu;

return kAvailableView.has(anchorName) ? anchorName : ViewNavigation.DefaultActiveMenu;
}

setAnchor(anchorName) {
const newDocumentURL = new URL(document.URL);
newDocumentURL.searchParams.set("view", anchorName);

window.history.replaceState(void 0, void 0, newDocumentURL);
}

/**
* @param {!HTMLElement} selectedNav
*/
Expand Down

0 comments on commit 7bff7ab

Please sign in to comment.