1 L.OSM.share = function (options) {
2 var control = L.OSM.sidebarPane(options, "share", "javascripts.share.title", "javascripts.share.title"),
3 marker = L.marker([0, 0], { draggable: true }),
4 locationFilter = new L.LocationFilter({
9 control.onAddPane = function (map, button, $ui) {
11 $("#content").addClass("overlay-right-sidebar");
13 var $linkSection = $("<div>")
14 .attr("class", "share-link p-3 border-bottom border-secondary-subtle")
18 .text(I18n.t("javascripts.share.link"))
19 .appendTo($linkSection);
21 var $form = $("<form>")
22 .appendTo($linkSection);
25 .attr("class", "form-check mb-3")
28 .attr("for", "link_marker")
29 .attr("class", "form-check-label")
30 .text(I18n.t("javascripts.share.include_marker")))
32 .attr("id", "link_marker")
33 .attr("type", "checkbox")
34 .attr("class", "form-check-input")
35 .bind("change", toggleMarker));
37 $("<div class='btn-group btn-group-sm mb-2'>")
39 .append($("<a class='btn btn-primary'>")
41 .attr("for", "long_input")
42 .attr("id", "long_link")
43 .text(I18n.t("javascripts.share.long_link")))
44 .append($("<a class='btn btn-primary'>")
45 .attr("for", "short_input")
46 .attr("id", "short_link")
47 .text(I18n.t("javascripts.share.short_link")))
48 .append($("<a class='btn btn-primary'>")
49 .attr("for", "embed_html")
50 .attr("id", "embed_link")
51 .attr("data-bs-title", I18n.t("javascripts.site.embed_html_disabled"))
53 .text(I18n.t("javascripts.share.embed")))
54 .on("click", "a", function (e) {
56 if (!$(this).hasClass("btn-primary")) return;
57 var id = "#" + $(this).attr("for");
59 .removeClass("active");
60 $(this).addClass("active");
61 $linkSection.find(".share-tab")
63 $linkSection.find(".share-tab:has(" + id + ")")
65 .find("input, textarea")
70 .attr("class", "share-tab")
73 .attr("id", "long_input")
75 .attr("class", "form-control form-control-sm font-monospace")
76 .attr("readonly", true)
77 .on("click", select));
80 .attr("class", "share-tab")
84 .attr("id", "short_input")
86 .attr("class", "form-control form-control-sm font-monospace")
87 .attr("readonly", true)
88 .on("click", select));
91 .attr("class", "share-tab")
96 .attr("id", "embed_html")
97 .attr("class", "form-control form-control-sm font-monospace")
98 .attr("readonly", true)
102 .attr("class", "text-body-secondary")
103 .text(I18n.t("javascripts.share.paste_html")));
107 var $geoUriSection = $("<div>")
108 .attr("class", "share-geo-uri p-3 border-bottom border-secondary-subtle")
112 .text(I18n.t("javascripts.share.geo_uri"))
113 .appendTo($geoUriSection);
116 .appendTo($geoUriSection)
118 .attr("id", "geo_uri"));
122 var $imageSection = $("<div>")
123 .attr("class", "share-image p-3")
127 .text(I18n.t("javascripts.share.image"))
128 .appendTo($imageSection);
131 .attr("id", "export-warning")
132 .attr("class", "text-body-secondary")
133 .text(I18n.t("javascripts.share.only_standard_layer"))
134 .appendTo($imageSection);
137 .attr("id", "export-image")
138 .attr("action", "/export/finish")
139 .attr("method", "post")
140 .appendTo($imageSection);
144 .attr("class", "row mb-3")
146 .attr("for", "mapnik_format")
147 .attr("class", "col-auto col-form-label")
148 .text(I18n.t("javascripts.share.format")))
150 .attr("class", "col-auto")
151 .append($("<select>")
152 .attr("name", "mapnik_format")
153 .attr("id", "mapnik_format")
154 .attr("class", "form-select w-auto")
155 .append($("<option>").val("png").text("PNG").prop("selected", true))
156 .append($("<option>").val("jpeg").text("JPEG"))
157 .append($("<option>").val("svg").text("SVG"))
158 .append($("<option>").val("pdf").text("PDF"))));
162 .attr("class", "row mb-3")
163 .attr("id", "mapnik_scale_row")
165 .attr("for", "mapnik_scale")
166 .attr("class", "col-auto col-form-label")
167 .text(I18n.t("javascripts.share.scale")))
169 .attr("class", "col-auto")
171 .attr("class", "input-group flex-nowrap")
173 .attr("class", "input-group-text")
176 .attr("name", "mapnik_scale")
177 .attr("id", "mapnik_scale")
178 .attr("type", "text")
179 .attr("class", "form-control")
180 .on("change", update))));
183 .attr("class", "row mb-3")
186 .attr("class", "col-auto")
188 .attr("class", "form-check")
190 .attr("for", "image_filter")
191 .attr("class", "form-check-label")
192 .text(I18n.t("javascripts.share.custom_dimensions")))
194 .attr("id", "image_filter")
195 .attr("type", "checkbox")
196 .attr("class", "form-check-input")
197 .bind("change", toggleFilter))));
199 ["minlon", "minlat", "maxlon", "maxlat", "lat", "lon"].forEach(function (name) {
201 .attr("id", "mapnik_" + name)
203 .attr("type", "hidden")
208 .attr("id", "map_format")
209 .attr("name", "format")
210 .attr("value", "mapnik")
211 .attr("type", "hidden")
215 .attr("id", "map_zoom")
216 .attr("name", "zoom")
217 .attr("value", map.getZoom())
218 .attr("type", "hidden")
222 .attr("id", "map_width")
223 .attr("name", "width")
225 .attr("type", "hidden")
229 .attr("id", "map_height")
230 .attr("name", "height")
232 .attr("type", "hidden")
235 var csrf_param = $("meta[name=csrf-param]").attr("content"),
236 csrf_token = $("meta[name=csrf-token]").attr("content");
239 .attr("name", csrf_param)
240 .attr("value", csrf_token)
241 .attr("type", "hidden")
245 layer: "<span id=\"mapnik_image_layer\"></span>",
246 width: "<span id=\"mapnik_image_width\"></span>",
247 height: "<span id=\"mapnik_image_height\"></span>"
251 .attr("class", "text-body-secondary")
252 .html(I18n.t("javascripts.share.image_dimensions", args))
256 .attr("type", "submit")
257 .attr("class", "btn btn-primary")
258 .attr("value", I18n.t("javascripts.share.download"))
262 .on("change", update)
265 marker.on("dragend", movedMarker);
266 map.on("move", movedMap);
267 map.on("moveend layeradd layerremove", update);
274 $("#mapnik_scale").val(getScale());
279 map.removeLayer(marker);
280 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
281 locationFilter.disable();
285 function toggleMarker() {
286 if ($(this).is(":checked")) {
287 marker.setLatLng(map.getCenter());
288 map.addLayer(marker);
289 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
291 map.removeLayer(marker);
292 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
297 function toggleFilter() {
298 if ($(this).is(":checked")) {
299 locationFilter.setBounds(map.getBounds().pad(-0.2));
300 locationFilter.enable();
302 locationFilter.disable();
307 function movedMap() {
308 marker.setLatLng(map.getCenter());
312 function movedMarker() {
313 if (map.hasLayer(marker)) {
314 map.off("move", movedMap);
315 map.on("moveend", updateOnce);
316 map.panTo(marker.getLatLng());
320 function updateOnce() {
321 map.off("moveend", updateOnce);
322 map.on("move", movedMap);
326 function escapeHTML(string) {
334 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
335 return htmlEscapes[match];
340 var canEmbed = map.getMapBaseLayerId() !== "tracestracktopo";
341 var bounds = map.getBounds();
344 .prop("checked", map.hasLayer(marker));
347 .prop("checked", locationFilter.isEnabled());
351 $("#short_input").val(map.getShortUrl(marker));
352 $("#long_input").val(map.getUrl(marker));
353 $("#short_link").attr("href", map.getShortUrl(marker));
354 $("#long_link").attr("href", map.getUrl(marker));
357 bbox: bounds.toBBoxString(),
358 layer: map.getMapBaseLayerId()
361 if (map.hasLayer(marker)) {
362 var latLng = marker.getLatLng().wrap();
363 params.marker = latLng.lat + "," + latLng.lng;
367 .toggleClass("btn-primary", canEmbed)
368 .toggleClass("btn-secondary", !canEmbed)
369 .tooltip(canEmbed ? "disable" : "enable");
370 if (!canEmbed && $("#embed_link").hasClass("active")) {
371 $("#long_link").click();
374 $("#embed_html").val(
375 "<iframe width=\"425\" height=\"350\" src=\"" +
376 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
377 "\" style=\"border: 1px solid black\"></iframe><br/>" +
378 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
379 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
384 .attr("href", map.getGeoUri(marker))
385 .html(map.getGeoUri(marker));
389 if (locationFilter.isEnabled()) {
390 bounds = locationFilter.getBounds();
393 var scale = $("#mapnik_scale").val(),
394 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
395 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
396 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
398 $("#mapnik_minlon").val(bounds.getWest());
399 $("#mapnik_minlat").val(bounds.getSouth());
400 $("#mapnik_maxlon").val(bounds.getEast());
401 $("#mapnik_maxlat").val(bounds.getNorth());
403 if (scale < maxScale) {
404 scale = roundScale(maxScale);
405 $("#mapnik_scale").val(scale);
408 const mapWidth = Math.round(size.x / scale / 0.00028);
409 const mapHeight = Math.round(size.y / scale / 0.00028);
410 $("#mapnik_image_width").text(mapWidth);
411 $("#mapnik_image_height").text(mapHeight);
413 const layer = map.getMapBaseLayerId();
414 const layerKeys = new Map([
415 ["mapnik", "standard"],
416 ["cyclemap", "cycle_map"],
417 ["transportmap", "transport_map"]
420 $("#mapnik_image_layer").text(layerKeys.has(layer) ? I18n.t(`javascripts.map.base.${layerKeys.get(layer)}`) : "");
421 $("#map_format").val(layer);
423 $("#map_zoom").val(map.getZoom());
424 $("#mapnik_lon").val(map.getCenter().lng);
425 $("#mapnik_lat").val(map.getCenter().lat);
426 $("#map_width").val(mapWidth);
427 $("#map_height").val(mapHeight);
429 if (["cyclemap", "transportmap"].includes(map.getMapBaseLayerId())) {
430 $("#export-image").show();
431 $("#mapnik_scale_row").hide();
432 $("#export-warning").hide();
433 } else if (map.getMapBaseLayerId() === "mapnik") {
434 $("#export-image").show();
435 $("#mapnik_scale_row").show();
436 $("#export-warning").hide();
438 $("#export-image").hide();
439 $("#export-warning").show();
447 function getScale() {
448 var bounds = map.getBounds(),
449 centerLat = bounds.getCenter().lat,
450 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
451 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
452 pixelsPerMeter = map.getSize().x / meters,
453 metersPerPixel = 1 / (92 * 39.3701);
454 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
457 function roundScale(scale) {
458 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
459 return precision * Math.ceil(scale / precision);