1 OSM.MapLibre.AttributionControl = class extends maplibregl.AttributionControl {
2 constructor({ includeReportLink, credit, ...options } = {}) {
3 if (includeReportLink) {
4 options.compact = false;
8 this._container = null;
9 this._includeReportLink = Boolean(includeReportLink);
10 this._credit = credit;
11 this._boundUpdateReportLink = this._updateReportLink.bind(this);
14 _updateAttributions() {
15 if (!this._map.style) return;
19 // Without the map being loaded, a user is unlikely to have good data-feedback
20 if (this._includeReportLink && this._map.loaded()) {
21 const reportLink = document.createElement("a");
22 reportLink.href = "/fixthemap";
23 reportLink.target = "_blank";
24 reportLink.rel = "noopener noreferrer";
25 reportLink.className = "maplibregl-ctrl-attrib-report-link";
26 reportLink.textContent = OSM.i18n.t("javascripts.embed.report_problem");
27 attribHTML += reportLink.outerHTML + " | ";
30 const copyrightLink = document.createElement("a");
31 copyrightLink.href = "/copyright";
32 copyrightLink.textContent = OSM.i18n.t("javascripts.map.openstreetmap_contributors");
34 attribHTML += OSM.i18n.t("javascripts.map.copyright_text", {
35 copyright_link: copyrightLink.outerHTML
39 attribHTML += this._credit.donate ? " ♥️ " : ". ";
40 attribHTML += this._buildCreditHtml(this._credit);
45 const termsLink = document.createElement("a");
46 termsLink.href = "https://wiki.osmfoundation.org/wiki/Terms_of_Use";
47 termsLink.target = "_blank";
48 termsLink.rel = "noopener noreferrer";
49 termsLink.textContent = OSM.i18n.t("javascripts.map.website_and_api_terms");
50 attribHTML += termsLink.outerHTML;
52 // check if attribution string is different to minimize DOM changes
53 if (attribHTML === this._attribHTML) return;
55 this._innerContainer.innerHTML = attribHTML;
56 this._attribHTML = attribHTML;
57 this._updateCompact();
59 // Update report link href after initial render
60 if (this._includeReportLink) {
61 this._updateReportLink();
65 _buildCreditHtml(credit) {
67 for (const childId in credit.children) {
68 children[childId] = this._buildCreditHtml(credit.children[childId]);
71 const text = OSM.i18n.t(`javascripts.map.${credit.id}`, children);
76 const link = document.createElement("a");
77 link.href = credit.href;
78 link.textContent = text;
81 link.className = "donate-attr";
83 link.target = "_blank";
84 link.rel = "noopener noreferrer";
87 return link.outerHTML;
93 if (this._includeReportLink) {
94 map.once("load", this._updateAttributions.bind(this));
95 map.on("moveend", this._boundUpdateReportLink);
98 return super.onAdd(map);
102 if (!this._map) return;
104 if (this._includeReportLink) {
105 this._map.off("moveend", this._boundUpdateReportLink);
111 _updateReportLink() {
112 if (!this._container) return;
114 const reportLink = this._container.querySelector(".maplibregl-ctrl-attrib-report-link");
115 if (!reportLink) return;
117 const center = this._map.getCenter();
118 const params = new URLSearchParams({
119 lat: center.lat.toFixed(5),
120 lon: center.lng.toFixed(5),
121 zoom: Math.floor(this._map.getZoom())
123 reportLink.href = `/fixthemap?${params.toString()}`;