Skip to content

Commit

Permalink
maint(pat navigationmarker): Simplify and improve the implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
thet committed Sep 8, 2022
1 parent 81c9110 commit 30d04e3
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions src/pat/navigationmarker/navigationmarker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import $ from "jquery";
import Base from "@patternslib/patternslib/src/core/base";

export default Base.extend({
Expand All @@ -7,49 +6,51 @@ export default Base.extend({
parser: "mockup",

init() {
const portal_url = document.body.dataset.portalUrl;
const href =
document.querySelector('head link[rel="canonical"]').href ||
window.location.href;
const hrefParts = href.split("/");

const current_url = this.base_url();
const portal_url = document.body.dataset?.portalUrl;
const nav_items = this.el.querySelectorAll("a");

for (const nav_item of nav_items) {
const navlink = nav_item.getAttribute("href", "").replace("/view", "");
if (href.indexOf(navlink) !== -1) {
const parent = $(nav_item).parent();

// check the input-openers within the path
const check = parent.find("> input");
if (check.length) {
check[0].checked = true;
}

// set "inPath" to all nav items which are within the current path
// check if parts of navlink are in canonical url parts
const navParts = navlink.split("/");
let inPath = false;
for (let i = 0, size = navParts.length; i < size; i++) {
// The last path-part must match.
inPath = false;
if (navParts[i] === hrefParts[i]) {
inPath = true;
}
}
if (navlink === portal_url && href !== portal_url) {
// Avoid marking "Home" with "inPath", when not actually there
inPath = false;
}
if (inPath) {
parent.addClass("inPath");
}

// set "current" to the current selected nav item, if it is in the navigation structure.
if (href === navlink) {
parent.addClass("current");
}
// Get the nav item's url and rebase it against the current url to
// make absolute or relative URLs FQDN URLs.
const nav_url = new URL(
this.prepare_url(nav_item.getAttribute("href", "")),
current_url
)?.href;

const wrapper = nav_item.parentNode;

if (nav_url === current_url) {
wrapper.classList.add("current");
} else if (
current_url.indexOf(`${nav_url}/`) === 0 && // Compare the current navigation item url with a slash at the end - it must match if it is in path.
nav_url !== portal_url // Do not set inPath for the "Home" url, as this would always be in the path.
) {
wrapper.classList.add("inPath");
} else {
// Not even in path.
continue;
}

// The path was at least found in the current url, so we need to
// check the input-openers within the path
// Find the first input which is the correct one, even if this
// navigation item has many children.
// These hidden checkboxes are used to open the navigation item for
// mobile navigation.
const check = wrapper.querySelector("input");
if (check) check.checked = true;
}
},

prepare_url(url) {
return url.replace("/view", "").replaceAll("@@", "").replace(/\/$/, "");
},

base_url() {
return this.prepare_url(
document.querySelector('head link[rel="canonical"]').href ||
window.location.href
);
},
});

0 comments on commit 30d04e3

Please sign in to comment.