add force option for rebase to overwrite existing entities

related: openstreetmap/iD#2467
This commit is contained in:
Bryan Housel
2014-12-04 19:50:32 -05:00
parent edb94fd4d6
commit 1c3d198b96
5 changed files with 20 additions and 11 deletions
+3 -4
View File
@@ -112,20 +112,19 @@ iD.Graph.prototype = {
// is used only during the history operation that merges newly downloaded
// data into each state. To external consumers, it should appear as if the
// graph always contained the newly downloaded data.
rebase: function(entities, stack) {
rebase: function(entities, stack, force) {
var base = this.base(),
i, j, k, id;
for (i = 0; i < entities.length; i++) {
var entity = entities[i];
if (base.entities[entity.id])
if (!force && base.entities[entity.id])
continue;
// Merging data into the base graph
base.entities[entity.id] = entity;
this._updateCalculated(undefined, entity,
base.parentWays, base.parentRels);
this._updateCalculated(undefined, entity, base.parentWays, base.parentRels);
// Restore provisionally-deleted nodes that are discovered to have an extant parent
if (entity.type === 'way') {
+4 -4
View File
@@ -42,8 +42,8 @@ iD.History = function(context) {
},
merge: function(entities, extent) {
stack[0].graph.rebase(entities, _.pluck(stack, 'graph'));
tree.rebase(entities);
stack[0].graph.rebase(entities, _.pluck(stack, 'graph'), false);
tree.rebase(entities, false);
dispatch.change(undefined, extent);
},
@@ -237,8 +237,8 @@ iD.History = function(context) {
var baseEntities = h.baseEntities.map(function(entity) {
return iD.Entity(entity);
});
stack[0].graph.rebase(baseEntities, _.pluck(stack, 'graph'));
tree.rebase(baseEntities);
stack[0].graph.rebase(baseEntities, _.pluck(stack, 'graph'), true);
tree.rebase(baseEntities, true);
}
stack = h.stack.map(function(d) {
+2 -2
View File
@@ -39,13 +39,13 @@ iD.Tree = function(head) {
var tree = {};
tree.rebase = function(entities) {
tree.rebase = function(entities, force) {
var insertions = {};
for (var i = 0; i < entities.length; i++) {
var entity = entities[i];
if (head.entities.hasOwnProperty(entity.id) || rectangles[entity.id])
if (!force && (head.entities.hasOwnProperty(entity.id) || rectangles[entity.id]))
continue;
insertions[entity.id] = entity;
+1 -1
View File
@@ -56,7 +56,7 @@ window.iD = function () {
connection.on('load.context', function loadContext(err, result) {
if (altGraph) {
altGraph.rebase(result.data, [altGraph]);
altGraph.rebase(result.data, [altGraph], false);
} else {
history.merge(result.data, result.extent);
}
+10
View File
@@ -93,6 +93,16 @@ describe('iD.Graph', function() {
expect(graph.entity('n')).to.equal(a);
});
it("gives precedence to new entities when force = true", function () {
var a = iD.Node({id: 'n'}),
b = iD.Node({id: 'n'}),
graph = iD.Graph([a]);
graph.rebase([b], [graph], true);
expect(graph.entity('n')).to.equal(b);
});
it("inherits entities from base prototypally", function () {
var graph = iD.Graph();