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 .attr("class", "form-control form-control-sm font-monospace")
72 .attr("readonly", true)
73 .on("click", select));
76 .attr("class", "share-tab")
80 .attr("id", "short_input")
82 .attr("class", "form-control form-control-sm font-monospace")
83 .attr("readonly", true)
84 .on("click", select));
87 .attr("class", "share-tab")
92 .attr("id", "embed_html")
93 .attr("class", "form-control form-control-sm font-monospace")
94 .attr("readonly", true)
98 .attr("class", "text-muted")
99 .text(I18n.t("javascripts.share.paste_html")));
103 var $geoUriSection = $("<div>")
104 .attr("class", "section share-geo-uri")
108 .text(I18n.t("javascripts.share.geo_uri"))
109 .appendTo($geoUriSection);
112 .appendTo($geoUriSection)
114 .attr("id", "geo_uri"));
118 var $imageSection = $("<div>")
119 .attr("class", "section share-image")
123 .text(I18n.t("javascripts.share.image"))
124 .appendTo($imageSection);
127 .attr("id", "export-warning")
128 .attr("class", "text-muted")
129 .text(I18n.t("javascripts.share.only_standard_layer"))
130 .appendTo($imageSection);
133 .attr("id", "export-image")
134 .attr("action", "/export/finish")
135 .attr("method", "post")
136 .appendTo($imageSection);
140 .attr("class", "row mb-3")
142 .attr("for", "mapnik_format")
143 .attr("class", "col-auto col-form-label")
144 .text(I18n.t("javascripts.share.format")))
146 .attr("class", "col-auto")
147 .append($("<select>")
148 .attr("name", "mapnik_format")
149 .attr("id", "mapnik_format")
150 .attr("class", "form-select w-auto")
151 .append($("<option>").val("png").text("PNG").prop("selected", true))
152 .append($("<option>").val("jpeg").text("JPEG"))
153 .append($("<option>").val("svg").text("SVG"))
154 .append($("<option>").val("pdf").text("PDF"))));
158 .attr("class", "row mb-3")
160 .attr("for", "mapnik_scale")
161 .attr("class", "col-auto col-form-label")
162 .text(I18n.t("javascripts.share.scale")))
164 .attr("class", "col-auto")
166 .attr("class", "input-group flex-nowrap")
168 .attr("class", "input-group-text")
171 .attr("name", "mapnik_scale")
172 .attr("id", "mapnik_scale")
173 .attr("type", "text")
174 .attr("class", "form-control")
175 .on("change", update))));
178 .attr("class", "row mb-3")
181 .attr("class", "col-auto")
183 .attr("class", "form-check")
185 .attr("for", "image_filter")
186 .attr("class", "form-check-label")
187 .text(I18n.t("javascripts.share.custom_dimensions")))
189 .attr("id", "image_filter")
190 .attr("type", "checkbox")
191 .attr("class", "form-check-input")
192 .bind("change", toggleFilter))));
194 ["minlon", "minlat", "maxlon", "maxlat"].forEach(function (name) {
196 .attr("id", "mapnik_" + name)
198 .attr("type", "hidden")
203 .attr("name", "format")
204 .attr("value", "mapnik")
205 .attr("type", "hidden")
208 var csrf_param = $("meta[name=csrf-param]").attr("content"),
209 csrf_token = $("meta[name=csrf-token]").attr("content");
212 .attr("name", csrf_param)
213 .attr("value", csrf_token)
214 .attr("type", "hidden")
218 width: "<span id=\"mapnik_image_width\"></span>",
219 height: "<span id=\"mapnik_image_height\"></span>"
223 .attr("class", "text-muted")
224 .html(I18n.t("javascripts.share.image_dimensions", args))
228 .attr("type", "submit")
229 .attr("class", "btn btn-primary")
230 .attr("value", I18n.t("javascripts.share.download"))
234 .on("change", update)
237 marker.on("dragend", movedMarker);
238 map.on("move", movedMap);
239 map.on("moveend layeradd layerremove", update);
246 $("#mapnik_scale").val(getScale());
251 map.removeLayer(marker);
252 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
253 locationFilter.disable();
257 function toggleMarker() {
258 if ($(this).is(":checked")) {
259 marker.setLatLng(map.getCenter());
260 map.addLayer(marker);
261 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
263 map.removeLayer(marker);
264 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
269 function toggleFilter() {
270 if ($(this).is(":checked")) {
271 locationFilter.setBounds(map.getBounds().pad(-0.2));
272 locationFilter.enable();
274 locationFilter.disable();
279 function movedMap() {
280 marker.setLatLng(map.getCenter());
284 function movedMarker() {
285 if (map.hasLayer(marker)) {
286 map.off("move", movedMap);
287 map.on("moveend", updateOnce);
288 map.panTo(marker.getLatLng());
292 function updateOnce() {
293 map.off("moveend", updateOnce);
294 map.on("move", movedMap);
298 function escapeHTML(string) {
306 return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
307 return htmlEscapes[match];
312 var bounds = map.getBounds();
315 .prop("checked", map.hasLayer(marker));
318 .prop("checked", locationFilter.isEnabled());
322 $("#short_input").val(map.getShortUrl(marker));
323 $("#long_input").val(map.getUrl(marker));
324 $("#short_link").attr("href", map.getShortUrl(marker));
325 $("#long_link").attr("href", map.getUrl(marker));
328 bbox: bounds.toBBoxString(),
329 layer: map.getMapBaseLayerId()
332 if (map.hasLayer(marker)) {
333 var latLng = marker.getLatLng().wrap();
334 params.marker = latLng.lat + "," + latLng.lng;
337 $("#embed_html").val(
338 "<iframe width=\"425\" height=\"350\" src=\"" +
339 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
340 "\" style=\"border: 1px solid black\"></iframe><br/>" +
341 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
342 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
347 .attr("href", map.getGeoUri(marker))
348 .html(map.getGeoUri(marker));
352 if (locationFilter.isEnabled()) {
353 bounds = locationFilter.getBounds();
356 var scale = $("#mapnik_scale").val(),
357 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
358 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
359 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
361 $("#mapnik_minlon").val(bounds.getWest());
362 $("#mapnik_minlat").val(bounds.getSouth());
363 $("#mapnik_maxlon").val(bounds.getEast());
364 $("#mapnik_maxlat").val(bounds.getNorth());
366 if (scale < maxScale) {
367 scale = roundScale(maxScale);
368 $("#mapnik_scale").val(scale);
371 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
372 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
374 if (map.getMapBaseLayerId() === "mapnik") {
375 $("#export-image").show();
376 $("#export-warning").hide();
378 $("#export-image").hide();
379 $("#export-warning").show();
387 function getScale() {
388 var bounds = map.getBounds(),
389 centerLat = bounds.getCenter().lat,
390 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
391 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
392 pixelsPerMeter = map.getSize().x / meters,
393 metersPerPixel = 1 / (92 * 39.3701);
394 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
397 function roundScale(scale) {
398 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
399 return precision * Math.ceil(scale / precision);