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 const layer = map.getMapBaseLayer();
341 var canEmbed = layer && layer.options.canEmbed;
342 var bounds = map.getBounds();
345 .prop("checked", map.hasLayer(marker));
348 .prop("checked", locationFilter.isEnabled());
352 $("#short_input").val(map.getShortUrl(marker));
353 $("#long_input").val(map.getUrl(marker));
354 $("#short_link").attr("href", map.getShortUrl(marker));
355 $("#long_link").attr("href", map.getUrl(marker));
358 bbox: bounds.toBBoxString(),
359 layer: map.getMapBaseLayerId()
362 if (map.hasLayer(marker)) {
363 var latLng = marker.getLatLng().wrap();
364 params.marker = latLng.lat + "," + latLng.lng;
368 .toggleClass("btn-primary", canEmbed)
369 .toggleClass("btn-secondary", !canEmbed)
370 .tooltip(canEmbed ? "disable" : "enable");
371 if (!canEmbed && $("#embed_link").hasClass("active")) {
372 $("#long_link").click();
375 $("#embed_html").val(
376 "<iframe width=\"425\" height=\"350\" src=\"" +
377 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
378 "\" style=\"border: 1px solid black\"></iframe><br/>" +
379 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
380 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
385 .attr("href", map.getGeoUri(marker))
386 .html(map.getGeoUri(marker));
390 if (locationFilter.isEnabled()) {
391 bounds = locationFilter.getBounds();
394 var scale = $("#mapnik_scale").val(),
395 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
396 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
397 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
399 $("#mapnik_minlon").val(bounds.getWest());
400 $("#mapnik_minlat").val(bounds.getSouth());
401 $("#mapnik_maxlon").val(bounds.getEast());
402 $("#mapnik_maxlat").val(bounds.getNorth());
404 if (scale < maxScale) {
405 scale = roundScale(maxScale);
406 $("#mapnik_scale").val(scale);
409 const mapWidth = Math.round(size.x / scale / 0.00028);
410 const mapHeight = Math.round(size.y / scale / 0.00028);
411 $("#mapnik_image_width").text(mapWidth);
412 $("#mapnik_image_height").text(mapHeight);
414 const layerId = map.getMapBaseLayerId();
415 const layerKeys = new Map([
416 ["mapnik", "standard"],
417 ["cyclemap", "cycle_map"],
418 ["transportmap", "transport_map"]
421 $("#mapnik_image_layer").text(layerKeys.has(layerId) ? I18n.t(`javascripts.map.base.${layerKeys.get(layerId)}`) : "");
422 $("#map_format").val(layerId);
424 $("#map_zoom").val(map.getZoom());
425 $("#mapnik_lon").val(map.getCenter().lng);
426 $("#mapnik_lat").val(map.getCenter().lat);
427 $("#map_width").val(mapWidth);
428 $("#map_height").val(mapHeight);
430 if (["cyclemap", "transportmap"].includes(map.getMapBaseLayerId())) {
431 $("#export-image").show();
432 $("#mapnik_scale_row").hide();
433 $("#export-warning").hide();
434 } else if (map.getMapBaseLayerId() === "mapnik") {
435 $("#export-image").show();
436 $("#mapnik_scale_row").show();
437 $("#export-warning").hide();
439 $("#export-image").hide();
440 $("#export-warning").show();
448 function getScale() {
449 var bounds = map.getBounds(),
450 centerLat = bounds.getCenter().lat,
451 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
452 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
453 pixelsPerMeter = map.getSize().x / meters,
454 metersPerPixel = 1 / (92 * 39.3701);
455 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
458 function roundScale(scale) {
459 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
460 return precision * Math.ceil(scale / precision);