From 3a4581f178fccd73326c70f430db4167fd6e93b2 Mon Sep 17 00:00:00 2001 From: Sandro Mani Date: Wed, 1 Apr 2020 19:00:28 +0200 Subject: [PATCH] Cleanup layer id handling --- actions/layers.js | 4 ++-- actions/theme.js | 4 ++-- components/ImportLayer.jsx | 14 +++----------- components/widgets/VectorLayerPicker.jsx | 2 -- plugins/API.jsx | 7 ++----- plugins/Identify.jsx | 2 +- plugins/Map.jsx | 2 +- reducers/layers.js | 11 +++++------ utils/LayerUtils.js | 16 ++++++++-------- utils/ServiceLayerUtils.js | 6 +++--- utils/ThemeUtils.js | 1 - 11 files changed, 27 insertions(+), 42 deletions(-) diff --git a/actions/layers.js b/actions/layers.js index 40d0d5dd6..90bec505e 100644 --- a/actions/layers.js +++ b/actions/layers.js @@ -149,10 +149,10 @@ function removeAllLayers() { }; } -function replacePlaceholderLayer(source, layer) { +function replacePlaceholderLayer(id, layer) { return { type: REPLACE_PLACEHOLDER_LAYER, - source, + id, layer } } diff --git a/actions/theme.js b/actions/theme.js index 0e96dc92a..ae57b8e82 100644 --- a/actions/theme.js +++ b/actions/theme.js @@ -172,8 +172,8 @@ function finishThemeSetup(dispatch, theme, themes, layerConfigs, insertPos, perm for(let key of Object.keys(externalLayers)) { let service = key.slice(0, 3); let serviceUrl = key.slice(4); - ServiceLayerUtils.findLayers(service, serviceUrl, externalLayers[key], (source, layer) => { - dispatch(replacePlaceholderLayer(source, layer)); + ServiceLayerUtils.findLayers(service, serviceUrl, externalLayers[key], (id, layer) => { + dispatch(replacePlaceholderLayer(id, layer)); }); } } diff --git a/components/ImportLayer.jsx b/components/ImportLayer.jsx index 7eef42952..540e8c59e 100644 --- a/components/ImportLayer.jsx +++ b/components/ImportLayer.jsx @@ -98,19 +98,13 @@ class ImportLayerList extends React.PureComponent { addServiceLayer = (entry) => { if(entry.resource) { let params = LayerUtils.splitLayerUrlParam(entry.resource); - ServiceLayerUtils.findLayers(params.type, params.url, [params], (source, layer) => { + ServiceLayerUtils.findLayers(params.type, params.url, [params], (id, layer) => { if(layer) { - this.props.addLayer(assign({ - id: entry.name + Date.now().toString(), - role: LayerRole.USERLAYER - }, layer, {sublayers: null})); + this.props.addLayer(layer); } }); } else if(entry.type === "wms" || entry.type === "wfs") { - this.props.addLayer(assign({ - id: entry.name + Date.now().toString(), - role: LayerRole.USERLAYER - }, entry, {sublayers: null})); + this.props.addLayer(assign({}, entry, {sublayers: null})); } } } @@ -298,10 +292,8 @@ class ImportLayer extends React.Component { if(!isEmpty(data.features)) { let features = data.features.map(feature => ({...feature, crs: feature.crs || "EPSG:4326"})); this.props.addLayerFeatures({ - id: filename + Date.now(), name: filename, title: filename.replace(/\.[^/.]+$/, ""), - role: LayerRole.USERLAYER, zoomToExtent: true }, features, true); } else { diff --git a/components/widgets/VectorLayerPicker.jsx b/components/widgets/VectorLayerPicker.jsx index 1267df1d3..dc2a62bd5 100644 --- a/components/widgets/VectorLayerPicker.jsx +++ b/components/widgets/VectorLayerPicker.jsx @@ -36,9 +36,7 @@ class VectorLayerPicker extends React.Component { let name = prompt("Enter layer name"); if(name) { let layer = { - id: uuid.v4(), title: name, - role: LayerRole.USERLAYER, type: 'vector' }; this.props.addLayer(layer); diff --git a/plugins/API.jsx b/plugins/API.jsx index 1ff7548d9..495ff7bc7 100644 --- a/plugins/API.jsx +++ b/plugins/API.jsx @@ -27,12 +27,9 @@ class API extends React.Component { } addExternalLayer = (resource) => { let params = LayerUtils.splitLayerUrlParam(resource); - ServiceLayerUtils.findLayers(params.type, params.url, [params], (source, layer) => { + ServiceLayerUtils.findLayers(params.type, params.url, [params], (id, layer) => { if(layer) { - this.props.addLayer(assign({ - id: layer.name + Date.now().toString(), - role: LayerRole.USERLAYER - }, layer, {sublayers: null})); + this.props.addLayer(layer); } }); } diff --git a/plugins/Identify.jsx b/plugins/Identify.jsx index 7830b4fcb..333a713ba 100644 --- a/plugins/Identify.jsx +++ b/plugins/Identify.jsx @@ -66,7 +66,7 @@ class Identify extends React.Component { for(let i = 0; i < queryLayers.length; ++i) { if(layer.externalLayers && layer.externalLayers[queryLayers[i]]) { layers.push(layer.externalLayers[queryLayers[i]]); - } else if(layers.length > 0 && layers[layers.length - 1].refid === layer.refid) { + } else if(layers.length > 0 && layers[layers.length - 1].id === layer.id) { layers[layers.length - 1].queryLayers.push(queryLayers[i]); } else { layers.push(assign({}, layer, {queryLayers: [queryLayers[i]]})); diff --git a/plugins/Map.jsx b/plugins/Map.jsx index 6fc8e9c6e..3ae09cd99 100644 --- a/plugins/Map.jsx +++ b/plugins/Map.jsx @@ -52,7 +52,7 @@ class MapPlugin extends React.Component { layers.push(assign({}, layer.externalLayers[sublayers[i]], { opacity: opacities[i] })); - } else if(layers.length > 0 && layers[layers.length - 1].refid === layer.refid) { + } else if(layers.length > 0 && layers[layers.length - 1].id === layer.id) { layers[layers.length - 1].params.LAYERS += "," + sublayers[i]; layers[layers.length - 1].params.OPACITIES += "," + opacities[i]; } else { diff --git a/reducers/layers.js b/reducers/layers.js index fd3cc4d67..6eea41aa3 100644 --- a/reducers/layers.js +++ b/reducers/layers.js @@ -63,8 +63,7 @@ function layers(state = {flat: [], swipe: undefined}, action) { case ADD_LAYER: { let newLayers = (state.flat || []).concat(); let newLayer = assign({}, action.layer, { - refid: uuid.v4(), - id: action.layer.id || (action.layer.name + "__" + newLayers.length), + id: action.layer.id || uuid.v4(), role: action.layer.role || LayerRole.USERLAYER, queryable: action.layer.queryable || false, visibility: action.layer.visibility !== undefined ? action.layer.visibility : true, @@ -113,7 +112,7 @@ function layers(state = {flat: [], swipe: undefined}, action) { if(idx === -1 || action.clear) { let newLayer = assign({}, action.layer, { type: 'vector', - refid: uuid.v4(), + id: action.layer.id || uuid.v4(), uuid: uuid.v4(), features: action.features, role: action.layer.role || LayerRole.USERLAYER, @@ -191,8 +190,8 @@ function layers(state = {flat: [], swipe: undefined}, action) { let newLayers = state.flat || []; if(action.layer) { newLayers = newLayers.map(layer => { - if(layer.type === 'placeholder' && layer.source === action.source) { - let newLayer = {...action.layer, refid: uuid.v4()}; + if(layer.type === 'placeholder' && layer.id === action.id) { + let newLayer = {...action.layer}; LayerUtils.addUUIDs(newLayer); if(newLayer.type === "wms") { assign(newLayer, LayerUtils.buildWMSLayerParams(newLayer)); @@ -203,7 +202,7 @@ function layers(state = {flat: [], swipe: undefined}, action) { } }); } else { - newLayers = newLayers.filter(layer => !(layer.type === 'placeholder' && layer.source === action.source)); + newLayers = newLayers.filter(layer => !(layer.type === 'placeholder' && layer.id === action.id)); } UrlParams.updateParams({l: LayerUtils.buildWMSLayerUrlParam(newLayers)}); return assign({}, state, {flat: newLayers}); diff --git a/utils/LayerUtils.js b/utils/LayerUtils.js index f6412d1b2..b0ba5ff02 100644 --- a/utils/LayerUtils.js +++ b/utils/LayerUtils.js @@ -33,7 +33,7 @@ const LayerUtils = { if(layerConfig.type === 'separator') { // No point restoring separators } else if(layerConfig.type !== 'theme') { - external = external.concat(LayerUtils.createExternalLayerPlaceholder(layerConfig, externalLayers)); + external = external.concat(LayerUtils.createExternalLayerPlaceholder(layerConfig, externalLayers, layerConfig.id)); } } exploded = [...external, ...exploded]; @@ -55,7 +55,7 @@ const LayerUtils = { } else if(layerConfig.type === 'separator') { reordered = reordered.concat(LayerUtils.createSeparatorLayer(layerConfig.name)); } else { - reordered = reordered.concat(LayerUtils.createExternalLayerPlaceholder(layerConfig, externalLayers)); + reordered = reordered.concat(LayerUtils.createExternalLayerPlaceholder(layerConfig, externalLayers, layerConfig.id)); } } LayerUtils.insertPermalinkLayers(reordered, permalinkLayers); @@ -66,25 +66,24 @@ const LayerUtils = { type: "separator", title: title, role: LayerRole.USERLAYER, - refid: uuid.v4(), uuid: uuid.v4(), id: uuid.v4() }]); }, - createExternalLayerPlaceholder: function(layerConfig, externalLayers) { + createExternalLayerPlaceholder: function(layerConfig, externalLayers, id) { let key = layerConfig.type + ":" + layerConfig.url; (externalLayers[key] = externalLayers[key] || []).push({ + id: id, name: layerConfig.name, opacity: layerConfig.opacity, visibility: layerConfig.visibility }); return LayerUtils.explodeLayers([{ + id: id, type: "placeholder", title: layerConfig.name, role: LayerRole.USERLAYER, loading: true, - source: layerConfig.type + ':' + layerConfig.url + '#' + layerConfig.name, - refid: uuid.v4(), uuid: uuid.v4() }]); }, @@ -195,6 +194,7 @@ const LayerUtils = { }, splitLayerUrlParam(entry) { const nameOpacityPattern = /([^\[]+)\[(\d+)]/; + let id = uuid.v4(); let type = 'theme'; let url = null; let opacity = 255; @@ -218,7 +218,7 @@ const LayerUtils = { type = 'separator'; name = name.slice(4); } - return {type, url, name, opacity, visibility}; + return {id, type, url, name, opacity, visibility}; }, pathEqualOrBelow(parent, child) { return isEqual(child.slice(0, parent.length), parent); @@ -392,7 +392,7 @@ const LayerUtils = { // Attempt to merge with previous if possible let target = newlayers.length > 0 ? newlayers[newlayers.length - 1] : null; let source = layer; - if(target && target.sublayers && target.refid === layer.refid) { + if(target && target.sublayers && target.id === layer.id) { let innertarget = target.sublayers[target.sublayers.length - 1]; let innersource = source.sublayers[0]; // Exploded entries have only one entry per sublayer level while(innertarget && innertarget.sublayers && innertarget.name === innersource.name) { diff --git a/utils/ServiceLayerUtils.js b/utils/ServiceLayerUtils.js index 4292a8cbb..d31977bd5 100644 --- a/utils/ServiceLayerUtils.js +++ b/utils/ServiceLayerUtils.js @@ -231,16 +231,16 @@ const ServiceLayerUtils = { let source = type + ':' + serviceUrl + '#' + layerConfig.name; if(layer) { layer = assign({}, layer, { + id: layerConfig.id, opacity: layerConfig.opacity, visibility: layerConfig.visibility, - id: layerConfig.name + Date.now().toString(), role: LayerRole.USERLAYER, sublayers: null }); - callback(source, layer); + callback(layerConfig.id, layer); } else { console.warn("Could not find layer " + layerConfig.name); - callback(source, null); + callback(layerConfig.id, null); } } }).catch(err => { diff --git a/utils/ThemeUtils.js b/utils/ThemeUtils.js index 67e37fb7a..2e29b6de4 100644 --- a/utils/ThemeUtils.js +++ b/utils/ThemeUtils.js @@ -62,7 +62,6 @@ const ThemeUtils = { }, createThemeLayer: function(theme, themes, role=LayerRole.THEME) { let layer = { - id: theme.name + Date.now().toString(), type: "wms", url: theme.url, version: theme.version,