| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
# globals: NodeFilter |
| 4 |
from __python__ import bound_methods, hash_literals |
| 5 |
|
| 6 |
from elementmaker import E |
| 7 |
from gettext import gettext as _ |
| 8 |
|
| 9 |
from book_list.details_list import sandbox_css, BORDER_RADIUS |
| 10 |
from book_list.library_data import library_data |
| 11 |
from date import format_date |
| 12 |
from dom import build_rule, clear, set_css, svgicon |
| 13 |
from session import get_interface_data |
| 14 |
from utils import fmt_sidx, safe_set_inner_html, sandboxed_html |
| 15 |
|
| 16 |
CUSTOM_LIST_CLASS = 'book-list-custom-list' |
| 17 |
ITEM_CLASS = CUSTOM_LIST_CLASS + '-item' |
| 18 |
|
| 19 |
|
| 20 |
def description(): |
| 21 |
return _('A customizable list (see Preferences->Sharing over the net->Book list template)') |
| 22 |
|
| 23 |
|
| 24 |
def custom_list_css(): |
| 25 |
ans = '' |
| 26 |
sel = '.' + CUSTOM_LIST_CLASS |
| 27 |
ans += build_rule(sel, cursor='pointer', user_select='none') |
| 28 |
sel += ' .' + ITEM_CLASS |
| 29 |
ans += build_rule(sel, |
| 30 |
margin='1ex 1em', |
| 31 |
padding_bottom='1ex', |
| 32 |
border_bottom='solid 1px currentColor', |
| 33 |
min_width='0', |
| 34 |
max_width='100%', |
| 35 |
box_sizing='border-box', |
| 36 |
overflow='hidden', |
| 37 |
) |
| 38 |
ans += build_rule(sel + ':hover', text_decoration='none') |
| 39 |
# No hover zoom; use highlight instead |
| 40 |
ans += build_rule(f'{sel}:hover', background_color='var(--calibre-color-primary-light)', border_radius='14px') |
| 41 |
ans += build_rule(f'{sel}:active', background_color='var(--calibre-color-primary-light-2)') |
| 42 |
ans += build_rule(f'{sel}:hover .custom-list-left > img', box_shadow='0 10px 24px rgba(0,0,0,0.14)') |
| 43 |
s = sel + ' .custom-list-left' |
| 44 |
ans += build_rule(s, margin_right='1em') |
| 45 |
ans += build_rule(s + ' > img', border_radius=BORDER_RADIUS + 'px', transition='box-shadow 160ms ease') |
| 46 |
sel += ' iframe' |
| 47 |
# To enable clicking anywhere on the item to load book details to work, we |
| 48 |
# have to set pointer-events: none |
| 49 |
# That has the side effect of disabling text selection |
| 50 |
ans += build_rule(sel, flex_grow='10', cursor='pointer', pointer_events='none') |
| 51 |
return ans |
| 52 |
|
| 53 |
|
| 54 |
def default_template(): |
| 55 |
# Should never actually be needed |
| 56 |
if not default_template.ans: |
| 57 |
default_template.ans = { |
| 58 |
'thumbnail': True, |
| 59 |
'thumbnail_height': 140, |
| 60 |
'height': 'auto', |
| 61 |
'comments_fields': ['comments'], |
| 62 |
'lines': [ |
| 63 |
_('<b>{title}</b> by {authors}'), |
| 64 |
_('{series_index} of <i>{series}</i>') + '|||{rating}', |
| 65 |
'{tags}', |
| 66 |
_('Date: {timestamp}') + '|||' + _('Published: {pubdate}') + '|||' + _('Publisher: {publisher}'), |
| 67 |
'', |
| 68 |
] |
| 69 |
} |
| 70 |
return default_template.ans |
| 71 |
|
| 72 |
|
| 73 |
def render_field(field, mi, book_id): # {{{ |
| 74 |
if field is 'id': |
| 75 |
return book_id + '' |
| 76 |
field_metadata = library_data.field_metadata |
| 77 |
if not field_metadata: |
| 78 |
return |
| 79 |
fm = field_metadata[field] |
| 80 |
if not fm or not mi: |
| 81 |
return |
| 82 |
val = mi[field] |
| 83 |
if val is undefined or val is None: |
| 84 |
return |
| 85 |
interface_data = get_interface_data() |
| 86 |
|
| 87 |
def add_val(val, is_html=False, join=None): |
| 88 |
if is_html and /[<>]/.test(val + ''): |
| 89 |
return safe_set_inner_html(E.span(), val) |
| 90 |
if join: |
| 91 |
val = val.join(join) |
| 92 |
else: |
| 93 |
val += '' |
| 94 |
return val |
| 95 |
|
| 96 |
def process_composite(field, fm, name, val): |
| 97 |
if fm.display and fm.display.contains_html: |
| 98 |
return add_val(val, is_html=True) |
| 99 |
if fm.is_multiple and fm.is_multiple.list_to_ui: |
| 100 |
all_vals = filter(None, map(str.strip, val.split(fm.is_multiple.list_to_ui))) |
| 101 |
return add_val(all_vals, join=fm.is_multiple.list_to_ui) |
| 102 |
return add_val(val) |
| 103 |
|
| 104 |
def process_authors(field, fm, name, val): |
| 105 |
return add_val(val, join=' & ') |
| 106 |
|
| 107 |
def process_publisher(field, fm, name, val): |
| 108 |
return add_val(val) |
| 109 |
|
| 110 |
def process_formats(field, fm, name, val): |
| 111 |
return add_val(val, join=', ') |
| 112 |
|
| 113 |
def process_rating(field, fm, name, val): |
| 114 |
stars = E.span() |
| 115 |
val = int(val or 0) |
| 116 |
if val > 0: |
| 117 |
for i in range(val // 2): |
| 118 |
stars.appendChild(svgicon('star')) |
| 119 |
if fm.display.allow_half_stars and (val % 2): |
| 120 |
stars.appendChild(svgicon('star-half')) |
| 121 |
return stars |
| 122 |
|
| 123 |
def process_identifiers(field, fm, name, val): |
| 124 |
if val: |
| 125 |
keys = Object.keys(val) |
| 126 |
if keys.length: |
| 127 |
ans = [] |
| 128 |
for key in keys: |
| 129 |
ans.push(key + ':' + val[key]) |
| 130 |
return add_val(ans, join=', ') |
| 131 |
|
| 132 |
def process_languages(field, fm, name, val): |
| 133 |
if val and val.length: |
| 134 |
langs = [mi.lang_names[k] for k in val] |
| 135 |
return add_val(langs, join=', ') |
| 136 |
|
| 137 |
def process_datetime(field, fm, name, val): |
| 138 |
if val: |
| 139 |
fmt = interface_data['gui_' + field + '_display_format'] or (fm['display'] or {}).date_format |
| 140 |
return add_val(format_date(val, fmt)) |
| 141 |
|
| 142 |
def process_series(field, fm, name, val): |
| 143 |
if val: |
| 144 |
return add_val(val) |
| 145 |
|
| 146 |
def process_series_index(field, fm, name, val): |
| 147 |
sval = mi[field[:-6]] |
| 148 |
if sval: |
| 149 |
if val is None or val is undefined: |
| 150 |
val = 1 |
| 151 |
return fmt_sidx(val, use_roman=interface_data.use_roman_numerals_for_series_number) |
| 152 |
|
| 153 |
def process_size(field, fm, name, val): |
| 154 |
val = val or 0 |
| 155 |
mb = 1024 * 1024 |
| 156 |
|
| 157 |
def fmt(val, suffix): |
| 158 |
ans = f'{val:.1f}' |
| 159 |
if ans.endsWith('.0'): |
| 160 |
ans = ans[:-2] |
| 161 |
return ans + suffix |
| 162 |
|
| 163 |
if val < mb: |
| 164 |
return fmt(val / 1024, 'KB') |
| 165 |
return fmt(val / mb, 'MB') |
| 166 |
|
| 167 |
name = fm.name or field |
| 168 |
datatype = fm.datatype |
| 169 |
if field is 'comments': |
| 170 |
return |
| 171 |
if datatype is 'composite': |
| 172 |
func = process_composite |
| 173 |
elif field is 'formats': |
| 174 |
func = process_formats |
| 175 |
elif datatype is 'rating': |
| 176 |
func = process_rating |
| 177 |
elif field is 'identifiers': |
| 178 |
func = process_identifiers |
| 179 |
elif field is 'authors': |
| 180 |
func = process_authors |
| 181 |
elif field is 'publisher': |
| 182 |
func = process_publisher |
| 183 |
elif field is 'languages': |
| 184 |
func = process_languages |
| 185 |
elif datatype is 'datetime': |
| 186 |
func = process_datetime |
| 187 |
elif datatype is 'series': |
| 188 |
func = process_series |
| 189 |
elif datatype is 'comments': |
| 190 |
if fm.display?.interpret_as is 'short-text': |
| 191 |
return add_val(val) |
| 192 |
else: |
| 193 |
return |
| 194 |
elif field.endswith('_index'): |
| 195 |
func = process_series_index |
| 196 |
elif field is 'size': |
| 197 |
func = process_size |
| 198 |
ans = None |
| 199 |
if func: |
| 200 |
ans = func(field, fm, name, val) |
| 201 |
else: |
| 202 |
if datatype is 'text' or datatype is 'enumeration': |
| 203 |
if val is not undefined and val is not None: |
| 204 |
join = fm.is_multiple.list_to_ui if fm.is_multiple else None |
| 205 |
ans = add_val(val, join=join) |
| 206 |
elif datatype is 'bool': |
| 207 |
ans = add_val(_('Yes') if val else _('No')) |
| 208 |
elif datatype is 'int' or datatype is 'float': |
| 209 |
if val is not undefined and val is not None: |
| 210 |
fmt = (fm.display or {}).number_format |
| 211 |
if fmt: |
| 212 |
val = fmt.format(val) |
| 213 |
else: |
| 214 |
val += '' |
| 215 |
ans = add_val(val) |
| 216 |
return ans |
| 217 |
# }}} |
| 218 |
|
| 219 |
|
| 220 |
def render_part(part, template, book_id, metadata): |
| 221 |
count = rendered_count = 0 |
| 222 |
ans = E.div() |
| 223 |
ans.innerHTML = part |
| 224 |
iterator = document.createNodeIterator(ans, NodeFilter.SHOW_TEXT) |
| 225 |
replacements = [] |
| 226 |
while True: |
| 227 |
n = iterator.nextNode() |
| 228 |
if not n: |
| 229 |
break |
| 230 |
rendered = E.span() |
| 231 |
for field in n.nodeValue.split(/({#?[_a-z0-9]+})/): |
| 232 |
if field[0] is '{' and field[-1] is '}': |
| 233 |
count += 1 |
| 234 |
val = render_field(field[1:-1], metadata, book_id) |
| 235 |
if val: |
| 236 |
rendered_count += 1 |
| 237 |
if jstype(val) is 'string': |
| 238 |
val = document.createTextNode(val) |
| 239 |
rendered.appendChild(val) |
| 240 |
else: |
| 241 |
rendered.appendChild(document.createTextNode(field)) |
| 242 |
replacements.push([rendered, n]) |
| 243 |
|
| 244 |
for new_child, old_child in replacements: |
| 245 |
old_child.parentNode.replaceChild(new_child, old_child) |
| 246 |
|
| 247 |
if count and not rendered_count: |
| 248 |
return |
| 249 |
return ans |
| 250 |
|
| 251 |
|
| 252 |
def render_line(line, template, book_id, metadata): |
| 253 |
parts = [] |
| 254 |
line = line or '\xa0' |
| 255 |
for p in line.split(/\|\|\|/): |
| 256 |
part = render_part(p, template, book_id, metadata) |
| 257 |
if part: |
| 258 |
parts.push(part) |
| 259 |
if not parts.length: |
| 260 |
return |
| 261 |
ans = E.div(class_='custom-line') |
| 262 |
for p in parts: |
| 263 |
ans.appendChild(p) |
| 264 |
if parts.length > 1: |
| 265 |
set_css(ans, display='flex', justify_content='space-between') |
| 266 |
return ans |
| 267 |
|
| 268 |
|
| 269 |
def render_template_text(template, book_id, metadata): |
| 270 |
ans = E.div() |
| 271 |
for line in template.lines: |
| 272 |
ldiv = render_line(line, template, book_id, metadata) |
| 273 |
if ldiv: |
| 274 |
ans.appendChild(ldiv) |
| 275 |
if template.comments_fields.length: |
| 276 |
html = '' |
| 277 |
for f in template.comments_fields: |
| 278 |
val = metadata[f] |
| 279 |
if val: |
| 280 |
html += f'<div style="margin-bottom:1.5ex">{val}</div>' |
| 281 |
|
| 282 |
if html: |
| 283 |
comments = sandboxed_html(html, sandbox_css()) |
| 284 |
ans.appendChild(comments) |
| 285 |
return ans |
| 286 |
|
| 287 |
|
| 288 |
def init(container): |
| 289 |
clear(container) |
| 290 |
container.appendChild(E.div(class_=CUSTOM_LIST_CLASS)) |
| 291 |
|
| 292 |
|
| 293 |
def on_img_load(img, load_type): |
| 294 |
div = img.parentNode |
| 295 |
if not div: |
| 296 |
return |
| 297 |
if load_type is not 'load': |
| 298 |
clear(div) |
| 299 |
div.appendChild(E.div( |
| 300 |
E.h2(img.dataset.title, style='text-align:center; font-size:larger; font-weight: bold'), |
| 301 |
E.div(_('by'), style='text-align: center'), |
| 302 |
E.h2(img.dataset.authors, style='text-align:center; font-size:larger; font-weight: bold') |
| 303 |
)) |
| 304 |
set_css(div, border='dashed 1px currentColor', border_radius=BORDER_RADIUS + 'px') |
| 305 |
|
| 306 |
|
| 307 |
def create_item(book_id, metadata, create_image, show_book_details, href): |
| 308 |
template = get_interface_data().custom_list_template or default_template() |
| 309 |
text_data = render_template_text(template, book_id, metadata) |
| 310 |
text_data.style.flexGrow = '10' |
| 311 |
text_data.style.overflow = 'hidden' |
| 312 |
if template.thumbnail: |
| 313 |
height = f'{template.thumbnail_height}px' |
| 314 |
else: |
| 315 |
if template.height is 'auto': |
| 316 |
extra = 5 if template.comments_fields.length else 1 |
| 317 |
height = (template.lines.length * 2.5 + extra) + 'ex' |
| 318 |
else: |
| 319 |
height = template.height |
| 320 |
if jstype(height) is 'number': |
| 321 |
height += 'px' |
| 322 |
ans = E.a( |
| 323 |
style=f'height:{height}; display: flex', |
| 324 |
class_=ITEM_CLASS, href=href |
| 325 |
) |
| 326 |
if template.thumbnail: |
| 327 |
h = template.thumbnail_height |
| 328 |
w = int(0.75 * h) |
| 329 |
img = create_image(book_id, w, h, on_img_load) |
| 330 |
authors = metadata.authors.join(' & ') if metadata.authors else _('Unknown') |
| 331 |
img.setAttribute('alt', _('{} by {}').format(metadata.title, authors)) |
| 332 |
img.dataset.title, img.dataset.authors = metadata.title, authors |
| 333 |
img.style.maxWidth = w + 'px' |
| 334 |
img.style.maxHeight = h + 'px' |
| 335 |
img_div = E.div(img, class_='custom-list-left', style=f'min-width: {w}px') |
| 336 |
ans.appendChild(img_div) |
| 337 |
ans.appendChild(text_data) |
| 338 |
ans.addEventListener('click', show_book_details, True) |
| 339 |
return ans |
| 340 |
|
| 341 |
|
| 342 |
def append_item(container, item): |
| 343 |
container.lastChild.appendChild(item) |