Skip to content

Commit

Permalink
Stable sort for background imagery list
Browse files Browse the repository at this point in the history
Sort by 1. best, 2. area descending, 3. name ascending
  • Loading branch information
bhousel committed May 12, 2016
1 parent 3beda5c commit 643b4ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 14 additions & 0 deletions js/id/renderer/background_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ iD.BackgroundSource = function(data) {
return best;
};

source.area = function() {
if (!data.polygon) return Number.MAX_VALUE; // worldwide
var area = d3.geo.area({ type: 'MultiPolygon', coordinates: [ data.polygon ] });
return isNaN(area) ? 0 : area;
};

source.imageryUsed = function() {
return source.id || name;
};
Expand Down Expand Up @@ -133,6 +139,10 @@ iD.BackgroundSource.None = function() {
return 'None';
};

source.area = function() {
return -1;
};

return source;
};

Expand All @@ -147,5 +157,9 @@ iD.BackgroundSource.Custom = function(template) {
return 'Custom (' + template + ')';
};

source.area = function() {
return -2;
};

return source;
};
17 changes: 9 additions & 8 deletions js/id/ui/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ iD.ui.Background = function(context) {
// Can be 0 from <1.3.0 use or due to issue #1923.
if (opacityDefault === 0) opacityDefault = 1.0;


function background(selection) {

function sortSources(a, b) {
return a.best() ? -1
: b.best() ? 1
: a.id === 'none' ? 1
: b.id === 'none' ? -1
: d3.ascending(a, b);
return a.best() && !b.best() ? -1
: b.best() && !a.best() ? 1
: d3.descending(a.area(), b.area()) || d3.ascending(a.name(), b.name()) || 0;
}

function setOpacity(d) {
Expand Down Expand Up @@ -87,8 +86,7 @@ iD.ui.Background = function(context) {
.filter(filter);

var layerLinks = layerList.selectAll('li.layer')
.data(sources, function(d) { return d.name(); })
.sort(sortSources);
.data(sources, function(d) { return d.name(); });

var enter = layerLinks.enter()
.insert('li', '.custom_layer')
Expand Down Expand Up @@ -120,10 +118,13 @@ iD.ui.Background = function(context) {
label.append('span')
.text(function(d) { return d.name(); });


layerLinks.exit()
.remove();

layerList.style('display', layerList.selectAll('li.layer').data().length > 0 ? 'block' : 'none');
layerList.selectAll('li.layer')
.sort(sortSources)
.style('display', layerList.selectAll('li.layer').data().length > 0 ? 'block' : 'none');
}

function update() {
Expand Down

3 comments on commit 643b4ad

@pnorman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date might be worth including in the sort

@bhousel
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But when the date matters, it's usually part of the name:

screenshot 2016-05-11 22 13 14

@pnorman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is not a more stable sort, but one that's better

Please sign in to comment.