From 1d1511b6983b5e068b73782fd869840232383db4 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Sat, 18 May 2024 16:37:40 +0200 Subject: [PATCH] Changes after review comments by conengmo --- docs/reference.rst | 2 +- folium/elements.py | 32 ++++++++++---------------------- folium/map.py | 4 ++-- tests/test_features.py | 3 ++- 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/docs/reference.rst b/docs/reference.rst index bb4740d20..da20b3377 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -35,7 +35,7 @@ Utilities --------------------- .. autoclass:: folium.utilities.JsCode -.. autoclass:: folium.elements.EventTargetMixin +.. autoclass:: folium.elements.EventHandler Plugins diff --git a/folium/elements.py b/folium/elements.py index d6f04dfe3..d017f7589 100644 --- a/folium/elements.py +++ b/folium/elements.py @@ -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 @@ -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) %} diff --git a/folium/map.py b/folium/map.py index fb00ee72d..01ac7d2ed 100644 --- a/folium/map.py +++ b/folium/map.py @@ -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, @@ -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. diff --git a/tests/test_features.py b/tests/test_features.py index 8dd51d61b..94d27d329 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -13,6 +13,7 @@ import folium from folium import Choropleth, ClickForMarker, GeoJson, Map, Popup +from folium.elements import EventHandler from folium.utilities import JsCode @@ -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