Mercurial > libervia-web
annotate libervia/pages/_browser/slideshow.py @ 1307:0a6698714557
browser: `slideshow` module implementation:
this module show a gallery of slides (usually images/videos) with mobile friendly swipe
capabilities. Comments are handled using a side panel.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Jul 2020 09:08:50 +0200 |
parents | |
children | 9344ca3b21a6 |
rev | line source |
---|---|
1307
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 from browser import document, window, DOMNode |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 from js_modules.swiper import Swiper |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 from template import Template |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 class SlideShow: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 def __init__(self): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 self.swiper = None |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 slideshow_tpl = Template('photo/slideshow.html') |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 self.slideshow_elt = slideshow_tpl.get_elt() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 self.comments_count_elt = self.slideshow_elt.select_one('.comments__count') |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 self.wrapper = self.slideshow_elt.select_one(".swiper-wrapper") |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 @property |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 def current_slide(self): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 if self.swiper is None: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 return None |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 try: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 return DOMNode(self.swiper.slides[self.swiper.realIndex]) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 # getting missing item in JS arrays returns KeyError |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 except KeyError: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 return None |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 @property |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 def current_item(self): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 """item attached to the current slide, if any""" |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 current = self.current_slide |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 if current is None: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 return |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 try: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 return current.js._item |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 except AttributeError: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 return None |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 @property |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 def index(self): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 if self.swiper is None: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 return None |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 return self.swiper.realIndex |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 @index.setter |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 def index(self, idx): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 if self.swiper is not None: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 self.swiper.slideTo(idx, 0) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 def attach(self): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 document.body <= self.slideshow_elt |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self.swiper = Swiper.new( |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 ".swiper-container", |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 { |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 "pagination": { |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 "el": ".swiper-pagination", |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 }, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 "navigation": { |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 "nextEl": ".swiper-button-next", |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 "prevEl": ".swiper-button-prev", |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 "hideOnClick": True, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 }, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 "scrollbar": { |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 "el": ".swiper-scrollbar", |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 }, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 "grabCursor": True, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 "keyboard": { |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 "enabled": True, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 "onlyInViewport": False, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 }, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 "mousewheel": True, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 } |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 ) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 window.addEventListener("keydown", self.on_key_down, True) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 self.slideshow_elt.select_one(".click_to_close").bind("click", self.on_close) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 self.slideshow_elt.select_one(".click_to_comment").bind("click", self.on_comment) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 self.swiper.on("slideChange", self.on_slide_change) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 self.on_slide_change() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 def add_slide(self, slide_elt): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 self.swiper.appendSlide([slide_elt]) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 self.swiper.update() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 def quit(self): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 self.slideshow_elt.remove() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 self.slideshow_elt = None |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 self.swiper.destroy(True, True) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 self.swiper = None |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 def on_key_down(self, evt): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 if evt.key == 'Escape': |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 self.quit() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 else: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 return |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 evt.preventDefault() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 def on_slide_change(self): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 item = self.current_item |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 if item is None: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 return |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 comments_count = item.get('comments_count') |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 self.comments_count_elt.text = comments_count or '' |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 def on_close(self, evt): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 evt.stopPropagation() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 evt.preventDefault() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 self.quit() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 def on_comment_close(self, evt): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 side_panel = self.comments_panel_elt.select_one('.comments_side_panel') |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 side_panel.classList.remove('open') |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 side_panel.bind("transitionend", lambda evt: self.comments_panel_elt.remove()) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 def on_comments_panel_click(self, evt): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 # we stop stop propagation to avoid the closing of the panel |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 evt.stopPropagation() |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 def on_comment(self, evt): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 item = self.current_item |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 if item is None: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 return |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 comments_panel_tpl = Template('blog/comments_panel.html') |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 try: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 comments = item['comments']['items'] |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 except KeyError: |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 comments = [] |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 self.comments_panel_elt = comments_panel_tpl.get_elt({ |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 "comments": comments, |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 "comments_service": item['comments_service'], |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 "comments_node": item['comments_node'], |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 }) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 document.body <= self.comments_panel_elt |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 side_panel = self.comments_panel_elt.select_one('.comments_side_panel') |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 window.setTimeout(lambda: side_panel.classList.add("open")) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 for close_elt in self.comments_panel_elt.select('.click_to_close'): |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 close_elt.bind("click", self.on_comment_close) |
0a6698714557
browser: `slideshow` module implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 side_panel.bind("click", self.on_comments_panel_click) |