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 .attr("class", "standard-form")
48 .appendTo($linkSection);
51 .attr("class", "form-row")
55 .attr("for", "link_marker")
58 .attr("id", "link_marker")
59 .attr("type", "checkbox")
60 .bind("change", toggleMarker))
61 .append(I18n.t("javascripts.share.include_marker")));
64 .attr("class", "share-tabs")
67 .attr("class", "active")
68 .attr("for", "long_input")
69 .attr("id", "long_link")
70 .text(I18n.t("javascripts.share.long_link")))
72 .attr("for", "short_input")
73 .attr("id", "short_link")
74 .text(I18n.t("javascripts.share.short_link")))
76 .attr("for", "embed_html")
78 .text(I18n.t("javascripts.share.embed")))
79 .on("click", "a", function(e) {
81 var id = "#" + $(this).attr("for");
82 $linkSection.find(".share-tabs a")
83 .removeClass("active");
84 $(this).addClass("active");
85 $linkSection.find(".share-tab")
87 $linkSection.find(".share-tab:has(" + id + ")")
89 .find("input, textarea")
94 .attr("class", "form-row share-tab")
95 .css("display", "block")
98 .attr("id", "long_input")
100 .on("click", select));
103 .attr("class", "form-row share-tab")
106 .attr("id", "short_input")
107 .attr("type", "text")
108 .on("click", select));
111 .attr("class", "form-row share-tab")
115 .attr("id", "embed_html")
116 .on("click", select))
119 .attr("class", "deemphasize")
120 .text(I18n.t("javascripts.share.paste_html"))
121 .appendTo($linkSection));
125 var $geoUriSection = $("<div>")
126 .attr("class", "section share-geo-uri")
130 .text(I18n.t("javascripts.share.geo_uri"))
131 .appendTo($geoUriSection);
134 .appendTo($geoUriSection)
136 .attr("id", "geo_uri"));
140 var $imageSection = $("<div>")
141 .attr("class", "section share-image")
145 .text(I18n.t("javascripts.share.image"))
146 .appendTo($imageSection);
149 .attr("id", "export-warning")
150 .attr("class", "deemphasize")
151 .text(I18n.t("javascripts.share.only_standard_layer"))
152 .appendTo($imageSection);
155 .attr("id", "export-image")
156 .attr("class", "standard-form")
157 .attr("action", "/export/finish")
158 .attr("method", "post")
159 .appendTo($imageSection);
162 .attr("class", "form-row")
166 .attr("for", "image_filter")
169 .attr("id", "image_filter")
170 .attr("type", "checkbox")
171 .bind("change", toggleFilter))
172 .append(I18n.t("javascripts.share.custom_dimensions")));
175 .attr("class", "form-row")
179 .attr("for", "mapnik_format")
180 .text(I18n.t("javascripts.share.format")))
181 .append($("<select>")
182 .attr("name", "mapnik_format")
183 .attr("id", "mapnik_format")
184 .append($("<option>").val("png").text("PNG").prop("selected", true))
185 .append($("<option>").val("jpeg").text("JPEG"))
186 .append($("<option>").val("svg").text("SVG"))
187 .append($("<option>").val("pdf").text("PDF")));
190 .attr("class", "form-row")
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 .attr("class", "deemphasize")
227 .html(I18n.t("javascripts.share.image_size") + " <span id=\"mapnik_image_width\"></span> x <span id=\"mapnik_image_height\"></span>")
231 .attr("type", "submit")
232 .attr("value", I18n.t("javascripts.share.download"))
236 .on("change", update)
239 marker.on("dragend", movedMarker);
240 map.on("move", movedMap);
241 map.on("moveend layeradd layerremove", update);
243 options.sidebar.addPane($ui);
249 map.removeLayer(marker);
250 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
251 locationFilter.disable();
259 $("#mapnik_scale").val(getScale());
260 marker.setLatLng(map.getCenter());
263 options.sidebar.togglePane($ui, button);
264 $(".leaflet-control .control-button").tooltip("hide");
267 function toggleMarker() {
268 if ($(this).is(":checked")) {
269 marker.setLatLng(map.getCenter());
270 map.addLayer(marker);
271 map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
273 map.removeLayer(marker);
274 map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
279 function toggleFilter() {
280 if ($(this).is(":checked")) {
281 locationFilter.setBounds(map.getBounds().pad(-0.2));
282 locationFilter.enable();
284 locationFilter.disable();
289 function movedMap() {
290 marker.setLatLng(map.getCenter());
294 function movedMarker() {
295 if (map.hasLayer(marker)) {
296 map.off("move", movedMap);
297 map.on("moveend", updateOnce);
298 map.panTo(marker.getLatLng());
302 function updateOnce() {
303 map.off("moveend", updateOnce);
304 map.on("move", movedMap);
308 function escapeHTML(string) {
316 return string === null ? "" : (string + "").replace(/[&<>"']/g, function(match) {
317 return htmlEscapes[match];
322 var bounds = map.getBounds();
325 .prop("checked", map.hasLayer(marker));
328 .prop("checked", locationFilter.isEnabled());
332 $("#short_input").val(map.getShortUrl(marker));
333 $("#long_input").val(map.getUrl(marker));
334 $("#short_link").attr("href", map.getShortUrl(marker));
335 $("#long_link").attr("href", map.getUrl(marker));
338 bbox: bounds.toBBoxString(),
339 layer: map.getMapBaseLayerId()
342 if (map.hasLayer(marker)) {
343 var latLng = marker.getLatLng().wrap();
344 params.marker = latLng.lat + "," + latLng.lng;
347 $("#embed_html").val(
348 "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"" +
349 escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
350 "\" style=\"border: 1px solid black\"></iframe><br/>" +
351 "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
352 escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
357 .attr("href", map.getGeoUri(marker))
358 .html(map.getGeoUri(marker));
362 if (locationFilter.isEnabled()) {
363 bounds = locationFilter.getBounds();
366 var scale = $("#mapnik_scale").val(),
367 size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
368 L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
369 maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
371 $("#mapnik_minlon").val(bounds.getWest());
372 $("#mapnik_minlat").val(bounds.getSouth());
373 $("#mapnik_maxlon").val(bounds.getEast());
374 $("#mapnik_maxlat").val(bounds.getNorth());
376 if (scale < maxScale) {
377 scale = roundScale(maxScale);
378 $("#mapnik_scale").val(scale);
381 $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
382 $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
384 if (map.getMapBaseLayerId() === "mapnik") {
385 $("#export-image").show();
386 $("#export-warning").hide();
388 $("#export-image").hide();
389 $("#export-warning").show();
397 function getScale() {
398 var bounds = map.getBounds(),
399 centerLat = bounds.getCenter().lat,
400 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
401 meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
402 pixelsPerMeter = map.getSize().x / meters,
403 metersPerPixel = 1 / (92 * 39.3701);
404 return Math.round(1 / (pixelsPerMeter * metersPerPixel));
407 function roundScale(scale) {
408 var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
409 return precision * Math.ceil(scale / precision);
412 return $container[0];