X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/067b0de4391a32124aef23c56d73e2bb492df8ca..c50e4f50c4fef0b11970202d3c7f95cdc5b620ba:/app/assets/javascripts/leaflet.share.js diff --git a/app/assets/javascripts/leaflet.share.js b/app/assets/javascripts/leaflet.share.js index b9c48f15a..325b168b2 100644 --- a/app/assets/javascripts/leaflet.share.js +++ b/app/assets/javascripts/leaflet.share.js @@ -1,39 +1,38 @@ L.OSM.share = function (options) { - var control = L.OSM.sidebarPane(options, "share", "javascripts.share.title", "javascripts.share.title"), - marker = L.marker([0, 0], { draggable: true }), - locationFilter = new L.LocationFilter({ - enableButton: false, - adjustButton: false - }); + const control = L.OSM.sidebarPane(options, "share", "javascripts.share.title", "javascripts.share.title"), + marker = L.marker([0, 0], { draggable: true }), + locationFilter = new L.LocationFilter({ + enableButton: false, + adjustButton: false + }); control.onAddPane = function (map, button, $ui) { // Link / Embed + $("#content").addClass("overlay-right-sidebar"); - var $linkSection = $("
") - .attr("class", "text-muted") + .attr("class", "text-body-secondary") .text(I18n.t("javascripts.share.paste_html"))); // Geo URI - var $geoUriSection = $("
") - .attr("class", "text-muted") + .attr("class", "text-body-secondary") .html(I18n.t("javascripts.share.image_dimensions", args)) .appendTo($form); @@ -216,7 +259,7 @@ L.OSM.share = function (options) { marker.on("dragend", movedMarker); map.on("move", movedMap); - map.on("moveend layeradd layerremove", update); + map.on("moveend baselayerchange overlayadd overlayremove", update); $ui .on("show", shown) @@ -276,7 +319,7 @@ L.OSM.share = function (options) { } function escapeHTML(string) { - var htmlEscapes = { + const htmlEscapes = { "&": "&", "<": "<", ">": ">", @@ -289,7 +332,9 @@ L.OSM.share = function (options) { } function update() { - var bounds = map.getBounds(); + const layer = map.getMapBaseLayer(); + const canEmbed = Boolean(layer && layer.options.canEmbed); + let bounds = map.getBounds(); $("#link_marker") .prop("checked", map.hasLayer(marker)); @@ -304,19 +349,27 @@ L.OSM.share = function (options) { $("#short_link").attr("href", map.getShortUrl(marker)); $("#long_link").attr("href", map.getUrl(marker)); - var params = { + const params = new URLSearchParams({ bbox: bounds.toBBoxString(), layer: map.getMapBaseLayerId() - }; + }); if (map.hasLayer(marker)) { - var latLng = marker.getLatLng().wrap(); - params.marker = latLng.lat + "," + latLng.lng; + const latLng = marker.getLatLng().wrap(); + params.set("marker", latLng.lat + "," + latLng.lng); + } + + $("#embed_link") + .toggleClass("btn-primary", canEmbed) + .toggleClass("btn-secondary", !canEmbed) + .tooltip(canEmbed ? "disable" : "enable"); + if (!canEmbed && $("#embed_link").hasClass("active")) { + $("#long_link").click(); } $("#embed_html").val( - "" + "" + escapeHTML(I18n.t("javascripts.share.view_larger_map")) + ""); @@ -333,10 +386,10 @@ L.OSM.share = function (options) { bounds = locationFilter.getBounds(); } - var scale = $("#mapnik_scale").val(), - size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()), - L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(), - maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136)); + let scale = $("#mapnik_scale").val(); + const size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()), + L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(), + maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136)); $("#mapnik_minlon").val(bounds.getWest()); $("#mapnik_minlat").val(bounds.getSouth()); @@ -348,16 +401,25 @@ L.OSM.share = function (options) { $("#mapnik_scale").val(scale); } - $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028)); - $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028)); + const mapWidth = Math.round(size.x / scale / 0.00028); + const mapHeight = Math.round(size.y / scale / 0.00028); + $("#mapnik_image_width").text(mapWidth); + $("#mapnik_image_height").text(mapHeight); - if (map.getMapBaseLayerId() === "mapnik") { - $("#export-image").show(); - $("#export-warning").hide(); - } else { - $("#export-image").hide(); - $("#export-warning").show(); - } + const canDownloadImage = Boolean(layer && layer.options.canDownloadImage); + + $("#mapnik_image_layer").text(canDownloadImage ? layer.options.name : ""); + $("#map_format").val(canDownloadImage ? layer.options.layerId : ""); + + $("#map_zoom").val(map.getZoom()); + $("#mapnik_lon").val(map.getCenter().lng); + $("#mapnik_lat").val(map.getCenter().lat); + $("#map_width").val(mapWidth); + $("#map_height").val(mapHeight); + + $("#export-image").toggle(canDownloadImage); + $("#export-warning").toggle(!canDownloadImage); + $("#mapnik_scale_row").toggle(canDownloadImage && layer.options.layerId === "mapnik"); } function select() { @@ -365,17 +427,17 @@ L.OSM.share = function (options) { } function getScale() { - var bounds = map.getBounds(), - centerLat = bounds.getCenter().lat, - halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180), - meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180, - pixelsPerMeter = map.getSize().x / meters, - metersPerPixel = 1 / (92 * 39.3701); + const bounds = map.getBounds(), + centerLat = bounds.getCenter().lat, + halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180), + meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180, + pixelsPerMeter = map.getSize().x / meters, + metersPerPixel = 1 / (92 * 39.3701); return Math.round(1 / (pixelsPerMeter * metersPerPixel)); } function roundScale(scale) { - var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2); + const precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2); return precision * Math.ceil(scale / precision); } };