From 9aab74c187b091f15b83b4116a97a0dbb71ba56a Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 12 Feb 2015 10:21:09 -0800 Subject: [PATCH] Test; simplify --- js/id/actions/delete_way.js | 20 +++++++++----------- js/id/core/relation.js | 10 ---------- test/spec/actions/delete_way.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/js/id/actions/delete_way.js b/js/id/actions/delete_way.js index 01b7851df..4ec50a4b3 100644 --- a/js/id/actions/delete_way.js +++ b/js/id/actions/delete_way.js @@ -32,18 +32,16 @@ iD.actions.DeleteWay = function(wayId) { }; action.disabled = function(graph) { - var way = graph.entity(wayId); - var reltypes = ['route','boundary','multipolygon']; - var required_roles = { 'multipolygon': 'outer' }; var disabled = false; - graph.parentRelations(way) - .forEach(function(parent) { - if (reltypes.indexOf(parent.tags.type)>-1) { - if (!required_roles[parent.tags.type] || parent.containsEntityInRole(way,required_roles[parent.tags.type])) { - disabled = 'part_of_relation'; - } - } - }); + + graph.parentRelations(graph.entity(wayId)).forEach(function(parent) { + var type = parent.tags.type, + role = parent.memberById(wayId).role || 'outer'; + if (type === 'route' || type === 'boundary' || (type === 'multipolygon' && role === 'outer')) { + disabled = 'part_of_relation'; + } + }); + return disabled; }; diff --git a/js/id/core/relation.js b/js/id/core/relation.js index 01b940ff9..761db2a65 100644 --- a/js/id/core/relation.js +++ b/js/id/core/relation.js @@ -115,16 +115,6 @@ _.extend(iD.Relation.prototype, { } }, - // Test whether the relation contains an entity in the given role. - containsEntityInRole: function(entity, role) { - for (var i = 0; i < this.members.length; i++) { - if (this.members[i].id === entity.id && this.members[i].type === entity.type && this.members[i].role === role) { - return true; - } - } - return false; - }, - addMember: function(member, index) { var members = this.members.slice(); members.splice(index === undefined ? members.length : index, 0, member); diff --git a/test/spec/actions/delete_way.js b/test/spec/actions/delete_way.js index ce678ef4f..fe4b687cb 100644 --- a/test/spec/actions/delete_way.js +++ b/test/spec/actions/delete_way.js @@ -68,4 +68,32 @@ describe("iD.actions.DeleteWay", function() { graph = iD.Graph([way, relation]).update(action); expect(graph.hasEntity(relation.id)).to.be.undefined; }); + + describe("#disabled", function () { + it("returns 'part_of_relation' for members of route and boundary relations", function () { + var a = iD.Way({id: 'a'}), + b = iD.Way({id: 'b'}), + route = iD.Relation({members: [{id: 'a'}], tags: {type: 'route'}}), + boundary = iD.Relation({members: [{id: 'b'}], tags: {type: 'boundary'}}), + graph = iD.Graph([a, b, route, boundary]); + expect(iD.actions.DeleteWay('a').disabled(graph)).to.equal('part_of_relation'); + expect(iD.actions.DeleteWay('b').disabled(graph)).to.equal('part_of_relation'); + }); + + it("returns 'part_of_relation' for outer members of multipolygons", function () { + var way = iD.Way({id: 'w'}), + relation = iD.Relation({members: [{id: 'w', role: 'outer'}], tags: {type: 'multipolygon'}}), + graph = iD.Graph([way, relation]), + action = iD.actions.DeleteWay(way.id); + expect(action.disabled(graph)).to.equal('part_of_relation'); + }); + + it("returns falsy for inner members of multipolygons", function () { + var way = iD.Way({id: 'w'}), + relation = iD.Relation({members: [{id: 'w', role: 'inner'}], tags: {type: 'multipolygon'}}), + graph = iD.Graph([way, relation]), + action = iD.actions.DeleteWay(way.id); + expect(action.disabled(graph)).not.ok; + }); + }); });