changeset 1342:1df0d1b28b83

browser (slideshow): options can now be attached to a slide: options can currently be used to: - add a flag, which will set a corresponding CSS class to the slide when active - use an exit callback, called when we move out from the slide
author Goffi <goffi@goffi.org>
date Mon, 24 Aug 2020 22:52:04 +0200
parents a8bc1ee455ae
children 8729d2708f65
files libervia/pages/_browser/slideshow.py
diffstat 1 files changed, 36 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/_browser/slideshow.py	Mon Aug 24 22:49:24 2020 +0200
+++ b/libervia/pages/_browser/slideshow.py	Mon Aug 24 22:52:04 2020 +0200
@@ -14,6 +14,8 @@
         self.hidden_elts = {}
         self.control_hidden = False
         self.click_timer = None
+        self._class_to_remove = set()
+        self._exit_callback = None
 
     @property
     def current_slide(self):
@@ -30,13 +32,24 @@
         """item attached to the current slide, if any"""
         current = self.current_slide
         if current is None:
-            return
+            return None
         try:
             return current.js._item
         except AttributeError:
             return None
 
     @property
+    def current_options(self):
+        """options attached to the current slide, if any"""
+        current = self.current_slide
+        if current is None:
+            return None
+        try:
+            return current.js._options
+        except AttributeError:
+            return None
+
+    @property
     def index(self):
         if self.swiper is None:
             return None
@@ -93,12 +106,14 @@
         self.on_slide_change(self.swiper)
         self.fullscreen(True)
 
-    def add_slide(self, elt, item_data=None):
+    def add_slide(self, elt, item_data=None, options=None):
         slide_elt = html.DIV(Class="swiper-slide")
         zoom_cont_elt = html.DIV(Class="swiper-zoom-container")
         slide_elt <= zoom_cont_elt
         zoom_cont_elt <= elt
         slide_elt._item = item_data
+        if options is not None:
+            slide_elt._options = options
         self.swiper.appendSlide([slide_elt])
         self.swiper.update()
 
@@ -144,11 +159,26 @@
         evt.preventDefault()
 
     def on_slide_change(self, swiper):
+        if self._exit_callback is not None:
+            self._exit_callback()
+            self._exit_callback = None
         item = self.current_item
-        if item is None:
-            return
-        comments_count = item.get('comments_count')
-        self.comments_count_elt.text = comments_count or ''
+        if item is not None:
+            comments_count = item.get('comments_count')
+            self.comments_count_elt.text = comments_count or ''
+
+        for cls in self._class_to_remove:
+            self.slideshow_elt.classList.remove(cls)
+
+        self._class_to_remove.clear()
+
+        options = self.current_options
+        if options is not None:
+            for flag in options.get('flags', []):
+                cls = f"flag_{flag.lower()}"
+                self.slideshow_elt.classList.add(cls)
+                self._class_to_remove.add(cls)
+            self._exit_callback = options.get("exit_callback", None)
 
     def toggle_hide_controls(self, evt):
         self.click_timer = None