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")));
39 .attr("class", "share-tabs")
42 .attr("class", "active")
43 .attr("for", "long_input")
44 .attr("id", "long_link")
45 .text(I18n.t("javascripts.share.long_link")))
47 .attr("for", "short_input")
48 .attr("id", "short_link")
49 .text(I18n.t("javascripts.share.short_link")))
51 .attr("for", "embed_html")
53 .text(I18n.t("javascripts.share.embed")))
54 .on("click", "a", function (e) {
56 var id = "#" + $(this).attr("for");
57 $linkSection.find(".share-tabs a")
58 .removeClass("active");
59 $(this).addClass("active");
60 $linkSection.find(".share-tab")
62 $linkSection.find(".share-tab:has(" + id + ")")
64 .find("input, textarea")
69 .attr("class", "share-tab")
70 .css("display", "block")
73 .attr("id", "long_input")
75 .on("click", select));
78 .attr("class", "share-tab")
81 .attr("id", "short_input")
83 .on("click", select));
86 .attr("class", "share-tab")
90 .attr("id", "embed_html")
94 .attr("class", "text-muted")
95 .text(I18n.t("javascripts.share.paste_html")));
99 var $geoUriSection = $("<div>")
100 .attr("class", "section share-geo-uri")
104 .text(I18n.t("javascripts.share.geo_uri"))
105 .appendTo($geoUriSection);
108 .appendTo($geoUriSection)
110 .attr("id", "geo_uri"));
114 var $imageSection = $("<div>")
115 .attr("class", "section share-image")
119 .text(I18n.t("javascripts.share.image"))
120 .appendTo($imageSection);
123 .attr("id", "export-warning")
124 .attr("class", "text-muted")
125 .text(I18n.t("javascripts.share.only_standard_layer"))
126 .appendTo($imageSection);
129 .attr("id", "export-image")
130 .attr("action", "/export/finish")
131 .attr("method", "post")
132 .appendTo($imageSection);
135 .attr("class", "mb-3 form-check")
139 .attr("for", "image_filter")
140 .attr("class", "form-check-label")
143 .attr("id", "image_filter")
144 .attr("type", "checkbox")
145 .attr("class", "form-check-input")
146 .bind("change", toggleFilter))
147 .append(I18n.t("javascripts.share.custom_dimensions")));
153 .attr("for", "mapnik_format")
154 .text(I18n.t("javascripts.share.format")))
155 .append($("<select>")
156 .attr("name", "mapnik_format")
157 .attr("id", "mapnik_format")
158 .append($("<option>").val("png").text("PNG").prop("selected", true))
159 .append($("<option>").val("jpeg").text("JPEG"))
160 .append($("<option>").val("svg").text("SVG"))
161 .append($("<option>").val("pdf").text("PDF")));
166 .attr("for", "mapnik_scale")
167 .text(I18n.t("javascripts.share.scale")))
170 .attr("name", "mapnik_scale")
171 .attr("id", "mapnik_scale")
172 .attr("type", "text")
173 .on("change", update));
175 ["minlon", "minlat", "maxlon", "maxlat"].forEach(function (name) {
177 .attr("id", "mapnik_" + name)
179 .attr("type", "hidden")
184 .attr("name", "format")
185 .attr("value", "mapnik")
186 .attr("type", "hidden")
189 var csrf_param = $("meta[name=csrf-param]").attr("content"),
190 csrf_token = $("meta[name=csrf-token]").attr("content");
193 .attr("name", csrf_param)
194 .attr("value", csrf_token)
195 .attr("type", "hidden")
199 width: "<span id=\"mapnik_image_width\"></span>",
200 height: "<span id=\"mapnik_image_height\"></span>"
204 .attr("class", "text-muted")
205 .html(I18n.t("javascripts.share.image_dimensions", args))
209 .attr("type", "submit")
210 .attr("class", "btn btn-primary")
211 .attr("value", I18n.t("javascripts.share.download"))
215 .on("change", update)
218 marker.on("dragend", movedMarker);
219 map.on("move", movedMap);
220 map.on("moveend layeradd layerremove", update);
227 $("#mapnik_scale").val(getScale());
232 map.removeLayer(marker);
233 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
234 locationFilter.disable();
238 function toggleMarker() {
239 if ($(this).is(":checked")) {
240 marker.setLatLng(map.getCenter());
241 map.addLayer(marker);
242 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
244 map.removeLayer(marker);
245 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
250 function toggleFilter() {
251 if ($(this).is(":checked")) {
252 locationFilter.setBounds(map.getBounds().pad(-0.2));
253 locationFilter.enable();
255 locationFilter.disable();
260 function movedMap() {
261 marker.setLatLng(map.getCenter());
265 function movedMarker() {
266 if (map.hasLayer(marker)) {
267 map.off("move", movedMap);
268 map.on("moveend", updateOnce);
269 map.panTo(marker.getLatLng());
273 function updateOnce() {
274 map.off("moveend", updateOnce);
275 map.on("move", movedMap);
279 function escapeHTML(string) {
287 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
288 return htmlEscapes[match];
293 var bounds = map.getBounds();
296 .prop("checked", map.hasLayer(marker));
299 .prop("checked", locationFilter.isEnabled());
303 $("#short_input").val(map.getShortUrl(marker));
304 $("#long_input").val(map.getUrl(marker));
305 $("#short_link").attr("href", map.getShortUrl(marker));
306 $("#long_link").attr("href", map.getUrl(marker));
309 bbox: bounds.toBBoxString(),
310 layer: map.getMapBaseLayerId()
313 if (map.hasLayer(marker)) {
314 var latLng = marker.getLatLng().wrap();
315 params.marker = latLng.lat + "," + latLng.lng;
318 $("#embed_html").val(
319 "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"" +
320 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
321 "\" style=\"border: 1px solid black\"></iframe><br/>" +
322 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
323 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
328 .attr("href", map.getGeoUri(marker))
329 .html(map.getGeoUri(marker));
333 if (locationFilter.isEnabled()) {
334 bounds = locationFilter.getBounds();
337 var scale = $("#mapnik_scale").val(),
338 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
339 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
340 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
342 $("#mapnik_minlon").val(bounds.getWest());
343 $("#mapnik_minlat").val(bounds.getSouth());
344 $("#mapnik_maxlon").val(bounds.getEast());
345 $("#mapnik_maxlat").val(bounds.getNorth());
347 if (scale < maxScale) {
348 scale = roundScale(maxScale);
349 $("#mapnik_scale").val(scale);
352 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
353 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
355 if (map.getMapBaseLayerId() === "mapnik") {
356 $("#export-image").show();
357 $("#export-warning").hide();
359 $("#export-image").hide();
360 $("#export-warning").show();
368 function getScale() {
369 var bounds = map.getBounds(),
370 centerLat = bounds.getCenter().lat,
371 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
372 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
373 pixelsPerMeter = map.getSize().x / meters,
374 metersPerPixel = 1 / (92 * 39.3701);
375 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
378 function roundScale(scale) {
379 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
380 return precision * Math.ceil(scale / precision);