diff --git a/src/plugins/maps_legacy/public/common/constants/index.ts b/src/plugins/maps_legacy/public/common/constants/index.ts new file mode 100644 index 00000000000000..8e677942b8bce0 --- /dev/null +++ b/src/plugins/maps_legacy/public/common/constants/index.ts @@ -0,0 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export const TMS_IN_YML_ID = 'TMS in config/kibana.yml'; + +export { ORIGIN } from './origin'; diff --git a/src/plugins/maps_legacy/public/index.ts b/src/plugins/maps_legacy/public/index.ts index 84edce6dbf8e97..5b70d861900f91 100644 --- a/src/plugins/maps_legacy/public/index.ts +++ b/src/plugins/maps_legacy/public/index.ts @@ -53,7 +53,7 @@ export { export * from '../common'; export * from './common/types'; -export { ORIGIN } from './common/constants/origin'; +export { ORIGIN, TMS_IN_YML_ID } from './common/constants'; export { WmsOptions } from './components/wms_options'; export { LegacyMapDeprecationMessage } from './components/legacy_map_deprecation_message'; diff --git a/src/plugins/maps_legacy/public/map/service_settings.js b/src/plugins/maps_legacy/public/map/service_settings.js index 7a00456b89a92e..8f0cb81576b2e6 100644 --- a/src/plugins/maps_legacy/public/map/service_settings.js +++ b/src/plugins/maps_legacy/public/map/service_settings.js @@ -22,9 +22,7 @@ import MarkdownIt from 'markdown-it'; import { EMSClient } from '@elastic/ems-client'; import { i18n } from '@kbn/i18n'; import { getKibanaVersion } from '../kibana_services'; -import { ORIGIN } from '../common/constants/origin'; - -const TMS_IN_YML_ID = 'TMS in config/kibana.yml'; +import { ORIGIN, TMS_IN_YML_ID } from '../common/constants'; export class ServiceSettings { constructor(mapConfig, tilemapsConfig) { diff --git a/src/plugins/vis_type_vega/public/services.ts b/src/plugins/vis_type_vega/public/services.ts index 43856c83248477..8d8a0e27ec2255 100644 --- a/src/plugins/vis_type_vega/public/services.ts +++ b/src/plugins/vis_type_vega/public/services.ts @@ -45,4 +45,3 @@ export const [getMapsLegacyConfig, setMapsLegacyConfig] = createGetterSetter getInjectedVars().enableExternalUrls; -export const getEmsTileLayerId = () => getMapsLegacyConfig().emsTileLayerId; diff --git a/src/plugins/vis_type_vega/public/vega_view/vega_map_view.js b/src/plugins/vis_type_vega/public/vega_view/vega_map_view.js index d925aaeeced66b..ce3cd1549b0c9e 100644 --- a/src/plugins/vis_type_vega/public/vega_view/vega_map_view.js +++ b/src/plugins/vis_type_vega/public/vega_view/vega_map_view.js @@ -21,41 +21,65 @@ import { i18n } from '@kbn/i18n'; import { vega } from '../lib/vega'; import { VegaBaseView } from './vega_base_view'; import { VegaMapLayer } from './vega_map_layer'; -import { getEmsTileLayerId, getUISettings } from '../services'; -import { lazyLoadMapsLegacyModules } from '../../../maps_legacy/public'; +import { getMapsLegacyConfig, getUISettings } from '../services'; +import { lazyLoadMapsLegacyModules, TMS_IN_YML_ID } from '../../../maps_legacy/public'; + +const isUserConfiguredTmsLayer = ({ tilemap }) => Boolean(tilemap.url); export class VegaMapView extends VegaBaseView { constructor(opts) { super(opts); } + async getMapStyleOptions() { + const isDarkMode = getUISettings().get('theme:darkMode'); + const mapsLegacyConfig = getMapsLegacyConfig(); + const tmsServices = await this._serviceSettings.getTMSServices(); + const mapConfig = this._parser.mapConfig; + + let mapStyle; + + if (mapConfig.mapStyle !== 'default') { + mapStyle = mapConfig.mapStyle; + } else { + if (isUserConfiguredTmsLayer(mapsLegacyConfig)) { + mapStyle = TMS_IN_YML_ID; + } else { + mapStyle = mapsLegacyConfig.emsTileLayerId.bright; + } + } + + const mapOptions = tmsServices.find((s) => s.id === mapStyle); + + if (!mapOptions) { + this.onWarn( + i18n.translate('visTypeVega.mapView.mapStyleNotFoundWarningMessage', { + defaultMessage: '{mapStyleParam} was not found', + values: { mapStyleParam: `"mapStyle":${mapStyle}` }, + }) + ); + return null; + } + + return { + ...mapOptions, + ...(await this._serviceSettings.getAttributesForTMSLayer(mapOptions, true, isDarkMode)), + }; + } + async _initViewCustomizations() { const mapConfig = this._parser.mapConfig; let baseMapOpts; let limitMinZ = 0; let limitMaxZ = 25; + // In some cases, Vega may be initialized twice, e.g. after awaiting... + if (!this._$container) return; + if (mapConfig.mapStyle !== false) { - const tmsServices = await this._serviceSettings.getTMSServices(); - // In some cases, Vega may be initialized twice, e.g. after awaiting... - if (!this._$container) return; - const emsTileLayerId = getEmsTileLayerId(); - const mapStyle = - mapConfig.mapStyle === 'default' ? emsTileLayerId.bright : mapConfig.mapStyle; - const isDarkMode = getUISettings().get('theme:darkMode'); - baseMapOpts = tmsServices.find((s) => s.id === mapStyle); - baseMapOpts = { - ...baseMapOpts, - ...(await this._serviceSettings.getAttributesForTMSLayer(baseMapOpts, true, isDarkMode)), - }; - if (!baseMapOpts) { - this.onWarn( - i18n.translate('visTypeVega.mapView.mapStyleNotFoundWarningMessage', { - defaultMessage: '{mapStyleParam} was not found', - values: { mapStyleParam: `"mapStyle": ${JSON.stringify(mapStyle)}` }, - }) - ); - } else { + baseMapOpts = await this.getMapStyleOptions(); + + if (baseMapOpts) { limitMinZ = baseMapOpts.minZoom; limitMaxZ = baseMapOpts.maxZoom; }