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")
27 .attr("for", "link_marker")
28 .attr("class", "form-check-label")
29 .text(I18n.t("javascripts.share.include_marker")))
31 .attr("id", "link_marker")
32 .attr("type", "checkbox")
33 .attr("class", "form-check-input")
34 .bind("change", toggleMarker));
36 $("<div class='btn-group btn-group-sm mb-2'>")
38 .append($("<a class='btn btn-primary'>")
40 .attr("for", "long_input")
41 .attr("id", "long_link")
42 .text(I18n.t("javascripts.share.long_link")))
43 .append($("<a class='btn btn-primary'>")
44 .attr("for", "short_input")
45 .attr("id", "short_link")
46 .text(I18n.t("javascripts.share.short_link")))
47 .append($("<a class='btn btn-primary'>")
48 .attr("for", "embed_html")
50 .text(I18n.t("javascripts.share.embed")))
51 .on("click", "a", function (e) {
53 var id = "#" + $(this).attr("for");
55 .removeClass("active");
56 $(this).addClass("active");
57 $linkSection.find(".share-tab")
59 $linkSection.find(".share-tab:has(" + id + ")")
61 .find("input, textarea")
66 .attr("class", "share-tab")
69 .attr("id", "long_input")
71 .on("click", select));
74 .attr("class", "share-tab")
78 .attr("id", "short_input")
80 .on("click", select));
83 .attr("class", "share-tab")
88 .attr("id", "embed_html")
92 .attr("class", "text-muted")
93 .text(I18n.t("javascripts.share.paste_html")));
97 var $geoUriSection = $("<div>")
98 .attr("class", "section share-geo-uri")
102 .text(I18n.t("javascripts.share.geo_uri"))
103 .appendTo($geoUriSection);
106 .appendTo($geoUriSection)
108 .attr("id", "geo_uri"));
112 var $imageSection = $("<div>")
113 .attr("class", "section share-image")
117 .text(I18n.t("javascripts.share.image"))
118 .appendTo($imageSection);
121 .attr("id", "export-warning")
122 .attr("class", "text-muted")
123 .text(I18n.t("javascripts.share.only_standard_layer"))
124 .appendTo($imageSection);
127 .attr("id", "export-image")
128 .attr("action", "/export/finish")
129 .attr("method", "post")
130 .appendTo($imageSection);
134 .attr("class", "row mb-3")
136 .attr("for", "mapnik_format")
137 .attr("class", "col-auto col-form-label")
138 .text(I18n.t("javascripts.share.format")))
140 .attr("class", "col-auto")
141 .append($("<select>")
142 .attr("name", "mapnik_format")
143 .attr("id", "mapnik_format")
144 .attr("class", "form-select w-auto")
145 .append($("<option>").val("png").text("PNG").prop("selected", true))
146 .append($("<option>").val("jpeg").text("JPEG"))
147 .append($("<option>").val("svg").text("SVG"))
148 .append($("<option>").val("pdf").text("PDF"))));
152 .attr("class", "row mb-3")
154 .attr("for", "mapnik_scale")
155 .attr("class", "col-auto col-form-label")
156 .text(I18n.t("javascripts.share.scale")))
158 .attr("class", "col-auto")
160 .attr("class", "input-group flex-nowrap")
162 .attr("class", "input-group-text")
165 .attr("name", "mapnik_scale")
166 .attr("id", "mapnik_scale")
167 .attr("type", "text")
168 .attr("class", "form-control")
169 .on("change", update))));
172 .attr("class", "row mb-3")
175 .attr("class", "col-auto")
177 .attr("class", "form-check")
179 .attr("for", "image_filter")
180 .attr("class", "form-check-label")
181 .text(I18n.t("javascripts.share.custom_dimensions")))
183 .attr("id", "image_filter")
184 .attr("type", "checkbox")
185 .attr("class", "form-check-input")
186 .bind("change", toggleFilter))));
188 ["minlon", "minlat", "maxlon", "maxlat"].forEach(function (name) {
190 .attr("id", "mapnik_" + name)
192 .attr("type", "hidden")
197 .attr("name", "format")
198 .attr("value", "mapnik")
199 .attr("type", "hidden")
202 var csrf_param = $("meta[name=csrf-param]").attr("content"),
203 csrf_token = $("meta[name=csrf-token]").attr("content");
206 .attr("name", csrf_param)
207 .attr("value", csrf_token)
208 .attr("type", "hidden")
212 width: "<span id=\"mapnik_image_width\"></span>",
213 height: "<span id=\"mapnik_image_height\"></span>"
217 .attr("class", "text-muted")
218 .html(I18n.t("javascripts.share.image_dimensions", args))
222 .attr("type", "submit")
223 .attr("class", "btn btn-primary")
224 .attr("value", I18n.t("javascripts.share.download"))
228 .on("change", update)
231 marker.on("dragend", movedMarker);
232 map.on("move", movedMap);
233 map.on("moveend layeradd layerremove", update);
240 $("#mapnik_scale").val(getScale());
245 map.removeLayer(marker);
246 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
247 locationFilter.disable();
251 function toggleMarker() {
252 if ($(this).is(":checked")) {
253 marker.setLatLng(map.getCenter());
254 map.addLayer(marker);
255 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
257 map.removeLayer(marker);
258 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
263 function toggleFilter() {
264 if ($(this).is(":checked")) {
265 locationFilter.setBounds(map.getBounds().pad(-0.2));
266 locationFilter.enable();
268 locationFilter.disable();
273 function movedMap() {
274 marker.setLatLng(map.getCenter());
278 function movedMarker() {
279 if (map.hasLayer(marker)) {
280 map.off("move", movedMap);
281 map.on("moveend", updateOnce);
282 map.panTo(marker.getLatLng());
286 function updateOnce() {
287 map.off("moveend", updateOnce);
288 map.on("move", movedMap);
292 function escapeHTML(string) {
300 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
301 return htmlEscapes[match];
306 var bounds = map.getBounds();
309 .prop("checked", map.hasLayer(marker));
312 .prop("checked", locationFilter.isEnabled());
316 $("#short_input").val(map.getShortUrl(marker));
317 $("#long_input").val(map.getUrl(marker));
318 $("#short_link").attr("href", map.getShortUrl(marker));
319 $("#long_link").attr("href", map.getUrl(marker));
322 bbox: bounds.toBBoxString(),
323 layer: map.getMapBaseLayerId()
326 if (map.hasLayer(marker)) {
327 var latLng = marker.getLatLng().wrap();
328 params.marker = latLng.lat + "," + latLng.lng;
331 $("#embed_html").val(
332 "<iframe width=\"425\" height=\"350\" src=\"" +
333 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
334 "\" style=\"border: 1px solid black\"></iframe><br/>" +
335 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
336 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
341 .attr("href", map.getGeoUri(marker))
342 .html(map.getGeoUri(marker));
346 if (locationFilter.isEnabled()) {
347 bounds = locationFilter.getBounds();
350 var scale = $("#mapnik_scale").val(),
351 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
352 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
353 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
355 $("#mapnik_minlon").val(bounds.getWest());
356 $("#mapnik_minlat").val(bounds.getSouth());
357 $("#mapnik_maxlon").val(bounds.getEast());
358 $("#mapnik_maxlat").val(bounds.getNorth());
360 if (scale < maxScale) {
361 scale = roundScale(maxScale);
362 $("#mapnik_scale").val(scale);
365 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
366 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
368 if (map.getMapBaseLayerId() === "mapnik") {
369 $("#export-image").show();
370 $("#export-warning").hide();
372 $("#export-image").hide();
373 $("#export-warning").show();
381 function getScale() {
382 var bounds = map.getBounds(),
383 centerLat = bounds.getCenter().lat,
384 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
385 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
386 pixelsPerMeter = map.getSize().x / meters,
387 metersPerPixel = 1 / (92 * 39.3701);
388 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
391 function roundScale(scale) {
392 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
393 return precision * Math.ceil(scale / precision);