From a801f7f72d730a1ba2c7916d57cb0aabc4dab583 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Mon, 5 Jan 2026 10:06:07 +0100 Subject: [PATCH] create `OSM.MapLibre.defaultSecondaryMapOptions` to share the map style between the secondary maps --- app/assets/javascripts/dashboard.js | 16 +--------------- app/assets/javascripts/maplibre.map.js | 16 ++++++++++++++++ app/assets/javascripts/user.js | 24 +++++------------------- 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/app/assets/javascripts/dashboard.js b/app/assets/javascripts/dashboard.js index dd7cadfed..965a9f765 100644 --- a/app/assets/javascripts/dashboard.js +++ b/app/assets/javascripts/dashboard.js @@ -1,25 +1,11 @@ //= require maplibre.map -//= require maplibre.i18n //= require maplibre.combinedcontrolgroup $(function () { - const defaultHomeZoom = 11; let map; if ($("#map").length) { - map = new maplibregl.Map({ - container: "map", - style: OSM.MapLibre.Styles.Mapnik, - attributionControl: false, - locale: OSM.MapLibre.Locale, - rollEnabled: false, - dragRotate: false, - pitchWithRotate: false, - bearingSnap: 180, - maxPitch: 0, - center: OSM.home ? [OSM.home.lon, OSM.home.lat] : [0, 0], - zoom: OSM.home ? defaultHomeZoom : 0 - }); + map = new maplibregl.Map(OSM.MapLibre.defaultSecondaryMapOptions); const position = $("html").attr("dir") === "rtl" ? "top-left" : "top-right"; const navigationControl = new maplibregl.NavigationControl({ showCompass: false }); diff --git a/app/assets/javascripts/maplibre.map.js b/app/assets/javascripts/maplibre.map.js index 9814e5acd..de4a890ed 100644 --- a/app/assets/javascripts/maplibre.map.js +++ b/app/assets/javascripts/maplibre.map.js @@ -1,4 +1,5 @@ //= require maplibre-gl/dist/maplibre-gl +//= require maplibre.i18n OSM.MapLibre.Styles.Mapnik = { version: 8, @@ -21,6 +22,21 @@ OSM.MapLibre.Styles.Mapnik = { ] }; +OSM.MapLibre.defaultHomeZoom = 11; +OSM.MapLibre.defaultSecondaryMapOptions = { + container: "map", + style: OSM.MapLibre.Styles.Mapnik, + attributionControl: false, + locale: OSM.MapLibre.Locale, + rollEnabled: false, + dragRotate: false, + pitchWithRotate: false, + bearingSnap: 180, + maxPitch: 0, + center: OSM.home ? [OSM.home.lon, OSM.home.lat] : [0, 0], + zoom: OSM.home ? OSM.MapLibre.defaultHomeZoom : 0 +}; + // Helper function to create Leaflet style (SVG comes from Leaflet) markers for MapLibre // new maplibregl.Marker({ color: color }) is simpler, but does not have the exact same gradient OSM.MapLibre.getMarker = function ({ icon = "dot", color = "var(--marker-red)", ...options }) { diff --git a/app/assets/javascripts/user.js b/app/assets/javascripts/user.js index 7d98f46f2..ced7d9665 100644 --- a/app/assets/javascripts/user.js +++ b/app/assets/javascripts/user.js @@ -1,10 +1,8 @@ //= require maplibre.map -//= require maplibre.i18n //= require maplibre.combinedcontrolgroup //= require ./home_location_name-endpoint $(function () { - const defaultHomeZoom = 11; let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon; if ($("#social_links").length) { @@ -52,19 +50,7 @@ $(function () { } if ($("#map").length) { - map = new maplibregl.Map({ - container: "map", - style: OSM.MapLibre.Styles.Mapnik, - attributionControl: false, - locale: OSM.MapLibre.Locale, - rollEnabled: false, - dragRotate: false, - pitchWithRotate: false, - bearingSnap: 180, - maxPitch: 0, - center: OSM.home ? [OSM.home.lon, OSM.home.lat] : [0, 0], - zoom: OSM.home ? defaultHomeZoom : 0 - }); + map = new maplibregl.Map(OSM.MapLibre.defaultSecondaryMapOptions); savedLat = $("#home_lat").val(); savedLon = $("#home_lon").val(); @@ -132,7 +118,7 @@ $(function () { const lat = $("#home_lat").val(), lon = $("#home_lon").val(); - map.flyTo({ center: [lon, lat], zoom: defaultHomeZoom }); + map.flyTo({ center: [lon, lat], zoom: OSM.MapLibre.defaultHomeZoom }); }); $("#home_delete").click(function () { @@ -204,10 +190,10 @@ $(function () { } function isCloseEnoughToMapCenter(location) { - const inputPt = map.project(location), - centerPt = map.project(map.getCenter()); + const inputPt = map.project(location); + const centerPt = map.project(map.getCenter()); - return Math.sqrt(Math.pow(centerPt.x - inputPt.x, 2) + Math.pow(centerPt.y - inputPt.y, 2)) < 10; + return centerPt.dist(inputPt) < 10; } function clearDeletedText() { -- 2.39.5