Skip to content

Commit

Permalink
Loads the attribution and display-name of the layer dynamically inste…
Browse files Browse the repository at this point in the history
…ad of relying on the vis-state.

For EMS-layers, load from EMS-metadata. For self-hosted layers, load from the kibana.yml config.
  • Loading branch information
thomasneirynck committed Jul 6, 2020
1 parent 21af99c commit b0e64fc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
48 changes: 30 additions & 18 deletions src/plugins/maps_legacy/public/map/service_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,31 @@ export class ServiceSettings {
};
}

_backfillSettings = (fileLayer) => {
// Older version of Kibana stored EMS state in the URL-params
// Creates object literal with required parameters as key-value pairs
const format = fileLayer.getDefaultFormatType();
const meta = fileLayer.getDefaultFormatMeta();

return {
name: fileLayer.getDisplayName(),
origin: fileLayer.getOrigin(),
id: fileLayer.getId(),
created_at: fileLayer.getCreatedAt(),
attribution: fileLayer.getHTMLAttribution(),
fields: fileLayer.getFieldsInLanguage(),
format: format, //legacy: format and meta are split up
meta: meta, //legacy, format and meta are split up
};
};

async getFileLayers() {
if (!this._mapConfig.includeElasticMapsService) {
return [];
}

const fileLayers = await this._emsClient.getFileLayers();
return fileLayers.map((fileLayer) => {
//backfill to older settings
const format = fileLayer.getDefaultFormatType();
const meta = fileLayer.getDefaultFormatMeta();

return {
name: fileLayer.getDisplayName(),
origin: fileLayer.getOrigin(),
id: fileLayer.getId(),
created_at: fileLayer.getCreatedAt(),
attribution: fileLayer.getHTMLAttribution(),
fields: fileLayer.getFieldsInLanguage(),
format: format, //legacy: format and meta are split up
meta: meta, //legacy, format and meta are split up
};
});
return fileLayers.map(this._backfillSettings);
}

/**
Expand Down Expand Up @@ -159,16 +162,25 @@ export class ServiceSettings {
this._emsClient.addQueryParams(additionalQueryParams);
}

async getEMSHotLink(fileLayerConfig) {
async getFileLayerFromConfig(fileLayerConfig) {
const fileLayers = await this._emsClient.getFileLayers();
const layer = fileLayers.find((fileLayer) => {
return fileLayers.find((fileLayer) => {
const hasIdByName = fileLayer.hasId(fileLayerConfig.name); //legacy
const hasIdById = fileLayer.hasId(fileLayerConfig.id);
return hasIdByName || hasIdById;
});
}

async getEMSHotLink(fileLayerConfig) {
const layer = await this.getFileLayerFromConfig(fileLayerConfig);
return layer ? layer.getEMSHotLink() : null;
}

async loadFileLayerConfig(fileLayerConfig) {
const fileLayer = await this.getFileLayerFromConfig(fileLayerConfig);
return fileLayer ? this._backfillSettings(fileLayer) : null;
}

async _getAttributesForEMSTMSLayer(isDesaturated, isDarkMode) {
const tmsServices = await this._emsClient.getTMSServices();
const emsTileLayerId = this._mapConfig.emsTileLayerId;
Expand Down
52 changes: 38 additions & 14 deletions src/plugins/region_map/public/region_map_visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import { getFormatService, getNotifications, getKibanaLegacy } from './kibana_se
import { truncatedColorMaps } from '../../charts/public';
import { tooltipFormatter } from './tooltip_formatter';
import { mapTooltipProvider } from '../../maps_legacy/public';
import _ from 'lodash';

export function createRegionMapVisualization({
regionmapsConfig,
serviceSettings,
uiSettings,
BaseMapsVisualization,
Expand Down Expand Up @@ -60,17 +62,18 @@ export function createRegionMapVisualization({
});
}

if (!this._params.selectedJoinField && this._params.selectedLayer) {
this._params.selectedJoinField = this._params.selectedLayer.fields[0];
const selectedLayer = await this._loadConfig(this._params.selectedLayer);
if (!this._params.selectedJoinField && selectedLayer) {
this._params.selectedJoinField = selectedLayer.fields[0];
}

if (!this._params.selectedLayer) {
if (!selectedLayer) {
return;
}

this._updateChoroplethLayerForNewMetrics(
this._params.selectedLayer.name,
this._params.selectedLayer.attribution,
selectedLayer.name,
selectedLayer.attribution,
this._params.showAllShapes,
results
);
Expand All @@ -90,29 +93,50 @@ export function createRegionMapVisualization({
this._kibanaMap.useUiStateFromVisualization(this._vis);
}

async _loadConfig(fileLayerConfig) {
// Load the selected layer from the metadata-service.
// Do not use the selectedLayer from the visState.
// These settings are stored in the URL and can be used to inject dirty display content.

if (fileLayerConfig.isEMS) {
//Hosted by EMS. Metadata needs to be resolved through EMS.
return await serviceSettings.loadFileLayerConfig(fileLayerConfig);
}

//Configured in the kibana.yml. Needs to be resolved through the settings.
const configuredLayer = regionmapsConfig.layers.find(
(layer) => layer.name === fileLayerConfig.name
);
return {
...configuredLayer,
attribution: _.escape(configuredLayer.attribution || ''),
};
}

async _updateParams() {
await super._updateParams();
const visParams = this._params;

if (!visParams.selectedJoinField && visParams.selectedLayer) {
visParams.selectedJoinField = visParams.selectedLayer.fields[0];
const selectedLayer = await this._loadConfig(this._params.selectedLayer);

if (!this._params.selectedJoinField && selectedLayer) {
this._params.selectedJoinField = selectedLayer.fields[0];
}

if (!visParams.selectedJoinField || !visParams.selectedLayer) {
if (!this._params.selectedJoinField || !selectedLayer) {
return;
}

this._updateChoroplethLayerForNewProperties(
visParams.selectedLayer.name,
visParams.selectedLayer.attribution,
selectedLayer.name,
selectedLayer.attribution,
this._params.showAllShapes
);

const metricFieldFormatter = getFormatService().deserialize(this._params.metric.format);

this._choroplethLayer.setJoinField(visParams.selectedJoinField.name);
this._choroplethLayer.setColorRamp(truncatedColorMaps[visParams.colorSchema].value);
this._choroplethLayer.setLineWeight(visParams.outlineWeight);
this._choroplethLayer.setJoinField(this._params.selectedJoinField.name);
this._choroplethLayer.setColorRamp(truncatedColorMaps[this._params.colorSchema].value);
this._choroplethLayer.setLineWeight(this._params.outlineWeight);
this._choroplethLayer.setTooltipFormatter(
this._tooltipFormatter,
metricFieldFormatter,
Expand Down

0 comments on commit b0e64fc

Please sign in to comment.