mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-13 01:02:58 +00:00
Clean up and test relation sorting code.
This commit is contained in:
@@ -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: [],
|
||||
|
||||
@@ -65,14 +65,9 @@ iD.ui.RawMembershipEditor = function(context) {
|
||||
});
|
||||
|
||||
result.sort(function(a, b) {
|
||||
var aId = parseInt(iD.Entity.id.toOSM(a.relation.id), 10);
|
||||
var bId = parseInt(iD.Entity.id.toOSM(b.relation.id), 10);
|
||||
if (aId < 0) aId = Number.MAX_VALUE / 2 - aId;
|
||||
if (bId < 0) bId = Number.MAX_VALUE / 2 - bId;
|
||||
|
||||
return d3.descending(aId, bId);
|
||||
return iD.Relation.creationOrder(a.relation, b.relation);
|
||||
});
|
||||
result.unshift(newRelation)
|
||||
result.unshift(newRelation);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user