From a9729e404d7c69d4df15a2726302029823b99506 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Wed, 23 Nov 2022 15:19:57 +0100 Subject: [PATCH 1/2] Leaflet Control --- folium/features.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/folium/features.py b/folium/features.py index 8385b3d46..2b1a12aee 100644 --- a/folium/features.py +++ b/folium/features.py @@ -21,6 +21,7 @@ from folium.utilities import ( _parse_size, camelize, + escape_backticks, get_bounds, get_obj_in_upper_tree, image_to_url, @@ -1899,3 +1900,39 @@ def __init__( self.add_child( PolyLine(val, color=key, weight=weight, opacity=opacity) ) # noqa + + +class CustomControl(MacroElement): + """Put any HTML on the map as a Leaflet Control.""" + + _template = Template( + """ + {% macro script(this, kwargs) %} + + L.Control.CustomControl = L.Control.extend({ + onAdd: function(map) { + let div = L.DomUtil.create('div'); + div.innerHTML = `{{ this.html }}`; + return div; + }, + onRemove: function(map) { + // Nothing to do here + } + }); + + L.control.customControl = function(opts) { + return new L.Control.CustomControl(opts); + } + + L.control.customControl( + { position: "{{ this.position }}" } + ).addTo({{ this._parent.get_name() }}); + + {% endmacro %} + """ + ) + + def __init__(self, html, position="bottomleft"): + super().__init__() + self.html = escape_backticks(html) + self.position = position From d209743705390b0588c210346f84c3a249e61dde Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Tue, 29 Nov 2022 17:56:57 +0100 Subject: [PATCH 2/2] switchable control --- folium/features.py | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/folium/features.py b/folium/features.py index 2b1a12aee..4fced617b 100644 --- a/folium/features.py +++ b/folium/features.py @@ -1903,30 +1903,30 @@ def __init__( class CustomControl(MacroElement): - """Put any HTML on the map as a Leaflet Control.""" + """Display static html and switch together with parent layer.""" _template = Template( """ {% macro script(this, kwargs) %} - L.Control.CustomControl = L.Control.extend({ - onAdd: function(map) { - let div = L.DomUtil.create('div'); - div.innerHTML = `{{ this.html }}`; - return div; - }, - onRemove: function(map) { - // Nothing to do here - } + {{ this.get_name() }} = L.control({ + position: "{{ this.position }}", }); - - L.control.customControl = function(opts) { - return new L.Control.CustomControl(opts); + {{ this.get_name() }}.onAdd = function(map) { + let div = L.DomUtil.create('div'); + div.innerHTML = `{{ this.html }}`; + return div; } + {{ this.get_name() }}.addTo({{ this.parent_map.get_name() }}); - L.control.customControl( - { position: "{{ this.position }}" } - ).addTo({{ this._parent.get_name() }}); + {%- if this.switch %} + {{ this._parent.get_name() }}.on('add', function(e) { + {{ this.get_name() }}.addTo({{ this.parent_map.get_name() }}); + }); + {{ this._parent.get_name() }}.on('remove', function(e) { + e.target._map.removeControl({{ this.get_name() }}); + }); + {%- endif %} {% endmacro %} """ @@ -1934,5 +1934,13 @@ class CustomControl(MacroElement): def __init__(self, html, position="bottomleft"): super().__init__() + self._name = "custom_control" self.html = escape_backticks(html) self.position = position + self.parent_map = None + self.switch = None + + def render(self, **kwargs): + self.parent_map = get_obj_in_upper_tree(self, Map) + self.switch = isinstance(self._parent, Layer) and self._parent.control + super().render(**kwargs)