comparison libervia/pages/_browser/slideshow.py @ 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 dbd573b0bc9c
comparison
equal deleted inserted replaced
1341:a8bc1ee455ae 1342:1df0d1b28b83
12 self.comments_count_elt = self.slideshow_elt.select_one('.comments__count') 12 self.comments_count_elt = self.slideshow_elt.select_one('.comments__count')
13 self.wrapper = self.slideshow_elt.select_one(".swiper-wrapper") 13 self.wrapper = self.slideshow_elt.select_one(".swiper-wrapper")
14 self.hidden_elts = {} 14 self.hidden_elts = {}
15 self.control_hidden = False 15 self.control_hidden = False
16 self.click_timer = None 16 self.click_timer = None
17 self._class_to_remove = set()
18 self._exit_callback = None
17 19
18 @property 20 @property
19 def current_slide(self): 21 def current_slide(self):
20 if self.swiper is None: 22 if self.swiper is None:
21 return None 23 return None
28 @property 30 @property
29 def current_item(self): 31 def current_item(self):
30 """item attached to the current slide, if any""" 32 """item attached to the current slide, if any"""
31 current = self.current_slide 33 current = self.current_slide
32 if current is None: 34 if current is None:
33 return 35 return None
34 try: 36 try:
35 return current.js._item 37 return current.js._item
38 except AttributeError:
39 return None
40
41 @property
42 def current_options(self):
43 """options attached to the current slide, if any"""
44 current = self.current_slide
45 if current is None:
46 return None
47 try:
48 return current.js._options
36 except AttributeError: 49 except AttributeError:
37 return None 50 return None
38 51
39 @property 52 @property
40 def index(self): 53 def index(self):
91 self.slideshow_elt.bind("dblclick", self.on_dblclick) 104 self.slideshow_elt.bind("dblclick", self.on_dblclick)
92 self.swiper.on("slideChange", self.on_slide_change) 105 self.swiper.on("slideChange", self.on_slide_change)
93 self.on_slide_change(self.swiper) 106 self.on_slide_change(self.swiper)
94 self.fullscreen(True) 107 self.fullscreen(True)
95 108
96 def add_slide(self, elt, item_data=None): 109 def add_slide(self, elt, item_data=None, options=None):
97 slide_elt = html.DIV(Class="swiper-slide") 110 slide_elt = html.DIV(Class="swiper-slide")
98 zoom_cont_elt = html.DIV(Class="swiper-zoom-container") 111 zoom_cont_elt = html.DIV(Class="swiper-zoom-container")
99 slide_elt <= zoom_cont_elt 112 slide_elt <= zoom_cont_elt
100 zoom_cont_elt <= elt 113 zoom_cont_elt <= elt
101 slide_elt._item = item_data 114 slide_elt._item = item_data
115 if options is not None:
116 slide_elt._options = options
102 self.swiper.appendSlide([slide_elt]) 117 self.swiper.appendSlide([slide_elt])
103 self.swiper.update() 118 self.swiper.update()
104 119
105 def quit(self): 120 def quit(self):
106 # we unhide 121 # we unhide
142 else: 157 else:
143 return 158 return
144 evt.preventDefault() 159 evt.preventDefault()
145 160
146 def on_slide_change(self, swiper): 161 def on_slide_change(self, swiper):
162 if self._exit_callback is not None:
163 self._exit_callback()
164 self._exit_callback = None
147 item = self.current_item 165 item = self.current_item
148 if item is None: 166 if item is not None:
149 return 167 comments_count = item.get('comments_count')
150 comments_count = item.get('comments_count') 168 self.comments_count_elt.text = comments_count or ''
151 self.comments_count_elt.text = comments_count or '' 169
170 for cls in self._class_to_remove:
171 self.slideshow_elt.classList.remove(cls)
172
173 self._class_to_remove.clear()
174
175 options = self.current_options
176 if options is not None:
177 for flag in options.get('flags', []):
178 cls = f"flag_{flag.lower()}"
179 self.slideshow_elt.classList.add(cls)
180 self._class_to_remove.add(cls)
181 self._exit_callback = options.get("exit_callback", None)
152 182
153 def toggle_hide_controls(self, evt): 183 def toggle_hide_controls(self, evt):
154 self.click_timer = None 184 self.click_timer = None
155 if 'click_to_hide' in evt.target.classList: 185 if 'click_to_hide' in evt.target.classList:
156 # we don't want to hide controls when a control is clicked 186 # we don't want to hide controls when a control is clicked