|
1 | | -import $ from "jquery"; |
2 | | -import Base from "@patternslib/patternslib/src/core/base"; |
3 | | - |
4 | | -export default Base.extend({ |
5 | | - name: "navigationmarker", |
6 | | - trigger: ".pat-navigationmarker", |
7 | | - parser: "mockup", |
8 | | - init: function () { |
9 | | - const portal_url = document.body.dataset.portalUrl; |
10 | | - var href = |
11 | | - document.querySelector('head link[rel="canonical"]').href || |
| 1 | +import { BasePattern } from "@patternslib/patternslib/src/core/basepattern"; |
| 2 | +import Parser from "@patternslib/patternslib/src/core/parser"; |
| 3 | +import registry from "@patternslib/patternslib/src/core/registry"; |
| 4 | + |
| 5 | +export const parser = new Parser("navigationmarker"); |
| 6 | +parser.addArgument("portal-url", undefined); |
| 7 | + |
| 8 | +class Pattern extends BasePattern { |
| 9 | + static name = "navigationmarker"; |
| 10 | + static trigger = ".pat-navigationmarker"; |
| 11 | + static parser = parser; |
| 12 | + |
| 13 | + init() { |
| 14 | + this.portal_url = this.options.portalUrl || document.body.dataset.portalUrl; |
| 15 | + |
| 16 | + this.scan_navigation(); |
| 17 | + } |
| 18 | + |
| 19 | + scan_navigation() { |
| 20 | + const current_url = |
| 21 | + document.querySelector('head link[rel="canonical"]')?.href || |
12 | 22 | window.location.href; |
13 | 23 |
|
14 | | - $("a", this.$el).each(function () { |
15 | | - var navlink = this.href.replace("/view", ""); |
16 | | - if (href.indexOf(navlink) !== -1) { |
17 | | - var parent = $(this).parent(); |
18 | | - |
19 | | - // check the input-openers within the path |
20 | | - var check = parent.find("> input"); |
21 | | - if (check.length) { |
22 | | - check[0].checked = true; |
23 | | - } |
24 | | - |
25 | | - // set "inPath" to all nav items which are within the current path |
26 | | - // check if parts of navlink are in canonical url parts |
27 | | - var hrefParts = href.split("/"); |
28 | | - var navParts = navlink.split("/"); |
29 | | - var inPath = false; |
30 | | - for (var i = 0, size = navParts.length; i < size; i++) { |
31 | | - // The last path-part must match. |
32 | | - inPath = false; |
33 | | - if (navParts[i] === hrefParts[i]) { |
34 | | - inPath = true; |
35 | | - } |
36 | | - } |
37 | | - if (navlink === portal_url && href !== portal_url) { |
38 | | - // Avoid marking "Home" with "inPath", when not actually there |
39 | | - inPath = false; |
40 | | - } |
41 | | - if (inPath) { |
42 | | - parent.addClass("inPath"); |
43 | | - } |
44 | | - |
45 | | - // set "current" to the current selected nav item, if it is in the navigation structure. |
46 | | - if (href === navlink) { |
47 | | - parent.addClass("current"); |
48 | | - } |
| 24 | + const nav_items = this.el.querySelectorAll("a"); |
| 25 | + |
| 26 | + for (const nav_item of nav_items) { |
| 27 | + const parent = nav_item.parentElement; |
| 28 | + const nav_url = nav_item.href.replace("/view", ""); |
| 29 | + |
| 30 | + // We can exit early, if the nav_url is not part of the current URL. |
| 31 | + if (current_url.indexOf(nav_url) === -1) { |
| 32 | + this.clear(parent); |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + // BBB |
| 37 | + // check the input-openers within the path |
| 38 | + const check = parent.querySelector(":scope > input[type=checkbox]"); |
| 39 | + if (check) { |
| 40 | + check.checked = true; |
49 | 41 | } |
50 | | - }); |
51 | | - }, |
52 | | -}); |
| 42 | + |
| 43 | + // set "inPath" to all nav items which are within the current path |
| 44 | + // check if parts of nav_url are in canonical url parts |
| 45 | + // |
| 46 | + const current_url_parts = current_url.split("/"); |
| 47 | + const nav_url_parts = nav_url.split("/"); |
| 48 | + let in_path = false; |
| 49 | + |
| 50 | + // The last part of the URL must match. |
| 51 | + const nav_url_part = nav_url_parts[nav_url_parts.length - 1]; |
| 52 | + const current_url_part = current_url_parts[nav_url_parts.length - 1]; |
| 53 | + if (nav_url_part === current_url_part) { |
| 54 | + in_path = true; |
| 55 | + } |
| 56 | + |
| 57 | + // Avoid marking "Home" with "inPath", when not actually there |
| 58 | + if (nav_url === this.portal_url && current_url !== this.portal_url) { |
| 59 | + in_path = false; |
| 60 | + } |
| 61 | + |
| 62 | + // Set the class |
| 63 | + if (in_path) { |
| 64 | + parent.classList.add("inPath"); |
| 65 | + } |
| 66 | + |
| 67 | + // set "current" to the current selected nav item, if it is in the navigation structure. |
| 68 | + if (current_url === nav_url) { |
| 69 | + parent.classList.add("current"); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + clear(element) { |
| 75 | + // Clear all classes |
| 76 | + if (element.classList.contains("inPath")) { |
| 77 | + element.classList.remove("inPath"); |
| 78 | + } |
| 79 | + if (element.classList.contains("current")) { |
| 80 | + element.classList.remove("current"); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Register Pattern class in the global pattern registry |
| 86 | +registry.register(Pattern); |
| 87 | + |
| 88 | +// Make it available |
| 89 | +export default Pattern; |
0 commit comments