Repository: 12rambau/sepal_ui

Issue:
create a custom marker cluster that can be hidden
currently the marker cluster cannot be hidden, only the underlying markers can. 

I twould be good if it was possible to hide all the underlying markers at once by changin the visible trait of the marker_cluster itself

Discussion:

Patch:
diff --git a/docs/source/conf.py b/docs/source/conf.py
index 07f6d872..13defac5 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -52,6 +52,7 @@ extensions = [
     "sphinx.ext.napoleon",
     "sphinx.ext.autosummary",
     "sphinx.ext.viewcode",
+    "sphinx.ext.todo",
     "sphinx_favicon",
     "notfound.extension",
     "sphinxcontrib.autoprogram",
@@ -155,3 +156,6 @@ html_css_files = ["css/custom.css", "css/icon.css"]
 autosummary_generate = True
 autoclass_content = "both"
 autodoc_typehints = "description"
+
+# -- Options for TODO ----------------------------------------------------------
+todo_include_todos = True
diff --git a/sepal_ui/mapping/__init__.py b/sepal_ui/mapping/__init__.py
index b668ab60..32e56a39 100644
--- a/sepal_ui/mapping/__init__.py
+++ b/sepal_ui/mapping/__init__.py
@@ -24,6 +24,7 @@ from .layer_state_control import *
 from .layers_control import *
 from .legend_control import *
 from .map_btn import *
+from .marker_cluster import *
 from .menu_control import *
 from .sepal_map import *
 from .zoom_control import *
diff --git a/sepal_ui/mapping/layers_control.py b/sepal_ui/mapping/layers_control.py
index 87550f91..a4efdec3 100644
--- a/sepal_ui/mapping/layers_control.py
+++ b/sepal_ui/mapping/layers_control.py
@@ -191,6 +191,9 @@ class LayersControl(MenuControl):
         self.menu.open_on_hover = True
         self.menu.close_delay = 200
 
+        # set the ize according to the content
+        self.set_size(None, None, None, None)
+
         # update the table at instance creation
         self.update_table({})
 
diff --git a/sepal_ui/mapping/marker_cluster.py b/sepal_ui/mapping/marker_cluster.py
new file mode 100644
index 00000000..2eb7978f
--- /dev/null
+++ b/sepal_ui/mapping/marker_cluster.py
@@ -0,0 +1,20 @@
+"""Custom implementation of the marker cluster to hide it at once."""
+
+from ipyleaflet import MarkerCluster
+from traitlets import Bool, observe
+
+
+class MarkerCluster(MarkerCluster):
+    """Overwrite the MarkerCluster to hide all the underlying cluster at once.
+
+    .. todo::
+        remove when https://github.com/jupyter-widgets/ipyleaflet/issues/1108 is solved
+    """
+
+    visible = Bool(True).tag(sync=True)
+
+    @observe("visible")
+    def toggle_markers(self, change):
+        """change the marker value according to the cluster viz."""
+        for marker in self.markers:
+            marker.visible = self.visible


Test patch:
diff --git a/tests/test_mapping/test_MarkerCluster.py b/tests/test_mapping/test_MarkerCluster.py
new file mode 100644
index 00000000..532eee9f
--- /dev/null
+++ b/tests/test_mapping/test_MarkerCluster.py
@@ -0,0 +1,22 @@
+"""Test the MarkerCluster control."""
+from ipyleaflet import Marker
+
+from sepal_ui import mapping as sm
+
+
+def test_init() -> None:
+    """Init a MarkerCluster."""
+    marker_cluster = sm.MarkerCluster()
+    assert isinstance(marker_cluster, sm.MarkerCluster)
+
+    return
+
+
+def test_visible() -> None:
+    """Hide MarkerCluster."""
+    markers = [Marker(location=(0, 0)) for _ in range(3)]
+    marker_cluster = sm.MarkerCluster(markers=markers)
+
+    # check that the visibility trait is linked
+    marker_cluster.visible = False
+    assert all(m.visible is False for m in markers)