@@ -1194,6 +1194,26 @@ class NetJSONGraphUtil {
11941194 } ;
11951195 }
11961196
1197+ /**
1198+ * Parse the URL hash into a mapping of map IDs to their parameters.
1199+ *
1200+ * The URL hash may contain multiple fragments separated by `;`, where each
1201+ * fragment corresponds to a single Netjsongraph.js instance on the page.
1202+ * Each fragment must include an `id` parameter identifying the map, along
1203+ * with additional parameters such as `nodeId` or `zoom`.
1204+ *
1205+ * Example:
1206+ * #id=map1&nodeId=2;id=map2&nodeId=4
1207+ *
1208+ * Result:
1209+ * {
1210+ * map1: URLSearchParams("id=map1&nodeId=2"),
1211+ * map2: URLSearchParams("id=map2&nodeId=4")
1212+ * }
1213+ *
1214+ * @returns {Object.<string, URLSearchParams> }
1215+ * An object mapping map IDs to their corresponding URLSearchParams.
1216+ */
11971217 parseUrlFragments ( ) {
11981218 const raw = window . location . hash . replace ( / ^ # / , "" ) ;
11991219 const fragments = { } ;
@@ -1207,23 +1227,39 @@ class NetJSONGraphUtil {
12071227 return fragments ;
12081228 }
12091229
1210- generateUrlFragments ( fragments ) {
1211- return Object . values ( fragments )
1230+ /**
1231+ * Reverse of parseUrlFragments.
1232+ *
1233+ * Converts a fragments object (map IDs → URLSearchParams) back into
1234+ * a semicolon-delimited string suitable for the URL hash.
1235+ *
1236+ * @param {Object.<string, URLSearchParams> } fragments
1237+ * @returns {string }
1238+ */
1239+ updateUrlFragments ( fragments , state ) {
1240+ const newHash = Object . values ( fragments )
12121241 . map ( ( urlParams ) => urlParams . toString ( ) )
12131242 . join ( ";" ) ;
1243+
1244+ // We store the selected node's data to the browser's history state.
1245+ // This allows the node's information to be retrieved instantly on a back/forward
1246+ // button click without needing to re-parse the entire nodes list.
1247+ history . pushState ( state , "" , `#${ newHash } ` ) ;
12141248 }
12151249
1216- setUrlFragments ( self , params ) {
1217- if ( ! self . config . bookmarkableActions . enabled || params . data . cluster ) return ;
1250+ addActionToUrl ( self , params ) {
1251+ if ( ! self . config . bookmarkableActions . enabled || params . data . cluster ) {
1252+ return ;
1253+ }
12181254 const fragments = this . parseUrlFragments ( ) ;
12191255 const id = self . config . bookmarkableActions . id ;
12201256 let nodeId ;
12211257 self . indexedNode = self . indexedNode || { } ;
1222- if ( params . componentSubType === "graph" ) {
1258+ if ( self . config . render === self . utils . graphRender ) {
12231259 nodeId = params . data . id ;
12241260 self . indexedNode [ nodeId ] = params . data ;
12251261 }
1226- if ( [ "scatter" , "effectScatter" ] . includes ( params . componentSubType ) ) {
1262+ if ( self . config . render === self . utils . mapRender ) {
12271263 nodeId = params . data . node . id ;
12281264 self . indexedNode [ nodeId ] = params . data . node ;
12291265 }
@@ -1232,24 +1268,23 @@ class NetJSONGraphUtil {
12321268 fragments [ id ] . set ( "id" , id ) ;
12331269 }
12341270 fragments [ id ] . set ( "nodeId" , nodeId ) ;
1235- const newHash = this . generateUrlFragments ( fragments ) ;
12361271 const state = self . indexedNode [ nodeId ] ;
1237- history . pushState ( state , "" , `# ${ newHash } ` ) ;
1272+ this . updateUrlFragments ( fragments , state ) ;
12381273 }
12391274
12401275 removeUrlFragment ( id ) {
12411276 const fragments = this . parseUrlFragments ( ) ;
12421277 if ( fragments [ id ] ) {
12431278 delete fragments [ id ] ;
12441279 }
1245- const newHash = this . generateUrlFragments ( fragments ) ;
12461280 const state = { id} ;
1247- history . pushState ( state , "" , `# ${ newHash } ` ) ;
1281+ this . updateUrlFragments ( fragments , state ) ;
12481282 }
12491283
12501284 setIndexedNodeFromUrlFragments ( self , fragments , node ) {
1251- if ( ! self . config . bookmarkableActions . enabled || ! Object . keys ( fragments ) . length )
1285+ if ( ! self . config . bookmarkableActions . enabled || ! Object . keys ( fragments ) . length ) {
12521286 return ;
1287+ }
12531288 const id = self . config . bookmarkableActions . id ;
12541289 const nodeId = fragments [ id ] ?. get ( "nodeId" ) ;
12551290 if ( nodeId === node . id ) {
@@ -1259,11 +1294,15 @@ class NetJSONGraphUtil {
12591294 }
12601295
12611296 applyUrlFragmentState ( self ) {
1262- if ( ! self . config . bookmarkableActions . enabled ) return ;
1297+ if ( ! self . config . bookmarkableActions . enabled ) {
1298+ return ;
1299+ }
12631300 const id = self . config . bookmarkableActions . id ;
12641301 const fragments = self . utils . parseUrlFragments ( ) ;
12651302 const nodeId = fragments [ id ] ?. get ( "nodeId" ) ;
1266- if ( ! self . indexedNode || ! self . indexedNode [ nodeId ] ) return ;
1303+ if ( ! self . indexedNode || ! self . indexedNode [ nodeId ] ) {
1304+ return ;
1305+ }
12671306 const node = self . indexedNode [ nodeId ] ;
12681307 const nodeType =
12691308 self . config . graphConfig . series . type || self . config . mapOptions . nodeConfig . type ;
0 commit comments