From 12769d2fc19b86310613a4e6b62c85c81e742905 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 29 Oct 2013 16:13:40 -0700 Subject: [PATCH] Calculate background source name (fixes #1935) --- js/id/renderer/background.js | 20 +++++---------- js/id/renderer/background_source.js | 39 ++++++++++++++++++++++++++--- js/id/ui/attribution.js | 4 +-- js/id/ui/background.js | 11 +++----- 4 files changed, 48 insertions(+), 26 deletions(-) diff --git a/js/id/renderer/background.js b/js/id/renderer/background.js index 838a1c8c9..067b4c31c 100644 --- a/js/id/renderer/background.js +++ b/js/id/renderer/background.js @@ -28,7 +28,7 @@ iD.Background = function(context) { q = iD.util.stringQs(location.hash.substring(1)); var id = b.id; - if (!id && b.name === 'Custom') { + if (id === 'custom') { id = 'custom:' + b.template; } @@ -46,17 +46,12 @@ iD.Background = function(context) { location.replace('#' + iD.util.qsString(q, true)); - var imageryUsed = []; - if (b.name === 'Custom') { - imageryUsed.push('Custom (' + b.template + ')'); - } else { - imageryUsed.push(b.id || b.name); - } + var imageryUsed = [b.imageryUsed()]; overlayLayers.forEach(function (d) { var source = d.source(); if (!source.isLocatorOverlay()) { - imageryUsed.push(source.id || source.name); + imageryUsed.push(source.imageryUsed()); } }); @@ -85,7 +80,7 @@ iD.Background = function(context) { gpx.call(gpxLayer); var overlays = selection.selectAll('.overlay-layer') - .data(overlayLayers, function(d) { return d.source().name; }); + .data(overlayLayers, function(d) { return d.source().name(); }); overlays.enter().insert('div', '.layer-data') .attr('class', 'layer-layer overlay-layer'); @@ -166,7 +161,7 @@ iD.Background = function(context) { background.showsLayer = function(d) { return d === baseLayer.source() || - (d.name === 'Custom' && baseLayer.source().name === 'Custom') || + (d.id === 'custom' && baseLayer.source().id === 'custom') || overlayLayers.some(function(l) { return l.source() === d; }); }; @@ -214,10 +209,7 @@ iD.Background = function(context) { chosen = q.background || q.layer; if (chosen && chosen.indexOf('custom:') === 0) { - background.baseLayerSource(iD.BackgroundSource({ - template: chosen.replace(/^custom:/, ''), - name: 'Custom' - })); + background.baseLayerSource(iD.BackgroundSource.Custom(chosen.replace(/^custom:/, ''))); } else { background.baseLayerSource(findSource(chosen) || findSource('Bing')); } diff --git a/js/id/renderer/background_source.js b/js/id/renderer/background_source.js index cb1d39597..8cd495c44 100644 --- a/js/id/renderer/background_source.js +++ b/js/id/renderer/background_source.js @@ -1,6 +1,7 @@ iD.BackgroundSource = function(data) { var source = _.clone(data), - offset = [0, 0]; + offset = [0, 0], + name = source.name; source.scaleExtent = data.scaleExtent || [0, 20]; @@ -16,6 +17,14 @@ iD.BackgroundSource = function(data) { return source; }; + source.name = function() { + return name; + }; + + source.imageryUsed = function() { + return source.id || name; + }; + source.url = function(coord) { return data.template .replace('{x}', coord[0]) @@ -42,7 +51,7 @@ iD.BackgroundSource = function(data) { }; source.isLocatorOverlay = function() { - return source.name === 'Locator Overlay'; + return name === 'Locator Overlay'; }; source.copyrightNotices = function() {}; @@ -114,5 +123,29 @@ iD.BackgroundSource.Bing = function(data, dispatch) { }; iD.BackgroundSource.None = function() { - return iD.BackgroundSource({ name: t('background.none'), id: 'None', template: '' }); + var source = iD.BackgroundSource({id: 'none', template: ''}); + + source.name = function() { + return t('background.none'); + }; + + source.imageryUsed = function() { + return 'None'; + }; + + return source; +}; + +iD.BackgroundSource.Custom = function(template) { + var source = iD.BackgroundSource({id: 'custom', template: template}); + + source.name = function() { + return t('background.custom'); + }; + + source.imageryUsed = function() { + return 'Custom (' + template + ')'; + }; + + return source; }; diff --git a/js/id/ui/attribution.js b/js/id/ui/attribution.js index f65590df9..2fef3c390 100644 --- a/js/id/ui/attribution.js +++ b/js/id/ui/attribution.js @@ -10,7 +10,7 @@ iD.ui.Attribution = function(context) { .attr('class', klass); var background = div.selectAll('.attribution') - .data(data, function(d) { return d.name; }); + .data(data, function(d) { return d.name(); }); background.enter() .append('span') @@ -22,7 +22,7 @@ iD.ui.Attribution = function(context) { return; } - var source = d.terms_text || d.id || d.name; + var source = d.terms_text || d.id || d.name(); if (d.logo) { source = ''; diff --git a/js/id/ui/background.js b/js/id/ui/background.js index 166e40e9f..c57d303ee 100644 --- a/js/id/ui/background.js +++ b/js/id/ui/background.js @@ -52,10 +52,7 @@ iD.ui.Background = function(context) { selectLayer(); return; } - context.background().baseLayerSource(iD.BackgroundSource({ - template: template, - name: 'Custom' - })); + context.background().baseLayerSource(iD.BackgroundSource.Custom(template)); selectLayer(); } @@ -76,7 +73,7 @@ iD.ui.Background = function(context) { .filter(filter); var layerLinks = layerList.selectAll('li.layer') - .data(sources, function(d) { return d.name; }); + .data(sources, function(d) { return d.name(); }); var enter = layerLinks.enter() .insert('li', '.custom_layer') @@ -96,7 +93,7 @@ iD.ui.Background = function(context) { .on('change', change); label.append('span') - .text(function(d) { return d.name; }); + .text(function(d) { return d.name(); }); layerLinks.exit() .remove(); @@ -222,7 +219,7 @@ iD.ui.Background = function(context) { var custom = backgroundList.append('li') .attr('class', 'custom_layer') - .datum({name: 'Custom'}); + .datum(iD.BackgroundSource.Custom()); var label = custom.append('label');