@@ -496,7 +496,6 @@ export var Util = {
496496 if ( [ '/' , '.' , '#' ] . includes ( link . url [ 0 ] ) ) link . target = '_self' ;
497497 }
498498 if ( ! justPan ) {
499- let newLayer = false ;
500499 layer = document . createElement ( 'layer-' ) ;
501500 layer . setAttribute ( 'src' , link . url ) ;
502501 layer . setAttribute ( 'checked' , '' ) ;
@@ -505,18 +504,12 @@ export var Util = {
505504 if ( link . type === 'text/html' ) {
506505 window . open ( link . url ) ;
507506 } else {
508- newLayer = true ;
509- if ( ! link . inPlace && zoomTo ) {
510- updateMapZoomTo ( zoomTo ) ;
511- }
507+ postTraversalSetup ( ) ;
512508 map . options . mapEl . appendChild ( layer ) ;
513509 }
514510 break ;
515511 case '_parent' :
516- newLayer = true ;
517- if ( ! link . inPlace && zoomTo ) {
518- updateMapZoomTo ( zoomTo ) ;
519- }
512+ postTraversalSetup ( ) ;
520513 for ( let l of map . options . mapEl . querySelectorAll ( 'layer-' ) )
521514 if ( l . _layer !== leafletLayer ) map . options . mapEl . removeChild ( l ) ;
522515 map . options . mapEl . appendChild ( layer ) ;
@@ -526,25 +519,11 @@ export var Util = {
526519 window . location . href = link . url ;
527520 break ;
528521 default :
529- newLayer = true ;
530- if ( ! link . inPlace && zoomTo ) {
531- updateMapZoomTo ( zoomTo ) ;
532- }
522+ postTraversalSetup ( ) ;
533523 opacity = leafletLayer . _layerEl . opacity ;
534524 leafletLayer . _layerEl . insertAdjacentElement ( 'beforebegin' , layer ) ;
535525 map . options . mapEl . removeChild ( leafletLayer . _layerEl ) ;
536526 }
537- if ( ! link . inPlace && newLayer )
538- layer . whenReady ( ) . then ( ( ) => {
539- if ( ! layer . extent ) {
540- layer . _layer . _setLayerElExtent ( ) ;
541- }
542- if ( zoomTo )
543- layer . parentElement . zoomTo ( + zoomTo . lat , + zoomTo . lng , + zoomTo . z ) ;
544- else layer . zoomTo ( ) ;
545- if ( opacity ) layer . opacity = opacity ;
546- map . getContainer ( ) . focus ( ) ;
547- } ) ;
548527 } else if ( zoomTo && ! link . inPlace && justPan ) {
549528 leafletLayer . _map . options . mapEl . zoomTo (
550529 + zoomTo . lat ,
@@ -554,7 +533,38 @@ export var Util = {
554533 if ( opacity ) layer . opacity = opacity ;
555534 }
556535
536+ function postTraversalSetup ( ) {
537+ // when the projection is changed as part of the link traversal process,
538+ // it's necessary to set the map viewer's lat, lon and zoom NOW, so that
539+ // the promises that are created when the viewer's projection is changed
540+ // can use the viewer's lat, lon and zoom properties that were in effect
541+ // before the projection change i.e. in the closure for that code
542+ // see mapml-viewer / map is=web-map projection attributeChangedCallback
543+ // specifically required for use cases like changing projection after
544+ // link traversal, e.g. BC link here https://maps4html.org/experiments/linking/features/
545+ if ( ! link . inPlace && zoomTo ) updateMapZoomTo ( zoomTo ) ;
546+ // the layer is newly created, so have to wait until it's fully init'd
547+ // before setting properties.
548+ layer . whenReady ( ) . then ( ( ) => {
549+ // TODO refactor _setLayerElExtent so that it's invoked automatically
550+ // by layer.extent getter TBD.
551+ if ( ! layer . extent ) {
552+ layer . _layer . _setLayerElExtent ( ) ;
553+ }
554+ // if the map projection isnt' changed by link traversal, it's necessary
555+ // to perform pan/zoom operations after the layer is ready
556+ if ( ! link . inPlace && zoomTo )
557+ layer . parentElement . zoomTo ( + zoomTo . lat , + zoomTo . lng , + zoomTo . z ) ;
558+ else if ( ! link . inPlace ) layer . zoomTo ( ) ;
559+ // not sure if this is necessary
560+ if ( opacity ) layer . opacity = opacity ;
561+ // this is necessary to display the FeatureIndexOverlay, I believe
562+ map . getContainer ( ) . focus ( ) ;
563+ } ) ;
564+ }
565+
557566 function updateMapZoomTo ( zoomTo ) {
567+ // can't use mapEl.zoomTo(...) here, it's too slow!
558568 map . options . mapEl . lat = + zoomTo . lat ;
559569 map . options . mapEl . lon = + zoomTo . lng ;
560570 map . options . mapEl . zoom = + zoomTo . z ;
0 commit comments