Merge pull request #2052 from asolove/sort-relations

Sort relation suggestions most recent first
This commit is contained in:
John Firebaugh
2013-12-11 06:45:55 -08:00
3 changed files with 32 additions and 2 deletions

View File

@@ -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: [],

View File

@@ -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;
}

View File

@@ -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);
});
});
});