Add history.toIntroGraph() for saving the edited introGraph to JSON

This commit is contained in:
Bryan Housel
2017-04-04 02:00:01 -04:00
parent 526bb75fb8
commit 2e81e71859
2 changed files with 67 additions and 3 deletions

View File

@@ -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",

View File

@@ -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;