1 L.OSM.share = function (options) {
2 var control = L.control(options),
3 marker = L.marker([0, 0], { draggable: true }),
4 locationFilter = new L.LocationFilter({
9 control.onAdd = function (map) {
10 var $container = $("<div>")
11 .attr("class", "control-share");
14 .attr("class", "control-button")
16 .attr("title", I18n.t("javascripts.share.title"))
17 .html("<span class=\"icon share\"></span>")
19 .appendTo($container);
22 .attr("class", "share-ui");
25 .attr("class", "sidebar_heading")
28 $("<button type='button' class='btn-close float-end mt-1'>")
29 .attr("aria-label", I18n.t("javascripts.close"))
30 .bind("click", toggle))
33 .text(I18n.t("javascripts.share.title")));
37 var $linkSection = $("<div>")
38 .attr("class", "section share-link")
42 .text(I18n.t("javascripts.share.link"))
43 .appendTo($linkSection);
45 var $form = $("<form>")
46 .appendTo($linkSection);
49 .attr("class", "form-check mb-3")
53 .attr("for", "link_marker")
54 .attr("class", "form-check-label")
57 .attr("id", "link_marker")
58 .attr("type", "checkbox")
59 .attr("class", "form-check-input")
60 .bind("change", toggleMarker))
61 .append(I18n.t("javascripts.share.include_marker")));
64 .attr("class", "share-tabs")
67 .attr("class", "active")
68 .attr("for", "long_input")
69 .attr("id", "long_link")
70 .text(I18n.t("javascripts.share.long_link")))
72 .attr("for", "short_input")
73 .attr("id", "short_link")
74 .text(I18n.t("javascripts.share.short_link")))
76 .attr("for", "embed_html")
78 .text(I18n.t("javascripts.share.embed")))
79 .on("click", "a", function (e) {
81 var id = "#" + $(this).attr("for");
82 $linkSection.find(".share-tabs a")
83 .removeClass("active");
84 $(this).addClass("active");
85 $linkSection.find(".share-tab")
87 $linkSection.find(".share-tab:has(" + id + ")")
89 .find("input, textarea")
94 .attr("class", "share-tab")
95 .css("display", "block")
98 .attr("id", "long_input")
100 .on("click", select));
103 .attr("class", "share-tab")
106 .attr("id", "short_input")
107 .attr("type", "text")
108 .on("click", select));
111 .attr("class", "share-tab")
115 .attr("id", "embed_html")
116 .on("click", select))
119 .attr("class", "text-muted")
120 .text(I18n.t("javascripts.share.paste_html"))
121 .appendTo($linkSection));
125 var $geoUriSection = $("<div>")
126 .attr("class", "section share-geo-uri")
130 .text(I18n.t("javascripts.share.geo_uri"))
131 .appendTo($geoUriSection);
134 .appendTo($geoUriSection)
136 .attr("id", "geo_uri"));
140 var $imageSection = $("<div>")
141 .attr("class", "section share-image")
145 .text(I18n.t("javascripts.share.image"))
146 .appendTo($imageSection);
149 .attr("id", "export-warning")
150 .attr("class", "text-muted")
151 .text(I18n.t("javascripts.share.only_standard_layer"))
152 .appendTo($imageSection);
155 .attr("id", "export-image")
156 .attr("action", "/export/finish")
157 .attr("method", "post")
158 .appendTo($imageSection);
161 .attr("class", "mb-3 form-check")
165 .attr("for", "image_filter")
166 .attr("class", "form-check-label")
169 .attr("id", "image_filter")
170 .attr("type", "checkbox")
171 .attr("class", "form-check-input")
172 .bind("change", toggleFilter))
173 .append(I18n.t("javascripts.share.custom_dimensions")));
179 .attr("for", "mapnik_format")
180 .text(I18n.t("javascripts.share.format")))
181 .append($("<select>")
182 .attr("name", "mapnik_format")
183 .attr("id", "mapnik_format")
184 .append($("<option>").val("png").text("PNG").prop("selected", true))
185 .append($("<option>").val("jpeg").text("JPEG"))
186 .append($("<option>").val("svg").text("SVG"))
187 .append($("<option>").val("pdf").text("PDF")));
192 .attr("for", "mapnik_scale")
193 .text(I18n.t("javascripts.share.scale")))
196 .attr("name", "mapnik_scale")
197 .attr("id", "mapnik_scale")
198 .attr("type", "text")
199 .on("change", update));
201 ["minlon", "minlat", "maxlon", "maxlat"].forEach(function (name) {
203 .attr("id", "mapnik_" + name)
205 .attr("type", "hidden")
210 .attr("name", "format")
211 .attr("value", "mapnik")
212 .attr("type", "hidden")
215 var csrf_param = $("meta[name=csrf-param]").attr("content"),
216 csrf_token = $("meta[name=csrf-token]").attr("content");
219 .attr("name", csrf_param)
220 .attr("value", csrf_token)
221 .attr("type", "hidden")
225 width: "<span id=\"mapnik_image_width\"></span>",
226 height: "<span id=\"mapnik_image_height\"></span>"
230 .attr("class", "text-muted")
231 .html(I18n.t("javascripts.share.image_dimensions", args))
235 .attr("type", "submit")
236 .attr("class", "btn btn-primary")
237 .attr("value", I18n.t("javascripts.share.download"))
241 .on("change", update)
244 marker.on("dragend", movedMarker);
245 map.on("move", movedMap);
246 map.on("moveend layeradd layerremove", update);
248 options.sidebar.addPane($ui);
254 map.removeLayer(marker);
255 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
256 locationFilter.disable();
264 $("#mapnik_scale").val(getScale());
265 marker.setLatLng(map.getCenter());
268 options.sidebar.togglePane($ui, button);
269 $(".leaflet-control .control-button").tooltip("hide");
272 function toggleMarker() {
273 if ($(this).is(":checked")) {
274 marker.setLatLng(map.getCenter());
275 map.addLayer(marker);
276 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
278 map.removeLayer(marker);
279 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
284 function toggleFilter() {
285 if ($(this).is(":checked")) {
286 locationFilter.setBounds(map.getBounds().pad(-0.2));
287 locationFilter.enable();
289 locationFilter.disable();
294 function movedMap() {
295 marker.setLatLng(map.getCenter());
299 function movedMarker() {
300 if (map.hasLayer(marker)) {
301 map.off("move", movedMap);
302 map.on("moveend", updateOnce);
303 map.panTo(marker.getLatLng());
307 function updateOnce() {
308 map.off("moveend", updateOnce);
309 map.on("move", movedMap);
313 function escapeHTML(string) {
321 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
322 return htmlEscapes[match];
327 var bounds = map.getBounds();
330 .prop("checked", map.hasLayer(marker));
333 .prop("checked", locationFilter.isEnabled());
337 $("#short_input").val(map.getShortUrl(marker));
338 $("#long_input").val(map.getUrl(marker));
339 $("#short_link").attr("href", map.getShortUrl(marker));
340 $("#long_link").attr("href", map.getUrl(marker));
343 bbox: bounds.toBBoxString(),
344 layer: map.getMapBaseLayerId()
347 if (map.hasLayer(marker)) {
348 var latLng = marker.getLatLng().wrap();
349 params.marker = latLng.lat + "," + latLng.lng;
352 $("#embed_html").val(
353 "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"" +
354 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
355 "\" style=\"border: 1px solid black\"></iframe><br/>" +
356 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
357 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
362 .attr("href", map.getGeoUri(marker))
363 .html(map.getGeoUri(marker));
367 if (locationFilter.isEnabled()) {
368 bounds = locationFilter.getBounds();
371 var scale = $("#mapnik_scale").val(),
372 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
373 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
374 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
376 $("#mapnik_minlon").val(bounds.getWest());
377 $("#mapnik_minlat").val(bounds.getSouth());
378 $("#mapnik_maxlon").val(bounds.getEast());
379 $("#mapnik_maxlat").val(bounds.getNorth());
381 if (scale < maxScale) {
382 scale = roundScale(maxScale);
383 $("#mapnik_scale").val(scale);
386 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
387 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
389 if (map.getMapBaseLayerId() === "mapnik") {
390 $("#export-image").show();
391 $("#export-warning").hide();
393 $("#export-image").hide();
394 $("#export-warning").show();
402 function getScale() {
403 var bounds = map.getBounds(),
404 centerLat = bounds.getCenter().lat,
405 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
406 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
407 pixelsPerMeter = map.getSize().x / meters,
408 metersPerPixel = 1 / (92 * 39.3701);
409 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
412 function roundScale(scale) {
413 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
414 return precision * Math.ceil(scale / precision);
417 return $container[0];