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")
164 .attr("for", "mapnik_scale")
165 .attr("class", "col-auto col-form-label")
166 .text(I18n.t("javascripts.share.scale")))
168 .attr("class", "col-auto")
170 .attr("class", "input-group flex-nowrap")
172 .attr("class", "input-group-text")
175 .attr("name", "mapnik_scale")
176 .attr("id", "mapnik_scale")
177 .attr("type", "text")
178 .attr("class", "form-control")
179 .on("change", update))));
182 .attr("class", "row mb-3")
185 .attr("class", "col-auto")
187 .attr("class", "form-check")
189 .attr("for", "image_filter")
190 .attr("class", "form-check-label")
191 .text(I18n.t("javascripts.share.custom_dimensions")))
193 .attr("id", "image_filter")
194 .attr("type", "checkbox")
195 .attr("class", "form-check-input")
196 .bind("change", toggleFilter))));
198 ["minlon", "minlat", "maxlon", "maxlat"].forEach(function (name) {
200 .attr("id", "mapnik_" + name)
202 .attr("type", "hidden")
207 .attr("name", "format")
208 .attr("value", "mapnik")
209 .attr("type", "hidden")
212 var csrf_param = $("meta[name=csrf-param]").attr("content"),
213 csrf_token = $("meta[name=csrf-token]").attr("content");
216 .attr("name", csrf_param)
217 .attr("value", csrf_token)
218 .attr("type", "hidden")
222 width: "<span id=\"mapnik_image_width\"></span>",
223 height: "<span id=\"mapnik_image_height\"></span>"
227 .attr("class", "text-body-secondary")
228 .html(I18n.t("javascripts.share.image_dimensions", args))
232 .attr("type", "submit")
233 .attr("class", "btn btn-primary")
234 .attr("value", I18n.t("javascripts.share.download"))
238 .on("change", update)
241 marker.on("dragend", movedMarker);
242 map.on("move", movedMap);
243 map.on("moveend layeradd layerremove", update);
250 $("#mapnik_scale").val(getScale());
255 map.removeLayer(marker);
256 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
257 locationFilter.disable();
261 function toggleMarker() {
262 if ($(this).is(":checked")) {
263 marker.setLatLng(map.getCenter());
264 map.addLayer(marker);
265 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
267 map.removeLayer(marker);
268 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
273 function toggleFilter() {
274 if ($(this).is(":checked")) {
275 locationFilter.setBounds(map.getBounds().pad(-0.2));
276 locationFilter.enable();
278 locationFilter.disable();
283 function movedMap() {
284 marker.setLatLng(map.getCenter());
288 function movedMarker() {
289 if (map.hasLayer(marker)) {
290 map.off("move", movedMap);
291 map.on("moveend", updateOnce);
292 map.panTo(marker.getLatLng());
296 function updateOnce() {
297 map.off("moveend", updateOnce);
298 map.on("move", movedMap);
302 function escapeHTML(string) {
310 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
311 return htmlEscapes[match];
316 var canEmbed = map.getMapBaseLayerId() !== "tracestracktopo";
317 var bounds = map.getBounds();
320 .prop("checked", map.hasLayer(marker));
323 .prop("checked", locationFilter.isEnabled());
327 $("#short_input").val(map.getShortUrl(marker));
328 $("#long_input").val(map.getUrl(marker));
329 $("#short_link").attr("href", map.getShortUrl(marker));
330 $("#long_link").attr("href", map.getUrl(marker));
333 bbox: bounds.toBBoxString(),
334 layer: map.getMapBaseLayerId()
337 if (map.hasLayer(marker)) {
338 var latLng = marker.getLatLng().wrap();
339 params.marker = latLng.lat + "," + latLng.lng;
343 .toggleClass("btn-primary", canEmbed)
344 .toggleClass("btn-secondary", !canEmbed)
345 .tooltip(canEmbed ? "disable" : "enable");
346 if (!canEmbed && $("#embed_link").hasClass("active")) {
347 $("#long_link").click();
350 $("#embed_html").val(
351 "<iframe width=\"425\" height=\"350\" src=\"" +
352 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
353 "\" style=\"border: 1px solid black\"></iframe><br/>" +
354 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
355 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
360 .attr("href", map.getGeoUri(marker))
361 .html(map.getGeoUri(marker));
365 if (locationFilter.isEnabled()) {
366 bounds = locationFilter.getBounds();
369 var scale = $("#mapnik_scale").val(),
370 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
371 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
372 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
374 $("#mapnik_minlon").val(bounds.getWest());
375 $("#mapnik_minlat").val(bounds.getSouth());
376 $("#mapnik_maxlon").val(bounds.getEast());
377 $("#mapnik_maxlat").val(bounds.getNorth());
379 if (scale < maxScale) {
380 scale = roundScale(maxScale);
381 $("#mapnik_scale").val(scale);
384 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
385 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
387 if (map.getMapBaseLayerId() === "mapnik") {
388 $("#export-image").show();
389 $("#export-warning").hide();
391 $("#export-image").hide();
392 $("#export-warning").show();
400 function getScale() {
401 var bounds = map.getBounds(),
402 centerLat = bounds.getCenter().lat,
403 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
404 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
405 pixelsPerMeter = map.getSize().x / meters,
406 metersPerPixel = 1 / (92 * 39.3701);
407 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
410 function roundScale(scale) {
411 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
412 return precision * Math.ceil(scale / precision);