diff --git a/js/iD/Connection.js b/js/iD/Connection.js index 30de3c9ab..089ce3cc0 100755 --- a/js/iD/Connection.js +++ b/js/iD/Connection.js @@ -1,4 +1,4 @@ -iD.Connection = function(graph) { +iD.Connection = function() { var apiURL = 'http://www.openstreetmap.org/api/0.6/'; var connection = {}; @@ -85,15 +85,20 @@ iD.Connection = function(graph) { return function(dom) { if (!dom.childNodes) return callback(new Error('Bad request')); var root = dom.childNodes[0]; - connection.graph.insert(_.map(root.getElementsByTagName('way'), objectData)); - connection.graph.insert(_.map(root.getElementsByTagName('node'), objectData)); - connection.graph.insert(_.map(root.getElementsByTagName('relation'), objectData)); - callback(null); + var entities = {}; + var addEntity = function (obj) { + var o = objectData(obj); + entities[o.id] = o; + }; + + _.forEach(root.getElementsByTagName('way'), addEntity); + _.forEach(root.getElementsByTagName('node'), addEntity); + _.forEach(root.getElementsByTagName('relation'), addEntity); + + callback(iD.Graph(entities)); }; } - connection.graph = graph; - connection.bboxFromAPI = bboxFromAPI; connection.wayFromAPI = wayFromAPI; connection.loadFromURL = loadFromURL; diff --git a/js/iD/graph/Graph.js b/js/iD/graph/Graph.js index 13b07703b..fe34e9fa6 100644 --- a/js/iD/graph/Graph.js +++ b/js/iD/graph/Graph.js @@ -27,21 +27,22 @@ iD.Graph.prototype = { return pois; }, - insert: function(a) { - for (var i = 0; i < a.length; i++) { - if (this.entities[a[i].id]) return; - this.entities[a[i].id] = a[i]; - } + merge: function(graph) { + var entities = _.clone(this.entities); + _.defaults(entities, graph.entities); + return iD.Graph(entities, this.annotation); }, replace: function(entity, annotation) { - var o = {}; - o[entity.id] = entity; - return iD.Graph(pdata.object(this.entities).set(o).get(), annotation); + var entities = _.clone(this.entities); + entities[entity.id] = entity; + return iD.Graph(entities, annotation); }, remove: function(entity, annotation) { - return iD.Graph(pdata.object(this.entities).remove(entity.id).get(), annotation); + var entities = _.clone(this.entities); + delete entities[entity.id]; + return iD.Graph(entities, annotation); }, // get all objects that intersect an extent. diff --git a/js/iD/graph/History.js b/js/iD/graph/History.js index 6ec3581a2..c4f04f8a1 100644 --- a/js/iD/graph/History.js +++ b/js/iD/graph/History.js @@ -9,6 +9,12 @@ iD.History.prototype = { return this.stack[this.index]; }, + merge: function(graph) { + for (var i = 0; i < this.stack.length; i++) { + this.stack[i] = this.stack[i].merge(graph); + } + }, + do: function(operation) { this.stack = this.stack.slice(0, this.index + 1); this.stack.push(operation(this.graph())); diff --git a/js/iD/renderer/Map.js b/js/iD/renderer/Map.js index f1d816041..1110b1a71 100755 --- a/js/iD/renderer/Map.js +++ b/js/iD/renderer/Map.js @@ -201,7 +201,14 @@ iD.Map = function(elem) { } var download = _.debounce(function() { - connection.bboxFromAPI(getExtent(), drawVector); + connection.bboxFromAPI(getExtent(), function (result) { + if (result instanceof Error) { + // TODO: handle + } else { + history.merge(result); + drawVector(); + } + }); }, 1000); function deselectClick() {