From 2e81e71859e85fa23598a397501c7ae655224234 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Tue, 4 Apr 2017 02:00:01 -0400 Subject: [PATCH] Add history.toIntroGraph() for saving the edited introGraph to JSON --- data/intro_graph.json | 4 +-- modules/core/history.js | 66 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/data/intro_graph.json b/data/intro_graph.json index 1486f6cd6..c6f543c82 100644 --- a/data/intro_graph.json +++ b/data/intro_graph.json @@ -12009,7 +12009,7 @@ "w170989131": { "id": "w170989131", "tags": { - "name": "St Joseph River", + "name": "Saint Joseph River", "waterway": "river" }, "nodes": [ @@ -14709,7 +14709,7 @@ "id": "w17964497", "tags": { "highway": "tertiary", - "name": "Constantine Street" + "name": "South Constantine Street" }, "nodes": [ "n185958643", diff --git a/modules/core/history.js b/modules/core/history.js index b881ec9c3..a7118da76 100644 --- a/modules/core/history.js +++ b/modules/core/history.js @@ -291,7 +291,7 @@ export function coreHistory(context) { // restore history state to a given checkpoint or reset completely reset: function(key) { - if (key !== undefined) { + if (key !== undefined && checkpoints.hasOwnProperty(key)) { stack = _.cloneDeep(checkpoints[key].stack); index = checkpoints[key].index; } else { @@ -305,6 +305,70 @@ export function coreHistory(context) { }, + toIntroGraph: function() { + var nextId = { n: 0, r: 0, w: 0 }, + permIds = {}, + graph = this.graph(), + baseEntities = {}; + + // clone base entities.. + _.forEach(graph.base().entities, function(entity) { + var copy = _.cloneDeepWith(entity, customizer); + baseEntities[copy.id] = copy; + }); + + // replace base entities with head entities.. + _.forEach(graph.entities, function(entity, id) { + if (entity) { + var copy = _.cloneDeepWith(entity, customizer); + baseEntities[copy.id] = copy; + } else { + delete baseEntities[id]; + } + }); + + // swap temporary for permanent ids.. + _.forEach(baseEntities, function(entity) { + if (Array.isArray(entity.nodes)) { + entity.nodes = entity.nodes.map(function(node) { + return permIds[node] || node; + }); + } + if (Array.isArray(entity.members)) { + entity.members = entity.members.map(function(member) { + member.id = permIds[member.id] || member.id; + return member; + }); + } + }); + + return JSON.stringify({ dataIntroGraph: baseEntities }); + + + function customizer(src) { + var copy = _.omit(_.cloneDeep(src), ['type', 'user', 'v', 'version', 'visible']); + if (_.isEmpty(copy.tags)) { + delete copy.tags; + } + + if (Array.isArray(copy.loc)) { + copy.loc[0] = +copy.loc[0].toFixed(6); + copy.loc[1] = +copy.loc[1].toFixed(6); + } + + var match = src.id.match(/([nrw])-\d*/); // temporary id + if (match !== null) { + var nrw = match[1], permId; + do { permId = nrw + (++nextId[nrw]); } + while (baseEntities.hasOwnProperty(permId)); + + copy.id = permIds[src.id] = permId; + } + return copy; + } + }, + + toJSON: function() { if (!this.hasChanges()) return;