Skip to content

Commit 5b568ab

Browse files
committed
Allow map to be configured to show popups or not and to use label for the popup value
1 parent 0e888d1 commit 5b568ab

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

app/javascript/controllers/better_together/map_controller.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export default class extends Controller {
77
center: String,
88
spaces: Array,
99
zoom: Number,
10-
extent: String
10+
extent: String,
11+
enablePopups: { type: Boolean, default: true }, // Default to enabling popups
12+
useLabelAsPopup: { type: Boolean, default: false } // Default to using popup_html
1113
}
1214

1315
connect() {
@@ -80,11 +82,16 @@ export default class extends Controller {
8082
}
8183

8284
const markers = points.map(point => {
83-
const { lat, lng, label } = point
85+
const { lat, lng, label, popup_html } = point
8486
const marker = L.marker([lat, lng]).addTo(this.map)
85-
if (label) {
86-
marker.bindPopup(label).openPopup() // Automatically open the popup
87+
88+
if (this.enablePopupsValue) {
89+
const popupContent = this.useLabelAsPopupValue ? label : popup_html
90+
if (popupContent) {
91+
marker.bindPopup(popupContent).openPopup() // Automatically open the popup
92+
}
8793
}
94+
8895
return marker
8996
})
9097

app/views/better_together/shared/_map.html.erb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
2-
<%# locals: (map:)%>
1+
<%# locals: (map:, enable_popups: true, use_label_as_popup: false) %>
32

43
<% if map %>
54
<div class="map" id="<%= dom_id(map) %>"
65
data-controller="better_together--map"
76
data-better_together--map-center-value="<%= map.center_for_leaflet %>"
8-
data-better_together--map-spaces-value="<%= map.leaflet_points %>"
7+
data-better_together--map-spaces-value="<%= map.leaflet_points.to_json %>"
98
data-better_together--map-zoom-value="<%= map.zoom %>"
10-
data-better_together--map-extent-value="<%= map.viewport %>">
9+
data-better_together--map-extent-value="<%= map.viewport %>"
10+
data-better_together--map-enable-popups-value="<%= enable_popups %>"
11+
data-better_together--map-use-label-as-popup-value="<%= use_label_as_popup %>">
1112
<div class="map-controls">
1213
<button class="btn btn-secondary" data-action="better_together--map#switchToOSM">Map</button>
1314
<button class="btn btn-secondary" data-action="better_together--map#switchToSatellite">Satellite</button>

0 commit comments

Comments
 (0)