Skip to content

Commit

Permalink
GeoJSON: option to keep layer highlighted when popup is open (#1836)
Browse files Browse the repository at this point in the history
* modified features.py

* combined two "if" statements in one, in the test

Co-authored-by: Frank Anema <33519926+Conengmo@users.noreply.github.com>

* Solved issues for PR: docstring, ValueError and restructuring of mouse event code

* added popup bind and unbind to fix the bug raised in PR

---------

Co-authored-by: Frank Anema <33519926+Conengmo@users.noreply.github.com>
  • Loading branch information
GiuseppeGalilei and Conengmo authored Dec 24, 2023
1 parent 0f4d57f commit 2586125
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ class GeoJson(Layer):
Function mapping a GeoJson Feature to a style dict.
highlight_function: function, default None
Function mapping a GeoJson Feature to a style dict for mouse events.
popup_keep_highlighted: bool, default False
Whether to keep the highlighting active while the popup is open
name : string, default None
The name of the Layer, as it will appear in LayerControls
overlay : bool, default True
Expand Down Expand Up @@ -572,7 +574,10 @@ class GeoJson(Layer):
{%- if this.highlight %}
mouseout: function(e) {
if(typeof e.target.setStyle === "function"){
{{ this.get_name() }}.resetStyle(e.target);
{%- if this.popup_keep_highlighted %}
if (!e.target.isPopupOpen())
{%- endif %}
{{ this.get_name() }}.resetStyle(e.target);
}
},
mouseover: function(e) {
Expand All @@ -581,6 +586,21 @@ class GeoJson(Layer):
e.target.setStyle(highlightStyle);
}
},
{%- if this.popup_keep_highlighted %}
popupopen: function(e) {
if(typeof e.target.setStyle === "function"){
const highlightStyle = {{ this.get_name() }}_highlighter(e.target.feature)
e.target.setStyle(highlightStyle);
e.target.bindPopup(e.popup)
}
},
popupclose: function(e) {
if(typeof e.target.setStyle === "function"){
{{ this.get_name() }}.resetStyle(e.target);
e.target.unbindPopup()
}
},
{%- endif %}
{%- endif %}
{%- if this.zoom_on_click %}
click: function(e) {
Expand Down Expand Up @@ -632,6 +652,7 @@ def __init__(
data: Any,
style_function: Optional[Callable] = None,
highlight_function: Optional[Callable] = None,
popup_keep_highlighted: bool = False,
name: Optional[str] = None,
overlay: bool = True,
control: bool = True,
Expand Down Expand Up @@ -659,6 +680,13 @@ def __init__(
raise TypeError(
"Only Marker, Circle, and CircleMarker are supported as GeoJson marker types."
)

if popup_keep_highlighted and popup is None:
raise ValueError(
"A popup is needed to use the popup_keep_highlighted feature"
)
self.popup_keep_highlighted = popup_keep_highlighted

self.marker = marker
self.options = parse_options(**kwargs)

Expand Down

0 comments on commit 2586125

Please sign in to comment.