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) {
12 var $linkSection = $("<div>")
13 .attr("class", "section share-link")
17 .text(I18n.t("javascripts.share.link"))
18 .appendTo($linkSection);
20 var $form = $("<form>")
21 .appendTo($linkSection);
24 .attr("class", "form-check mb-3")
28 .attr("for", "link_marker")
29 .attr("class", "form-check-label")
32 .attr("id", "link_marker")
33 .attr("type", "checkbox")
34 .attr("class", "form-check-input")
35 .bind("change", toggleMarker))
36 .append(I18n.t("javascripts.share.include_marker")));
38 $("<div class='btn-group btn-group-sm mb-2'>")
40 .append($("<a class='btn btn-primary'>")
42 .attr("for", "long_input")
43 .attr("id", "long_link")
44 .text(I18n.t("javascripts.share.long_link")))
45 .append($("<a class='btn btn-primary'>")
46 .attr("for", "short_input")
47 .attr("id", "short_link")
48 .text(I18n.t("javascripts.share.short_link")))
49 .append($("<a class='btn btn-primary'>")
50 .attr("for", "embed_html")
52 .text(I18n.t("javascripts.share.embed")))
53 .on("click", "a", function (e) {
55 var id = "#" + $(this).attr("for");
57 .removeClass("active");
58 $(this).addClass("active");
59 $linkSection.find(".share-tab")
61 $linkSection.find(".share-tab:has(" + id + ")")
63 .find("input, textarea")
68 .attr("class", "share-tab")
69 .css("display", "block")
72 .attr("id", "long_input")
74 .on("click", select));
77 .attr("class", "share-tab")
80 .attr("id", "short_input")
82 .on("click", select));
85 .attr("class", "share-tab")
89 .attr("id", "embed_html")
93 .attr("class", "text-muted")
94 .text(I18n.t("javascripts.share.paste_html")));
98 var $geoUriSection = $("<div>")
99 .attr("class", "section share-geo-uri")
103 .text(I18n.t("javascripts.share.geo_uri"))
104 .appendTo($geoUriSection);
107 .appendTo($geoUriSection)
109 .attr("id", "geo_uri"));
113 var $imageSection = $("<div>")
114 .attr("class", "section share-image")
118 .text(I18n.t("javascripts.share.image"))
119 .appendTo($imageSection);
122 .attr("id", "export-warning")
123 .attr("class", "text-muted")
124 .text(I18n.t("javascripts.share.only_standard_layer"))
125 .appendTo($imageSection);
128 .attr("id", "export-image")
129 .attr("action", "/export/finish")
130 .attr("method", "post")
131 .appendTo($imageSection);
134 .attr("class", "mb-3 form-check")
138 .attr("for", "image_filter")
139 .attr("class", "form-check-label")
142 .attr("id", "image_filter")
143 .attr("type", "checkbox")
144 .attr("class", "form-check-input")
145 .bind("change", toggleFilter))
146 .append(I18n.t("javascripts.share.custom_dimensions")));
152 .attr("for", "mapnik_format")
153 .text(I18n.t("javascripts.share.format")))
154 .append($("<select>")
155 .attr("name", "mapnik_format")
156 .attr("id", "mapnik_format")
157 .append($("<option>").val("png").text("PNG").prop("selected", true))
158 .append($("<option>").val("jpeg").text("JPEG"))
159 .append($("<option>").val("svg").text("SVG"))
160 .append($("<option>").val("pdf").text("PDF")));
165 .attr("for", "mapnik_scale")
166 .text(I18n.t("javascripts.share.scale")))
169 .attr("name", "mapnik_scale")
170 .attr("id", "mapnik_scale")
171 .attr("type", "text")
172 .on("change", update));
174 ["minlon", "minlat", "maxlon", "maxlat"].forEach(function (name) {
176 .attr("id", "mapnik_" + name)
178 .attr("type", "hidden")
183 .attr("name", "format")
184 .attr("value", "mapnik")
185 .attr("type", "hidden")
188 var csrf_param = $("meta[name=csrf-param]").attr("content"),
189 csrf_token = $("meta[name=csrf-token]").attr("content");
192 .attr("name", csrf_param)
193 .attr("value", csrf_token)
194 .attr("type", "hidden")
198 width: "<span id=\"mapnik_image_width\"></span>",
199 height: "<span id=\"mapnik_image_height\"></span>"
203 .attr("class", "text-muted")
204 .html(I18n.t("javascripts.share.image_dimensions", args))
208 .attr("type", "submit")
209 .attr("class", "btn btn-primary")
210 .attr("value", I18n.t("javascripts.share.download"))
214 .on("change", update)
217 marker.on("dragend", movedMarker);
218 map.on("move", movedMap);
219 map.on("moveend layeradd layerremove", update);
226 $("#mapnik_scale").val(getScale());
231 map.removeLayer(marker);
232 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
233 locationFilter.disable();
237 function toggleMarker() {
238 if ($(this).is(":checked")) {
239 marker.setLatLng(map.getCenter());
240 map.addLayer(marker);
241 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
243 map.removeLayer(marker);
244 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
249 function toggleFilter() {
250 if ($(this).is(":checked")) {
251 locationFilter.setBounds(map.getBounds().pad(-0.2));
252 locationFilter.enable();
254 locationFilter.disable();
259 function movedMap() {
260 marker.setLatLng(map.getCenter());
264 function movedMarker() {
265 if (map.hasLayer(marker)) {
266 map.off("move", movedMap);
267 map.on("moveend", updateOnce);
268 map.panTo(marker.getLatLng());
272 function updateOnce() {
273 map.off("moveend", updateOnce);
274 map.on("move", movedMap);
278 function escapeHTML(string) {
286 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
287 return htmlEscapes[match];
292 var bounds = map.getBounds();
295 .prop("checked", map.hasLayer(marker));
298 .prop("checked", locationFilter.isEnabled());
302 $("#short_input").val(map.getShortUrl(marker));
303 $("#long_input").val(map.getUrl(marker));
304 $("#short_link").attr("href", map.getShortUrl(marker));
305 $("#long_link").attr("href", map.getUrl(marker));
308 bbox: bounds.toBBoxString(),
309 layer: map.getMapBaseLayerId()
312 if (map.hasLayer(marker)) {
313 var latLng = marker.getLatLng().wrap();
314 params.marker = latLng.lat + "," + latLng.lng;
317 $("#embed_html").val(
318 "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"" +
319 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
320 "\" style=\"border: 1px solid black\"></iframe><br/>" +
321 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
322 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
327 .attr("href", map.getGeoUri(marker))
328 .html(map.getGeoUri(marker));
332 if (locationFilter.isEnabled()) {
333 bounds = locationFilter.getBounds();
336 var scale = $("#mapnik_scale").val(),
337 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
338 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
339 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
341 $("#mapnik_minlon").val(bounds.getWest());
342 $("#mapnik_minlat").val(bounds.getSouth());
343 $("#mapnik_maxlon").val(bounds.getEast());
344 $("#mapnik_maxlat").val(bounds.getNorth());
346 if (scale < maxScale) {
347 scale = roundScale(maxScale);
348 $("#mapnik_scale").val(scale);
351 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
352 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
354 if (map.getMapBaseLayerId() === "mapnik") {
355 $("#export-image").show();
356 $("#export-warning").hide();
358 $("#export-image").hide();
359 $("#export-warning").show();
367 function getScale() {
368 var bounds = map.getBounds(),
369 centerLat = bounds.getCenter().lat,
370 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
371 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
372 pixelsPerMeter = map.getSize().x / meters,
373 metersPerPixel = 1 / (92 * 39.3701);
374 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
377 function roundScale(scale) {
378 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
379 return precision * Math.ceil(scale / precision);