|
| 1 | +from functools import wraps |
1 | 2 | from typing import List, Tuple |
2 | 3 |
|
3 | 4 | from branca.element import ( |
|
9 | 10 | ) |
10 | 11 |
|
11 | 12 | from folium.template import Template |
12 | | -from folium.utilities import JsCode |
| 13 | +from folium.utilities import JsCode, camelize |
| 14 | + |
| 15 | + |
| 16 | +def leaflet_method(fn): |
| 17 | + @wraps(fn) |
| 18 | + def inner(self, *args, **kwargs): |
| 19 | + self.add_child(MethodCall(self, fn.__name__, *args, **kwargs)) |
| 20 | + |
| 21 | + return inner |
13 | 22 |
|
14 | 23 |
|
15 | 24 | class JSCSSMixin(MacroElement): |
@@ -148,3 +157,27 @@ def __init__(self, element_name: str, element_parent_name: str): |
148 | 157 | super().__init__() |
149 | 158 | self.element_name = element_name |
150 | 159 | self.element_parent_name = element_parent_name |
| 160 | + |
| 161 | + |
| 162 | +class MethodCall(MacroElement): |
| 163 | + """Abstract class to add an element to another element.""" |
| 164 | + |
| 165 | + _template = Template( |
| 166 | + """ |
| 167 | + {% macro script(this, kwargs) %} |
| 168 | + {{ this.target }}.{{ this.method }}( |
| 169 | + {% for arg in this.args %} |
| 170 | + {{ arg | tojavascript }}, |
| 171 | + {% endfor %} |
| 172 | + {{ this.kwargs | tojavascript }} |
| 173 | + ); |
| 174 | + {% endmacro %} |
| 175 | + """ |
| 176 | + ) |
| 177 | + |
| 178 | + def __init__(self, target: MacroElement, method: str, *args, **kwargs): |
| 179 | + super().__init__() |
| 180 | + self.target = target.get_name() |
| 181 | + self.method = camelize(method) |
| 182 | + self.args = args |
| 183 | + self.kwargs = kwargs |
0 commit comments