diff --git a/index.html b/index.html index 16dce5779..5853a8a50 100644 --- a/index.html +++ b/index.html @@ -71,6 +71,7 @@ + diff --git a/js/id/actions/add_relation_member.js b/js/id/actions/add_relation_member.js new file mode 100644 index 000000000..45e24a0b0 --- /dev/null +++ b/js/id/actions/add_relation_member.js @@ -0,0 +1,9 @@ +iD.actions.AddRelationMember = function(relationId, member, index) { + return function(graph) { + var relation = graph.entity(relationId), + members = relation.members.slice(); + + members.splice((index === undefined) ? members.length : index, 0, member); + return graph.replace(relation.update({members: members})); + }; +}; diff --git a/test/index.html b/test/index.html index 0777751d5..425258d20 100644 --- a/test/index.html +++ b/test/index.html @@ -66,6 +66,7 @@ + @@ -124,6 +125,7 @@ + diff --git a/test/index_packaged.html b/test/index_packaged.html index 630ed1088..8def4c0da 100644 --- a/test/index_packaged.html +++ b/test/index_packaged.html @@ -31,6 +31,7 @@ + diff --git a/test/spec/actions/add_relation_member.js b/test/spec/actions/add_relation_member.js new file mode 100644 index 000000000..aeae8e353 --- /dev/null +++ b/test/spec/actions/add_relation_member.js @@ -0,0 +1,37 @@ +describe("iD.actions.AddRelationMember", function () { + it("adds a member at the end of the relation", function () { + var relation = iD.Relation(), + graph = iD.Graph([relation]); + + graph = iD.actions.AddRelationMember(relation.id, {id: '1'})(graph); + + expect(graph.entity(relation.id).members).to.eql([{id: '1'}]); + }); + + it("adds a member at index 0", function () { + var relation = iD.Relation({members: [{id: '1'}]}), + graph = iD.Graph([relation]); + + graph = iD.actions.AddRelationMember(relation.id, {id: '2'}, 0)(graph); + + expect(graph.entity(relation.id).members).to.eql([{id: '2'}, {id: '1'}]); + }); + + it("adds a member at a positive index", function () { + var relation = iD.Relation({members: [{id: '1'}, {id: '3'}]}), + graph = iD.Graph([relation]); + + graph = iD.actions.AddRelationMember(relation.id, {id: '2'}, 1)(graph); + + expect(graph.entity(relation.id).members).to.eql([{id: '1'}, {id: '2'}, {id: '3'}]); + }); + + it("adds a member at a negative index", function () { + var relation = iD.Relation({members: [{id: '1'}, {id: '3'}]}), + graph = iD.Graph([relation]); + + graph = iD.actions.AddRelationMember(relation.id, {id: '2'}, -1)(graph); + + expect(graph.entity(relation.id).members).to.eql([{id: '1'}, {id: '2'}, {id: '3'}]); + }); +});