Skip to content

Commit 00270dd

Browse files
committed
Collect map layer credits into a data structure
1 parent 6f04849 commit 00270dd

File tree

1 file changed

+102
-55
lines changed

1 file changed

+102
-55
lines changed

app/assets/javascripts/leaflet.map.js

+102-55
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,85 @@ L.OSM.Map = L.Map.extend({
1515
initialize: function (id, options) {
1616
L.Map.prototype.initialize.call(this, id, options);
1717

18-
var copyright_link = $("<a>", {
19-
href: "/copyright",
20-
text: I18n.t("javascripts.map.openstreetmap_contributors")
21-
}).prop("outerHTML");
22-
var copyright = I18n.t("javascripts.map.copyright_text", { copyright_link: copyright_link });
23-
24-
var donate = $("<a>", {
25-
"href": "https://supporting.openstreetmap.org",
26-
"class": "donate-attr",
27-
"text": I18n.t("javascripts.map.make_a_donation")
28-
}).prop("outerHTML");
29-
30-
var terms = $("<a>", {
31-
href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
32-
text: I18n.t("javascripts.map.website_and_api_terms")
33-
}).prop("outerHTML");
34-
35-
var cyclosm_link = $("<a>", {
36-
href: "https://www.cyclosm.org",
37-
target: "_blank",
38-
text: I18n.t("javascripts.map.cyclosm_name")
39-
}).prop("outerHTML");
40-
var osm_france_link = $("<a>", {
41-
href: "https://openstreetmap.fr/",
42-
target: "_blank",
43-
text: I18n.t("javascripts.map.osm_france")
44-
}).prop("outerHTML");
45-
var cyclosm = I18n.t("javascripts.map.cyclosm_credit", { cyclosm_link: cyclosm_link, osm_france_link: osm_france_link });
46-
47-
var thunderforest_link = $("<a>", {
48-
href: "https://www.thunderforest.com/",
49-
target: "_blank",
50-
text: I18n.t("javascripts.map.andy_allan")
51-
}).prop("outerHTML");
52-
var thunderforest = I18n.t("javascripts.map.thunderforest_credit", { thunderforest_link: thunderforest_link });
53-
54-
var tracestrack_link = $("<a>", {
55-
href: "https://www.tracestrack.com/",
56-
target: "_blank",
57-
text: I18n.t("javascripts.map.tracestrack")
58-
}).prop("outerHTML");
59-
var tracestrack = I18n.t("javascripts.map.tracestrack_credit", { tracestrack_link: tracestrack_link });
60-
61-
var hotosm_link = $("<a>", {
62-
href: "https://www.hotosm.org/",
63-
target: "_blank",
64-
text: I18n.t("javascripts.map.hotosm_name")
65-
}).prop("outerHTML");
66-
var hotosm = I18n.t("javascripts.map.hotosm_credit", { hotosm_link: hotosm_link, osm_france_link: osm_france_link });
18+
const layerCredits = {
19+
mapnik: {
20+
id: "make_a_donation",
21+
href: "https://supporting.openstreetmap.org",
22+
donate: true
23+
},
24+
cyclosm: {
25+
id: "cyclosm_credit",
26+
children: {
27+
cyclosm_link: {
28+
id: "cyclosm_name",
29+
href: "https://www.cyclosm.org"
30+
},
31+
osm_france_link: {
32+
id: "osm_france",
33+
href: "https://openstreetmap.fr/"
34+
}
35+
}
36+
},
37+
thunderforest: {
38+
id: "thunderforest_credit",
39+
children: {
40+
thunderforest_link: {
41+
id: "andy_allan",
42+
href: "https://www.thunderforest.com/"
43+
}
44+
}
45+
},
46+
tracestrack: {
47+
id: "tracestrack_credit",
48+
children: {
49+
tracestrack_link: {
50+
id: "tracestrack",
51+
href: "https://www.tracestrack.com/"
52+
}
53+
}
54+
},
55+
hotosm: {
56+
id: "hotosm_credit",
57+
children: {
58+
hotosm_link: {
59+
id: "hotosm_name",
60+
href: "https://www.hotosm.org/"
61+
},
62+
osm_france_link: {
63+
id: "osm_france",
64+
href: "https://openstreetmap.fr/"
65+
}
66+
}
67+
}
68+
};
6769

6870
this.baseLayers = [];
6971

7072
this.baseLayers.push(new L.OSM.Mapnik({
71-
attribution: copyright + " &hearts; " + donate + ". " + terms,
73+
attribution: makeAttribution("mapnik"),
7274
code: "M",
7375
keyid: "mapnik",
7476
name: I18n.t("javascripts.map.base.standard")
7577
}));
7678

7779
this.baseLayers.push(new L.OSM.CyclOSM({
78-
attribution: copyright + ". " + cyclosm + ". " + terms,
80+
attribution: makeAttribution("cyclosm"),
7981
code: "Y",
8082
keyid: "cyclosm",
8183
name: I18n.t("javascripts.map.base.cyclosm")
8284
}));
8385

8486
if (OSM.THUNDERFOREST_KEY) {
8587
this.baseLayers.push(new L.OSM.CycleMap({
86-
attribution: copyright + ". " + thunderforest + ". " + terms,
88+
attribution: makeAttribution("thunderforest"),
8789
apikey: OSM.THUNDERFOREST_KEY,
8890
code: "C",
8991
keyid: "cyclemap",
9092
name: I18n.t("javascripts.map.base.cycle_map")
9193
}));
9294

9395
this.baseLayers.push(new L.OSM.TransportMap({
94-
attribution: copyright + ". " + thunderforest + ". " + terms,
96+
attribution: makeAttribution("thunderforest"),
9597
apikey: OSM.THUNDERFOREST_KEY,
9698
code: "T",
9799
keyid: "transportmap",
@@ -101,7 +103,7 @@ L.OSM.Map = L.Map.extend({
101103

102104
if (OSM.TRACESTRACK_KEY) {
103105
this.baseLayers.push(new L.OSM.TracestrackTopo({
104-
attribution: copyright + ". " + tracestrack + ". " + terms,
106+
attribution: makeAttribution("tracestrack"),
105107
apikey: OSM.TRACESTRACK_KEY,
106108
code: "P",
107109
keyid: "tracestracktopo",
@@ -110,7 +112,7 @@ L.OSM.Map = L.Map.extend({
110112
}
111113

112114
this.baseLayers.push(new L.OSM.HOT({
113-
attribution: copyright + ". " + hotosm + ". " + terms,
115+
attribution: makeAttribution("hotosm"),
114116
code: "H",
115117
keyid: "hot",
116118
name: I18n.t("javascripts.map.base.hot")
@@ -132,6 +134,51 @@ L.OSM.Map = L.Map.extend({
132134
this.setMaxZoom(event.layer.options.maxZoom);
133135
}
134136
});
137+
138+
function makeAttribution(id) {
139+
const layerCredit = layerCredits[id];
140+
let attribution = "";
141+
142+
attribution += I18n.t("javascripts.map.copyright_text", {
143+
copyright_link: $("<a>", {
144+
href: "/copyright",
145+
text: I18n.t("javascripts.map.openstreetmap_contributors")
146+
}).prop("outerHTML")
147+
});
148+
149+
attribution += layerCredit.donate ? " &hearts; " : ". ";
150+
attribution += makeCredit(layerCredit);
151+
attribution += ". ";
152+
153+
attribution += $("<a>", {
154+
href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
155+
text: I18n.t("javascripts.map.website_and_api_terms")
156+
}).prop("outerHTML");
157+
158+
return attribution;
159+
}
160+
161+
function makeCredit(credit) {
162+
const children = {};
163+
for (const childId in credit.children) {
164+
children[childId] = makeCredit(credit.children[childId]);
165+
}
166+
const text = I18n.t(`javascripts.map.${credit.id}`, children);
167+
if (credit.href) {
168+
const link = $("<a>", {
169+
href: credit.href,
170+
text: text
171+
});
172+
if (credit.donate) {
173+
link.addClass("donate-attr");
174+
} else {
175+
link.attr("target", "_blank");
176+
}
177+
return link.prop("outerHTML");
178+
} else {
179+
return text;
180+
}
181+
}
135182
},
136183

137184
updateLayers: function (layerParam) {

0 commit comments

Comments
 (0)