@@ -21,41 +21,65 @@ import { i18n } from '@kbn/i18n';
2121import { vega } from '../lib/vega' ;
2222import { VegaBaseView } from './vega_base_view' ;
2323import { VegaMapLayer } from './vega_map_layer' ;
24- import { getEmsTileLayerId , getUISettings } from '../services' ;
25- import { lazyLoadMapsLegacyModules } from '../../../maps_legacy/public' ;
24+ import { getMapsLegacyConfig , getUISettings } from '../services' ;
25+ import { lazyLoadMapsLegacyModules , TMS_IN_YML_ID } from '../../../maps_legacy/public' ;
26+
27+ const isUserConfiguredTmsLayer = ( { tilemap } ) => Boolean ( tilemap . url ) ;
2628
2729export class VegaMapView extends VegaBaseView {
2830 constructor ( opts ) {
2931 super ( opts ) ;
3032 }
3133
34+ async getMapStyleOptions ( ) {
35+ const isDarkMode = getUISettings ( ) . get ( 'theme:darkMode' ) ;
36+ const mapsLegacyConfig = getMapsLegacyConfig ( ) ;
37+ const tmsServices = await this . _serviceSettings . getTMSServices ( ) ;
38+ const mapConfig = this . _parser . mapConfig ;
39+
40+ let mapStyle ;
41+
42+ if ( mapConfig . mapStyle !== 'default' ) {
43+ mapStyle = mapConfig . mapStyle ;
44+ } else {
45+ if ( isUserConfiguredTmsLayer ( mapsLegacyConfig ) ) {
46+ mapStyle = TMS_IN_YML_ID ;
47+ } else {
48+ mapStyle = mapsLegacyConfig . emsTileLayerId . bright ;
49+ }
50+ }
51+
52+ const mapOptions = tmsServices . find ( ( s ) => s . id === mapStyle ) ;
53+
54+ if ( ! mapOptions ) {
55+ this . onWarn (
56+ i18n . translate ( 'visTypeVega.mapView.mapStyleNotFoundWarningMessage' , {
57+ defaultMessage : '{mapStyleParam} was not found' ,
58+ values : { mapStyleParam : `"mapStyle":${ mapStyle } ` } ,
59+ } )
60+ ) ;
61+ return null ;
62+ }
63+
64+ return {
65+ ...mapOptions ,
66+ ...( await this . _serviceSettings . getAttributesForTMSLayer ( mapOptions , true , isDarkMode ) ) ,
67+ } ;
68+ }
69+
3270 async _initViewCustomizations ( ) {
3371 const mapConfig = this . _parser . mapConfig ;
3472 let baseMapOpts ;
3573 let limitMinZ = 0 ;
3674 let limitMaxZ = 25 ;
3775
76+ // In some cases, Vega may be initialized twice, e.g. after awaiting...
77+ if ( ! this . _$container ) return ;
78+
3879 if ( mapConfig . mapStyle !== false ) {
39- const tmsServices = await this . _serviceSettings . getTMSServices ( ) ;
40- // In some cases, Vega may be initialized twice, e.g. after awaiting...
41- if ( ! this . _$container ) return ;
42- const emsTileLayerId = getEmsTileLayerId ( ) ;
43- const mapStyle =
44- mapConfig . mapStyle === 'default' ? emsTileLayerId . bright : mapConfig . mapStyle ;
45- const isDarkMode = getUISettings ( ) . get ( 'theme:darkMode' ) ;
46- baseMapOpts = tmsServices . find ( ( s ) => s . id === mapStyle ) ;
47- baseMapOpts = {
48- ...baseMapOpts ,
49- ...( await this . _serviceSettings . getAttributesForTMSLayer ( baseMapOpts , true , isDarkMode ) ) ,
50- } ;
51- if ( ! baseMapOpts ) {
52- this . onWarn (
53- i18n . translate ( 'visTypeVega.mapView.mapStyleNotFoundWarningMessage' , {
54- defaultMessage : '{mapStyleParam} was not found' ,
55- values : { mapStyleParam : `"mapStyle": ${ JSON . stringify ( mapStyle ) } ` } ,
56- } )
57- ) ;
58- } else {
80+ baseMapOpts = await this . getMapStyleOptions ( ) ;
81+
82+ if ( baseMapOpts ) {
5983 limitMinZ = baseMapOpts . minZoom ;
6084 limitMaxZ = baseMapOpts . maxZoom ;
6185 }
0 commit comments