Skip to content

Commit 0b099cc

Browse files
committed
fix(pat navigation): Initialize the click handler on the element directly.
This way it needs to be initialized only once, even if the subtree changes. Remove the init_listeners from the mutation observer for better performance.
1 parent 1e5afe7 commit 0b099cc

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/pat/navigation/navigation.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ export default Base.extend({
3434
init_listeners() {
3535
const current = this.options.currentClass;
3636

37+
events.add_event_listener(
38+
this.el,
39+
"click",
40+
"pat_navigation_click_handler",
41+
(ev) => {
42+
if (ev.target.matches("a:not(.pat-inject)")) {
43+
// Remove all set current classes
44+
this.clear_items();
45+
// Mark the current item
46+
this.mark_current(ev.target);
47+
}
48+
}
49+
);
50+
3751
// Mark the navigation items after pat-inject triggered within this navigation menu.
3852
this.$el.on("patterns-inject-triggered", "a", (ev) => {
3953
// Remove all set current classes
@@ -43,17 +57,6 @@ export default Base.extend({
4357
this.mark_current(ev.target);
4458
});
4559

46-
const items_non_inject = this.el.querySelectorAll("a:not(.pat-inject)");
47-
for (const it of items_non_inject) {
48-
events.add_event_listener(it, "click", "pat_nav_item_non_inject", (ev) => {
49-
// Remove all set current classes
50-
this.clear_items();
51-
52-
// Mark the current item
53-
this.mark_current(ev.target);
54-
});
55-
}
56-
5760
// Automatically and recursively load the ``.current`` item.
5861
if (this.el.classList.contains("navigation-load-current")) {
5962
// Check for current elements injected here.
@@ -67,8 +70,6 @@ export default Base.extend({
6770

6871
// Re-init when navigation changes.
6972
const observer = new MutationObserver(() => {
70-
this.init_listeners();
71-
7273
if (this.el.querySelector(this.options.currentClass)) {
7374
log.debug("Mark navigation items based on existing current class");
7475
this.mark_current();

0 commit comments

Comments
 (0)