@@ -2,6 +2,7 @@ import Base from "../../core/base";
2
2
import Parser from "../../core/parser" ;
3
3
import logging from "../../core/logging" ;
4
4
import events from "../../core/events" ;
5
+ import utils from "../../core/utils" ;
5
6
6
7
const log = logging . getLogger ( "navigation" ) ;
7
8
@@ -18,14 +19,7 @@ export default Base.extend({
18
19
this . options = parser . parse ( this . el , this . options ) ;
19
20
20
21
this . init_listeners ( ) ;
21
-
22
- if ( this . el . querySelector ( this . options . currentClass ) ) {
23
- log . debug ( "Mark navigation items based on existing current class" ) ;
24
- this . mark_current ( ) ;
25
- } else {
26
- log . debug ( "Mark navigation items based on URL pattern." ) ;
27
- this . mark_items_url ( ) ;
28
- }
22
+ this . init_markings ( ) ;
29
23
} ,
30
24
31
25
/**
@@ -68,15 +62,13 @@ export default Base.extend({
68
62
this . el . querySelector ( `a.${ current } , .${ current } a` ) ?. click ( ) ;
69
63
}
70
64
65
+ const debounced_init_markings = utils . debounce (
66
+ this . init_markings . bind ( this ) ,
67
+ 100
68
+ ) ;
71
69
// Re-init when navigation changes.
72
70
const observer = new MutationObserver ( ( ) => {
73
- if ( this . el . querySelector ( this . options . currentClass ) ) {
74
- log . debug ( "Mark navigation items based on existing current class" ) ;
75
- this . mark_current ( ) ;
76
- } else {
77
- log . debug ( "Mark navigation items based on URL pattern." ) ;
78
- this . mark_items_url ( ) ;
79
- }
71
+ debounced_init_markings ( ) ;
80
72
} ) ;
81
73
observer . observe ( this . el , {
82
74
childList : true ,
@@ -86,6 +78,19 @@ export default Base.extend({
86
78
} ) ;
87
79
} ,
88
80
81
+ /**
82
+ * Initial run to mark the current item and its parents.
83
+ */
84
+ init_markings ( ) {
85
+ if ( this . el . querySelector ( this . options . currentClass ) ) {
86
+ log . debug ( "Mark navigation items based on existing current class" ) ;
87
+ this . mark_current ( ) ;
88
+ } else {
89
+ log . debug ( "Mark navigation items based on URL pattern." ) ;
90
+ this . mark_items_url ( ) ;
91
+ }
92
+ } ,
93
+
89
94
/**
90
95
* Get a matching parent or stop at stop_el.
91
96
*
0 commit comments