From 481b80e5cba525aaf70c36364ff7328e7e9992f2 Mon Sep 17 00:00:00 2001 From: Quincy Morgan <2046746+quincylvania@users.noreply.github.com> Date: Tue, 10 Nov 2020 10:00:44 -0500 Subject: [PATCH] Fetch the parent relations when downloading a single entity, e.g. when launching iD with a feature selected (close #6731) --- modules/core/context.js | 3 +++ modules/services/osm.js | 20 +++++++++++++++++++- modules/ui/improveOSM_details.js | 6 ++++-- modules/ui/keepRight_details.js | 6 ++++-- modules/ui/osmose_details.js | 6 ++++-- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/modules/core/context.js b/modules/core/context.js index 56f6a7e0b..28090e259 100644 --- a/modules/core/context.js +++ b/modules/core/context.js @@ -177,10 +177,13 @@ export function coreContext() { _deferred.add(handle); }; + // Download the full entity and its parent relations. The callback may be called multiple times. context.loadEntity = (entityID, callback) => { if (_connection) { const cid = _connection.getConnectionId(); _connection.loadEntity(entityID, afterLoad(cid, callback)); + // We need to fetch the parent relations separately. + _connection.loadEntityRelations(entityID, afterLoad(cid, callback)); } }; diff --git a/modules/services/osm.js b/modules/services/osm.js index 557cee9eb..b13030c7a 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -635,7 +635,8 @@ export default { }, - // Load a single entity by id (ways and relations use the `/full` call) + // Load a single entity by id (ways and relations use the `/full` call to include + // nodes and members). Parent relations are not included, see `loadEntityRelations`. // GET /api/0.6/node/#id // GET /api/0.6/[way|relation]/#id/full loadEntity: function(id, callback) { @@ -670,6 +671,23 @@ export default { }, + // Load the relations of a single entity with the given. + // GET /api/0.6/[node|way|relation]/#id/relations + loadEntityRelations: function(id, callback) { + var type = osmEntity.id.type(id); + var osmID = osmEntity.id.toOSM(id); + var options = { skipSeen: false }; + + this.loadFromAPI( + '/api/0.6/' + type + '/' + osmID + '/relations.json', + function(err, entities) { + if (callback) callback(err, { data: entities }); + }, + options + ); + }, + + // Load multiple entities in chunks // (note: callback may be called multiple times) // Unlike `loadEntity`, child nodes and members are not fetched diff --git a/modules/ui/improveOSM_details.js b/modules/ui/improveOSM_details.js index 5ccbd33bc..9c64496d8 100644 --- a/modules/ui/improveOSM_details.js +++ b/modules/ui/improveOSM_details.js @@ -86,8 +86,10 @@ export function uiImproveOsmDetails(context) { if (entity) { context.enter(modeSelect(context, [entityID])); } else { - context.loadEntity(entityID, () => { - context.enter(modeSelect(context, [entityID])); + context.loadEntity(entityID, (err, result) => { + if (err) return; + const entity = result.data.find(e => e.id === entityID); + if (entity) context.enter(modeSelect(context, [entityID])); }); } }); diff --git a/modules/ui/keepRight_details.js b/modules/ui/keepRight_details.js index 44cd6e1de..9005f2f96 100644 --- a/modules/ui/keepRight_details.js +++ b/modules/ui/keepRight_details.js @@ -91,8 +91,10 @@ export function uiKeepRightDetails(context) { if (entity) { context.enter(modeSelect(context, [entityID])); } else { - context.loadEntity(entityID, () => { - context.enter(modeSelect(context, [entityID])); + context.loadEntity(entityID, (err, result) => { + if (err) return; + const entity = result.data.find(e => e.id === entityID); + if (entity) context.enter(modeSelect(context, [entityID])); }); } }); diff --git a/modules/ui/osmose_details.js b/modules/ui/osmose_details.js index 92c9e0b0c..2ad35eb12 100644 --- a/modules/ui/osmose_details.js +++ b/modules/ui/osmose_details.js @@ -169,8 +169,10 @@ export function uiOsmoseDetails(context) { if (entity) { context.enter(modeSelect(context, [entityID])); } else { - context.loadEntity(entityID, () => { - context.enter(modeSelect(context, [entityID])); + context.loadEntity(entityID, (err, result) => { + if (err) return; + const entity = result.data.find(e => e.id === entityID); + if (entity) context.enter(modeSelect(context, [entityID])); }); } });