@@ -2,6 +2,7 @@ import Base from "../../core/base";
22import Parser from "../../core/parser" ;
33import logging from "../../core/logging" ;
44import events from "../../core/events" ;
5+ import utils from "../../core/utils" ;
56
67const log = logging . getLogger ( "navigation" ) ;
78
@@ -18,14 +19,7 @@ export default Base.extend({
1819 this . options = parser . parse ( this . el , this . options ) ;
1920
2021 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 ( ) ;
2923 } ,
3024
3125 /**
@@ -68,15 +62,13 @@ export default Base.extend({
6862 this . el . querySelector ( `a.${ current } , .${ current } a` ) ?. click ( ) ;
6963 }
7064
65+ const debounced_init_markings = utils . debounce (
66+ this . init_markings . bind ( this ) ,
67+ 100
68+ ) ;
7169 // Re-init when navigation changes.
7270 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 ( ) ;
8072 } ) ;
8173 observer . observe ( this . el , {
8274 childList : true ,
@@ -86,6 +78,19 @@ export default Base.extend({
8678 } ) ;
8779 } ,
8880
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+
8994 /**
9095 * Get a matching parent or stop at stop_el.
9196 *
0 commit comments