diff --git a/js/id/core/relation.js b/js/id/core/relation.js index 8fbdd05b2..bd1be9877 100644 --- a/js/id/core/relation.js +++ b/js/id/core/relation.js @@ -8,6 +8,14 @@ iD.Relation = iD.Entity.relation = function iD_Relation() { iD.Relation.prototype = Object.create(iD.Entity.prototype); +iD.Relation.creationOrder = function(a, b) { + var aId = parseInt(iD.Entity.id.toOSM(a.id), 10); + var bId = parseInt(iD.Entity.id.toOSM(b.id), 10); + + if (aId < 0 || bId < 0) return aId - bId; + return bId - aId; +}; + _.extend(iD.Relation.prototype, { type: 'relation', members: [], diff --git a/js/id/ui/raw_membership_editor.js b/js/id/ui/raw_membership_editor.js index ef86f12f7..fbda18c5f 100644 --- a/js/id/ui/raw_membership_editor.js +++ b/js/id/ui/raw_membership_editor.js @@ -40,10 +40,11 @@ iD.ui.RawMembershipEditor = function(context) { } function relations(q) { - var result = [{ + var newRelation = { relation: null, value: t('inspector.new_relation') - }], + }, + result = [], graph = context.graph(); context.intersects(context.extent()).forEach(function(entity) { @@ -63,6 +64,11 @@ iD.ui.RawMembershipEditor = function(context) { }); }); + result.sort(function(a, b) { + return iD.Relation.creationOrder(a.relation, b.relation); + }); + result.unshift(newRelation); + return result; } diff --git a/test/spec/core/relation.js b/test/spec/core/relation.js index 103928e2b..f0de2b1ad 100644 --- a/test/spec/core/relation.js +++ b/test/spec/core/relation.js @@ -470,4 +470,20 @@ describe('iD.Relation', function () { expect(r.multipolygon(g)).to.eql([[[a.loc, b.loc, c.loc]]]); }); }); + + describe(".creationOrder comparator", function () { + specify("orders existing relations newest-first", function () { + var a = iD.Relation({ id: 'r1' }), + b = iD.Relation({ id: 'r2' }); + expect(iD.Relation.creationOrder(a, b)).to.be.above(0); + expect(iD.Relation.creationOrder(b, a)).to.be.below(0); + }); + + specify("orders new relations newest-first", function () { + var a = iD.Relation({ id: 'r-1' }), + b = iD.Relation({ id: 'r-2' }); + expect(iD.Relation.creationOrder(a, b)).to.be.above(0); + expect(iD.Relation.creationOrder(b, a)).to.be.below(0); + }); + }); });