Skip to content

Commit 5b0fc43

Browse files
committed
maint(pat navigation): Switch to class based pattern.
1 parent 6483649 commit 5b0fc43

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/pat/navigation/navigation.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import Base from "../../core/base";
1+
import $ from "jquery";
2+
import { BasePattern } from "../../core/basepattern";
23
import Parser from "../../core/parser";
34
import logging from "../../core/logging";
45
import events from "../../core/events";
6+
import registry from "../../core/registry";
57

68
const log = logging.getLogger("navigation");
79

@@ -10,16 +12,18 @@ parser.addArgument("item-wrapper", "li");
1012
parser.addArgument("in-path-class", "navigation-in-path");
1113
parser.addArgument("current-class", "current");
1214

13-
export default Base.extend({
14-
name: "navigation",
15-
trigger: ".pat-navigation",
15+
class Pattern extends BasePattern {
16+
static name = "navigation";
17+
static trigger = ".pat-navigation";
18+
static parser = parser;
1619

1720
init() {
1821
this.options = parser.parse(this.el, this.options);
22+
this.$el = $(this.el);
1923

2024
this.init_listeners();
2125
this.init_markings();
22-
},
26+
}
2327

2428
/**
2529
* Initialize listeners for the navigation.
@@ -64,7 +68,7 @@ export default Base.extend({
6468
});
6569
this.el.querySelector(`a.${current}, .${current} a`)?.click();
6670
}
67-
},
71+
}
6872

6973
/**
7074
* Initial run to mark the current item and its parents.
@@ -77,7 +81,7 @@ export default Base.extend({
7781
log.debug("Mark navigation items based on URL pattern.");
7882
this.mark_items_url();
7983
}
80-
},
84+
}
8185

8286
/**
8387
* Get a matching parent or stop at stop_el.
@@ -99,7 +103,7 @@ export default Base.extend({
99103
}
100104
matching_parent = matching_parent.parentNode;
101105
}
102-
},
106+
}
103107

104108
/**
105109
* Mark an item and it's wrapper as current.
@@ -120,7 +124,7 @@ export default Base.extend({
120124
this.mark_in_path(wrapper || item);
121125
log.debug("Statically set current item marked as current", item);
122126
}
123-
},
127+
}
124128

125129
/**
126130
* Mark all parent navigation elements as in path.
@@ -140,7 +144,7 @@ export default Base.extend({
140144
}
141145
path_el = this.get_parent(path_el, this.options.itemWrapper, this.el);
142146
}
143-
},
147+
}
144148

145149
/**
146150
* Mark all navigation items that are in the path of the current url.
@@ -183,7 +187,7 @@ export default Base.extend({
183187
continue;
184188
}
185189
}
186-
},
190+
}
187191

188192
/**
189193
* Clear all navigation items from the inPath and current classes
@@ -196,7 +200,7 @@ export default Base.extend({
196200
item.classList.remove(this.options.inPathClass);
197201
item.classList.remove(this.options.currentClass);
198202
}
199-
},
203+
}
200204

201205
/**
202206
* Prepare a URL for comparison.
@@ -208,7 +212,7 @@ export default Base.extend({
208212
*/
209213
prepare_url(url) {
210214
return url?.replace("/view", "").replaceAll("@@", "").replace(/\/$/, "");
211-
},
215+
}
212216

213217
/**
214218
* Get the URL of the current page.
@@ -223,5 +227,9 @@ export default Base.extend({
223227
document.querySelector('head link[rel="canonical"]')?.href ||
224228
window.location.href
225229
);
226-
},
227-
});
230+
}
231+
}
232+
233+
registry.register(Pattern);
234+
export default Pattern;
235+
export { Pattern };

src/pat/navigation/navigation.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "../inject/inject";
22
import Pattern from "./navigation";
33
import Registry from "../../core/registry";
4+
import events from "../../core/events";
45
import utils from "../../core/utils";
56

67
describe("1 - Navigation pattern tests", function () {
@@ -246,7 +247,7 @@ describe("3 - Navigation pattern tests - Mark items based on URL", () => {
246247
document.body.dataset.portalUrl = portal_url;
247248
};
248249

249-
it("navigation roundtrip", () => {
250+
it("navigation roundtrip", async () => {
250251
document.body.innerHTML = `
251252
<nav class="pat-navigation"
252253
data-pat-navigation="in-path-class: inPath">
@@ -292,6 +293,7 @@ describe("3 - Navigation pattern tests - Mark items based on URL", () => {
292293
set_url("https://patternslib.com/");
293294

294295
const instance = new Pattern(document.querySelector(".pat-navigation"));
296+
await events.await_pattern_init(instance);
295297

296298
const it0 = document.querySelector("a[href='/']");
297299
const it1 = document.querySelector("a[href='/path1']");
@@ -384,7 +386,7 @@ describe("4 - Navigation pattern tests - Mark items based based clicking without
384386
document.body.innerHTML = "";
385387
});
386388

387-
it("navigation roundtrip", () => {
389+
it("navigation roundtrip", async () => {
388390
document.body.innerHTML = `
389391
<nav class="pat-navigation"
390392
data-pat-navigation="in-path-class: inPath">
@@ -419,6 +421,7 @@ describe("4 - Navigation pattern tests - Mark items based based clicking without
419421
`;
420422

421423
const instance = new Pattern(document.querySelector(".pat-navigation"));
424+
await events.await_pattern_init(instance);
422425

423426
const it0 = document.querySelector("a[href='/home']");
424427
const it1 = document.querySelector("a[href='/path1']");

0 commit comments

Comments
 (0)