X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/33bca7cd655577da0b2b2aac398db221a31700ad..b4a9d7537785f1b44ea4db42bed69127fbe323ae:/app/assets/javascripts/leaflet.share.js diff --git a/app/assets/javascripts/leaflet.share.js b/app/assets/javascripts/leaflet.share.js index 7713a6b14..325b168b2 100644 --- a/app/assets/javascripts/leaflet.share.js +++ b/app/assets/javascripts/leaflet.share.js @@ -1,16 +1,16 @@ 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 = $("
") + const $linkSection = $("
") .attr("class", "share-link p-3 border-bottom border-secondary-subtle") .appendTo($ui); @@ -18,7 +18,7 @@ L.OSM.share = function (options) { .text(I18n.t("javascripts.share.link")) .appendTo($linkSection); - var $form = $("
") + let $form = $("") .appendTo($linkSection); $("
") @@ -54,7 +54,7 @@ L.OSM.share = function (options) { .on("click", "a", function (e) { e.preventDefault(); if (!$(this).hasClass("btn-primary")) return; - var id = "#" + $(this).attr("for"); + const id = "#" + $(this).attr("for"); $(this).siblings("a") .removeClass("active"); $(this).addClass("active"); @@ -104,7 +104,7 @@ L.OSM.share = function (options) { // Geo URI - var $geoUriSection = $("
") + const $geoUriSection = $("
") .attr("class", "share-geo-uri p-3 border-bottom border-secondary-subtle") .appendTo($ui); @@ -119,7 +119,7 @@ L.OSM.share = function (options) { // Image - var $imageSection = $("
") + const $imageSection = $("
") .attr("class", "share-image p-3") .appendTo($ui); @@ -201,44 +201,34 @@ L.OSM.share = function (options) { .attr("class", "form-check-input") .bind("change", toggleFilter)))); - ["minlon", "minlat", "maxlon", "maxlat", "lat", "lon"].forEach(function (name) { + const mapnikNames = ["minlon", "minlat", "maxlon", "maxlat", "lat", "lon"]; + + for (const name of mapnikNames) { $("") .attr("id", "mapnik_" + name) .attr("name", name) .attr("type", "hidden") .appendTo($form); - }); - - $("") - .attr("id", "map_format") - .attr("name", "format") - .attr("value", "mapnik") - .attr("type", "hidden") - .appendTo($form); - - $("") - .attr("id", "map_zoom") - .attr("name", "zoom") - .attr("value", map.getZoom()) - .attr("type", "hidden") - .appendTo($form); + } - $("") - .attr("id", "map_width") - .attr("name", "width") - .attr("value", 0) - .attr("type", "hidden") - .appendTo($form); + const hiddenExportDefaults = { + format: "mapnik", + zoom: map.getZoom(), + width: 0, + height: 0 + }; - $("") - .attr("id", "map_height") - .attr("name", "height") - .attr("value", 0) - .attr("type", "hidden") - .appendTo($form); + for (const name in hiddenExportDefaults) { + $("") + .attr("id", "map_" + name) + .attr("name", name) + .attr("value", hiddenExportDefaults[name]) + .attr("type", "hidden") + .appendTo($form); + } - var csrf_param = $("meta[name=csrf-param]").attr("content"), - csrf_token = $("meta[name=csrf-token]").attr("content"); + const csrf_param = $("meta[name=csrf-param]").attr("content"), + csrf_token = $("meta[name=csrf-token]").attr("content"); $("") .attr("name", csrf_param) @@ -246,7 +236,7 @@ L.OSM.share = function (options) { .attr("type", "hidden") .appendTo($form); - var args = { + const args = { layer: "", width: "", height: "" @@ -269,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) @@ -329,7 +319,7 @@ L.OSM.share = function (options) { } function escapeHTML(string) { - var htmlEscapes = { + const htmlEscapes = { "&": "&", "<": "<", ">": ">", @@ -343,8 +333,8 @@ L.OSM.share = function (options) { function update() { const layer = map.getMapBaseLayer(); - var canEmbed = Boolean(layer && layer.options.canEmbed); - var bounds = map.getBounds(); + const canEmbed = Boolean(layer && layer.options.canEmbed); + let bounds = map.getBounds(); $("#link_marker") .prop("checked", map.hasLayer(marker)); @@ -359,14 +349,14 @@ 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") @@ -379,7 +369,7 @@ L.OSM.share = function (options) { $("#embed_html").val( "
" + "" + escapeHTML(I18n.t("javascripts.share.view_larger_map")) + ""); @@ -396,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()); @@ -437,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); } };