Stable sort for background imagery list

Sort by 1. best, 2. area descending, 3. name ascending
This commit is contained in:
Bryan Housel
2016-05-11 21:09:10 -04:00
parent 3beda5cf84
commit 643b4ad49b
2 changed files with 23 additions and 8 deletions
+14
View File
@@ -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;
};
@@ -133,6 +139,10 @@ iD.BackgroundSource.None = function() {
return 'None';
};
source.area = function() {
return -1;
};
return source;
};
@@ -147,5 +157,9 @@ iD.BackgroundSource.Custom = function(template) {
return 'Custom (' + template + ')';
};
source.area = function() {
return -2;
};
return source;
};
+9 -8
View File
@@ -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) {
@@ -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')
@@ -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() {