Debian Codegraph @

src/pyj/book_list/details_list.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 from elementmaker import E
6 from gettext import gettext as _
7
8 from book_list.theme import effective_book_list_is_dark, css_for_variables, get_color
9 from date import format_date
10 from dom import build_rule, clear, set_css, svgicon
11 from session import get_interface_data
12 from utils import fmt_sidx, safe_set_inner_html, sandboxed_html
13
14 DETAILS_LIST_CLASS = 'book-list-details-list'
15 ITEM_CLASS = DETAILS_LIST_CLASS + '-item'
16
17
18 def description():
19 return _('A list with thumbnails and some book details')
20
21 THUMBNAIL_MAX_WIDTH = 35 * 3
22 THUMBNAIL_MAX_HEIGHT = int(THUMBNAIL_MAX_WIDTH * 3 / 2)
23 BORDER_RADIUS = 6
24
25
26 def details_list_css():
27 ans = ''
28 sel = '.' + DETAILS_LIST_CLASS
29 ans += build_rule(sel,
30 cursor='pointer',
31 user_select='none',
32 display='flex',
33 flex_direction='column',
34 gap='0.75rem',
35 padding_top='0.75rem',
36 padding_bottom='0.75rem',
37 min_width='0',
38 max_width='100%',
39 box_sizing='border-box',
40 )
41 ans += build_rule(f'{sel} > .book-list-item',
42 min_width='0',
43 max_width='100%',
44 box_sizing='border-box',
45 )
46 # Do not underline list-row links on hover (keep the row feeling like a card)
47 ans += build_rule(sel + ' a:hover', text_decoration='none')
48 # Remove legacy separator rows; use gap + card surfaces instead
49 ans += build_rule(sel + ' > div', margin='0', padding='0', border='none')
50 ans += build_rule(f'{sel} .{ITEM_CLASS}',
51 display='flex',
52 align_items='stretch',
53 gap='1rem',
54 padding='0.85rem 1rem',
55 border_radius='18px',
56 border='var(--calibre-border-style-subtle)',
57 background='var(--calibre-surface-1)',
58 box_shadow='0 16px 44px rgba(0,0,0,0.10)',
59 text_decoration='none',
60 min_width='0',
61 max_width='100%',
62 width='100%',
63 box_sizing='border-box',
64 overflow='hidden',
65 )
66 # No hover zoom; use highlight instead
67 ans += build_rule(f'{sel} .{ITEM_CLASS}:hover',
68 background_color='var(--calibre-color-primary-light)',
69 border_color='var(--calibre-color-border)',
70 box_shadow='0 20px 56px rgba(0,0,0,0.12), 0 0 0 4px var(--calibre-color-primary-light)'
71 )
72 ans += build_rule(f'{sel} .{ITEM_CLASS}:active',
73 background_color='var(--calibre-color-primary-light-2)',
74 transform='translateY(0.5px)'
75 )
76 ans += build_rule(f'{sel} .{ITEM_CLASS}:hover .details-list-left > img', box_shadow='0 10px 24px rgba(0,0,0,0.14)')
77 s = sel + ' .details-list-left'
78 ans += build_rule(s,
79 margin_right='1em',
80 min_width=f'{THUMBNAIL_MAX_WIDTH}px',
81 height=f'{THUMBNAIL_MAX_HEIGHT}px',
82 display='flex',
83 align_items='stretch'
84 )
85 # Make the cover fill the full row height
86 ans += build_rule(s + ' > img',
87 border_radius=BORDER_RADIUS + 'px',
88 width=f'{THUMBNAIL_MAX_WIDTH}px',
89 height=f'{THUMBNAIL_MAX_HEIGHT}px',
90 display='block',
91 object_fit='cover',
92 border='var(--calibre-border-style-subtle)',
93 transition='box-shadow 160ms ease'
94 )
95 s = sel + ' .details-list-right'
96 ans += build_rule(s,
97 flex_grow='10',
98 flex_shrink='1',
99 flex_basis='0',
100 min_width='0',
101 max_width='100%',
102 overflow_x='hidden',
103 overflow_y='visible',
104 display='flex',
105 flex_direction='column',
106 min_height='0',
107 box_sizing='border-box',
108 )
109 ans += build_rule(sel + ' .details-main-head',
110 display='flex',
111 justify_content='space-between',
112 gap='0.75rem',
113 overflow_x='hidden',
114 overflow_y='hidden',
115 flex_shrink='0',
116 min_height='0',
117 min_width='0',
118 max_width='100%',
119 box_sizing='border-box',
120 )
121 ans += build_rule(sel + ' .details-head-primary',
122 display='flex',
123 flex_direction='column',
124 flex='1 1 0',
125 min_width='0',
126 max_width='100%',
127 min_height='0',
128 overflow='hidden',
129 )
130 ans += build_rule(sel + ' .details-title-authors',
131 flex='0 0 auto',
132 min_width='0',
133 max_width='100%',
134 overflow='hidden',
135 )
136 ans += build_rule(sel + ' .details-title',
137 display='block',
138 white_space='nowrap',
139 overflow='hidden',
140 text_overflow='ellipsis'
141 )
142 ans += build_rule(sel + ' .details-authors',
143 display='block',
144 margin_top='0.1rem',
145 opacity='0.88',
146 white_space='nowrap',
147 overflow='hidden',
148 text_overflow='ellipsis'
149 )
150 ans += build_rule(sel + ' .details-comments',
151 min_height='4ex',
152 margin_top='0.65ex',
153 height='0',
154 border='none',
155 flex_grow='1',
156 )
157
158 # Wrap badges (~2 rows): flex cannot use line-clamp; cap height and clip (align-content top).
159 ans += sel + ''' .details-meta {
160 margin-top: 0.35ex;
161 display: flex;
162 flex-wrap: wrap;
163 align-content: flex-start;
164 gap: 0.35rem 0.6rem;
165 opacity: 0.78;
166 font-size: 0.92em;
167 line-height: 1.25;
168 overflow: hidden;
169 flex: 0 0 auto;
170 min-width: 0;
171 max-width: 100%;
172 max-height: calc(2 * (1.25em + 0.32rem) + 0.35rem);
173 }
174 '''
175 ans += build_rule(sel + ' .details-meta:empty',
176 display='none', margin_top='0'
177 )
178 ans += build_rule(sel + ' .details-badge',
179 display='inline-flex',
180 align_items='center',
181 gap='0.25rem',
182 padding='0.15rem 0.45rem',
183 border_radius='999px',
184 border='var(--calibre-border-style-subtle)',
185 background='var(--calibre-color-surface-3)',
186 max_width='100%',
187 white_space='nowrap',
188 overflow='hidden',
189 text_overflow='ellipsis'
190 )
191 ans += build_rule(sel + ' .details-series',
192 display='block',
193 white_space='nowrap',
194 overflow='hidden',
195 text_overflow='ellipsis',
196 max_width='100%',
197 )
198 ans += build_rule(sel + ' .details-rating',
199 display='block',
200 white_space='nowrap',
201 overflow='hidden',
202 text_overflow='ellipsis',
203 max_width='100%',
204 )
205 top = sel + ' .details-main-head'
206 extra_data = top + ' > div:last-child'
207 primary = sel + ' .details-head-primary'
208 ans += build_rule(extra_data,
209 text_align='right',
210 flex='0 3 auto',
211 min_width='0',
212 max_width='min(18rem, 48%)',
213 overflow_x='hidden',
214 overflow_y='visible',
215 box_sizing='border-box',
216 )
217 # Narrow viewports: stack [title+meta] above rating/series. Shrink order: comments > extra > meta > title (via flex-shrink).
218 ans += r'''
219 @media (max-width: 640px) {
220 __TOP__ {
221 flex-direction: column;
222 align-items: stretch;
223 gap: 0.5rem;
224 }
225 __EXTRA__ {
226 text-align: left;
227 margin-top: 0.15rem;
228 align-self: stretch;
229 max-width: 100%;
230 flex: 0 3 auto;
231 }
232 __PRIMARY__ {
233 max-width: 100%;
234 flex: 0 1 auto;
235 min-height: 0;
236 }
237 __TA__ {
238 max-width: 100%;
239 }
240 }
241 '''.replace('__TOP__', top).replace('__EXTRA__', extra_data).replace('__PRIMARY__', primary).replace('__TA__', sel + ' .details-title-authors')
242 return ans
243
244
245 def init(container):
246 clear(container)
247 container.appendChild(E.div(class_=DETAILS_LIST_CLASS))
248
249
250 def on_img_load(img, load_type):
251 div = img.parentNode
252 if not div:
253 return
254 if load_type is not 'load':
255 clear(div)
256 div.appendChild(E.div(
257 E.h2(img.dataset.title, style='text-align:center; font-size:larger; font-weight: bold'),
258 E.div(_('by'), style='text-align: center'),
259 E.h2(img.dataset.authors, style='text-align:center; font-size:larger; font-weight: bold')
260 ))
261 set_css(div, border='dashed 1px currentColor', border_radius=BORDER_RADIUS + 'px')
262
263
264 def sandbox_css():
265 is_dark_theme = effective_book_list_is_dark()
266 if not sandbox_css.ans or sandbox_css.is_dark_theme is not is_dark_theme:
267 sandbox_css.ans = css_for_variables() + '\n\n'
268 sandbox_css.ans += 'html {{ overflow: hidden; color: {} }}'.format(get_color('window-foreground'))
269 sandbox_css.is_dark_theme = is_dark_theme
270 return sandbox_css.ans
271
272
273 def create_item(book_id, metadata, create_image, show_book_details, href):
274 authors = metadata.authors.join(' & ') if metadata.authors else _('Unknown')
275 img = create_image(book_id, THUMBNAIL_MAX_WIDTH, THUMBNAIL_MAX_HEIGHT, on_img_load)
276 img.setAttribute('alt', _('{} by {}').format(metadata.title, authors))
277 img.dataset.title, img.dataset.authors = metadata.title, authors
278 img_div = E.div(img, class_='details-list-left')
279 extra_data = E.div()
280 comments = sandboxed_html(metadata.comments, sandbox_css())
281 comments.classList.add('details-comments')
282 if not metadata.comments:
283 comments.style.display = 'none'
284 interface_data = get_interface_data()
285 if metadata.rating:
286 stars = E.span(class_='details-rating')
287 for i in range(int(metadata.rating) // 2):
288 stars.appendChild(svgicon('star'))
289 extra_data.appendChild(stars), extra_data.appendChild(E.br())
290 if metadata.series:
291 try:
292 ival = float(metadata.series_index)
293 except Exception:
294 ival = 1.0
295 ival = fmt_sidx(ival, use_roman=interface_data.use_roman_numerals_for_series_number)
296 extra_data.appendChild(safe_set_inner_html(E.span(class_='details-series'), _('{0} of <i>{1}</i>').format(ival, metadata.series)))
297
298 # Extra metadata line (publisher / published / formats / languages / tags)
299 meta = E.div(class_='details-meta')
300
301 def add_badge(icon, text, title=None):
302 if not text:
303 return
304 b = E.span(class_='details-badge')
305 if icon:
306 b.appendChild(svgicon(icon))
307 b.appendChild(E.span(text))
308 if title:
309 b.title = title
310 meta.appendChild(b)
311
312 if metadata.pubdate:
313 try:
314 add_badge('date', format_date(metadata.pubdate, 'yyyy-MM-dd'), _('Published'))
315 except Exception:
316 pass
317 fmts = metadata.formats or []
318 if fmts and fmts.length:
319 add_badge('book', fmts.slice(0, 4).join(' · '), _('Formats'))
320 langs = metadata.lang_names or metadata.languages or []
321 if langs and langs.length:
322 add_badge('global-search', langs.slice(0, 3).join(' · '), _('Languages'))
323 tgs = metadata.tags or []
324 if tgs and tgs.length:
325 for tg in tgs.slice(0, 4):
326 add_badge('tags', tg, _('Tag'))
327 if metadata.publisher:
328 add_badge('library', metadata.publisher, _('Publisher'))
329
330 right = E.div(
331 class_='details-list-right',
332 E.div(
333 class_='details-main-head',
334 E.div(
335 class_='details-head-primary',
336 E.div(
337 class_='details-title-authors',
338 E.b(metadata.title or _('Unknown'), class_='details-title'),
339 E.span(authors, class_='details-authors'),
340 ),
341 meta,
342 ),
343 extra_data
344 ),
345 comments,
346 )
347 ans = E.div(E.a(img_div, right, href=href,
348 style=f'min-height:{THUMBNAIL_MAX_HEIGHT}px; display: flex',
349 class_=ITEM_CLASS,
350 ))
351 ans.addEventListener('click', show_book_details, True)
352 return ans
353
354
355 def append_item(container, item):
356 container.lastChild.appendChild(item)