Delete relations that become empty

Fixes #465
Fixes #1454
This commit is contained in:
John Firebaugh
2013-08-27 12:23:25 -07:00
parent e5c1944e37
commit 163c85bacb
8 changed files with 57 additions and 4 deletions
+6 -1
View File
@@ -15,7 +15,12 @@ iD.actions.DeleteNode = function(nodeId) {
graph.parentRelations(node)
.forEach(function(parent) {
graph = graph.replace(parent.removeMembersWithID(nodeId));
parent = parent.removeMembersWithID(nodeId);
graph = graph.replace(parent);
if (parent.isDegenerate()) {
graph = iD.actions.DeleteRelation(parent.id)(graph);
}
});
return graph.remove(node);
+6 -1
View File
@@ -11,7 +11,12 @@ iD.actions.DeleteRelation = function(relationId) {
graph.parentRelations(relation)
.forEach(function(parent) {
graph = graph.replace(parent.removeMembersWithID(relationId));
parent = parent.removeMembersWithID(relationId);
graph = graph.replace(parent);
if (parent.isDegenerate()) {
graph = iD.actions.DeleteRelation(parent.id)(graph);
}
});
_.uniq(_.pluck(relation.members, 'id')).forEach(function(memberId) {
+6 -1
View File
@@ -11,7 +11,12 @@ iD.actions.DeleteWay = function(wayId) {
graph.parentRelations(way)
.forEach(function(parent) {
graph = graph.replace(parent.removeMembersWithID(wayId));
parent = parent.removeMembersWithID(wayId);
graph = graph.replace(parent);
if (parent.isDegenerate()) {
graph = iD.actions.DeleteRelation(parent.id)(graph);
}
});
_.uniq(way.nodes).forEach(function(nodeId) {
+4
View File
@@ -31,6 +31,10 @@ _.extend(iD.Relation.prototype, {
});
},
isDegenerate: function() {
return this.members.length === 0;
},
// Return an array of members, each extended with an 'index' property whose value
// is the member index.
indexedMembers: function() {
+8
View File
@@ -42,4 +42,12 @@ describe("iD.actions.DeleteNode", function () {
graph = action(iD.Graph([node1, node2, way]));
expect(graph.hasEntity(way.id)).to.be.undefined;
});
it("deletes parent relations that become empty", function () {
var node1 = iD.Node(),
relation = iD.Relation({members: [{ id: node1.id }]}),
action = iD.actions.DeleteNode(node1.id),
graph = action(iD.Graph([node1, relation]));
expect(graph.hasEntity(relation.id)).to.be.undefined;
});
});
+8
View File
@@ -74,6 +74,14 @@ describe("iD.actions.DeleteRelation", function () {
expect(graph.hasEntity(node.id)).to.be.undefined;
});
it("deletes parent relations that become empty", function () {
var child = iD.Relation(),
parent = iD.Relation({members: [{ id: child.id }]}),
action = iD.actions.DeleteRelation(child.id),
graph = action(iD.Graph([child, parent]));
expect(graph.hasEntity(parent.id)).to.be.undefined;
});
describe("#disabled", function() {
it("returns 'incomplete_relation' if the relation is incomplete", function() {
var relation = iD.Relation({members: [{id: 'w'}]}),
+9 -1
View File
@@ -8,7 +8,7 @@ describe("iD.actions.DeleteWay", function() {
it("removes a way from parent relations", function() {
var way = iD.Way(),
relation = iD.Relation({members: [{ id: way.id }]}),
relation = iD.Relation({members: [{ id: way.id }, { id: 'w-2' }]}),
action = iD.actions.DeleteWay(way.id),
graph = iD.Graph([way, relation]).update(action);
expect(_.pluck(graph.entity(relation.id).members, 'id')).not.to.contain(way.id);
@@ -60,4 +60,12 @@ describe("iD.actions.DeleteWay", function() {
graph = iD.Graph([node, way]).update(action);
expect(graph.hasEntity(node.id)).not.to.be.undefined;
});
it("deletes parent relations that become empty", function () {
var way = iD.Way(),
relation = iD.Relation({members: [{ id: way.id }]}),
action = iD.actions.DeleteWay(way.id),
graph = iD.Graph([way, relation]).update(action);
expect(graph.hasEntity(relation.id)).to.be.undefined;
});
});
+10
View File
@@ -56,6 +56,16 @@ describe('iD.Relation', function () {
});
});
describe("#isDegenerate", function () {
it("returns true for a relation without members", function () {
expect(iD.Relation().isDegenerate()).to.equal(true);
});
it("returns false for a relation with members", function () {
expect(iD.Relation({members: [{id: 'a', role: 'inner'}]}).isDegenerate()).to.equal(false);
});
});
describe("#memberByRole", function () {
it("returns the first member with the given role", function () {
var r = iD.Relation({members: [