Skip to content

Commit 643b4ad

Browse files
committed
Stable sort for background imagery list
Sort by 1. best, 2. area descending, 3. name ascending
1 parent 3beda5c commit 643b4ad

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

js/id/renderer/background_source.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ iD.BackgroundSource = function(data) {
2727
return best;
2828
};
2929

30+
source.area = function() {
31+
if (!data.polygon) return Number.MAX_VALUE; // worldwide
32+
var area = d3.geo.area({ type: 'MultiPolygon', coordinates: [ data.polygon ] });
33+
return isNaN(area) ? 0 : area;
34+
};
35+
3036
source.imageryUsed = function() {
3137
return source.id || name;
3238
};
@@ -133,6 +139,10 @@ iD.BackgroundSource.None = function() {
133139
return 'None';
134140
};
135141

142+
source.area = function() {
143+
return -1;
144+
};
145+
136146
return source;
137147
};
138148

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

160+
source.area = function() {
161+
return -2;
162+
};
163+
150164
return source;
151165
};

js/id/ui/background.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ iD.ui.Background = function(context) {
1313
// Can be 0 from <1.3.0 use or due to issue #1923.
1414
if (opacityDefault === 0) opacityDefault = 1.0;
1515

16+
1617
function background(selection) {
1718

1819
function sortSources(a, b) {
19-
return a.best() ? -1
20-
: b.best() ? 1
21-
: a.id === 'none' ? 1
22-
: b.id === 'none' ? -1
23-
: d3.ascending(a, b);
20+
return a.best() && !b.best() ? -1
21+
: b.best() && !a.best() ? 1
22+
: d3.descending(a.area(), b.area()) || d3.ascending(a.name(), b.name()) || 0;
2423
}
2524

2625
function setOpacity(d) {
@@ -87,8 +86,7 @@ iD.ui.Background = function(context) {
8786
.filter(filter);
8887

8988
var layerLinks = layerList.selectAll('li.layer')
90-
.data(sources, function(d) { return d.name(); })
91-
.sort(sortSources);
89+
.data(sources, function(d) { return d.name(); });
9290

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

121+
123122
layerLinks.exit()
124123
.remove();
125124

126-
layerList.style('display', layerList.selectAll('li.layer').data().length > 0 ? 'block' : 'none');
125+
layerList.selectAll('li.layer')
126+
.sort(sortSources)
127+
.style('display', layerList.selectAll('li.layer').data().length > 0 ? 'block' : 'none');
127128
}
128129

129130
function update() {

0 commit comments

Comments
 (0)