Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

highlight_color for vector classes #1866

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion folium/vector_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def __init__(
locations: TypeMultiLine,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
**kwargs: TypePathOptions
):
super().__init__()
self.locations = validate_multi_locations(locations)
Expand Down Expand Up @@ -181,14 +182,36 @@ class PolyLine(BaseMultiLocation):
{{ this.locations|tojson }},
{{ this.options|tojson }}
).addTo({{this._parent.get_name()}});
{% if this.options.highlight_color %}
{{ this.get_name() }}.options.original_color = {{ this.options.color|tojson }};

function highlightLayer(e) {
if (e.target.getTooltip()) {
e.target.setStyle({ color: e.target.options.highlight_color });
}
}

function resetHighlight(e) {
e.target.setStyle({ color: e.target.options.original_color });
}

{{ this.get_name() }}.on({
mouseover: highlightLayer,
mouseout: resetHighlight
});
{% endif %}
{% endmacro %}
"""
)

def __init__(self, locations, popup=None, tooltip=None, **kwargs):
def __init__(
self, locations, popup=None, tooltip=None, highlight_color=None, **kwargs
):
super().__init__(locations, popup=popup, tooltip=tooltip)
self._name = "PolyLine"
self.options = path_options(line=True, **kwargs)
if highlight_color is not None:
self.options["highlight_color"] = highlight_color


class Polygon(BaseMultiLocation):
Expand Down Expand Up @@ -220,6 +243,24 @@ class Polygon(BaseMultiLocation):
{{ this.locations|tojson }},
{{ this.options|tojson }}
).addTo({{this._parent.get_name()}});
{% if this.options.highlight_color %}
{{ this.get_name() }}.options.original_color = {{ this.options.color|tojson }};

function highlightLayer(e) {
if (e.target.getTooltip()) {
e.target.setStyle({ color: e.target.options.highlight_color });
}
}

function resetHighlight(e) {
e.target.setStyle({ color: e.target.options.original_color });
}

{{ this.get_name() }}.on({
mouseover: highlightLayer,
mouseout: resetHighlight
});
{% endif %}
{% endmacro %}
"""
)
Expand All @@ -229,11 +270,14 @@ def __init__(
locations: TypeMultiLine,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
highlight_color: Optional[str] = None,
**kwargs: TypePathOptions
):
super().__init__(locations, popup=popup, tooltip=tooltip)
self._name = "Polygon"
self.options = path_options(line=True, radius=None, **kwargs)
if highlight_color is not None:
self.options["highlight_color"] = highlight_color


class Rectangle(MacroElement):
Expand Down Expand Up @@ -262,6 +306,24 @@ class Rectangle(MacroElement):
{{ this.locations|tojson }},
{{ this.options|tojson }}
).addTo({{this._parent.get_name()}});
{% if this.options.highlight_color %}
{{ this.get_name() }}.options.original_color = {{ this.options.color|tojson }};

function highlightLayer(e) {
if (e.target.getTooltip()) {
e.target.setStyle({ color: e.target.options.highlight_color });
}
}

function resetHighlight(e) {
e.target.setStyle({ color: e.target.options.original_color });
}

{{ this.get_name() }}.on({
mouseover: highlightLayer,
mouseout: resetHighlight
});
{% endif %}
{% endmacro %}
"""
)
Expand All @@ -271,6 +333,7 @@ def __init__(
bounds: TypeLine,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
highlight_color: Optional[str] = None,
**kwargs: TypePathOptions
):
super().__init__()
Expand All @@ -284,6 +347,8 @@ def __init__(
self.add_child(
tooltip if isinstance(tooltip, Tooltip) else Tooltip(str(tooltip))
)
if highlight_color is not None:
self.options["highlight_color"] = highlight_color

def _get_self_bounds(self) -> List[List[Optional[float]]]:
"""Compute the bounds of the object itself."""
Expand Down Expand Up @@ -322,6 +387,24 @@ class Circle(Marker):
{{ this.location|tojson }},
{{ this.options|tojson }}
).addTo({{ this._parent.get_name() }});
{% if this.options.highlight_color %}
{{ this.get_name() }}.options.original_color = {{ this.options.color|tojson }};

function highlightLayer(e) {
if (e.target.getTooltip()) {
e.target.setStyle({ color: e.target.options.highlight_color });
}
}

function resetHighlight(e) {
e.target.setStyle({ color: e.target.options.original_color });
}

{{ this.get_name() }}.on({
mouseover: highlightLayer,
mouseout: resetHighlight
});
{% endif %}
{% endmacro %}
"""
)
Expand All @@ -332,11 +415,14 @@ def __init__(
radius: float = 50,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
highlight_color: Optional[str] = None,
**kwargs: TypePathOptions
):
super().__init__(location, popup=popup, tooltip=tooltip)
self._name = "circle"
self.options = path_options(line=False, radius=radius, **kwargs)
if highlight_color is not None:
self.options["highlight_color"] = highlight_color


class CircleMarker(Marker):
Expand Down Expand Up @@ -368,6 +454,24 @@ class CircleMarker(Marker):
{{ this.location|tojson }},
{{ this.options|tojson }}
).addTo({{ this._parent.get_name() }});
{% if this.options.highlight_color %}
{{ this.get_name() }}.options.original_color = {{ this.options.color|tojson }};

function highlightLayer(e) {
if (e.target.getTooltip()) {
e.target.setStyle({ color: e.target.options.highlight_color });
}
}

function resetHighlight(e) {
e.target.setStyle({ color: e.target.options.original_color });
}

{{ this.get_name() }}.on({
mouseover: highlightLayer,
mouseout: resetHighlight
});
{% endif %}
{% endmacro %}
"""
)
Expand All @@ -378,8 +482,11 @@ def __init__(
radius: float = 10,
popup: Union[Popup, str, None] = None,
tooltip: Union[Tooltip, str, None] = None,
highlight_color: Optional[str] = None,
**kwargs: TypePathOptions
):
super().__init__(location, popup=popup, tooltip=tooltip)
self._name = "CircleMarker"
self.options = path_options(line=False, radius=radius, **kwargs)
if highlight_color is not None:
self.options["highlight_color"] = highlight_color