Skip to content

Commit 2586125

Browse files
GeoJSON: option to keep layer highlighted when popup is open (#1836)
* modified features.py * combined two "if" statements in one, in the test Co-authored-by: Frank Anema <[email protected]> * 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 <[email protected]>
1 parent 0f4d57f commit 2586125

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

folium/features.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ class GeoJson(Layer):
473473
Function mapping a GeoJson Feature to a style dict.
474474
highlight_function: function, default None
475475
Function mapping a GeoJson Feature to a style dict for mouse events.
476+
popup_keep_highlighted: bool, default False
477+
Whether to keep the highlighting active while the popup is open
476478
name : string, default None
477479
The name of the Layer, as it will appear in LayerControls
478480
overlay : bool, default True
@@ -572,7 +574,10 @@ class GeoJson(Layer):
572574
{%- if this.highlight %}
573575
mouseout: function(e) {
574576
if(typeof e.target.setStyle === "function"){
575-
{{ this.get_name() }}.resetStyle(e.target);
577+
{%- if this.popup_keep_highlighted %}
578+
if (!e.target.isPopupOpen())
579+
{%- endif %}
580+
{{ this.get_name() }}.resetStyle(e.target);
576581
}
577582
},
578583
mouseover: function(e) {
@@ -581,6 +586,21 @@ class GeoJson(Layer):
581586
e.target.setStyle(highlightStyle);
582587
}
583588
},
589+
{%- if this.popup_keep_highlighted %}
590+
popupopen: function(e) {
591+
if(typeof e.target.setStyle === "function"){
592+
const highlightStyle = {{ this.get_name() }}_highlighter(e.target.feature)
593+
e.target.setStyle(highlightStyle);
594+
e.target.bindPopup(e.popup)
595+
}
596+
},
597+
popupclose: function(e) {
598+
if(typeof e.target.setStyle === "function"){
599+
{{ this.get_name() }}.resetStyle(e.target);
600+
e.target.unbindPopup()
601+
}
602+
},
603+
{%- endif %}
584604
{%- endif %}
585605
{%- if this.zoom_on_click %}
586606
click: function(e) {
@@ -632,6 +652,7 @@ def __init__(
632652
data: Any,
633653
style_function: Optional[Callable] = None,
634654
highlight_function: Optional[Callable] = None,
655+
popup_keep_highlighted: bool = False,
635656
name: Optional[str] = None,
636657
overlay: bool = True,
637658
control: bool = True,
@@ -659,6 +680,13 @@ def __init__(
659680
raise TypeError(
660681
"Only Marker, Circle, and CircleMarker are supported as GeoJson marker types."
661682
)
683+
684+
if popup_keep_highlighted and popup is None:
685+
raise ValueError(
686+
"A popup is needed to use the popup_keep_highlighted feature"
687+
)
688+
self.popup_keep_highlighted = popup_keep_highlighted
689+
662690
self.marker = marker
663691
self.options = parse_options(**kwargs)
664692

0 commit comments

Comments
 (0)