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")
29 .text(I18n.t("javascripts.close"))
30 .attr("class", "icon close")
31 .bind("click", toggle))
34 .text(I18n.t("javascripts.share.title")));
38 var $linkSection = $("<div>")
39 .attr("class", "section share-link")
43 .text(I18n.t("javascripts.share.link"))
44 .appendTo($linkSection);
46 var $form = $("<form>")
47 .appendTo($linkSection);
50 .attr("class", "form-check form-group")
54 .attr("for", "link_marker")
55 .attr("class", "form-check-label")
58 .attr("id", "link_marker")
59 .attr("type", "checkbox")
60 .attr("class", "form-check-input")
61 .bind("change", toggleMarker))
62 .append(I18n.t("javascripts.share.include_marker")));
65 .attr("class", "share-tabs")
68 .attr("class", "active")
69 .attr("for", "long_input")
70 .attr("id", "long_link")
71 .text(I18n.t("javascripts.share.long_link")))
73 .attr("for", "short_input")
74 .attr("id", "short_link")
75 .text(I18n.t("javascripts.share.short_link")))
77 .attr("for", "embed_html")
79 .text(I18n.t("javascripts.share.embed")))
80 .on("click", "a", function (e) {
82 var id = "#" + $(this).attr("for");
83 $linkSection.find(".share-tabs a")
84 .removeClass("active");
85 $(this).addClass("active");
86 $linkSection.find(".share-tab")
88 $linkSection.find(".share-tab:has(" + id + ")")
90 .find("input, textarea")
95 .attr("class", "share-tab")
96 .css("display", "block")
99 .attr("id", "long_input")
100 .attr("type", "text")
101 .on("click", select));
104 .attr("class", "share-tab")
107 .attr("id", "short_input")
108 .attr("type", "text")
109 .on("click", select));
112 .attr("class", "share-tab")
116 .attr("id", "embed_html")
117 .on("click", select))
120 .attr("class", "text-muted")
121 .text(I18n.t("javascripts.share.paste_html"))
122 .appendTo($linkSection));
126 var $geoUriSection = $("<div>")
127 .attr("class", "section share-geo-uri")
131 .text(I18n.t("javascripts.share.geo_uri"))
132 .appendTo($geoUriSection);
135 .appendTo($geoUriSection)
137 .attr("id", "geo_uri"));
141 var $imageSection = $("<div>")
142 .attr("class", "section share-image")
146 .text(I18n.t("javascripts.share.image"))
147 .appendTo($imageSection);
150 .attr("id", "export-warning")
151 .attr("class", "text-muted")
152 .text(I18n.t("javascripts.share.only_standard_layer"))
153 .appendTo($imageSection);
156 .attr("id", "export-image")
157 .attr("action", "/export/finish")
158 .attr("method", "post")
159 .appendTo($imageSection);
162 .attr("class", "form-group form-check")
166 .attr("for", "image_filter")
167 .attr("class", "form-check-label")
170 .attr("id", "image_filter")
171 .attr("type", "checkbox")
172 .attr("class", "form-check-input")
173 .bind("change", toggleFilter))
174 .append(I18n.t("javascripts.share.custom_dimensions")));
180 .attr("for", "mapnik_format")
181 .text(I18n.t("javascripts.share.format")))
182 .append($("<select>")
183 .attr("name", "mapnik_format")
184 .attr("id", "mapnik_format")
185 .append($("<option>").val("png").text("PNG").prop("selected", true))
186 .append($("<option>").val("jpeg").text("JPEG"))
187 .append($("<option>").val("svg").text("SVG"))
188 .append($("<option>").val("pdf").text("PDF")));
193 .attr("for", "mapnik_scale")
194 .text(I18n.t("javascripts.share.scale")))
197 .attr("name", "mapnik_scale")
198 .attr("id", "mapnik_scale")
199 .attr("type", "text")
200 .on("change", update));
202 ["minlon", "minlat", "maxlon", "maxlat"].forEach(function (name) {
204 .attr("id", "mapnik_" + name)
206 .attr("type", "hidden")
211 .attr("name", "format")
212 .attr("value", "mapnik")
213 .attr("type", "hidden")
216 var csrf_param = $("meta[name=csrf-param]").attr("content"),
217 csrf_token = $("meta[name=csrf-token]").attr("content");
220 .attr("name", csrf_param)
221 .attr("value", csrf_token)
222 .attr("type", "hidden")
226 width: "<span id=\"mapnik_image_width\"></span>",
227 height: "<span id=\"mapnik_image_height\"></span>"
231 .attr("class", "text-muted")
232 .html(I18n.t("javascripts.share.image_dimensions", args))
236 .attr("type", "submit")
237 .attr("class", "btn btn-primary")
238 .attr("value", I18n.t("javascripts.share.download"))
242 .on("change", update)
245 marker.on("dragend", movedMarker);
246 map.on("move", movedMap);
247 map.on("moveend layeradd layerremove", update);
249 options.sidebar.addPane($ui);
255 map.removeLayer(marker);
256 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
257 locationFilter.disable();
265 $("#mapnik_scale").val(getScale());
266 marker.setLatLng(map.getCenter());
269 options.sidebar.togglePane($ui, button);
270 $(".leaflet-control .control-button").tooltip("hide");
273 function toggleMarker() {
274 if ($(this).is(":checked")) {
275 marker.setLatLng(map.getCenter());
276 map.addLayer(marker);
277 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
279 map.removeLayer(marker);
280 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
285 function toggleFilter() {
286 if ($(this).is(":checked")) {
287 locationFilter.setBounds(map.getBounds().pad(-0.2));
288 locationFilter.enable();
290 locationFilter.disable();
295 function movedMap() {
296 marker.setLatLng(map.getCenter());
300 function movedMarker() {
301 if (map.hasLayer(marker)) {
302 map.off("move", movedMap);
303 map.on("moveend", updateOnce);
304 map.panTo(marker.getLatLng());
308 function updateOnce() {
309 map.off("moveend", updateOnce);
310 map.on("move", movedMap);
314 function escapeHTML(string) {
322 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
323 return htmlEscapes[match];
328 var bounds = map.getBounds();
331 .prop("checked", map.hasLayer(marker));
334 .prop("checked", locationFilter.isEnabled());
338 $("#short_input").val(map.getShortUrl(marker));
339 $("#long_input").val(map.getUrl(marker));
340 $("#short_link").attr("href", map.getShortUrl(marker));
341 $("#long_link").attr("href", map.getUrl(marker));
344 bbox: bounds.toBBoxString(),
345 layer: map.getMapBaseLayerId()
348 if (map.hasLayer(marker)) {
349 var latLng = marker.getLatLng().wrap();
350 params.marker = latLng.lat + "," + latLng.lng;
353 $("#embed_html").val(
354 "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"" +
355 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
356 "\" style=\"border: 1px solid black\"></iframe><br/>" +
357 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
358 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
363 .attr("href", map.getGeoUri(marker))
364 .html(map.getGeoUri(marker));
368 if (locationFilter.isEnabled()) {
369 bounds = locationFilter.getBounds();
372 var scale = $("#mapnik_scale").val(),
373 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
374 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
375 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
377 $("#mapnik_minlon").val(bounds.getWest());
378 $("#mapnik_minlat").val(bounds.getSouth());
379 $("#mapnik_maxlon").val(bounds.getEast());
380 $("#mapnik_maxlat").val(bounds.getNorth());
382 if (scale < maxScale) {
383 scale = roundScale(maxScale);
384 $("#mapnik_scale").val(scale);
387 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
388 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
390 if (map.getMapBaseLayerId() === "mapnik") {
391 $("#export-image").show();
392 $("#export-warning").hide();
394 $("#export-image").hide();
395 $("#export-warning").show();
403 function getScale() {
404 var bounds = map.getBounds(),
405 centerLat = bounds.getCenter().lat,
406 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
407 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
408 pixelsPerMeter = map.getSize().x / meters,
409 metersPerPixel = 1 / (92 * 39.3701);
410 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
413 function roundScale(scale) {
414 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
415 return precision * Math.ceil(scale / precision);
418 return $container[0];