]> git.openstreetmap.org Git - rails.git/commitdiff
Merge pull request #3704 from AntonKhorev/leaflet-control-groups
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 19 Oct 2022 14:42:08 +0000 (15:42 +0100)
committerGitHub <noreply@github.com>
Wed, 19 Oct 2022 14:42:08 +0000 (15:42 +0100)
Group map controls explicitly

app/assets/javascripts/index.js
app/assets/javascripts/leaflet.locate.js [new file with mode: 0644]
app/assets/stylesheets/common.scss

index e5590c9f7ec3cf607f75ff8260f8aa8387075b2f..49266f919f374770f0b7463020c0dae762e4e424 100644 (file)
@@ -2,6 +2,7 @@
 //= require leaflet.sidebar
 //= require leaflet.sidebar-pane
 //= require leaflet.locatecontrol/src/L.Control.Locate
+//= require leaflet.locate
 //= require leaflet.layers
 //= require leaflet.key
 //= require leaflet.note
@@ -98,62 +99,60 @@ $(document).ready(function () {
     }
   });
 
-  var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
-
-  L.OSM.zoom({ position: position })
+  var sidebar = L.OSM.sidebar("#map-ui")
     .addTo(map);
 
-  var locate = L.control.locate({
-    position: position,
-    icon: "icon geolocate",
-    iconLoading: "icon geolocate",
-    strings: {
-      title: I18n.t("javascripts.map.locate.title"),
-      popup: function (options) {
-        return I18n.t("javascripts.map.locate." + options.unit + "Popup", { count: options.distance });
-      }
-    }
-  }).addTo(map);
+  var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
 
-  var locateContainer = locate.getContainer();
+  function addControlGroup(controls) {
+    controls.forEach(function (control) {
+      control.addTo(map);
+    });
 
-  $(locateContainer)
-    .removeClass("leaflet-control-locate leaflet-bar")
-    .addClass("control-locate")
-    .children("a")
-    .attr("href", "#")
-    .removeClass("leaflet-bar-part leaflet-bar-part-single")
-    .addClass("control-button");
+    var firstContainer = controls[0].getContainer();
+    $(firstContainer).find(".control-button").first()
+      .addClass("control-button-first");
 
-  var sidebar = L.OSM.sidebar("#map-ui")
-    .addTo(map);
+    var lastContainer = controls[controls.length - 1].getContainer();
+    $(lastContainer).find(".control-button").last()
+      .addClass("control-button-last");
+  }
 
-  L.OSM.layers({
-    position: position,
-    layers: map.baseLayers,
-    sidebar: sidebar
-  }).addTo(map);
-
-  L.OSM.key({
-    position: position,
-    sidebar: sidebar
-  }).addTo(map);
-
-  L.OSM.share({
-    "position": position,
-    "sidebar": sidebar,
-    "short": true
-  }).addTo(map);
-
-  L.OSM.note({
-    position: position,
-    sidebar: sidebar
-  }).addTo(map);
-
-  L.OSM.query({
-    position: position,
-    sidebar: sidebar
-  }).addTo(map);
+  addControlGroup([
+    L.OSM.zoom({ position: position }),
+    L.OSM.locate({ position: position })
+  ]);
+
+  addControlGroup([
+    L.OSM.layers({
+      position: position,
+      layers: map.baseLayers,
+      sidebar: sidebar
+    }),
+    L.OSM.key({
+      position: position,
+      sidebar: sidebar
+    }),
+    L.OSM.share({
+      "position": position,
+      "sidebar": sidebar,
+      "short": true
+    })
+  ]);
+
+  addControlGroup([
+    L.OSM.note({
+      position: position,
+      sidebar: sidebar
+    })
+  ]);
+
+  addControlGroup([
+    L.OSM.query({
+      position: position,
+      sidebar: sidebar
+    })
+  ]);
 
   L.control.scale()
     .addTo(map);
diff --git a/app/assets/javascripts/leaflet.locate.js b/app/assets/javascripts/leaflet.locate.js
new file mode 100644 (file)
index 0000000..d43a5e4
--- /dev/null
@@ -0,0 +1,26 @@
+L.OSM.locate = function (options) {
+  var control = L.control.locate(Object.assign({
+    icon: "icon geolocate",
+    iconLoading: "icon geolocate",
+    strings: {
+      title: I18n.t("javascripts.map.locate.title"),
+      popup: function (options) {
+        return I18n.t("javascripts.map.locate." + options.unit + "Popup", { count: options.distance });
+      }
+    }
+  }, options));
+
+  control.onAdd = function (map) {
+    var container = Object.getPrototypeOf(this).onAdd.apply(this, [map]);
+    $(container)
+      .removeClass("leaflet-control-locate leaflet-bar")
+      .addClass("control-locate")
+      .children("a")
+      .attr("href", "#")
+      .removeClass("leaflet-bar-part leaflet-bar-part-single")
+      .addClass("control-button");
+    return container;
+  };
+
+  return control;
+};
index 5ff48dc6c54c226cbe4a4de27bef891858ac23eb..312f7ca24166e3da48ffa2c3ae017102d6954097 100644 (file)
@@ -358,8 +358,6 @@ body.small-nav {
   width: 40px;
   background-color: #333;
   background-color: rgba(0,0,0,.6);
-  border-radius: 4px 0 0 4px;
-  margin-bottom: 10px;
   outline: none;
 
   &:hover,
@@ -378,26 +376,18 @@ body.small-nav {
     background-color: $vibrant-green;
   }
 
-  .icon {
-    margin: 10px;
+  &-first {
+    border-start-start-radius: 4px;
   }
-}
 
-.leaflet-control .zoomin,
-.control-layers .control-button {
-  margin-bottom: 0px;
-  border-radius: 4px 0 0 0;
-}
-
-.leaflet-control .zoomout,
-.control-key .control-button {
-  margin-bottom: 0;
-  border-radius: 0;
-}
+  &-last {
+    border-end-start-radius: 4px;
+    margin-bottom: 10px;
+  }
 
-.control-locate .control-button,
-.control-share .control-button {
-  border-radius: 0 0 0 4px;
+  .icon {
+    margin: 10px;
+  }
 }
 
 /* Rules for the sidebar and main map area */