Delete relations that become empty (#2270)

This commit is contained in:
John Firebaugh
2014-06-26 09:54:34 -07:00
parent 700e863b6c
commit eb55e36c3c
3 changed files with 27 additions and 6 deletions

View File

@@ -1,5 +1,13 @@
iD.actions.DeleteMember = function(relationId, memberIndex) {
return function(graph) {
return graph.replace(graph.entity(relationId).removeMember(memberIndex));
var relation = graph.entity(relationId)
.removeMember(memberIndex);
graph = graph.replace(relation);
if (relation.isDegenerate())
graph = iD.actions.DeleteRelation(relation.id)(graph);
return graph;
};
};

View File

@@ -17,6 +17,10 @@ iD.ui.RawMemberEditor = function(context) {
context.perform(
iD.actions.DeleteMember(d.relation.id, d.index),
t('operations.delete_member.annotation'));
if (!context.hasEntity(d.relation.id)) {
context.enter(iD.modes.Browse(context));
}
}
function rawMemberEditor(selection) {

View File

@@ -1,9 +1,18 @@
describe("iD.actions.DeleteMember", function () {
it("removes the member at the specified index", function () {
var node = iD.Node(),
relation = iD.Relation({members: [{id: node.id}]}),
action = iD.actions.DeleteMember(relation.id, 0),
graph = action(iD.Graph([node, relation]));
expect(graph.entity(relation.id).members).to.eql([]);
var a = iD.Node({id: 'a'}),
b = iD.Node({id: 'b'}),
r = iD.Relation({members: [{id: 'a'}, {id: 'b'}]}),
action = iD.actions.DeleteMember(r.id, 0),
graph = action(iD.Graph([a, b, r]));
expect(graph.entity(r.id).members).to.eql([{id: 'b'}]);
});
it("deletes relations that become degenerate", function () {
var a = iD.Node({id: 'a'}),
r = iD.Relation({id: 'r', members: [{id: 'a'}]}),
action = iD.actions.DeleteMember(r.id, 0),
graph = action(iD.Graph([a, r]));
expect(graph.hasEntity('r')).to.be.undefined;
});
});