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 = $("<div>")
+ const $linkSection = $("<div>")
.attr("class", "share-link p-3 border-bottom border-secondary-subtle")
.appendTo($ui);
.text(I18n.t("javascripts.share.link"))
.appendTo($linkSection);
- var $form = $("<form>")
+ let $form = $("<form>")
.appendTo($linkSection);
$("<div>")
.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");
// Geo URI
- var $geoUriSection = $("<div>")
+ const $geoUriSection = $("<div>")
.attr("class", "share-geo-uri p-3 border-bottom border-secondary-subtle")
.appendTo($ui);
// Image
- var $imageSection = $("<div>")
+ const $imageSection = $("<div>")
.attr("class", "share-image p-3")
.appendTo($ui);
.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) {
$("<input>")
.attr("id", "mapnik_" + name)
.attr("name", name)
.attr("type", "hidden")
.appendTo($form);
- });
-
- $("<input>")
- .attr("id", "map_format")
- .attr("name", "format")
- .attr("value", "mapnik")
- .attr("type", "hidden")
- .appendTo($form);
-
- $("<input>")
- .attr("id", "map_zoom")
- .attr("name", "zoom")
- .attr("value", map.getZoom())
- .attr("type", "hidden")
- .appendTo($form);
+ }
- $("<input>")
- .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
+ };
- $("<input>")
- .attr("id", "map_height")
- .attr("name", "height")
- .attr("value", 0)
- .attr("type", "hidden")
- .appendTo($form);
+ for (const name in hiddenExportDefaults) {
+ $("<input>")
+ .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");
$("<input>")
.attr("name", csrf_param)
.attr("type", "hidden")
.appendTo($form);
- var args = {
+ const args = {
layer: "<span id=\"mapnik_image_layer\"></span>",
width: "<span id=\"mapnik_image_width\"></span>",
height: "<span id=\"mapnik_image_height\"></span>"
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)
}
function escapeHTML(string) {
- var htmlEscapes = {
+ const htmlEscapes = {
"&": "&",
"<": "<",
">": ">",
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));
$("#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")
$("#embed_html").val(
"<iframe width=\"425\" height=\"350\" src=\"" +
- escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
+ escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + params) +
"\" style=\"border: 1px solid black\"></iframe><br/>" +
"<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
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());
}
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);
}
};