Skip to content

Commit

Permalink
Changes after review comments by conengmo
Browse files Browse the repository at this point in the history
  • Loading branch information
hansthen committed May 18, 2024
1 parent 934ab68 commit 1d1511b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Utilities
---------------------

.. autoclass:: folium.utilities.JsCode
.. autoclass:: folium.elements.EventTargetMixin
.. autoclass:: folium.elements.EventHandler


Plugins
Expand Down
32 changes: 10 additions & 22 deletions folium/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ def _add_link(self, name: str, url: str, default_list: List[Tuple[str, str]]):
default_list.append((name, url))


class EventTargetMixin(Element):
'''Add Event Handlers to an element.
class EventHandler(MacroElement):
'''
Examples
--------
>>> import folium
Expand Down Expand Up @@ -85,35 +84,24 @@ class EventTargetMixin(Element):
>>> highlight = JsCode(
... """
... function highlight(e) {
... e.target.original_color = e.layer.options.color;
... e.target.setStyle({ color: "green" });
... }
... e.target.original_color = e.layer.options.color;
... e.target.setStyle({ color: "green" });
... }
... """
... )
>>>
>>> reset = JsCode(
... """
... function reset(e) {
... e.target.setStyle({ color: e.target.original_color });
... }
... function reset(e) {
... e.target.setStyle({ color: e.target.original_color });
... }
... """
... )
>>>
>>> g.on(mouseover=highlight, mouseout=reset)
>>> g.add_child(EventHandler("mouseover", highlight))
>>> g.add_child(EventHandler("mouseout", reset))
'''

def on(self, **kwargs: JsCode):
for event, handler in kwargs.items():
self.add_child(EventHandler(event, handler))
return self

def render(self, **kwargs) -> None:
super().render(**kwargs)


class EventHandler(MacroElement):
"""Render Event Handlers."""

_template = Template(
"""
{% macro script(this, kwargs) %}
Expand Down
4 changes: 2 additions & 2 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from branca.element import Element, Figure, Html, MacroElement
from jinja2 import Template

from folium.elements import ElementAddToElement, EventTargetMixin
from folium.elements import ElementAddToElement
from folium.utilities import (
TypeBounds,
TypeJsonValue,
Expand All @@ -21,7 +21,7 @@
)


class Layer(EventTargetMixin, MacroElement):
class Layer(MacroElement):
"""An abstract class for everything that is a Layer on the map.
It will be used to define whether an object will be included in
LayerControls.
Expand Down
3 changes: 2 additions & 1 deletion tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import folium
from folium import Choropleth, ClickForMarker, GeoJson, Map, Popup
from folium.elements import EventHandler
from folium.utilities import JsCode


Expand Down Expand Up @@ -296,7 +297,7 @@ def test_geojson_event_handler():
}
"""
)
geojson.on(mouseover=fn)
geojson.add_child(EventHandler("mouseover", fn))
rendered = m.get_root().render()
assert fn.js_code in rendered

Expand Down

0 comments on commit 1d1511b

Please sign in to comment.