Skip to content

Commit 2080c88

Browse files
authored
Merge pull request #1058 from Patternslib/navigation-mark-after-inject
feat(pat navigation): Mark the navigation items after injection.
2 parents 9c0dcb5 + ec2a795 commit 2080c88

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/pat/navigation/navigation.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ export default Base.extend({
4545
this.$el.on("patterns-inject-triggered", "a", (ev) => {
4646
// Remove all set current classes
4747
this.clear_items();
48-
4948
// Mark the current item
5049
this.mark_current(ev.target);
5150
});
5251

52+
// Mark the navigation items after pat-inject injected into this navigation menu.
53+
this.$el.on("patterns-injected-scanned", () => {
54+
this.init_markings();
55+
});
56+
5357
// Automatically and recursively load the ``.current`` item.
5458
if (this.el.classList.contains("navigation-load-current")) {
5559
// Check for current elements injected here.

src/pat/navigation/navigation.test.js

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Pattern from "./navigation";
33
import Registry from "../../core/registry";
44
import utils from "../../core/utils";
55

6-
describe("Navigation pattern tests", function () {
6+
describe("1 - Navigation pattern tests", function () {
77
beforeEach(function () {
88
document.body.innerHTML = `
99
<div id="page_wrapper">
@@ -152,7 +152,80 @@ describe("Navigation pattern tests", function () {
152152
});
153153
});
154154

155-
describe("Navigation pattern tests - Mark items based on URL", () => {
155+
describe("2 - Navigation pattern tests - mark after navigation injection", function () {
156+
let _window_location;
157+
158+
beforeEach(() => {
159+
_window_location = global.window.location;
160+
delete global.window.location;
161+
document.body.innerHTML = "";
162+
});
163+
164+
afterEach(() => {
165+
global.window.location = _window_location;
166+
document.body.innerHTML = "";
167+
});
168+
169+
const set_url = (url, portal_url) => {
170+
global.window.location = {
171+
href: url,
172+
};
173+
174+
portal_url = portal_url || url;
175+
176+
document.body.dataset.portalUrl = portal_url;
177+
};
178+
179+
it("navigation roundtrip", async () => {
180+
document.body.innerHTML = `
181+
<div id="injected_nav">
182+
<div class="w1">
183+
<a href="/path/to" class="a1">link a1</a>
184+
<div class="w11">
185+
<a href="/path/to/test" class="a11">link a11</a>
186+
</div>
187+
</div>
188+
</div>
189+
<a
190+
href="#injected_nav"
191+
class="pat-inject load-nav"
192+
data-pat-inject="target: #injection_target">load navigation</a>
193+
<nav
194+
id="injection_target"
195+
class="pat-navigation nav"
196+
data-pat-navigation="item-wrapper: div">
197+
</nav>
198+
`;
199+
200+
set_url("https://patternslib.com/path/to/test");
201+
202+
Registry.scan(document.body);
203+
204+
const nav = document.querySelector("nav");
205+
const load_nav = document.querySelector(".load-nav");
206+
load_nav.click();
207+
208+
await utils.timeout(1); // wait for MutationObserver
209+
210+
const w1 = nav.querySelector(".w1");
211+
const a1 = nav.querySelector(".a1");
212+
const w11 = nav.querySelector(".w11");
213+
const a11 = nav.querySelector(".a11");
214+
215+
console.log(document.body.innerHTML);
216+
217+
expect(w1.classList.contains("current")).toBeFalsy();
218+
expect(w1.classList.contains("navigation-in-path")).toBeTruthy();
219+
expect(a1.classList.contains("current")).toBeFalsy();
220+
expect(a1.classList.contains("navigation-in-path")).toBeTruthy();
221+
expect(w11.classList.contains("current")).toBeTruthy();
222+
expect(w11.classList.contains("navigation-in-path")).toBeFalsy();
223+
expect(a11.classList.contains("current")).toBeTruthy();
224+
expect(a11.classList.contains("navigation-in-path")).toBeFalsy();
225+
});
226+
});
227+
228+
describe("3 - Navigation pattern tests - Mark items based on URL", () => {
156229
let _window_location;
157230

158231
beforeEach(() => {
@@ -304,7 +377,7 @@ describe("Navigation pattern tests - Mark items based on URL", () => {
304377
});
305378
});
306379

307-
describe("Navigation pattern tests - Mark items based based clicking without pat-inject.", () => {
380+
describe("4 - Navigation pattern tests - Mark items based based clicking without pat-inject.", () => {
308381
beforeEach(() => {
309382
document.body.innerHTML = "";
310383
});

0 commit comments

Comments
 (0)