|
4 | 4 |
|
5 | 5 | from .._cache import _check_dependencies |
6 | 6 |
|
7 | | -branca, jinja2 = _check_dependencies('branca', 'jinja2') |
8 | 7 |
|
9 | | - |
10 | | -class BindColormap(branca.element.MacroElement): |
| 8 | +class BindColormap: |
11 | 9 | """ |
12 | 10 | Binds a colormap to a Folium layer with visibility synchronization. |
13 | 11 |
|
14 | 12 | This class creates a connection between a Folium map layer and a color scale, |
15 | 13 | showing/hiding the colormap legend when the layer visibility changes. |
16 | 14 | """ |
17 | 15 |
|
18 | | - def __init__(self, layer, colormap, display=True): |
| 16 | + def __new__(cls, layer, colormap, display=True): |
19 | 17 | """ |
20 | 18 | :param layer: Folium layer object (``FeatureGroup``, ``GeoJson``, etc.) to bind with. |
21 | 19 | :type layer: folium.FeatureGroup | folium.GeoJson |
@@ -55,26 +53,35 @@ def __init__(self, layer, colormap, display=True): |
55 | 53 | <https://nbviewer.org/gist/BibMartin/f153aa957ddc5fadc64929abdee9ff2e>`_]. |
56 | 54 | """ |
57 | 55 |
|
58 | | - super().__init__() |
59 | | - |
60 | | - self.layer = layer |
61 | | - self.colormap = colormap |
62 | | - |
63 | | - display_first = "'block'" if display else "'none'" |
64 | | - |
65 | | - # noinspection SpellCheckingInspection |
66 | | - template_str = u""" |
67 | | - {% macro script(this, kwargs) %} |
68 | | - {{this.colormap.get_name()}}.svg[0][0].style.display = {display_first}; |
69 | | - {{this._parent.get_name()}}.on('overlayadd', function(eventLayer) { |
70 | | - if (eventLayer.layer == {{this.layer.get_name()}}) { |
71 | | - {{this.colormap.get_name()}}.svg[0][0].style.display = 'block'; |
72 | | - }}); |
73 | | - {{this._parent.get_name()}}.on('overlayremove', function(eventLayer) { |
74 | | - if (eventLayer.layer == {{this.layer.get_name()}}) { |
75 | | - {{this.colormap.get_name()}}.svg[0][0].style.display = 'none'; |
76 | | - }}); |
77 | | - {% endmacro %} |
78 | | - """ |
79 | | - |
80 | | - self._template = jinja2.Template(template_str.replace('{display_first}', display_first)) |
| 56 | + branca, jinja2 = _check_dependencies('branca', 'jinja2') |
| 57 | + |
| 58 | + class _BindColormap(branca.element.MacroElement): |
| 59 | + |
| 60 | + def __init__(self, _layer, _colormap, _display): |
| 61 | + super().__init__() |
| 62 | + |
| 63 | + self.layer = _layer |
| 64 | + self.colormap = _colormap |
| 65 | + |
| 66 | + display_first = "'block'" if _display else "'none'" |
| 67 | + |
| 68 | + # noinspection SpellCheckingInspection |
| 69 | + template_str = u""" |
| 70 | + {% macro script(this, kwargs) %} |
| 71 | + {{this.colormap.get_name()}}.svg[0][0].style.display = {display_first}; |
| 72 | + {{this._parent.get_name()}}.on('overlayadd', function(eventLayer) { |
| 73 | + if (eventLayer.layer == {{this.layer.get_name()}}) { |
| 74 | + {{this.colormap.get_name()}}.svg[0][0].style.display = 'block'; |
| 75 | + }}); |
| 76 | + {{this._parent.get_name()}}.on('overlayremove', function(eventLayer) { |
| 77 | + if (eventLayer.layer == {{this.layer.get_name()}}) { |
| 78 | + {{this.colormap.get_name()}}.svg[0][0].style.display = 'none'; |
| 79 | + }}); |
| 80 | + {% endmacro %} |
| 81 | + """ |
| 82 | + |
| 83 | + self._template = jinja2.Template( |
| 84 | + template_str.replace('{display_first}', display_first)) |
| 85 | + |
| 86 | + # Instantiate the dynamic subclass and return it directly |
| 87 | + return _BindColormap(layer, colormap, display) |
0 commit comments