mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-15 21:48:20 +02:00
Merge pull request #70 from jfirebaugh/refactor
Preserve Graph immutability
This commit is contained in:
+12
-7
@@ -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;
|
||||
|
||||
+10
-9
@@ -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.
|
||||
|
||||
@@ -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()));
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user