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_layers_exported_as_image"))
137 .filter(layer => layer.options.canDownloadImage)
138 .map(layer => $("<li>").text(layer.options.name))))
139 .appendTo($imageSection);
142 .attr("id", "export-image")
143 .attr("action", "/export/finish")
144 .attr("method", "post")
145 .appendTo($imageSection);
149 .attr("class", "row mb-3")
151 .attr("for", "mapnik_format")
152 .attr("class", "col-auto col-form-label")
153 .text(I18n.t("javascripts.share.format")))
155 .attr("class", "col-auto")
156 .append($("<select>")
157 .attr("name", "mapnik_format")
158 .attr("id", "mapnik_format")
159 .attr("class", "form-select w-auto")
160 .append($("<option>").val("png").text("PNG").prop("selected", true))
161 .append($("<option>").val("jpeg").text("JPEG"))
162 .append($("<option>").val("svg").text("SVG"))
163 .append($("<option>").val("pdf").text("PDF"))));
167 .attr("class", "row mb-3")
168 .attr("id", "mapnik_scale_row")
170 .attr("for", "mapnik_scale")
171 .attr("class", "col-auto col-form-label")
172 .text(I18n.t("javascripts.share.scale")))
174 .attr("class", "col-auto")
176 .attr("class", "input-group flex-nowrap")
178 .attr("class", "input-group-text")
181 .attr("name", "mapnik_scale")
182 .attr("id", "mapnik_scale")
183 .attr("type", "text")
184 .attr("class", "form-control")
185 .on("change", update))));
188 .attr("class", "row mb-3")
191 .attr("class", "col-auto")
193 .attr("class", "form-check")
195 .attr("for", "image_filter")
196 .attr("class", "form-check-label")
197 .text(I18n.t("javascripts.share.custom_dimensions")))
199 .attr("id", "image_filter")
200 .attr("type", "checkbox")
201 .attr("class", "form-check-input")
202 .bind("change", toggleFilter))));
204 ["minlon", "minlat", "maxlon", "maxlat", "lat", "lon"].forEach(function (name) {
206 .attr("id", "mapnik_" + name)
208 .attr("type", "hidden")
213 .attr("id", "map_format")
214 .attr("name", "format")
215 .attr("value", "mapnik")
216 .attr("type", "hidden")
220 .attr("id", "map_zoom")
221 .attr("name", "zoom")
222 .attr("value", map.getZoom())
223 .attr("type", "hidden")
227 .attr("id", "map_width")
228 .attr("name", "width")
230 .attr("type", "hidden")
234 .attr("id", "map_height")
235 .attr("name", "height")
237 .attr("type", "hidden")
240 var csrf_param = $("meta[name=csrf-param]").attr("content"),
241 csrf_token = $("meta[name=csrf-token]").attr("content");
244 .attr("name", csrf_param)
245 .attr("value", csrf_token)
246 .attr("type", "hidden")
250 layer: "<span id=\"mapnik_image_layer\"></span>",
251 width: "<span id=\"mapnik_image_width\"></span>",
252 height: "<span id=\"mapnik_image_height\"></span>"
256 .attr("class", "text-body-secondary")
257 .html(I18n.t("javascripts.share.image_dimensions", args))
261 .attr("type", "submit")
262 .attr("class", "btn btn-primary")
263 .attr("value", I18n.t("javascripts.share.download"))
267 .on("change", update)
270 marker.on("dragend", movedMarker);
271 map.on("move", movedMap);
272 map.on("moveend layeradd layerremove", update);
279 $("#mapnik_scale").val(getScale());
284 map.removeLayer(marker);
285 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
286 locationFilter.disable();
290 function toggleMarker() {
291 if ($(this).is(":checked")) {
292 marker.setLatLng(map.getCenter());
293 map.addLayer(marker);
294 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
296 map.removeLayer(marker);
297 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
302 function toggleFilter() {
303 if ($(this).is(":checked")) {
304 locationFilter.setBounds(map.getBounds().pad(-0.2));
305 locationFilter.enable();
307 locationFilter.disable();
312 function movedMap() {
313 marker.setLatLng(map.getCenter());
317 function movedMarker() {
318 if (map.hasLayer(marker)) {
319 map.off("move", movedMap);
320 map.on("moveend", updateOnce);
321 map.panTo(marker.getLatLng());
325 function updateOnce() {
326 map.off("moveend", updateOnce);
327 map.on("move", movedMap);
331 function escapeHTML(string) {
339 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
340 return htmlEscapes[match];
345 const layer = map.getMapBaseLayer();
346 var canEmbed = Boolean(layer && layer.options.canEmbed);
347 var bounds = map.getBounds();
350 .prop("checked", map.hasLayer(marker));
353 .prop("checked", locationFilter.isEnabled());
357 $("#short_input").val(map.getShortUrl(marker));
358 $("#long_input").val(map.getUrl(marker));
359 $("#short_link").attr("href", map.getShortUrl(marker));
360 $("#long_link").attr("href", map.getUrl(marker));
363 bbox: bounds.toBBoxString(),
364 layer: map.getMapBaseLayerId()
367 if (map.hasLayer(marker)) {
368 var latLng = marker.getLatLng().wrap();
369 params.marker = latLng.lat + "," + latLng.lng;
373 .toggleClass("btn-primary", canEmbed)
374 .toggleClass("btn-secondary", !canEmbed)
375 .tooltip(canEmbed ? "disable" : "enable");
376 if (!canEmbed && $("#embed_link").hasClass("active")) {
377 $("#long_link").click();
380 $("#embed_html").val(
381 "<iframe width=\"425\" height=\"350\" src=\"" +
382 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
383 "\" style=\"border: 1px solid black\"></iframe><br/>" +
384 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
385 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
390 .attr("href", map.getGeoUri(marker))
391 .html(map.getGeoUri(marker));
395 if (locationFilter.isEnabled()) {
396 bounds = locationFilter.getBounds();
399 var scale = $("#mapnik_scale").val(),
400 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
401 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
402 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
404 $("#mapnik_minlon").val(bounds.getWest());
405 $("#mapnik_minlat").val(bounds.getSouth());
406 $("#mapnik_maxlon").val(bounds.getEast());
407 $("#mapnik_maxlat").val(bounds.getNorth());
409 if (scale < maxScale) {
410 scale = roundScale(maxScale);
411 $("#mapnik_scale").val(scale);
414 const mapWidth = Math.round(size.x / scale / 0.00028);
415 const mapHeight = Math.round(size.y / scale / 0.00028);
416 $("#mapnik_image_width").text(mapWidth);
417 $("#mapnik_image_height").text(mapHeight);
419 const canDownloadImage = Boolean(layer && layer.options.canDownloadImage);
421 $("#mapnik_image_layer").text(canDownloadImage ? layer.options.name : "");
422 $("#map_format").val(canDownloadImage ? layer.options.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 $("#export-image").toggle(canDownloadImage);
431 $("#export-warning").toggle(!canDownloadImage);
432 $("#mapnik_scale_row").toggle(canDownloadImage && layer.options.layerId === "mapnik");
439 function getScale() {
440 var bounds = map.getBounds(),
441 centerLat = bounds.getCenter().lat,
442 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
443 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
444 pixelsPerMeter = map.getSize().x / meters,
445 metersPerPixel = 1 / (92 * 39.3701);
446 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
449 function roundScale(scale) {
450 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
451 return precision * Math.ceil(scale / precision);