| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
import traceback |
| 6 |
from elementmaker import E |
| 7 |
from gettext import gettext as _ |
| 8 |
|
| 9 |
from ajax import ajax, ajax_send, encode_query_component |
| 10 |
from book_list.delete_book import refresh_after_delete, start_delete_book |
| 11 |
from book_list.globals import get_session_data |
| 12 |
from book_list.item_list import create_item, create_item_list, create_side_action |
| 13 |
from book_list.library_data import ( |
| 14 |
all_libraries, book_after, book_metadata, cover_url, current_library_id, |
| 15 |
current_virtual_library, download_url, library_data, load_status, |
| 16 |
set_book_metadata, download_data_file_url, web_search_url |
| 17 |
) |
| 18 |
from book_list.router import back, home, open_book, report_a_load_failure, show_note, open_book_url_in_library, search_url_in_library |
| 19 |
from book_list.theme import ( |
| 20 |
color_scheme, get_color, get_color_as_rgba |
| 21 |
) |
| 22 |
from book_list.top_bar import clear_buttons, create_top_bar, set_title |
| 23 |
from book_list.ui import query_as_href, set_panel_handler, show_panel |
| 24 |
from book_list.views import search_query_for |
| 25 |
from date import format_date |
| 26 |
from dom import add_extra_css, build_rule, clear, ensure_id, svgicon, unique_id |
| 27 |
from file_uploads import update_status_widget, upload_files_widget, upload_status_widget |
| 28 |
from modals import create_custom_dialog, error_dialog, warning_dialog, question_dialog |
| 29 |
from read_book.touch import ( |
| 30 |
copy_touch, install_handlers, interpret_single_gesture, touch_id, update_touch |
| 31 |
) |
| 32 |
from session import get_interface_data |
| 33 |
from utils import ( |
| 34 |
conditional_timeout, debounce, fmt_sidx, human_readable, parse_url_params, |
| 35 |
safe_set_inner_html, sandboxed_html |
| 36 |
) |
| 37 |
from widgets import create_button, create_spinner |
| 38 |
|
| 39 |
bd_counter = 0 |
| 40 |
|
| 41 |
CLASS_NAME = 'book-details-panel' |
| 42 |
SEARCH_INTERNET_CLASS = 'book-details-search-internet' |
| 43 |
COPY_TO_LIBRARY_CLASS = 'book-details-copy-to-library' |
| 44 |
DATA_FILES_CLASS = 'book-details-data-files' |
| 45 |
FORMAT_PRIORITIES = [ |
| 46 |
'EPUB', 'AZW3', 'DOCX', 'LIT', 'MOBI', 'ODT', 'RTF', 'MD', 'MARKDOWN', 'TXT', 'PDF' |
| 47 |
] |
| 48 |
|
| 49 |
|
| 50 |
def sort_formats_key(fmt): |
| 51 |
ans = FORMAT_PRIORITIES.indexOf(fmt) |
| 52 |
if ans < 0: |
| 53 |
ans = FORMAT_PRIORITIES.length |
| 54 |
return ans |
| 55 |
|
| 56 |
|
| 57 |
def get_preferred_format(metadata, output_format, input_formats, for_download): |
| 58 |
formats = (metadata and metadata.formats) or [] |
| 59 |
formats = [f.toUpperCase() for f in formats] |
| 60 |
fmt = 'EPUB' if output_format is 'PDF' else output_format |
| 61 |
if formats.length and formats.indexOf(fmt) is -1: |
| 62 |
found = False |
| 63 |
formats = sorted(formats, key=sort_formats_key) |
| 64 |
for q in formats: |
| 65 |
if input_formats[q]: |
| 66 |
fmt = q |
| 67 |
found = True |
| 68 |
break |
| 69 |
if for_download and not found and formats.length: |
| 70 |
fmt = formats[0] |
| 71 |
return fmt.toUpperCase() |
| 72 |
|
| 73 |
IGNORED_FIELDS = {'title', 'sort', 'uuid', 'id', 'urls_from_identifiers', 'lang_names', 'last_modified', 'path'} |
| 74 |
default_sort = { |
| 75 |
f: i + 1 for i, f in enumerate(( |
| 76 |
'title', 'title_sort', 'authors', 'author_sort', 'series', 'rating', 'pubdate', 'tags', \ |
| 77 |
'timestamp', 'pubdate', 'identifiers', 'languages', 'publisher', 'last_modified') |
| 78 |
)} |
| 79 |
default_sort['formats'] = 999 |
| 80 |
|
| 81 |
|
| 82 |
def field_sorter(field_metadata): |
| 83 |
return def (field): |
| 84 |
lvl = '{:03d}'.format(default_sort[field] or 998) |
| 85 |
fm = (field_metadata[field] or {})[field] or {} |
| 86 |
return lvl + (fm.name or 'zzzzz') |
| 87 |
|
| 88 |
|
| 89 |
def href_for_search(name, val, use_quotes=True): |
| 90 |
query = ('{}:"={}"' if use_quotes else '{}:{}').format(name, str.replace(val, '"', r'\"')) |
| 91 |
q = search_query_for(query) |
| 92 |
return query_as_href(q) |
| 93 |
|
| 94 |
|
| 95 |
def on_fmt_click(ev): |
| 96 |
fmt = ev.currentTarget.dataset.format |
| 97 |
book_id = int(ev.currentTarget.dataset.bookId) |
| 98 |
title, sz = this |
| 99 |
|
| 100 |
create_custom_dialog(title, def (parent, close_modal): |
| 101 |
def action(which): |
| 102 |
close_modal() |
| 103 |
which(book_id, fmt) |
| 104 |
if fmt.lower() is 'pdf': |
| 105 |
read_button = download_url(book_id, fmt, 'inline') |
| 106 |
else: |
| 107 |
read_button = action.bind(None, read_format) |
| 108 |
parent.appendChild(E.div( |
| 109 |
E.div(_('What would you like to do with the {} format?').format(fmt)), |
| 110 |
E.div(class_='button-box', |
| 111 |
create_button(_('Read'), 'book', read_button), |
| 112 |
create_button(_('Download'), 'cloud-download', download_url(book_id, fmt), |
| 113 |
_('File size: {}').format(human_readable(sz)), |
| 114 |
download_filename=f'{title}.{fmt.toLowerCase()}') |
| 115 |
) |
| 116 |
)) |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
def adjust_iframe_height(iframe): |
| 121 |
de = iframe.contentWindow.document.documentElement |
| 122 |
# scrollHeight is inaccurate on Firefox |
| 123 |
iframe.style.height = de.offsetHeight + 5 + 'px' |
| 124 |
iframe.dataset.last_window_width = window.innerWidth + '' |
| 125 |
return de |
| 126 |
|
| 127 |
|
| 128 |
def setup_iframe(iframe): |
| 129 |
de = adjust_iframe_height(iframe) |
| 130 |
for a in de.querySelectorAll('a[href]'): |
| 131 |
a.setAttribute('target', '_parent') |
| 132 |
|
| 133 |
def forward_touch_events(ev): |
| 134 |
container = window.parent.document.getElementById(render_book.container_id) |
| 135 |
if container: |
| 136 |
dup = v'new ev.constructor(ev.type, ev)' |
| 137 |
container.dispatchEvent(dup) |
| 138 |
|
| 139 |
for key in ('start', 'move', 'end', 'cancel'): |
| 140 |
iframe.contentWindow.addEventListener(f'touch{key}', forward_touch_events) |
| 141 |
|
| 142 |
|
| 143 |
def adjust_all_iframes(ev): |
| 144 |
for iframe in document.querySelectorAll(f'.{CLASS_NAME} iframe'): |
| 145 |
ww = parseInt(iframe.dataset.last_window_width) |
| 146 |
if ww is not window.innerWidth: |
| 147 |
adjust_iframe_height(iframe) |
| 148 |
|
| 149 |
|
| 150 |
def add_stars_to(stars, val, allow_half_stars): |
| 151 |
for i in range(val // 2): |
| 152 |
stars.appendChild(svgicon('star')) |
| 153 |
if allow_half_stars and (val % 2): |
| 154 |
stars.appendChild(svgicon('star-half')) |
| 155 |
|
| 156 |
|
| 157 |
if window?: |
| 158 |
window.addEventListener('resize', debounce(adjust_all_iframes, 250)) |
| 159 |
|
| 160 |
|
| 161 |
def hex_decode(lib): |
| 162 |
v'const uint8Array = new Uint8Array(lib.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));' |
| 163 |
v'lib = new TextDecoder("utf-8").decode(uint8Array);' |
| 164 |
return lib |
| 165 |
|
| 166 |
|
| 167 |
def decode_calibre_url_library_id(lib): |
| 168 |
if lib.startswith('_hex_-'): |
| 169 |
lib = hex_decode(lib[6:]) |
| 170 |
elif lib is '_': |
| 171 |
lib = current_library_id() |
| 172 |
return lib |
| 173 |
|
| 174 |
|
| 175 |
def replace_calibre_links_to_books(html): |
| 176 |
parser = v'new DOMParser()' |
| 177 |
dom = parser.parseFromString(html, 'text/html') |
| 178 |
changed = False |
| 179 |
dom.body.querySelectorAll('a[href]').forEach(def (link): |
| 180 |
nonlocal changed |
| 181 |
if not link.href or not link.href.startswith('calibre://'): |
| 182 |
return |
| 183 |
url = v'new URL(link.href, window.location.origin)' |
| 184 |
if url.hostname is not 'show-book' and url.hostname is not 'view-book' and url.hostname is not 'search': |
| 185 |
return |
| 186 |
path = url.pathname |
| 187 |
parts = str.split(path, '/') |
| 188 |
lib, book_id = parts[1], parts[2] |
| 189 |
lib = decode_calibre_url_library_id(lib) |
| 190 |
if url.hostname is 'search': |
| 191 |
q = vl = '' |
| 192 |
if url.searchParams.get('eq'): |
| 193 |
q = hex_decode(url.searchParams.get('eq')) |
| 194 |
elif url.searchParams.get('q'): |
| 195 |
q = url.searchParams.get('q') |
| 196 |
if not q: |
| 197 |
return |
| 198 |
if url.searchParams.get('encoded_virtual_library'): |
| 199 |
vl = hex_decode(url.searchParams.get('encoded_virtual_library')) |
| 200 |
elif url.searchParams.get('virtual_library'): |
| 201 |
vl = url.searchParams.get('virtual_library') |
| 202 |
if vl is '_': |
| 203 |
vl = '' |
| 204 |
link.href = search_url_in_library(lib, q, vl) |
| 205 |
changed = True |
| 206 |
else: |
| 207 |
try: |
| 208 |
book_id = int(book_id) |
| 209 |
except: |
| 210 |
return |
| 211 |
if url.hostname is 'show-book': |
| 212 |
new_href = query_as_href({'book_id': book_id + '', 'library_id': lib}, 'book_details') |
| 213 |
else: |
| 214 |
fmt = parts[3] |
| 215 |
extra_query = {} |
| 216 |
url.searchParams.forEach(def (val, key): |
| 217 |
if key is 'open_at': |
| 218 |
key = 'bookpos' |
| 219 |
extra_query[key] = val |
| 220 |
) |
| 221 |
new_href = open_book_url_in_library(lib, book_id, fmt, extra_query) |
| 222 |
if url.search: |
| 223 |
new_href |
| 224 |
link.href = new_href |
| 225 |
changed = True |
| 226 |
) |
| 227 |
if changed: |
| 228 |
return v'new XMLSerializer().serializeToString(dom);' |
| 229 |
return html |
| 230 |
|
| 231 |
|
| 232 |
def adjusting_sandboxed_html(html, extra_css): |
| 233 |
color = get_color_as_rgba('window-foreground') |
| 234 |
css = f'\n\n:root {{ color-scheme: {color_scheme()} }}\n\nhtml, body {{ overflow: hidden; color: rgba({color[0]}, {color[1]}, {color[2]}, {color[3]}) }}' |
| 235 |
if extra_css: |
| 236 |
css += '\n\n' + extra_css |
| 237 |
# allow-same-origin is needed for resizing and allow-top-navigation is |
| 238 |
# needed for links with target="_parent" |
| 239 |
ans = sandboxed_html(html, css, 'allow-same-origin allow-top-navigation-by-user-activation') |
| 240 |
ans.addEventListener('load', def (ev): setup_iframe(ev.target);) |
| 241 |
ans.style.height = '50vh' |
| 242 |
ans.dataset.last_window_width = '0' |
| 243 |
return ans |
| 244 |
|
| 245 |
|
| 246 |
def render_metadata(mi, table, book_id, iframe_css): # {{{ |
| 247 |
field_metadata = library_data.field_metadata |
| 248 |
interface_data = get_interface_data() |
| 249 |
|
| 250 |
def allowed_fields(field): |
| 251 |
if field.endswith('_index'): |
| 252 |
fm = field_metadata[field[:-len('_index')]] |
| 253 |
if fm and fm.datatype is 'series': |
| 254 |
return False |
| 255 |
if field.startswith('#'): |
| 256 |
return True |
| 257 |
if field in IGNORED_FIELDS or field.endswith('_sort'): |
| 258 |
return False |
| 259 |
if mi[field] is undefined: |
| 260 |
return False |
| 261 |
return True |
| 262 |
|
| 263 |
fields = library_data.book_display_fields |
| 264 |
vertical_categories = {x: True for x in (library_data.book_details_vertical_categories or [])} |
| 265 |
if not fields or not fields.length or get_session_data().get('show_all_metadata'): |
| 266 |
if field_metadata: |
| 267 |
fields = sorted(filter(allowed_fields, mi), key=field_sorter(field_metadata)) |
| 268 |
else: |
| 269 |
fields = filter(allowed_fields, fields) |
| 270 |
else: |
| 271 |
fields = filter(allowed_fields, fields) |
| 272 |
comments = [] |
| 273 |
link_maps = mi.link_maps or v'{}' |
| 274 |
|
| 275 |
def show_note_action(field, item_id, item_val): |
| 276 |
show_note(field, item_id, item_val) |
| 277 |
|
| 278 |
def add_note_link(field, name, val, parent): |
| 279 |
if mi.items_with_notes and mi.items_with_notes[field] and mi.items_with_notes[field][val]: |
| 280 |
parent.appendChild(document.createTextNode(' ')) |
| 281 |
parent.appendChild(E.a( |
| 282 |
svgicon('pencil'), title=_('Show notes for: {}').format(val), href='javascript:void(0)', onclick=show_note_action.bind( |
| 283 |
None, field, mi.items_with_notes[field][val], val), class_='blue-link')) |
| 284 |
|
| 285 |
def add_row(field, name, val, is_searchable=False, is_html=False, join=None, search_text=None, use_quotes=True): |
| 286 |
if val is undefined or val is None: |
| 287 |
return |
| 288 |
is_vertical = vertical_categories[field] |
| 289 |
|
| 290 |
def add_val(v): |
| 291 |
if not v.appendChild: |
| 292 |
v += '' |
| 293 |
parent = table.lastChild.lastChild |
| 294 |
if is_searchable: |
| 295 |
websearch_link = False |
| 296 |
no_link = False |
| 297 |
if field is 'authors': |
| 298 |
no_link = interface_data.default_author_link is '__NO_SEARCH_LINK__' |
| 299 |
websearch_link = bool(interface_data.default_author_link) and interface_data.default_author_link is not 'search-calibre' |
| 300 |
else: |
| 301 |
fm = field_metadata[field] |
| 302 |
if fm?.display and fm.display.web_search_template and fm.display.web_search_template.partition(':')[0] in ('http', 'https'): |
| 303 |
websearch_link = True |
| 304 |
text_rep = search_text or v |
| 305 |
target = '_self' |
| 306 |
if websearch_link and jstype(v) is not 'string': |
| 307 |
websearch_link = False |
| 308 |
calibre_search_url = href_for_search(is_searchable, text_rep, use_quotes=use_quotes) |
| 309 |
calibre_search_tooltip = _('Click to see books with {0}: {1}').format(name, text_rep) |
| 310 |
if websearch_link: |
| 311 |
url = web_search_url(book_id, field, v) |
| 312 |
target = '_blank' |
| 313 |
tooltip = _('Click to browse for {} on the web').format(text_rep) |
| 314 |
else: |
| 315 |
url = calibre_search_url |
| 316 |
tooltip = calibre_search_tooltip |
| 317 |
if no_link: |
| 318 |
parent.appendChild(v if v.appendChild else document.createTextNode(v)) |
| 319 |
else: |
| 320 |
parent.appendChild(E.a(v, target=target, title=tooltip, class_='blue-link', href=url)) |
| 321 |
if link_maps[field] and link_maps[field][text_rep]: |
| 322 |
url = link_maps[field][text_rep] |
| 323 |
if url.startswith('https://') or url.startswith('http://'): |
| 324 |
parent.appendChild(document.createTextNode(' ')) |
| 325 |
parent.appendChild(E.a( |
| 326 |
svgicon('external-link'), title=_('Click to open') + ': ' + url, href=url, target='_blank', class_='blue-link')) |
| 327 |
if websearch_link or no_link: |
| 328 |
parent.appendChild(document.createTextNode(' ')) |
| 329 |
parent.appendChild(E.a( |
| 330 |
svgicon('search'), title=calibre_search_tooltip, href=calibre_search_url, class_='blue-link')) |
| 331 |
else: |
| 332 |
parent.appendChild(v if v.appendChild else document.createTextNode(v)) |
| 333 |
if jstype(v) is 'string' and not is_html: |
| 334 |
add_note_link(field, name, v, parent) |
| 335 |
|
| 336 |
table.appendChild(E.tr(E.td(name), E.td())) |
| 337 |
if is_html and /[<>]/.test(val + ''): |
| 338 |
table.lastChild.lastChild.appendChild(adjusting_sandboxed_html(val + '', iframe_css)) |
| 339 |
else: |
| 340 |
if not join: |
| 341 |
add_val(val) |
| 342 |
else: |
| 343 |
for v in val: |
| 344 |
add_val(v) |
| 345 |
if v is not val[-1]: |
| 346 |
if is_vertical: |
| 347 |
table.lastChild.lastChild.appendChild(E.br()) |
| 348 |
else: |
| 349 |
table.lastChild.lastChild.appendChild(document.createTextNode(join)) |
| 350 |
return table.lastChild.lastChild |
| 351 |
|
| 352 |
def process_composite(field, fm, name, val): |
| 353 |
if fm.display and fm.display.contains_html: |
| 354 |
add_row(field, name, val, is_html=True) |
| 355 |
return |
| 356 |
if fm.is_multiple and fm.is_multiple.list_to_ui: |
| 357 |
all_vals = filter(None, map(str.strip, val.split(fm.is_multiple.list_to_ui))) |
| 358 |
add_row(field, name, all_vals, is_searchable=field, join=fm.is_multiple.list_to_ui) |
| 359 |
else: |
| 360 |
add_row(field, name, val, is_searchable=field) |
| 361 |
|
| 362 |
def process_authors(field, fm, name, val): |
| 363 |
add_row(field, name, val, is_searchable=field, join=' & ') |
| 364 |
|
| 365 |
def process_publisher(field, fm, name, val): |
| 366 |
add_row(field, name, val, is_searchable=field) |
| 367 |
|
| 368 |
def process_formats(field, fm, name, val): |
| 369 |
if val.length and book_id?: |
| 370 |
table.appendChild(E.tr(E.td(name), E.td())) |
| 371 |
td = table.lastChild.lastChild |
| 372 |
for fmt in val: |
| 373 |
fmt = fmt.toUpperCase() |
| 374 |
td.appendChild(E.a( |
| 375 |
fmt, class_='blue-link', href='javascript:void(0)', |
| 376 |
title=_('Read or download this book in the {} format').format(fmt), |
| 377 |
onclick=on_fmt_click.bind([mi.title, mi.format_sizes[fmt] or 0]), |
| 378 |
data_format=fmt, data_book_id='' + book_id)) |
| 379 |
if fmt is not val[-1]: |
| 380 |
td.appendChild(document.createTextNode(', ')) |
| 381 |
|
| 382 |
def process_rating(field, fm, name, val): |
| 383 |
stars = E.span() |
| 384 |
val = int(val or 0) |
| 385 |
if val > 0: |
| 386 |
add_stars_to(stars, val, fm.display?.allow_half_stars) |
| 387 |
add_row(field, name, stars, is_searchable=field, search_text=val / 2 + '') |
| 388 |
|
| 389 |
def process_identifiers(field, fm, name, val): |
| 390 |
def ids_sorter(x): |
| 391 |
return (x[1] or '').toLowerCase() |
| 392 |
|
| 393 |
if val and mi.urls_from_identifiers and mi.urls_from_identifiers.length > 0: |
| 394 |
td = E.td() |
| 395 |
for _, id_name, id_value, url in sorted(mi.urls_from_identifiers or [], key=ids_sorter): |
| 396 |
if td.childNodes.length: |
| 397 |
td.appendChild(document.createTextNode(', ')) |
| 398 |
label = '{}:{}'.format(id_name or '', id_value or '') |
| 399 |
td.appendChild(E.a(class_='blue-link', title=label, target='_new', href=url, label)) |
| 400 |
if td.childNodes.length: |
| 401 |
table.appendChild(E.tr(E.td(name), td)) |
| 402 |
|
| 403 |
def process_size(field, fm, name, val): |
| 404 |
if val: |
| 405 |
try: |
| 406 |
add_row(field, name, human_readable(int(val))) |
| 407 |
except: |
| 408 |
add_row(field, name, val + '') |
| 409 |
|
| 410 |
def process_languages(field, fm, name, val): |
| 411 |
if val and val.length: |
| 412 |
table.appendChild(E.tr(E.td(name), E.td())) |
| 413 |
td = table.lastChild.lastChild |
| 414 |
for k in val: |
| 415 |
if mi.lang_names: |
| 416 |
lang = mi.lang_names[k] or k |
| 417 |
else: |
| 418 |
lang = k |
| 419 |
td.appendChild(E.a(lang, |
| 420 |
class_='blue-link', |
| 421 |
title=_('Click to see books with language: {}').format(lang), |
| 422 |
href=href_for_search(field, k) |
| 423 |
)) |
| 424 |
if k is not val[-1]: |
| 425 |
td.appendChild(document.createTextNode(', ')) |
| 426 |
|
| 427 |
def process_datetime(field, fm, name, val): |
| 428 |
if val: |
| 429 |
fmt = interface_data['gui_' + field + '_display_format'] or (fm['display'] or {}).date_format |
| 430 |
formatted_val = format_date(val, fmt) |
| 431 |
if formatted_val: |
| 432 |
add_row(field, name, formatted_val, is_searchable=field, search_text=val) |
| 433 |
|
| 434 |
def process_series(field, fm, name, val): |
| 435 |
if val: |
| 436 |
ifield = field + '_index' |
| 437 |
try: |
| 438 |
ival = float(mi[ifield]) |
| 439 |
except Exception: |
| 440 |
ival = 1.0 |
| 441 |
ival = fmt_sidx(ival, use_roman=interface_data.use_roman_numerals_for_series_number) |
| 442 |
table.appendChild(E.tr(E.td(name), E.td())) |
| 443 |
s = safe_set_inner_html(E.span(), _('{0} of <a>{1}</a>').format(ival, val)) |
| 444 |
a = s.getElementsByTagName('a') |
| 445 |
if a and a.length: |
| 446 |
a = a[0] |
| 447 |
a.setAttribute('href', href_for_search(field, val)) |
| 448 |
a.setAttribute('title', _('Click to see books with {0}: {1}').format(name, val)) |
| 449 |
a.setAttribute('class', 'blue-link') |
| 450 |
else: |
| 451 |
print('WARNING: Translation of series template is incorrect as it does not have an <a> tag') |
| 452 |
table.lastChild.lastChild.appendChild(s) |
| 453 |
add_note_link(field, name, val, table.lastChild.lastChild) |
| 454 |
|
| 455 |
def process_field(field, fm): |
| 456 |
name = fm.name or field |
| 457 |
datatype = fm.datatype |
| 458 |
val = mi[field] |
| 459 |
if field is 'comments' or datatype is 'comments' or fm.display?.composite_show_in_comments: |
| 460 |
if not val: |
| 461 |
return |
| 462 |
ias = fm.display?.interpret_as or 'html' |
| 463 |
hp = fm.display?.heading_position or 'hide' |
| 464 |
if ias is 'long-text': |
| 465 |
if hp is 'side': |
| 466 |
add_row(field, name, val).style.whiteSpace = 'pre-wrap' |
| 467 |
return |
| 468 |
val = E.pre(val, style='white-space:pre-wrap').outerHTML |
| 469 |
elif ias is 'short-text': |
| 470 |
if hp is 'side': |
| 471 |
add_row(field, name, val) |
| 472 |
return |
| 473 |
val = E.span(val).outerHTML |
| 474 |
else: |
| 475 |
if field is 'comments' and '<' not in val: |
| 476 |
val = '\n'.join(['<p class="description">{}</p>'.format(x.replace(/\n/g, '<br>')) for x in val.split('\n\n')]) |
| 477 |
if hp is 'side': |
| 478 |
add_row(field, name, val, is_html=True) |
| 479 |
return |
| 480 |
comments.push([field, val]) |
| 481 |
return |
| 482 |
func = None |
| 483 |
if datatype is 'composite': |
| 484 |
func = process_composite |
| 485 |
elif field is 'formats': |
| 486 |
func = process_formats |
| 487 |
elif datatype is 'rating': |
| 488 |
func = process_rating |
| 489 |
elif field is 'identifiers': |
| 490 |
func = process_identifiers |
| 491 |
elif field is 'authors': |
| 492 |
func = process_authors |
| 493 |
elif field is 'publisher': |
| 494 |
func = process_publisher |
| 495 |
elif field is 'languages': |
| 496 |
func = process_languages |
| 497 |
elif field is 'size': |
| 498 |
func = process_size |
| 499 |
elif datatype is 'datetime': |
| 500 |
func = process_datetime |
| 501 |
elif datatype is 'series': |
| 502 |
func = process_series |
| 503 |
if func: |
| 504 |
func(field, fm, name, val) |
| 505 |
else: |
| 506 |
if datatype is 'text' or datatype is 'enumeration': |
| 507 |
if val is not undefined and val is not None: |
| 508 |
join = fm.is_multiple.list_to_ui if fm.is_multiple else None |
| 509 |
add_row(field, name, val, join=join, is_searchable=field) |
| 510 |
elif datatype is 'bool': |
| 511 |
if library_data.bools_are_tristate: |
| 512 |
v = _('Yes') if val else ('' if val is undefined or val is None else _('No')) |
| 513 |
else: |
| 514 |
v = _('Yes') if val else _('No') |
| 515 |
if v: |
| 516 |
add_row(field, name, v, is_searchable=field, use_quotes=False) |
| 517 |
elif datatype is 'int' or datatype is 'float': |
| 518 |
if val is not undefined and val is not None: |
| 519 |
fmt = (fm.display or {}).number_format |
| 520 |
if fmt: |
| 521 |
formatted_val = fmt.format(val) |
| 522 |
if formatted_val: |
| 523 |
val += '' |
| 524 |
add_row(field, name, formatted_val, is_searchable=field, search_text=val) |
| 525 |
else: |
| 526 |
val += '' |
| 527 |
add_row(field, name, val, is_searchable=field, search_text=val) |
| 528 |
|
| 529 |
for field in fields: |
| 530 |
fm = field_metadata[field] |
| 531 |
if not fm: |
| 532 |
continue |
| 533 |
try: |
| 534 |
process_field(field, fm) |
| 535 |
except Exception: |
| 536 |
print('Failed to render metadata field: ' + field) |
| 537 |
traceback.print_exc() |
| 538 |
|
| 539 |
all_html = '' |
| 540 |
for field, comment in comments: |
| 541 |
if comment: |
| 542 |
fm = field_metadata[field] |
| 543 |
if fm.display?.heading_position is 'above': |
| 544 |
name = fm.name or field |
| 545 |
all_html += f'<h3>{name}</h3>' |
| 546 |
all_html += comment |
| 547 |
iframe = adjusting_sandboxed_html(replace_calibre_links_to_books(all_html), iframe_css) |
| 548 |
iframe.style.marginTop = '2ex' |
| 549 |
table.parentNode.appendChild(iframe) |
| 550 |
# }}} |
| 551 |
|
| 552 |
|
| 553 |
def basic_table_rules(sel): |
| 554 |
style = '' |
| 555 |
style += build_rule(sel + 'table.metadata td', vertical_align='top') |
| 556 |
style += build_rule( |
| 557 |
sel + 'table.metadata td:first-of-type', |
| 558 |
font_style='normal', |
| 559 |
font_weight='700', |
| 560 |
color='var(--calibre-color-faded-text)', |
| 561 |
padding_right='0.5em', |
| 562 |
white_space='nowrap', |
| 563 |
text_align='right' |
| 564 |
) |
| 565 |
# Values column: prefer wrapping long tokens (paths, ids) on narrow screens |
| 566 |
style += build_rule(sel + 'table.metadata td:last-of-type', overflow_wrap='anywhere', word_break='break-word') |
| 567 |
return style |
| 568 |
|
| 569 |
|
| 570 |
add_extra_css(def (): |
| 571 |
sel = '.' + CLASS_NAME + ' ' |
| 572 |
style = basic_table_rules(sel) |
| 573 |
style += build_rule(sel + ' .next-book-button:hover', transform='scale(1.5)') |
| 574 |
# Modern layout for book details page |
| 575 |
style += build_rule(sel + '.cs-bd-wrap', |
| 576 |
padding='1rem', |
| 577 |
max_width='1100px', |
| 578 |
margin_left='auto', |
| 579 |
margin_right='auto' |
| 580 |
) |
| 581 |
style += build_rule(sel + '.cs-bd-initial-loading', |
| 582 |
min_height='min(70vh, 720px)', |
| 583 |
display='flex', |
| 584 |
align_items='center', |
| 585 |
justify_content='center' |
| 586 |
) |
| 587 |
style += build_rule(sel + '.cs-bd-initial-loading-card', |
| 588 |
width='min(820px, 100%)', |
| 589 |
padding='1.1rem 1.1rem', |
| 590 |
border='var(--calibre-border-style-subtle)', |
| 591 |
border_radius='18px', |
| 592 |
background_color='rgba(0,0,0,0.02)', |
| 593 |
box_shadow='0 10px 30px rgba(0,0,0,0.06)' |
| 594 |
) |
| 595 |
style += build_rule(sel + '.cs-bd-initial-loading-row', |
| 596 |
display='flex', |
| 597 |
align_items='center', |
| 598 |
justify_content='center', |
| 599 |
gap='0.6rem', |
| 600 |
opacity='0.9', |
| 601 |
font_weight='700' |
| 602 |
) |
| 603 |
style += build_rule(sel + '.cs-bd-initial-loading-sub', |
| 604 |
margin_top='0.55rem', |
| 605 |
text_align='center', |
| 606 |
opacity='0.65' |
| 607 |
) |
| 608 |
style += build_rule(sel + '.cs-bd-hero', |
| 609 |
display='grid', |
| 610 |
grid_template_columns='220px 1fr', |
| 611 |
gap='1.25rem', |
| 612 |
align_items='start', |
| 613 |
padding='1rem', |
| 614 |
border='var(--calibre-border-style-subtle)', |
| 615 |
border_radius='18px', |
| 616 |
background_color='rgba(0,0,0,0.02)', |
| 617 |
box_shadow='0 10px 30px rgba(0,0,0,0.06)' |
| 618 |
) |
| 619 |
style += build_rule(sel + '.cs-bd-cover', |
| 620 |
position='relative', |
| 621 |
width='220px' |
| 622 |
) |
| 623 |
style += build_rule(sel + '.cs-bd-cover img', |
| 624 |
width='100%', |
| 625 |
height='auto', |
| 626 |
display='block', |
| 627 |
border_radius='18px', |
| 628 |
box_shadow='0 18px 48px rgba(0,0,0,0.18)', |
| 629 |
border='var(--calibre-border-style-subtle)' |
| 630 |
) |
| 631 |
# Prev/next navigation is rendered below the details. |
| 632 |
style += build_rule(sel + '.cs-bd-nav', |
| 633 |
margin_top='1rem', |
| 634 |
display='flex', |
| 635 |
justify_content='space-between', |
| 636 |
gap='0.75rem' |
| 637 |
) |
| 638 |
style += build_rule(sel + '.cs-bd-nav a.calibre-push-button', |
| 639 |
flex='1 1 0', |
| 640 |
display='inline-flex', |
| 641 |
align_items='center', |
| 642 |
justify_content='center', |
| 643 |
gap='0.4rem' |
| 644 |
) |
| 645 |
style += r''' |
| 646 |
@media (max-width: 720px) { |
| 647 |
.book-details-panel .cs-bd-nav { gap: 0.6rem; } |
| 648 |
.book-details-panel .cs-bd-nav a.calibre-push-button { justify-content: center; } |
| 649 |
} |
| 650 |
''' |
| 651 |
style += build_rule(sel + '.cs-bd-info', min_width='0') |
| 652 |
style += build_rule(sel + '.cs-bd-title', |
| 653 |
font_size='1.45em', |
| 654 |
font_weight='800', |
| 655 |
line_height='1.2', |
| 656 |
margin_bottom='0.35rem' |
| 657 |
) |
| 658 |
style += build_rule(sel + '.cs-bd-authors', |
| 659 |
opacity='0.85', |
| 660 |
font_size='1.05em', |
| 661 |
margin_bottom='0.75rem', |
| 662 |
overflow='hidden', |
| 663 |
text_overflow='ellipsis' |
| 664 |
) |
| 665 |
style += build_rule(sel + '.cs-bd-meta', |
| 666 |
display='flex', |
| 667 |
flex_wrap='wrap', |
| 668 |
gap='0.35rem 0.6rem', |
| 669 |
margin_bottom='0.9rem', |
| 670 |
opacity='0.9' |
| 671 |
) |
| 672 |
style += build_rule(sel + '.cs-bd-badge', |
| 673 |
display='inline-flex', |
| 674 |
align_items='center', |
| 675 |
gap='0.25rem', |
| 676 |
padding='0.15rem 0.45rem', |
| 677 |
border_radius='999px', |
| 678 |
border='var(--calibre-border-style-subtle)', |
| 679 |
background='rgba(0,0,0,0.02)', |
| 680 |
max_width='100%', |
| 681 |
white_space='nowrap', |
| 682 |
overflow='hidden', |
| 683 |
text_overflow='ellipsis' |
| 684 |
) |
| 685 |
style += build_rule(sel + '.cs-bd-actions', |
| 686 |
display='flex', |
| 687 |
flex_wrap='wrap', |
| 688 |
gap='0.5rem', |
| 689 |
margin_top='0.2rem' |
| 690 |
) |
| 691 |
style += build_rule(sel + '.cs-bd-details', |
| 692 |
margin_top='1rem', |
| 693 |
padding='1rem', |
| 694 |
border='var(--calibre-border-style-subtle)', |
| 695 |
border_radius='18px', |
| 696 |
background_color=get_color('window-background'), |
| 697 |
box_shadow='0 1px 2px rgba(0,0,0,0.06)', |
| 698 |
min_width='0', |
| 699 |
max_width='100%', |
| 700 |
box_sizing='border-box', |
| 701 |
overflow_x='hidden', |
| 702 |
overflow_wrap='break-word', |
| 703 |
word_break='break-word', |
| 704 |
text_align='left', |
| 705 |
) |
| 706 |
style += build_rule(sel + '.cs-bd-details table.metadata', |
| 707 |
width='100%', |
| 708 |
max_width='100%', |
| 709 |
table_layout='fixed', |
| 710 |
box_sizing='border-box', |
| 711 |
) |
| 712 |
style += build_rule(sel + '.cs-bd-details table.metadata td:first-of-type', text_align='left') |
| 713 |
style += build_rule(sel + '.cs-bd-details pre', white_space='pre-wrap', word_break='break-word', max_width='100%', overflow_x='auto') |
| 714 |
style += build_rule(sel + '.cs-bd-details code', word_break='break-word', overflow_wrap='anywhere') |
| 715 |
style += r''' |
| 716 |
@media (max-width: 720px) { |
| 717 |
.book-details-panel .cs-bd-wrap { padding: 0.75rem; min-width: 0; } |
| 718 |
.book-details-panel .cs-bd-hero { grid-template-columns: 1fr; min-width: 0; } |
| 719 |
.book-details-panel .cs-bd-cover { width: min(320px, 100%); margin-left: auto; margin-right: auto; } |
| 720 |
.book-details-panel .cs-bd-title { font-size: 1.25em; } |
| 721 |
.book-details-panel .cs-bd-details { padding: 0.75rem; } |
| 722 |
.book-details-panel .cs-bd-details table.metadata td { |
| 723 |
white-space: normal; |
| 724 |
word-break: break-word; |
| 725 |
overflow-wrap: anywhere; |
| 726 |
} |
| 727 |
.book-details-panel .cs-bd-details table.metadata td:first-of-type { |
| 728 |
white-space: normal; |
| 729 |
text-align: left; |
| 730 |
padding-right: 0.35rem; |
| 731 |
} |
| 732 |
} |
| 733 |
''' |
| 734 |
# Loading overlay (prevents flicker when fetching metadata) |
| 735 |
style += build_rule('.' + CLASS_NAME + ' .cs-bd-loading-wrap', position='relative') |
| 736 |
style += build_rule('.' + CLASS_NAME + ' .cs-bd-loading-overlay', |
| 737 |
position='absolute', left='0', top='0', right='0', bottom='0', |
| 738 |
display='flex', align_items='center', justify_content='center', |
| 739 |
background_color='rgba(255,255,255,0.55)', |
| 740 |
opacity='0', |
| 741 |
pointer_events='none', |
| 742 |
transition='opacity 160ms ease' |
| 743 |
) |
| 744 |
style += build_rule('.' + CLASS_NAME + ' .cs-bd-loading .cs-bd-loading-overlay', opacity='1') |
| 745 |
style += build_rule('.' + CLASS_NAME + ' .cs-bd-loading .cs-bd-loading-content', opacity='0.45', transition='opacity 160ms ease') |
| 746 |
style += r''' |
| 747 |
@keyframes cs_spin_bd { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } |
| 748 |
.book-details-panel .cs-bd-loading-overlay svg { animation: cs_spin_bd 1.2s linear infinite; opacity: 0.85; } |
| 749 |
''' |
| 750 |
sel = '.' + SEARCH_INTERNET_CLASS |
| 751 |
style += build_rule(sel, margin='1ex 1em') |
| 752 |
style += build_rule(sel + ' ul > li', list_style_type='none') |
| 753 |
style += build_rule(sel + ' ul > li > a', padding='2ex 1em', display='block', width='100%') |
| 754 |
sel = '.' + COPY_TO_LIBRARY_CLASS |
| 755 |
style += build_rule(sel, margin='1ex 1em') |
| 756 |
sel = '.' + DATA_FILES_CLASS |
| 757 |
style += build_rule(sel, margin='1ex 1em') |
| 758 |
return style |
| 759 |
) |
| 760 |
|
| 761 |
current_fetch = None |
| 762 |
|
| 763 |
|
| 764 |
def no_book(container): |
| 765 |
container.appendChild(E.div( |
| 766 |
style='margin: 1ex 1em', |
| 767 |
_('No book found') |
| 768 |
)) |
| 769 |
|
| 770 |
|
| 771 |
def on_img_err(err): |
| 772 |
img = err.target |
| 773 |
if img.parentNode: |
| 774 |
img.parentNode.style.display = 'none' |
| 775 |
|
| 776 |
|
| 777 |
def preferred_format(book_id, for_download): |
| 778 |
interface_data = get_interface_data() |
| 779 |
return get_preferred_format(book_metadata(book_id), interface_data.output_format, interface_data.input_formats, for_download) |
| 780 |
|
| 781 |
|
| 782 |
def read_format(book_id, fmt): |
| 783 |
open_book(book_id, fmt) |
| 784 |
|
| 785 |
|
| 786 |
def read_book(book_id): |
| 787 |
fmt = preferred_format(book_id) |
| 788 |
if fmt and fmt.lower() is 'pdf': |
| 789 |
window.open(download_url(book_id, fmt, 'inline'), '_blank') |
| 790 |
else: |
| 791 |
read_format(book_id, fmt) |
| 792 |
|
| 793 |
|
| 794 |
def download_format(book_id, fmt): |
| 795 |
window.location = download_url(book_id, fmt) |
| 796 |
|
| 797 |
|
| 798 |
def download_book(book_id): |
| 799 |
fmt = preferred_format(book_id, True) |
| 800 |
download_format(book_id, fmt) |
| 801 |
|
| 802 |
|
| 803 |
def next_book(book_id, delta): |
| 804 |
next_book_id = book_after(book_id, delta) |
| 805 |
if next_book_id: |
| 806 |
q = parse_url_params() |
| 807 |
q.book_id = next_book_id + '' |
| 808 |
show_panel('book_details', query=q, replace=True) |
| 809 |
|
| 810 |
|
| 811 |
def render_book(container_id, book_id): |
| 812 |
render_book.book_id = book_id |
| 813 |
render_book.container_id = container_id |
| 814 |
is_random = parse_url_params().book_id is '0' |
| 815 |
c = document.getElementById(container_id) |
| 816 |
if not c: |
| 817 |
return |
| 818 |
install_touch_handlers(c, book_id) |
| 819 |
metadata = book_metadata(book_id) |
| 820 |
render_book.title = metadata.title |
| 821 |
set_title(c, metadata.title) |
| 822 |
authors = metadata.authors.join(' & ') if metadata.authors else _('Unknown') |
| 823 |
alt = _('{} by {}\n\nClick to read').format(metadata.title, authors) |
| 824 |
if is_random: |
| 825 |
pass |
| 826 |
imgdiv = E.div(class_='cs-bd-cover', |
| 827 |
E.img( |
| 828 |
alt=alt, title=alt, data_title=metadata.title, data_authors=authors, |
| 829 |
onclick=read_book.bind(None, book_id), |
| 830 |
style='cursor: pointer;' |
| 831 |
), |
| 832 |
) |
| 833 |
img = imgdiv.getElementsByTagName('img')[0] |
| 834 |
img.onerror = on_img_err |
| 835 |
img.src = cover_url(book_id) |
| 836 |
c = c.lastChild |
| 837 |
wrap = E.div(class_='cs-bd-wrap') |
| 838 |
c.appendChild(wrap) |
| 839 |
|
| 840 |
# Hero: cover + basic info |
| 841 |
hero = E.div(class_='cs-bd-hero') |
| 842 |
wrap.appendChild(hero) |
| 843 |
hero.appendChild(imgdiv) |
| 844 |
info = E.div(class_='cs-bd-info', data_book_id='' + book_id) |
| 845 |
hero.appendChild(info) |
| 846 |
|
| 847 |
info.appendChild(E.div(metadata.title or _('Unknown'), class_='cs-bd-title')) |
| 848 |
info.appendChild(E.div(authors, class_='cs-bd-authors')) |
| 849 |
|
| 850 |
meta = E.div(class_='cs-bd-meta') |
| 851 |
info.appendChild(meta) |
| 852 |
|
| 853 |
def add_badge(icon, text, title=None): |
| 854 |
if not text: |
| 855 |
return |
| 856 |
b = E.span(class_='cs-bd-badge') |
| 857 |
if icon: |
| 858 |
b.appendChild(svgicon(icon)) |
| 859 |
b.appendChild(E.span(text)) |
| 860 |
if title: |
| 861 |
b.title = title |
| 862 |
meta.appendChild(b) |
| 863 |
|
| 864 |
# Rating, series, publisher, pubdate, formats (high-signal basics) |
| 865 |
if metadata.rating: |
| 866 |
stars = E.span(style='white-space:nowrap') |
| 867 |
add_stars_to(stars, int(metadata.rating or 0), True) |
| 868 |
add_badge(None, stars, _('Rating')) |
| 869 |
if metadata.series: |
| 870 |
try: |
| 871 |
ival = float(metadata.series_index) |
| 872 |
except Exception: |
| 873 |
ival = 1.0 |
| 874 |
ival = fmt_sidx(ival, use_roman=get_interface_data().use_roman_numerals_for_series_number) |
| 875 |
add_badge('bookmark', _('{0} of {1}').format(ival, metadata.series), _('Series')) |
| 876 |
if metadata.publisher: |
| 877 |
add_badge('library', metadata.publisher, _('Publisher')) |
| 878 |
if metadata.pubdate: |
| 879 |
try: |
| 880 |
add_badge('date', format_date(metadata.pubdate, 'yyyy-MM-dd'), _('Published')) |
| 881 |
except Exception: |
| 882 |
pass |
| 883 |
if metadata.formats and metadata.formats.length: |
| 884 |
add_badge('book', metadata.formats.slice(0, 4).join(' · '), _('Formats')) |
| 885 |
|
| 886 |
actions = E.div(class_='cs-bd-actions') |
| 887 |
info.appendChild(actions) |
| 888 |
read_button = create_button(_('Read'), 'book', read_book.bind(None, book_id), _('Read this book [V]')) |
| 889 |
fmt = preferred_format(book_id, True) |
| 890 |
download_button = create_button(_('Download'), 'cloud-download', download_url(book_id, fmt), |
| 891 |
_('Download this book in the {0} format ({1})').format(fmt, human_readable(metadata.format_sizes[fmt] or 0)), |
| 892 |
download_filename=f'{metadata.title}.{fmt.toLowerCase()}') |
| 893 |
row = E.div(read_button, download_button) |
| 894 |
row.classList.add('cs-bd-actions') |
| 895 |
if not metadata.formats or not metadata.formats.length: |
| 896 |
row.style.display = 'none' |
| 897 |
# Put buttons into actions block (keeps hero tidy) |
| 898 |
actions.appendChild(row) |
| 899 |
|
| 900 |
# Secondary actions: Convert/Edit/Delete + More actions. |
| 901 |
# These are placed right below the Read/Download buttons, and "More actions" |
| 902 |
# opens a floating dialog (not a new page/panel). |
| 903 |
def show_more_actions_modal(ev=None): |
| 904 |
create_custom_dialog(_('More actions…'), def (parent, close_modal): |
| 905 |
if get_session_data().get('show_all_metadata'): |
| 906 |
title, subtitle = _('Show important metadata'), _('Show only the important metadata fields in the book details') |
| 907 |
else: |
| 908 |
title, subtitle = _('Show all metadata'), _('Show all metadata fields in the book details') |
| 909 |
items = [ |
| 910 |
create_item(_('Search the internet'), |
| 911 |
subtitle=_('Search for this author or book on various websites'), |
| 912 |
action=def (): |
| 913 |
close_modal() |
| 914 |
show_subsequent_panel('search_internet', replace=True) |
| 915 |
), |
| 916 |
create_item(title, subtitle=subtitle, |
| 917 |
action=def (): |
| 918 |
close_modal() |
| 919 |
toggle_fields() |
| 920 |
), |
| 921 |
create_item(_('Copy to library'), |
| 922 |
subtitle=_('Copy or move this book to another calibre library'), |
| 923 |
action=def (): |
| 924 |
close_modal() |
| 925 |
show_subsequent_panel('copy_to_library', replace=True) |
| 926 |
), |
| 927 |
create_item(_('Manage data files'), |
| 928 |
subtitle=_('Upload or download additional data files to this book'), |
| 929 |
action=def (): |
| 930 |
close_modal() |
| 931 |
show_subsequent_panel('data_files', replace=True) |
| 932 |
), |
| 933 |
] |
| 934 |
wrapper = E.div() |
| 935 |
parent.appendChild(wrapper) |
| 936 |
create_item_list(wrapper, items) |
| 937 |
) |
| 938 |
|
| 939 |
row2 = E.div(style='flex-basis: 100%; width: 100%;') |
| 940 |
row2.classList.add('cs-bd-actions') |
| 941 |
|
| 942 |
def on_convert(ev): |
| 943 |
convert_book() |
| 944 |
|
| 945 |
def on_edit(ev): |
| 946 |
edit_metadata() |
| 947 |
|
| 948 |
def on_delete(ev): |
| 949 |
delete_book() |
| 950 |
|
| 951 |
def on_more(ev): |
| 952 |
show_more_actions_modal(ev) |
| 953 |
|
| 954 |
row2.appendChild(create_button(_('Convert'), 'convert', on_convert, _('Convert this book to another format [C]'))) |
| 955 |
row2.appendChild(create_button(_('Edit'), 'edit', on_edit, _('Edit the metadata for this book [E]'))) |
| 956 |
row2.appendChild(create_button(_('Delete'), 'trash', on_delete, _('Delete this book'))) |
| 957 |
row2.appendChild(create_button('', 'ellipsis-v', on_more, _('More actions'))) |
| 958 |
actions.appendChild(row2) |
| 959 |
|
| 960 |
# Details section (full metadata) |
| 961 |
details = E.div(class_='cs-bd-details') |
| 962 |
wrap.appendChild(details) |
| 963 |
md = E.div(style='margin-bottom: 0') |
| 964 |
table = E.table(class_='metadata') |
| 965 |
details.appendChild(md) |
| 966 |
md.appendChild(table) |
| 967 |
|
| 968 |
render_metadata(metadata, table, book_id) |
| 969 |
|
| 970 |
# Prev/next navigation should be below the details section. |
| 971 |
def make_nav_button(is_prev): |
| 972 |
delta = -1 if is_prev else 1 |
| 973 |
nbid = book_after(book_id, delta) |
| 974 |
if not nbid or nbid is book_id or is_random: |
| 975 |
return None |
| 976 |
ans = create_button( |
| 977 |
_('Previous') if is_prev else _('Next'), |
| 978 |
'chevron-left' if is_prev else 'chevron-right', |
| 979 |
next_book.bind(None, book_id, delta), |
| 980 |
_('Previous book [Ctrl+Left]') if is_prev else _('Next book [Ctrl+Right]') |
| 981 |
) |
| 982 |
if not is_prev: |
| 983 |
ans.insertBefore(ans.firstChild.nextSibling, ans.firstChild) |
| 984 |
ans.style.justifyContent = 'start' if is_prev else 'end' |
| 985 |
return ans |
| 986 |
|
| 987 |
prev_btn = make_nav_button(True) |
| 988 |
next_btn = make_nav_button(False) |
| 989 |
if prev_btn or next_btn: |
| 990 |
nav = E.div(class_='cs-bd-nav') |
| 991 |
nav.appendChild(prev_btn or E.span()) |
| 992 |
nav.appendChild(next_btn or E.span()) |
| 993 |
wrap.appendChild(nav) |
| 994 |
|
| 995 |
|
| 996 |
def add_top_bar_buttons(container_id): |
| 997 |
container = document.getElementById(container_id) |
| 998 |
if container: |
| 999 |
clear_buttons(container) |
| 1000 |
# Book actions are rendered in the hero section (below Read/Download). |
| 1001 |
|
| 1002 |
|
| 1003 |
def proceed_after_succesful_fetch_metadata(container_id, book_id): |
| 1004 |
render_book(container_id, book_id) |
| 1005 |
add_top_bar_buttons(container_id) |
| 1006 |
|
| 1007 |
|
| 1008 |
def metadata_fetched(container_id, book_id, proceed, end_type, xhr, event): |
| 1009 |
nonlocal current_fetch |
| 1010 |
if current_fetch is None or current_fetch is not xhr: |
| 1011 |
return # Fetching was aborted |
| 1012 |
current_fetch = None |
| 1013 |
c = document.getElementById(container_id) |
| 1014 |
if not c: |
| 1015 |
return |
| 1016 |
c = c.lastChild |
| 1017 |
# Always cleanup transient loading UI so it never leaves layout artifacts |
| 1018 |
# (e.g. empty lines) after the request completes or is aborted. |
| 1019 |
try: |
| 1020 |
c.classList.remove('cs-bd-loading') |
| 1021 |
c.classList.remove('cs-bd-loading-wrap') |
| 1022 |
except Exception: |
| 1023 |
pass |
| 1024 |
try: |
| 1025 |
overlay = c.querySelector('.cs-bd-loading-overlay') |
| 1026 |
if overlay and overlay.parentNode: |
| 1027 |
overlay.parentNode.removeChild(overlay) |
| 1028 |
except Exception: |
| 1029 |
pass |
| 1030 |
try: |
| 1031 |
content = c.querySelector('.cs-bd-loading-content') |
| 1032 |
if content and content.parentNode: |
| 1033 |
# If we created a wrapper to fade existing content, unwrap it now. |
| 1034 |
while content.firstChild: |
| 1035 |
c.insertBefore(content.firstChild, content) |
| 1036 |
content.parentNode.removeChild(content) |
| 1037 |
except Exception: |
| 1038 |
pass |
| 1039 |
if end_type is 'load': |
| 1040 |
try: |
| 1041 |
data = JSON.parse(xhr.responseText) |
| 1042 |
except Exception as err: |
| 1043 |
error_dialog(_('Could not fetch metadata for book'), _('Server returned an invalid response'), err.toString()) |
| 1044 |
return |
| 1045 |
clear(c) |
| 1046 |
book_id = int(data['id']) |
| 1047 |
set_book_metadata(book_id, data) |
| 1048 |
proceed(container_id, book_id) |
| 1049 |
elif end_type is not 'abort': |
| 1050 |
clear(c) |
| 1051 |
c.appendChild(E.div( |
| 1052 |
style='margin: 1ex 1em', |
| 1053 |
_('Could not fetch metadata for book'), |
| 1054 |
E.div(style='margin: 1ex 1em') |
| 1055 |
)) |
| 1056 |
safe_set_inner_html(c.lastChild.lastChild, xhr.error_html) |
| 1057 |
|
| 1058 |
|
| 1059 |
def fetch_metadata(container_id, book_id, proceed): |
| 1060 |
nonlocal current_fetch |
| 1061 |
container = document.getElementById(container_id) |
| 1062 |
if not container: |
| 1063 |
return |
| 1064 |
if current_fetch: |
| 1065 |
current_fetch.abort() |
| 1066 |
current_fetch = ajax('interface-data/book-metadata/' + book_id, metadata_fetched.bind(None, container_id, book_id, proceed), |
| 1067 |
query={'library_id': current_library_id(), 'vl': current_virtual_library()}) |
| 1068 |
current_fetch.send() |
| 1069 |
body = container.lastChild |
| 1070 |
# If we already have content, do not clear; show an overlay to prevent flicker. |
| 1071 |
if body and body.firstChild: |
| 1072 |
body.classList.add('cs-bd-loading-wrap') |
| 1073 |
body.classList.add('cs-bd-loading') |
| 1074 |
# Ensure content wrapper exists so we can fade it |
| 1075 |
content = body.querySelector('.cs-bd-loading-content') |
| 1076 |
if not content: |
| 1077 |
content = E.div(class_='cs-bd-loading-content') |
| 1078 |
# Move existing children into content wrapper |
| 1079 |
while body.firstChild: |
| 1080 |
content.appendChild(body.firstChild) |
| 1081 |
body.appendChild(content) |
| 1082 |
overlay = body.querySelector('.cs-bd-loading-overlay') |
| 1083 |
if not overlay: |
| 1084 |
overlay = E.div(class_='cs-bd-loading-overlay', svgicon('cog'), E.span('\xa0' + _('Fetching metadata…'))) |
| 1085 |
body.appendChild(overlay) |
| 1086 |
else: |
| 1087 |
clear(body) |
| 1088 |
body.classList.add('cs-bd-loading-wrap') |
| 1089 |
wrap = E.div(class_='cs-bd-wrap cs-bd-initial-loading') |
| 1090 |
card = E.div(class_='cs-bd-initial-loading-card') |
| 1091 |
card.appendChild(E.div(class_='cs-bd-initial-loading-row', |
| 1092 |
create_spinner(), |
| 1093 |
E.span(_('Fetching metadata…')) |
| 1094 |
)) |
| 1095 |
card.appendChild(E.div(class_='cs-bd-initial-loading-sub', _('Fetching metadata for the book, please wait') + '…')) |
| 1096 |
wrap.appendChild(card) |
| 1097 |
body.appendChild(wrap) |
| 1098 |
|
| 1099 |
|
| 1100 |
def install_touch_handlers(container, book_id): |
| 1101 |
ongoing_touches = {} |
| 1102 |
gesture_id = 0 |
| 1103 |
container_id = container.id |
| 1104 |
|
| 1105 |
def has_active_touches(): |
| 1106 |
for tid in ongoing_touches: |
| 1107 |
t = ongoing_touches[tid] |
| 1108 |
if t.active: |
| 1109 |
return True |
| 1110 |
return False |
| 1111 |
|
| 1112 |
def handle_touch(ev): |
| 1113 |
nonlocal gesture_id, ongoing_touches |
| 1114 |
container = document.getElementById(container_id) |
| 1115 |
if not container: |
| 1116 |
return |
| 1117 |
if ev.type is 'touchstart': |
| 1118 |
for touch in ev.changedTouches: |
| 1119 |
ongoing_touches[touch_id(touch)] = copy_touch(touch) |
| 1120 |
gesture_id += 1 |
| 1121 |
elif ev.type is 'touchmove': |
| 1122 |
for touch in ev.changedTouches: |
| 1123 |
t = ongoing_touches[touch_id(touch)] |
| 1124 |
if t: |
| 1125 |
update_touch(t, touch) |
| 1126 |
elif ev.type is 'touchcancel': |
| 1127 |
for touch in ev.changedTouches: |
| 1128 |
v'delete ongoing_touches[touch_id(touch)]' |
| 1129 |
elif ev.type is 'touchend': |
| 1130 |
for touch in ev.changedTouches: |
| 1131 |
t = ongoing_touches[touch_id(touch)] |
| 1132 |
if t: |
| 1133 |
t.active = False |
| 1134 |
update_touch(t, touch) |
| 1135 |
if not has_active_touches(): |
| 1136 |
touches = ongoing_touches |
| 1137 |
ongoing_touches = {} |
| 1138 |
num = len(touches) |
| 1139 |
if num is 1: |
| 1140 |
gesture = interpret_single_gesture(touches[Object.keys(touches)[0]], gesture_id) |
| 1141 |
if gesture.type is 'swipe' and gesture.axis is 'horizontal': |
| 1142 |
delta = -1 if gesture.direction is 'right' else 1 |
| 1143 |
next_book(book_id, delta) |
| 1144 |
|
| 1145 |
install_handlers(container, { |
| 1146 |
'handle_touchstart': handle_touch, |
| 1147 |
'handle_touchmove': handle_touch, |
| 1148 |
'handle_touchend': handle_touch, |
| 1149 |
'handle_touchcancel': handle_touch, |
| 1150 |
}, True) |
| 1151 |
|
| 1152 |
|
| 1153 |
def create_book_details(container): |
| 1154 |
q = parse_url_params() |
| 1155 |
current_book_id = q.book_id |
| 1156 |
if current_book_id is undefined or current_book_id is None: |
| 1157 |
no_book(container) |
| 1158 |
return |
| 1159 |
current_book_id = int(current_book_id) |
| 1160 |
container_id = container.parentNode.id |
| 1161 |
if current_book_id is not 0 and book_metadata(current_book_id): |
| 1162 |
render_book(container_id, current_book_id) |
| 1163 |
add_top_bar_buttons(container_id) |
| 1164 |
else: |
| 1165 |
fetch_metadata(container_id, current_book_id, proceed_after_succesful_fetch_metadata) |
| 1166 |
|
| 1167 |
|
| 1168 |
def report_load_failure(container): |
| 1169 |
report_a_load_failure( |
| 1170 |
container, _('Failed to load books from calibre library, with error:'), |
| 1171 |
load_status.error_html) |
| 1172 |
|
| 1173 |
|
| 1174 |
def check_for_books_loaded(): |
| 1175 |
container = this |
| 1176 |
if load_status.loading: |
| 1177 |
conditional_timeout(container.id, 5, check_for_books_loaded) |
| 1178 |
return |
| 1179 |
container = container.lastChild |
| 1180 |
clear(container) |
| 1181 |
if not load_status.ok: |
| 1182 |
report_load_failure(container) |
| 1183 |
return |
| 1184 |
create_book_details(container) |
| 1185 |
|
| 1186 |
|
| 1187 |
def onkeydown(container_id, close_action, ev): |
| 1188 |
if render_book.book_id: |
| 1189 |
if ev.ctrlKey: |
| 1190 |
if ev.key is 'ArrowLeft': |
| 1191 |
next_book(render_book.book_id, -1) |
| 1192 |
ev.preventDefault(), ev.stopPropagation() |
| 1193 |
elif ev.key is 'ArrowRight': |
| 1194 |
next_book(render_book.book_id, 1) |
| 1195 |
ev.preventDefault(), ev.stopPropagation() |
| 1196 |
elif not ev.ctrlKey and not ev.metaKey and not ev.shiftKey: |
| 1197 |
if ev.key is 'Escape': |
| 1198 |
ev.preventDefault(), ev.stopPropagation() |
| 1199 |
close_action() |
| 1200 |
elif ev.key is 'Delete': |
| 1201 |
ev.preventDefault(), ev.stopPropagation() |
| 1202 |
delete_book() |
| 1203 |
elif ev.key is 'v' or ev.key is 'V': |
| 1204 |
read_book(render_book.book_id) |
| 1205 |
elif ev.key is 'c' or ev.key is 'C': |
| 1206 |
convert_book() |
| 1207 |
elif ev.key is 'e' or ev.key is 'E': |
| 1208 |
edit_metadata() |
| 1209 |
|
| 1210 |
|
| 1211 |
def init(container_id): |
| 1212 |
container = document.getElementById(container_id) |
| 1213 |
clear(container) |
| 1214 |
close_action, close_icon = back, 'close' |
| 1215 |
q = parse_url_params() |
| 1216 |
ca = q.close_action |
| 1217 |
if ca is 'home': |
| 1218 |
close_action, close_icon = def (): home();, 'home' |
| 1219 |
elif ca is 'book_list': |
| 1220 |
close_action = def (): |
| 1221 |
show_panel('book_list', {'book_id': q.book_id}) |
| 1222 |
create_top_bar(container, title='', action=close_action, icon=close_icon) |
| 1223 |
window.scrollTo(0, 0) # Ensure we are at the top of the window |
| 1224 |
container.appendChild(E.div(class_=CLASS_NAME, tabindex='0')) |
| 1225 |
container.lastChild.addEventListener('keydown', onkeydown.bind(None, container_id, close_action), {'passive': False, 'capture': True}) |
| 1226 |
container.lastChild.focus() |
| 1227 |
container.lastChild.appendChild(E.div(_('Loading books from the calibre library, please wait...'), style='margin: 1ex 1em')) |
| 1228 |
conditional_timeout(container_id, 5, check_for_books_loaded) |
| 1229 |
|
| 1230 |
|
| 1231 |
def show_subsequent_panel(name, replace=False): |
| 1232 |
q = parse_url_params() |
| 1233 |
q.book_id = (render_book.book_id or q.book_id) + '' |
| 1234 |
show_panel('book_details^' + name, query=q, replace=replace) |
| 1235 |
|
| 1236 |
|
| 1237 |
def edit_metadata(): |
| 1238 |
q = parse_url_params() |
| 1239 |
q.book_id = (render_book.book_id or q.book_id) + '' |
| 1240 |
show_panel('edit_metadata', query=q, replace=False) |
| 1241 |
|
| 1242 |
|
| 1243 |
def convert_book(): |
| 1244 |
q = parse_url_params() |
| 1245 |
q.book_id = (render_book.book_id or q.book_id) + '' |
| 1246 |
show_panel('convert_book', query=q, replace=False) |
| 1247 |
|
| 1248 |
|
| 1249 |
def create_more_actions_panel(container_id): |
| 1250 |
container = document.getElementById(container_id) |
| 1251 |
clear(container) |
| 1252 |
create_top_bar(container, title=_('More actions…'), icon='back') |
| 1253 |
if get_session_data().get('show_all_metadata'): |
| 1254 |
title, subtitle = _('Show important metadata'), _('Show only the important metadata fields in the book details') |
| 1255 |
else: |
| 1256 |
title, subtitle = _('Show all metadata'), _('Show all metadata fields in the book details') |
| 1257 |
items = [ |
| 1258 |
create_item(_('Search the internet'), subtitle=_('Search for this author or book on various websites'), |
| 1259 |
action=def (): |
| 1260 |
show_subsequent_panel('search_internet', replace=True) |
| 1261 |
), |
| 1262 |
create_item(title, subtitle=subtitle, action=toggle_fields), |
| 1263 |
create_item(_('Copy to library'), subtitle=_('Copy or move this book to another calibre library'), |
| 1264 |
action=def (): |
| 1265 |
show_subsequent_panel('copy_to_library', replace=True) |
| 1266 |
), |
| 1267 |
create_item(_('Manage data files'), subtitle=_('Upload or download additional data files to this book'), |
| 1268 |
action=def (): |
| 1269 |
show_subsequent_panel('data_files', replace=True) |
| 1270 |
), |
| 1271 |
] |
| 1272 |
container.appendChild(E.div()) |
| 1273 |
create_item_list(container.lastChild, items) |
| 1274 |
|
| 1275 |
|
| 1276 |
def return_to_book_details(): |
| 1277 |
q = parse_url_params() |
| 1278 |
show_panel('book_details', query=q, replace=True) |
| 1279 |
|
| 1280 |
|
| 1281 |
def toggle_fields(): |
| 1282 |
sd = get_session_data() |
| 1283 |
sd.set('show_all_metadata', False if sd.get('show_all_metadata') else True) |
| 1284 |
return_to_book_details() |
| 1285 |
|
| 1286 |
|
| 1287 |
def url_for(template, data): |
| 1288 |
def eqc(x): |
| 1289 |
return encode_query_component(x).replace(/%20/g, '+') |
| 1290 |
return template.format(title=eqc(data.title), author=eqc(data.author)) |
| 1291 |
|
| 1292 |
|
| 1293 |
EXTENSIONS_TO_OPEN_IN_BROWSER = { |
| 1294 |
'pdf': True, |
| 1295 |
'jpeg': True, |
| 1296 |
'jpg': True, |
| 1297 |
'png': True, |
| 1298 |
'gif': True, |
| 1299 |
'webp': True, |
| 1300 |
'txt': True, |
| 1301 |
'html': True, |
| 1302 |
'htm': True, |
| 1303 |
'xhtml': True, |
| 1304 |
} |
| 1305 |
|
| 1306 |
|
| 1307 |
def download_data_file(book_id, relpath): |
| 1308 |
ext = relpath.toLowerCase().rpartition('.')[2] |
| 1309 |
open_inline = bool(EXTENSIONS_TO_OPEN_IN_BROWSER[ext]) |
| 1310 |
cd = 'inline' if open_inline else 'attachment' |
| 1311 |
url = download_data_file_url(book_id, relpath, cd) |
| 1312 |
if open_inline: |
| 1313 |
window.open(url) |
| 1314 |
else: |
| 1315 |
window.location = url |
| 1316 |
|
| 1317 |
|
| 1318 |
def on_progress(container_id, book_id, loaded, total, xhr): |
| 1319 |
container = document.getElementById(container_id) |
| 1320 |
if container and total: |
| 1321 |
update_status_widget(container, loaded, total) |
| 1322 |
|
| 1323 |
|
| 1324 |
def data_files_submitted(container_id, book_id, end_type, xhr, ev): |
| 1325 |
if end_type is 'abort': |
| 1326 |
back() |
| 1327 |
return |
| 1328 |
if end_type is not 'load': |
| 1329 |
error_dialog(_('Failed to upload files to server'), _( |
| 1330 |
'Uploading data files for book: {} failed.').format(book_id), xhr.error_html) |
| 1331 |
return |
| 1332 |
try: |
| 1333 |
res = JSON.parse(xhr.responseText) |
| 1334 |
except Exception as err: |
| 1335 |
error_dialog(_('Could not upload data files for book'), _('Server returned an invalid response'), err.toString()) |
| 1336 |
return |
| 1337 |
mi = book_metadata(book_id) |
| 1338 |
if mi: |
| 1339 |
mi.data_files = res.data_files |
| 1340 |
if res.error: |
| 1341 |
error_dialog(_('Could not upload data files for book'), _('There was an error: ') + res.error) |
| 1342 |
else: |
| 1343 |
back() |
| 1344 |
|
| 1345 |
|
| 1346 |
def submit_data_files(top_container_id, container_id, book_id, added): |
| 1347 |
c = document.getElementById(container_id) |
| 1348 |
clear(c) |
| 1349 |
c.appendChild(E.div(style='margin: 1ex 1rem', _('Uploading changes to server, please wait...'))) |
| 1350 |
w = upload_status_widget() |
| 1351 |
c.appendChild(w) |
| 1352 |
ajax_send( |
| 1353 |
f'data-files/upload/{book_id}/{current_library_id()}', added, |
| 1354 |
data_files_submitted.bind(None, container_id, book_id), on_progress.bind(None, container_id, book_id)) |
| 1355 |
|
| 1356 |
|
| 1357 |
def files_added(check_existing, top_container_id, book_id, container_id, files): |
| 1358 |
container = document.getElementById(container_id) |
| 1359 |
mi = book_metadata(book_id) |
| 1360 |
if not container or not mi or not files[0]: |
| 1361 |
return |
| 1362 |
added = [] |
| 1363 |
|
| 1364 |
def exists(fname): |
| 1365 |
if mi.data_files: |
| 1366 |
for relpath in mi.data_files: |
| 1367 |
q = relpath.partition('/')[2] |
| 1368 |
if q is fname: |
| 1369 |
return True |
| 1370 |
return False |
| 1371 |
|
| 1372 |
if check_existing: |
| 1373 |
existing = [file.name for file in files if exists(file.name)] |
| 1374 |
if existing.length: |
| 1375 |
return question_dialog( |
| 1376 |
_('Replace existing data files?'), |
| 1377 |
_('The following data files already exist, are you sure you want to replace them?') + ' ' + existing.join(', '), |
| 1378 |
def (yes): |
| 1379 |
if yes: |
| 1380 |
files_added(False, top_container_id, book_id, container_id, files) |
| 1381 |
else: |
| 1382 |
back() |
| 1383 |
) |
| 1384 |
|
| 1385 |
for file in files: |
| 1386 |
data = {'name': file.name, 'size': file.size, 'type': file.type, 'data_url': None} |
| 1387 |
added.push(data) |
| 1388 |
r = FileReader() |
| 1389 |
r.onload = def (evt): |
| 1390 |
data.data_url = evt.target.result |
| 1391 |
for entry in added: |
| 1392 |
if not entry.data_url: |
| 1393 |
return |
| 1394 |
submit_data_files(top_container_id, container_id, book_id, added) |
| 1395 |
r.readAsDataURL(file) |
| 1396 |
|
| 1397 |
|
| 1398 |
def upload_data_file(container_id): |
| 1399 |
if not render_book.book_id or not book_metadata(render_book.book_id): |
| 1400 |
return return_to_book_details() |
| 1401 |
book_id = render_book.book_id |
| 1402 |
container = document.getElementById(container_id) |
| 1403 |
clear(container) |
| 1404 |
create_top_bar(container, title=_('Upload data files'), icon='back') |
| 1405 |
div = E.div(id=unique_id()) |
| 1406 |
container.appendChild(div) |
| 1407 |
upload_files_widget(div, files_added.bind(None, True, container_id, book_id), _( |
| 1408 |
'Upload files by <a>selecting the files</a> or drag and drop of the files here.'), |
| 1409 |
single_file=False) |
| 1410 |
|
| 1411 |
|
| 1412 |
def rebuild_data_files_list(container_id, book_id, relpath_being_deleted): |
| 1413 |
def delete_data_file(relpath, fname, ev): |
| 1414 |
question_dialog(_('Are you sure?'), _('Do you want to permanently delete the data file {0} from the book {1}?').format( |
| 1415 |
fname, mi.title), def (yes): |
| 1416 |
if yes: |
| 1417 |
data = [relpath] |
| 1418 |
ajax_send(f'data-files/remove/{book_id}/{current_library_id()}', |
| 1419 |
data, data_file_deleted.bind(None, container_id, book_id, relpath)) |
| 1420 |
rebuild_data_files_list(container_id, book_id, relpath_being_deleted) |
| 1421 |
) |
| 1422 |
|
| 1423 |
def ddf(relpath, fname, ev): |
| 1424 |
download_data_file(book_id, relpath) |
| 1425 |
|
| 1426 |
def upload_data_file(ev): |
| 1427 |
show_subsequent_panel('upload_data_file', replace=False) |
| 1428 |
|
| 1429 |
container = document.getElementById(container_id) |
| 1430 |
mi = book_metadata(book_id) |
| 1431 |
if not container or not mi: |
| 1432 |
return |
| 1433 |
container = container.querySelector('[data-component=data-files-list-container]') |
| 1434 |
clear(container) |
| 1435 |
items = [create_item(_('Upload new data file'), icon='plus', action=upload_data_file)] |
| 1436 |
if mi.data_files: |
| 1437 |
fname_map = {relpath: relpath.partition('/')[2] for relpath in mi.data_files} |
| 1438 |
df = sorted(Object.keys(fname_map), key=def (x): return fname_map[x].toLowerCase();) |
| 1439 |
for relpath in df: |
| 1440 |
fname = fname_map[relpath] |
| 1441 |
being_deleted = relpath is relpath_being_deleted |
| 1442 |
subtitle = None |
| 1443 |
side_actions = [] |
| 1444 |
if being_deleted: |
| 1445 |
subtitle = _('This file is being deleted') |
| 1446 |
else: |
| 1447 |
side_actions.push(create_side_action( |
| 1448 |
'trash', tooltip=_('Delete the file: {}').format(fname), action=delete_data_file.bind(None, relpath, fname) |
| 1449 |
)) |
| 1450 |
items.push(create_item(fname, icon='cloud-download', subtitle=subtitle, side_actions=side_actions, action=ddf.bind(None, relpath, fname))) |
| 1451 |
create_item_list(container, items) |
| 1452 |
|
| 1453 |
|
| 1454 |
def data_file_deleted(container_id, book_id, relpath, end_type, xhr, ev): |
| 1455 |
if end_type is 'abort': |
| 1456 |
back() |
| 1457 |
return |
| 1458 |
if end_type is not 'load': |
| 1459 |
error_dialog(_('Failed to delete data file from server'), _( |
| 1460 |
'Deleting data file for book: {} failed.').format(book_id), xhr.error_html) |
| 1461 |
return |
| 1462 |
try: |
| 1463 |
res = JSON.parse(xhr.responseText) |
| 1464 |
except Exception as err: |
| 1465 |
error_dialog(_('Could not delete data file for book'), _('Server returned an invalid response'), err.toString()) |
| 1466 |
return |
| 1467 |
mi = book_metadata(book_id) |
| 1468 |
if mi: |
| 1469 |
mi.data_files = res.data_files |
| 1470 |
if res.errors: |
| 1471 |
error_dialog(_('Failed to delete data file'), _('Failed to delete data file: {}').format(relpath), res.errors[relpath]) |
| 1472 |
rebuild_data_files_list(container_id, book_id) |
| 1473 |
|
| 1474 |
|
| 1475 |
def data_files(container_id): |
| 1476 |
if not render_book.book_id or not book_metadata(render_book.book_id): |
| 1477 |
return return_to_book_details() |
| 1478 |
book_id = render_book.book_id |
| 1479 |
|
| 1480 |
container = document.getElementById(container_id) |
| 1481 |
clear(container) |
| 1482 |
create_top_bar(container, title=_('Manage data files'), icon='back') |
| 1483 |
container.appendChild(E.div(class_=DATA_FILES_CLASS)) |
| 1484 |
container = container.lastChild |
| 1485 |
container.appendChild(E.div(data_component='data-files-list-container')) |
| 1486 |
rebuild_data_files_list(container_id, book_id) |
| 1487 |
|
| 1488 |
|
| 1489 |
def search_internet(container_id): |
| 1490 |
if not render_book.book_id or not book_metadata(render_book.book_id): |
| 1491 |
return return_to_book_details() |
| 1492 |
container = document.getElementById(container_id) |
| 1493 |
clear(container) |
| 1494 |
create_top_bar(container, title=_('Search the internet'), icon='back') |
| 1495 |
mi = book_metadata(render_book.book_id) |
| 1496 |
data = {'title': mi.title, 'author': mi.authors[0] if mi.authors else _('Unknown')} |
| 1497 |
interface_data = get_interface_data() |
| 1498 |
|
| 1499 |
def link_for(name, template): |
| 1500 |
return E.a(name, class_='simple-link', href=url_for(template, data), target='_blank') |
| 1501 |
|
| 1502 |
author_links = E.ul() |
| 1503 |
book_links = E.ul() |
| 1504 |
|
| 1505 |
if interface_data.search_the_net_urls: |
| 1506 |
for entry in interface_data.search_the_net_urls: |
| 1507 |
links = book_links if entry.type is 'book' else author_links |
| 1508 |
links.appendChild(E.li(link_for(entry.name, entry.url))) |
| 1509 |
for name, url in [ |
| 1510 |
(_('Goodreads'), 'https://www.goodreads.com/book/author/{author}'), |
| 1511 |
(_('Wikipedia'), 'https://en.wikipedia.org/w/index.php?search={author}'), |
| 1512 |
(_('Google Books'), 'https://www.google.com/search?tbm=bks&q=inauthor:%22{author}%22'), |
| 1513 |
]: |
| 1514 |
author_links.appendChild(E.li(link_for(name, url))) |
| 1515 |
|
| 1516 |
for name, url in [ |
| 1517 |
(_('Goodreads'), 'https://www.goodreads.com/search?q={author}+{title}&search%5Bsource%5D=goodreads&search_type=books&tab=books'), |
| 1518 |
(_('Google Books'), 'https://www.google.com/search?tbm=bks&q=inauthor:%22{author}%22+intitle:%22{title}%22'), |
| 1519 |
(_('Amazon'), 'https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords={author}+{title}'), |
| 1520 |
]: |
| 1521 |
book_links.appendChild(E.li(link_for(name, url))) |
| 1522 |
|
| 1523 |
container.appendChild(E.div(class_=SEARCH_INTERNET_CLASS, |
| 1524 |
safe_set_inner_html(E.h2(), _('Search for the author <i>{}</i> at:').format(data.author)), |
| 1525 |
author_links, |
| 1526 |
E.hr(), |
| 1527 |
safe_set_inner_html(E.h2(), _('Search for the book <i>{}</i> at:').format(data.title)), |
| 1528 |
book_links, |
| 1529 |
)) |
| 1530 |
|
| 1531 |
|
| 1532 |
# Copy to library {{{ |
| 1533 |
|
| 1534 |
|
| 1535 |
def do_copy_to_library(book_id, target_library_id, target_library_name): |
| 1536 |
def handle_result(move, close_func, end_type, xhr, ev): |
| 1537 |
close_func() |
| 1538 |
title = book_metadata(book_id).title |
| 1539 |
return_to_book_details() |
| 1540 |
if end_type is 'abort': |
| 1541 |
return |
| 1542 |
if end_type is not 'load': |
| 1543 |
error_dialog(_('Failed to copy book'), _( |
| 1544 |
'Failed to copy the book "{}". Click "Show details" for more information.').format(title), |
| 1545 |
xhr.error_html) |
| 1546 |
return |
| 1547 |
try: |
| 1548 |
response = JSON.parse(xhr.responseText)[book_id] |
| 1549 |
if not response: |
| 1550 |
raise Exception('bad') |
| 1551 |
except: |
| 1552 |
error_dialog(_('Failed to copy book'), _( |
| 1553 |
'Failed to copy the book "{}" because of an invalid response from calibre').format(title)) |
| 1554 |
return |
| 1555 |
if not response.ok: |
| 1556 |
error_dialog(_('Failed to copy book'), _( |
| 1557 |
'Failed to copy the book "{}". Click "Show details" for more information.').format(title), |
| 1558 |
response.payload |
| 1559 |
) |
| 1560 |
return |
| 1561 |
payload = response.payload |
| 1562 |
if payload.action is 'duplicate': |
| 1563 |
warning_dialog(_('Book already exists'), _( |
| 1564 |
'Could not copy as a book with the same title and authors already exists in the {} library').format(target_library_name)) |
| 1565 |
|
| 1566 |
elif payload.action is 'automerge': |
| 1567 |
warning_dialog(_('Book merged'), _( |
| 1568 |
'The files from the book were merged into a book with the same title and authors in the {} library').format(target_library_name)) |
| 1569 |
if move and payload.action in ('add', 'automerge'): |
| 1570 |
refresh_after_delete(book_id, current_library_id()) |
| 1571 |
|
| 1572 |
def trigger_copy(container_id, move, close_func): |
| 1573 |
try: |
| 1574 |
choice = document.querySelector(f'#{dupes_id} input[name="dupes"]:checked').value |
| 1575 |
except: |
| 1576 |
choice = document.querySelector(f'#{dupes_id} input[name="dupes"]').value |
| 1577 |
sd.set('copy_to_library_dupes', choice) |
| 1578 |
duplicate_action, automerge_action = choice.split(';', 2) |
| 1579 |
|
| 1580 |
data = {'book_ids': [book_id], 'move': move, 'duplicate_action': duplicate_action, 'automerge_action': automerge_action} |
| 1581 |
container = document.getElementById(container_id) |
| 1582 |
clear(container) |
| 1583 |
container.appendChild(E.div( |
| 1584 |
_('Contacting calibre to copy book, please wait...'))) |
| 1585 |
ajax_send(f'cdb/copy-to-library/{target_library_id}/{current_library_id()}', |
| 1586 |
data, handle_result.bind(None, move, close_func)) |
| 1587 |
|
| 1588 |
def radio(value, text): |
| 1589 |
return E.div(style='margin-top: 1rem', E.input(type='radio', name='dupes', value=value, checked=value is saved_value), '\xa0', E.span(text)) |
| 1590 |
|
| 1591 |
title = book_metadata(book_id).title |
| 1592 |
dupes_id = unique_id() |
| 1593 |
sd = get_session_data() |
| 1594 |
saved_value = sd.get('copy_to_library_dupes') |
| 1595 |
create_custom_dialog(_( |
| 1596 |
'Copy book to "{target_library_name}"').format(target_library_name=target_library_name), |
| 1597 |
def (container, close_func): |
| 1598 |
container_id = ensure_id(container) |
| 1599 |
container.appendChild(E.div( |
| 1600 |
E.div(_('Copying: {}').format(title)), |
| 1601 |
E.div(id=dupes_id, |
| 1602 |
E.p(_('If there are already existing books in "{}" with the same title and authors,' |
| 1603 |
' how would you like to handle them?').format(target_library_name)), |
| 1604 |
radio('add;overwrite', _('Copy anyway')), |
| 1605 |
radio('ignore;overwrite', _('Do not copy')), |
| 1606 |
radio('add_formats_to_existing;overwrite', _('Merge into existing books, overwriting existing files')), |
| 1607 |
radio('add_formats_to_existing;ignore', _('Merge into existing books, keeping existing files')), |
| 1608 |
radio('add_formats_to_existing;new record', _('Merge into existing books, putting conflicting files into a new book record')), |
| 1609 |
), |
| 1610 |
E.div( |
| 1611 |
class_='button-box', |
| 1612 |
create_button(_('Cancel'), None, close_func), |
| 1613 |
create_button(_('Move'), None, trigger_copy.bind(None, container_id, True, close_func)), |
| 1614 |
create_button(_('Copy'), None, trigger_copy.bind(None, container_id, False, close_func), highlight=True), |
| 1615 |
) |
| 1616 |
)) |
| 1617 |
if not container.querySelector(f'#{dupes_id} input[name="dupes"]:checked'): |
| 1618 |
container.querySelector(f'#{dupes_id} input[name="dupes"]').checked = True |
| 1619 |
) |
| 1620 |
|
| 1621 |
|
| 1622 |
def copy_to_library(container_id): |
| 1623 |
if not render_book.book_id or not book_metadata(render_book.book_id): |
| 1624 |
return return_to_book_details() |
| 1625 |
container = document.getElementById(container_id) |
| 1626 |
clear(container) |
| 1627 |
create_top_bar(container, title=_('Copy to library'), icon='back') |
| 1628 |
libraries = all_libraries() |
| 1629 |
container.appendChild(E.div(class_=COPY_TO_LIBRARY_CLASS)) |
| 1630 |
container = container.lastChild |
| 1631 |
if libraries.length < 2: |
| 1632 |
container.appendChild(E.div(_('There are no other calibre libraries available to copy the book to'))) |
| 1633 |
return |
| 1634 |
container.appendChild(E.h2(_('Choose the library to copy to below'))) |
| 1635 |
items = [] |
| 1636 |
for library_id, library_name in libraries: |
| 1637 |
if library_id is current_library_id(): |
| 1638 |
continue |
| 1639 |
items.push(create_item(library_name, action=do_copy_to_library.bind(None, render_book.book_id, library_id, library_name))) |
| 1640 |
container.appendChild(E.div()) |
| 1641 |
create_item_list(container.lastChild, items) |
| 1642 |
# }}} |
| 1643 |
|
| 1644 |
|
| 1645 |
def delete_book(): |
| 1646 |
start_delete_book(current_library_id(), render_book.book_id, render_book.title or _('Unknown')) |
| 1647 |
|
| 1648 |
|
| 1649 |
def set_panel_handlers(): |
| 1650 |
set_panel_handler('book_details', init) |
| 1651 |
set_panel_handler('book_details^more_actions', create_more_actions_panel) |
| 1652 |
set_panel_handler('book_details^search_internet', search_internet) |
| 1653 |
set_panel_handler('book_details^copy_to_library', copy_to_library) |
| 1654 |
set_panel_handler('book_details^data_files', data_files) |
| 1655 |
set_panel_handler('book_details^upload_data_file', upload_data_file) |