| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals, bound_methods |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from book_list.globals import get_session_data |
| 9 |
from dom import clear, set_css, build_rule |
| 10 |
|
| 11 |
|
| 12 |
COVER_GRID_CLASS = 'book-list-cover-grid' |
| 13 |
# Default tile width as % of viewport (must stay in sync with session default cover_grid_tile_pct). |
| 14 |
COVER_GRID_VIEWPORT_FRAC = 0.165 |
| 15 |
COVER_GRID_PCT_DEFAULT = Math.round(100 * COVER_GRID_VIEWPORT_FRAC) |
| 16 |
# Viewport percentage (slider): ~8% = many columns, ~45% = large tiles / few columns. |
| 17 |
COVER_GRID_PCT_MIN = 8 |
| 18 |
COVER_GRID_PCT_MAX = 45 |
| 19 |
|
| 20 |
|
| 21 |
THUMBNAIL_MAX_WIDTH = 3 * 85 |
| 22 |
THUMBNAIL_MAX_HEIGHT = int(THUMBNAIL_MAX_WIDTH * 3 / 2) |
| 23 |
# Slightly lower floor so small containers get more slider range before hitting a px plateau. |
| 24 |
THUMBNAIL_MIN_WIDTH = 3 * 16 |
| 25 |
THUMBNAIL_MIN_HEIGHT = int(THUMBNAIL_MIN_WIDTH * 3 / 2) |
| 26 |
BORDER_RADIUS = 10 |
| 27 |
# Row gap (vertical). Column gap is computed per width so tile width stays at slider size |
| 28 |
# column gap is clamped to [ROW_GAP, tile_width]. |
| 29 |
COVER_GRID_ROW_GAP = 10 |
| 30 |
|
| 31 |
|
| 32 |
def cover_grid_column_layout(container_w, tile_px, row_gap): |
| 33 |
"""Pick column count and horizontal gap: n*T + (n-1)*g == W, row_gap <= g <= tile_px.""" |
| 34 |
W = float(container_w) |
| 35 |
T = float(tile_px) |
| 36 |
R = float(row_gap) |
| 37 |
if W < 32 or T < 1: |
| 38 |
return 1, 0 |
| 39 |
# Max columns if gaps were at least R (each extra column adds T+R except first T). |
| 40 |
n_hi = int(Math.floor((W + R) / (T + R))) |
| 41 |
n_cap = int(Math.floor(W / T)) |
| 42 |
if n_hi > n_cap: |
| 43 |
n_hi = n_cap |
| 44 |
if n_hi < 1: |
| 45 |
n_hi = 1 |
| 46 |
n = 1 |
| 47 |
g = 0.0 |
| 48 |
if n_hi >= 2: |
| 49 |
cand = n_hi |
| 50 |
while cand >= 2: |
| 51 |
g_try = (W - cand * T) / (cand - 1) |
| 52 |
if g_try + 1e-6 >= R and g_try <= T + 1e-6: |
| 53 |
n = cand |
| 54 |
g = g_try |
| 55 |
break |
| 56 |
cand -= 1 |
| 57 |
return n, g |
| 58 |
|
| 59 |
|
| 60 |
def get_cover_grid_pct(): |
| 61 |
raw = get_session_data().get('cover_grid_tile_pct', COVER_GRID_PCT_DEFAULT) |
| 62 |
if jstype(raw) is 'number': |
| 63 |
n = raw |
| 64 |
else: |
| 65 |
n = parseFloat(str(raw)) |
| 66 |
if n != n: |
| 67 |
n = COVER_GRID_PCT_DEFAULT |
| 68 |
return Math.max(COVER_GRID_PCT_MIN, Math.min(COVER_GRID_PCT_MAX, n)) |
| 69 |
|
| 70 |
|
| 71 |
def sync_cover_grid_tile_px(): |
| 72 |
"""Fixed tile width from slider; column count + horizontal gap fill row (gap in [row, tile]).""" |
| 73 |
grid = document.querySelector('.' + COVER_GRID_CLASS) |
| 74 |
if not grid: |
| 75 |
return |
| 76 |
cw = grid.clientWidth |
| 77 |
if cw < 32: |
| 78 |
return |
| 79 |
pct = get_cover_grid_pct() |
| 80 |
px = Math.round(cw * pct / 100) |
| 81 |
px = Math.max(THUMBNAIL_MIN_WIDTH, Math.min(THUMBNAIL_MAX_WIDTH, px)) |
| 82 |
grid.style.setProperty('--cs-cover-tile-px', str(px) + 'px') |
| 83 |
n_cols, col_gap = cover_grid_column_layout(cw, px, COVER_GRID_ROW_GAP) |
| 84 |
grid.style.gridTemplateColumns = 'repeat(' + str(n_cols) + ', ' + str(px) + 'px)' |
| 85 |
grid.style.rowGap = str(COVER_GRID_ROW_GAP) + 'px' |
| 86 |
if n_cols > 1: |
| 87 |
grid.style.columnGap = str(col_gap) + 'px' |
| 88 |
else: |
| 89 |
grid.style.columnGap = '0px' |
| 90 |
|
| 91 |
|
| 92 |
def apply_cover_grid_pct_css(): |
| 93 |
document.documentElement.style.setProperty('--cs-cover-grid-pct', str(get_cover_grid_pct())) |
| 94 |
sync_cover_grid_tile_px() |
| 95 |
raf = window.requestAnimationFrame |
| 96 |
if raf: |
| 97 |
raf(sync_cover_grid_tile_px) |
| 98 |
else: |
| 99 |
window.setTimeout(sync_cover_grid_tile_px, 0) |
| 100 |
|
| 101 |
|
| 102 |
def ensure_cover_grid_resize_listener(): |
| 103 |
if window.cs_cover_grid_resize_bound: |
| 104 |
return |
| 105 |
window.cs_cover_grid_resize_bound = True |
| 106 |
|
| 107 |
def on_resize(ev): |
| 108 |
apply_cover_grid_pct_css() |
| 109 |
|
| 110 |
window.addEventListener('resize', on_resize, False) |
| 111 |
|
| 112 |
|
| 113 |
def attach_cover_grid_resize_observer(grid): |
| 114 |
# Disconnect any previous observer so we don't accumulate observers across init() calls. |
| 115 |
if window.cs_cover_grid_resize_observer: |
| 116 |
window.cs_cover_grid_resize_observer.disconnect() |
| 117 |
window.cs_cover_grid_resize_observer = None |
| 118 |
if not window.ResizeObserver: |
| 119 |
return |
| 120 |
|
| 121 |
def on_observed(entries): |
| 122 |
# Throttle to one sync per animation frame (the sidebar CSS transition fires many |
| 123 |
# resize notifications); reuse the same flag as append_item for consistency. |
| 124 |
if window.cs_cover_grid_tile_sync_scheduled: |
| 125 |
return |
| 126 |
window.cs_cover_grid_tile_sync_scheduled = True |
| 127 |
|
| 128 |
def flush(): |
| 129 |
window.cs_cover_grid_tile_sync_scheduled = False |
| 130 |
sync_cover_grid_tile_px() |
| 131 |
|
| 132 |
raf = window.requestAnimationFrame |
| 133 |
if raf: |
| 134 |
raf(flush) |
| 135 |
else: |
| 136 |
window.setTimeout(flush, 0) |
| 137 |
|
| 138 |
obs = new window.ResizeObserver(on_observed) |
| 139 |
obs.observe(grid) |
| 140 |
window.cs_cover_grid_resize_observer = obs |
| 141 |
|
| 142 |
|
| 143 |
def on_cover_grid_size_input(ev): |
| 144 |
v = parseFloat(ev.target.value) |
| 145 |
if v != v: |
| 146 |
return |
| 147 |
v = Math.max(COVER_GRID_PCT_MIN, Math.min(COVER_GRID_PCT_MAX, v)) |
| 148 |
v = Math.round(v * 40) / 40 |
| 149 |
get_session_data().set('cover_grid_tile_pct', v) |
| 150 |
apply_cover_grid_pct_css() |
| 151 |
|
| 152 |
|
| 153 |
def sync_cover_grid_size_control(control, mode): |
| 154 |
if not control: |
| 155 |
return |
| 156 |
r = control.querySelector('.cs-cover-grid-size-range') |
| 157 |
if r: |
| 158 |
r.value = str(get_cover_grid_pct()) |
| 159 |
if mode is 'cover_grid': |
| 160 |
ensure_cover_grid_resize_listener() |
| 161 |
apply_cover_grid_pct_css() |
| 162 |
set_css(control, display='inline-flex') |
| 163 |
else: |
| 164 |
set_css(control, display='none') |
| 165 |
|
| 166 |
|
| 167 |
def create_cover_grid_size_control(mode): |
| 168 |
lab = E.span(class_='cs-cover-grid-size-label') |
| 169 |
lab.textContent = _('Cover size') |
| 170 |
r = E.input(type='range', class_='cs-cover-grid-size-range', min=str(COVER_GRID_PCT_MIN), max=str(COVER_GRID_PCT_MAX), step='0.025') |
| 171 |
r.value = str(get_cover_grid_pct()) |
| 172 |
r.title = _('Adjust cover grid tile size') |
| 173 |
r.addEventListener('input', on_cover_grid_size_input) |
| 174 |
control = E.div(lab, r, class_='cs-cover-grid-size-control') |
| 175 |
control.setAttribute('role', 'group') |
| 176 |
control.setAttribute('aria-label', _('Cover grid size')) |
| 177 |
sync_cover_grid_size_control(control, mode) |
| 178 |
return control |
| 179 |
|
| 180 |
|
| 181 |
def cover_grid_thumb_size(): |
| 182 |
# Match CSS: width is pct of .book-list-cover-grid container (not full viewport). |
| 183 |
frac = get_cover_grid_pct() / 100 |
| 184 |
grid = document.querySelector('.' + COVER_GRID_CLASS) |
| 185 |
base = grid.clientWidth if grid and grid.clientWidth > 40 else 0 |
| 186 |
if not base: |
| 187 |
base = window.innerWidth |
| 188 |
w = Math.round(base * frac) |
| 189 |
h = Math.round(w * 3 / 2) |
| 190 |
|
| 191 |
# Clamp to match CSS max/min constraints. |
| 192 |
w = Math.max(THUMBNAIL_MIN_WIDTH, Math.min(w, THUMBNAIL_MAX_WIDTH)) |
| 193 |
h = Math.max(THUMBNAIL_MIN_HEIGHT, Math.min(h, THUMBNAIL_MAX_HEIGHT)) |
| 194 |
return w, h |
| 195 |
|
| 196 |
|
| 197 |
def description(): |
| 198 |
return _('A grid of book covers') |
| 199 |
|
| 200 |
|
| 201 |
def cover_grid_css(): |
| 202 |
grid_root = '.' + COVER_GRID_CLASS |
| 203 |
sel = grid_root |
| 204 |
item_sel = sel + ' > a' |
| 205 |
margin_unit = 'px' |
| 206 |
ans = ':root { --cs-cover-grid-pct: %s; }\n' % COVER_GRID_PCT_DEFAULT |
| 207 |
# NOTE: overflow must be visible so cover shadows are not clipped. |
| 208 |
# Tile px, column count, and column-gap are set from JS (sync_cover_grid_tile_px). |
| 209 |
ans += '.book-list-cover-grid { --cs-cover-tile-px: 120px; }\n' |
| 210 |
ans += build_rule(sel, display='grid', |
| 211 |
justify_items='stretch', align_items='end', justify_content='start', |
| 212 |
user_select='none', overflow='visible', |
| 213 |
position='relative', |
| 214 |
margin_top='5' + margin_unit, |
| 215 |
row_gap=str(COVER_GRID_ROW_GAP) + margin_unit, |
| 216 |
column_gap='0' + margin_unit, |
| 217 |
width='100%', min_width='0', max_width='100%', box_sizing='border-box') |
| 218 |
ans += build_rule( |
| 219 |
'.book-list-container .cs-book-list-wrap, .local-books-list .cs-book-list-wrap', |
| 220 |
width='100%', min_width='0', max_width='100%', box_sizing='border-box') |
| 221 |
ans += build_rule( |
| 222 |
'.book-list-container [data-component="book_list"], .local-books-list [data-component="book_list"]', |
| 223 |
display='block', width='100%', min_width='0', max_width='100%', box_sizing='border-box') |
| 224 |
|
| 225 |
# Grid cell stretches horizontally; min column width follows --cs-cover-tile-px (slider / resize). |
| 226 |
sel = item_sel |
| 227 |
ans += build_rule( |
| 228 |
sel, |
| 229 |
margin='0', |
| 230 |
display='block', |
| 231 |
width='100%', |
| 232 |
min_width='0', |
| 233 |
max_width='100%', |
| 234 |
height='auto', |
| 235 |
aspect_ratio='2 / 3', |
| 236 |
cursor='pointer', |
| 237 |
position='relative', |
| 238 |
overflow='visible', |
| 239 |
box_sizing='border-box', |
| 240 |
transition='background-color 160ms ease, width 0.2s ease', |
| 241 |
container_type='inline-size', |
| 242 |
) |
| 243 |
# No hover zoom; use highlight instead |
| 244 |
ans += build_rule(f'{sel}:hover', background_color='var(--calibre-color-primary-light)') |
| 245 |
ans += build_rule(f'{sel}:active', background_color='var(--calibre-color-primary-light-2)') |
| 246 |
# Inner frame defines the real cover bounds (shadow + radius + mask alignment) |
| 247 |
ans += build_rule(f'{item_sel} > .cs-cover-frame', |
| 248 |
position='relative', |
| 249 |
width='100%', |
| 250 |
height='100%', |
| 251 |
overflow='hidden', |
| 252 |
border_radius=BORDER_RADIUS + 'px', |
| 253 |
box_shadow='0 clamp(10px, 1.8vh, 14px) clamp(26px, 4.2vh, 48px) rgba(0,0,0,0.18)', |
| 254 |
border='var(--calibre-border-style-subtle)', |
| 255 |
background_color='rgba(0,0,0,0.02)', |
| 256 |
transition='box-shadow 160ms ease, background-color 160ms ease, border-color 160ms ease' |
| 257 |
) |
| 258 |
ans += build_rule(f'{item_sel}:hover > .cs-cover-frame', |
| 259 |
background_color='var(--calibre-color-primary-light)', |
| 260 |
border_color='var(--calibre-color-border)', |
| 261 |
box_shadow='0 clamp(14px, 2.6vh, 18px) clamp(34px, 5.2vh, 62px) rgba(0,0,0,0.24), 0 0 0 4px var(--calibre-color-primary-light)' |
| 262 |
) |
| 263 |
|
| 264 |
# Cover overlay (title + author) |
| 265 |
ans += build_rule(f'{item_sel} .cs-cover-frame > .cs-cover-mask', |
| 266 |
position='absolute', left='0', right='0', bottom='0', |
| 267 |
padding='0.85rem 0.75rem 0.6rem 0.75rem', |
| 268 |
display='flex', flex_direction='column', gap='0.25rem', |
| 269 |
justify_content='flex-end', |
| 270 |
# Use full-width children for reliable ellipsis + right alignment. |
| 271 |
align_items='stretch', |
| 272 |
text_align='right', |
| 273 |
# Black transparent gradient (bottom -> top): darker bottom, smoother transition. |
| 274 |
background='linear-gradient(to top, rgba(0,0,0,0.97) 0%, rgba(0,0,0,0.88) 18%, rgba(0,0,0,0.62) 48%, rgba(0,0,0,0.20) 78%, rgba(0,0,0,0.0) 100%)', |
| 275 |
color='rgba(255,255,255,0.97)', |
| 276 |
text_shadow='0 2px 10px rgba(0,0,0,0.65)', |
| 277 |
pointer_events='none' |
| 278 |
) |
| 279 |
# hide the overlay when the cover is too narrow since it is useless |
| 280 |
ans += '''\n@container (max-width: 10ch) { |
| 281 |
.cs-cover-mask { display: none !important; } |
| 282 |
} |
| 283 |
''' |
| 284 |
ans += build_rule(f'{item_sel} .cs-cover-frame > .cs-cover-mask .cs-cover-mask-title', |
| 285 |
font_size='0.95rem', font_weight='800', |
| 286 |
width='100%', |
| 287 |
white_space='nowrap', overflow='hidden', text_overflow='ellipsis' |
| 288 |
) |
| 289 |
ans += build_rule(f'{item_sel} .cs-cover-frame > .cs-cover-mask .cs-cover-mask-authors', |
| 290 |
font_size='0.82rem', font_weight='600', opacity='0.92', |
| 291 |
width='100%', |
| 292 |
white_space='nowrap', overflow='hidden', text_overflow='ellipsis' |
| 293 |
) |
| 294 |
|
| 295 |
ans += build_rule(grid_root + ' > .cover-grid-filler', |
| 296 |
position='absolute', |
| 297 |
left='0', |
| 298 |
top='0', |
| 299 |
width='1px', |
| 300 |
height='1px', |
| 301 |
margin='0', |
| 302 |
padding='0', |
| 303 |
overflow='hidden', |
| 304 |
visibility='hidden', |
| 305 |
pointer_events='none', |
| 306 |
z_index='-1', |
| 307 |
) |
| 308 |
|
| 309 |
# Container for cover failed to load message |
| 310 |
ans += build_rule(sel + ' > div', position='relative', top='-50%', transform='translateY(50%)', margin='0') |
| 311 |
|
| 312 |
# The actual cover (fills the frame so mask aligns perfectly) |
| 313 |
ans += build_rule(f'{item_sel} .cs-cover-frame > img', |
| 314 |
width='100%', |
| 315 |
height='100%', |
| 316 |
display='block', |
| 317 |
object_fit='cover' |
| 318 |
) |
| 319 |
ans += build_rule('.cs-cover-grid-size-control', |
| 320 |
display='inline-flex', |
| 321 |
align_items='center', |
| 322 |
gap='0.65rem', |
| 323 |
flex='1 1 15rem', |
| 324 |
min_width='min(100%, 13rem)', |
| 325 |
max_width='24rem', |
| 326 |
box_sizing='border-box', |
| 327 |
) |
| 328 |
ans += build_rule('.cs-cover-grid-size-control .cs-cover-grid-size-label', |
| 329 |
font_size='0.88rem', |
| 330 |
font_weight='600', |
| 331 |
opacity='0.88', |
| 332 |
white_space='nowrap', |
| 333 |
flex_shrink='0', |
| 334 |
) |
| 335 |
ans += build_rule('.cs-cover-grid-size-control .cs-cover-grid-size-range', |
| 336 |
flex='1 1 auto', |
| 337 |
min_width='3rem', |
| 338 |
margin='0', |
| 339 |
) |
| 340 |
return ans |
| 341 |
|
| 342 |
|
| 343 |
def init(container): |
| 344 |
clear(container) |
| 345 |
container.appendChild(E.div(class_=COVER_GRID_CLASS)) |
| 346 |
for i in range(12): |
| 347 |
container.lastChild.appendChild(E.div(class_='cover-grid-filler')) |
| 348 |
ensure_cover_grid_resize_listener() |
| 349 |
attach_cover_grid_resize_observer(container.lastChild) |
| 350 |
apply_cover_grid_pct_css() |
| 351 |
|
| 352 |
|
| 353 |
def on_img_load(img, load_type): |
| 354 |
div = img.parentNode |
| 355 |
if not div: |
| 356 |
return |
| 357 |
if load_type is not 'load': |
| 358 |
clear(div) |
| 359 |
div.appendChild(E.div( |
| 360 |
E.h2(img.dataset.title, style='text-align:center; font-size:larger; font-weight: bold'), |
| 361 |
E.div(_('by'), style='text-align: center'), |
| 362 |
E.h2(img.dataset.authors, style='text-align:center; font-size:larger; font-weight: bold') |
| 363 |
)) |
| 364 |
set_css(div, border='dashed 1px currentColor', border_radius=BORDER_RADIUS + 'px') |
| 365 |
|
| 366 |
|
| 367 |
def create_item(book_id, metadata, create_image, show_book_details, href): |
| 368 |
authors = metadata.authors.join(' & ') if metadata.authors else _('Unknown') |
| 369 |
w, h = cover_grid_thumb_size() |
| 370 |
img = create_image(book_id, w, h, on_img_load) |
| 371 |
tooltip = _('{} by {}').format(metadata.title, authors) |
| 372 |
img.setAttribute('alt', tooltip) |
| 373 |
img.dataset.title, img.dataset.authors = metadata.title, authors |
| 374 |
mask = E.div( |
| 375 |
class_='cs-cover-mask', |
| 376 |
E.div(metadata.title or '', class_='cs-cover-mask-title'), |
| 377 |
E.div(authors or '', class_='cs-cover-mask-authors') |
| 378 |
) |
| 379 |
frame = E.div(img, mask, class_='cs-cover-frame') |
| 380 |
ans = E.a(frame, onclick=show_book_details, title=tooltip, href=href) |
| 381 |
return ans |
| 382 |
|
| 383 |
|
| 384 |
def append_item(container, item): |
| 385 |
first_filler = container.lastChild.querySelector('.cover-grid-filler') |
| 386 |
container.lastChild.insertBefore(item, first_filler) |
| 387 |
# Throttle: render_ids may append many tiles; one sync per frame is enough. |
| 388 |
if window.cs_cover_grid_tile_sync_scheduled: |
| 389 |
return |
| 390 |
window.cs_cover_grid_tile_sync_scheduled = True |
| 391 |
|
| 392 |
def flush(): |
| 393 |
window.cs_cover_grid_tile_sync_scheduled = False |
| 394 |
apply_cover_grid_pct_css() |
| 395 |
|
| 396 |
raf = window.requestAnimationFrame |
| 397 |
if raf: |
| 398 |
raf(flush) |
| 399 |
else: |
| 400 |
window.setTimeout(flush, 0) |