diff --git a/js/id/core/graph.js b/js/id/core/graph.js index 9422acc73..7d0f1452a 100644 --- a/js/id/core/graph.js +++ b/js/id/core/graph.js @@ -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') { diff --git a/js/id/core/history.js b/js/id/core/history.js index 87c9b0075..e6d948b90 100644 --- a/js/id/core/history.js +++ b/js/id/core/history.js @@ -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) { diff --git a/js/id/core/tree.js b/js/id/core/tree.js index d456436c5..17d5419f0 100644 --- a/js/id/core/tree.js +++ b/js/id/core/tree.js @@ -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; diff --git a/js/id/id.js b/js/id/id.js index 1635576f7..b1147c2bb 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -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); } diff --git a/test/spec/core/graph.js b/test/spec/core/graph.js index e8d6aa83c..0559c6df1 100644 --- a/test/spec/core/graph.js +++ b/test/spec/core/graph.js @@ -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();