-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
book_details.pyj
827 lines (704 loc) · 32.5 KB
/
book_details.pyj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from __python__ import hash_literals
import traceback
from elementmaker import E
from gettext import gettext as _
from ajax import ajax, ajax_send, encode_query_component
from book_list.delete_book import refresh_after_delete, start_delete_book
from book_list.globals import get_session_data
from book_list.item_list import create_item, create_item_list
from book_list.library_data import (
all_libraries, book_after, book_metadata, cover_url, current_library_id,
current_virtual_library, download_url, library_data, load_status,
set_book_metadata
)
from book_list.router import back, home, open_book, report_a_load_failure
from book_list.theme import get_color, get_font_size
from book_list.top_bar import add_button, clear_buttons, create_top_bar, set_title
from book_list.ui import query_as_href, set_panel_handler, show_panel
from book_list.views import search_query_for
from date import format_date
from dom import add_extra_css, build_rule, clear, ensure_id, svgicon, unique_id
from modals import create_custom_dialog, error_dialog, warning_dialog
from session import get_interface_data
from utils import (
conditional_timeout, debounce, fmt_sidx, human_readable, parse_url_params,
safe_set_inner_html, sandboxed_html
)
from widgets import create_button, create_spinner
bd_counter = 0
CLASS_NAME = 'book-details-panel'
SEARCH_INTERNET_CLASS = 'book-details-search-internet'
COPY_TO_LIBRARY_CLASS = 'book-details-copy-to-library'
FORMAT_PRIORITIES = [
'EPUB', 'AZW3', 'DOCX', 'LIT', 'MOBI', 'ODT', 'RTF', 'MD', 'MARKDOWN', 'TXT', 'PDF'
]
def sort_formats_key(fmt):
ans = FORMAT_PRIORITIES.indexOf(fmt)
if ans < 0:
ans = FORMAT_PRIORITIES.length
return ans
def get_preferred_format(metadata, output_format, input_formats):
formats = (metadata and metadata.formats) or v'[]'
formats = [f.toUpperCase() for f in formats]
fmt = 'EPUB' if output_format is 'PDF' else output_format
if formats.length and formats.indexOf(fmt) is -1:
for q in sorted(formats, key=sort_formats_key):
if input_formats[q]:
fmt = q
break
return fmt.toUpperCase()
IGNORED_FIELDS = {'title', 'sort', 'uuid', 'id', 'urls_from_identifiers', 'lang_names', 'last_modified', 'path'}
default_sort = {f:i+1 for i, f in enumerate(('title', 'title_sort', 'authors', 'author_sort', 'series', 'rating', 'pubdate', 'tags', 'timestamp', 'pubdate', 'identifiers', 'languages', 'publisher', 'last_modified'))}
default_sort['formats'] = 999
def field_sorter(field_metadata):
return def(field):
lvl = '{:03d}'.format(default_sort[field] or 998)
fm = (field_metadata[field] or {})[field] or {}
return lvl + (fm.name or 'zzzzz')
def href_for_search(name, val):
query = '{}:"={}"'.format(name, str.replace(val, '"', r'\"'))
q = search_query_for(query)
return query_as_href(q)
def on_fmt_click(ev):
fmt = ev.currentTarget.dataset.format
book_id = int(ev.currentTarget.dataset.bookId)
title, sz = this
create_custom_dialog(title, def(parent, close_modal):
def action(which):
close_modal()
which(book_id, fmt)
parent.appendChild(E.div(
E.div(_('What would you like to do with the {} format?').format(fmt)),
E.div(class_='button-box',
create_button(_('Read'), 'book', action.bind(None, read_format)),
'\xa0',
create_button(_('Download'), 'cloud-download', download_url(book_id, fmt),
_('File size: {}').format(human_readable(sz)),
download_filename=f'{title}.{fmt.toLowerCase()}')
)
))
)
def adjust_iframe_height(iframe):
de = iframe.contentWindow.document.documentElement
# scrollHeight is inaccurate on Firefox
iframe.style.height = de.offsetHeight + 5 + 'px'
iframe.dataset.last_window_width = window.innerWidth + ''
return de
def setup_iframe(iframe):
de = adjust_iframe_height(iframe)
for a in de.querySelectorAll('a[href]'):
a.setAttribute('target', '_blank')
def adjust_all_iframes(ev):
for iframe in document.querySelectorAll(f'.{CLASS_NAME} iframe'):
ww = parseInt(iframe.dataset.last_window_width)
if ww is not window.innerWidth:
adjust_iframe_height(iframe)
def add_stars_to(stars, val, allow_half_stars):
for i in range(val // 2):
stars.appendChild(svgicon('star'))
if allow_half_stars and (val % 2):
stars.appendChild(svgicon('star-half'))
window.addEventListener('resize', debounce(adjust_all_iframes, 250))
def adjusting_sandboxed_html(html):
css = 'html, body {{ overflow: hidden; color: {} }}'.format(get_color('window-foreground'))
# allow-same-origin is needed for resizing and allow-popups is needed for
# target="_blank"
ans = sandboxed_html(html, css, 'allow-same-origin allow-popups allow-popups-to-escape-sandbox')
ans.addEventListener('load', def(ev): setup_iframe(ev.target);)
ans.style.height = '50vh'
ans.dataset.last_window_width = '0'
return ans
def render_metadata(mi, table, book_id): # {{{
field_metadata = library_data.field_metadata
interface_data = get_interface_data()
def allowed_fields(field):
if field.endswith('_index'):
fm = field_metadata[field[:-len('_index')]]
if fm and fm.datatype is 'series':
return False
if field.startswith('#'):
return True
if field in IGNORED_FIELDS or field.endswith('_sort'):
return False
if mi[field] is undefined:
return False
return True
fields = library_data.book_display_fields
if not fields or not fields.length or get_session_data().get('show_all_metadata'):
fields = sorted(filter(allowed_fields, mi), key=field_sorter(field_metadata))
else:
fields = filter(allowed_fields, fields)
comments = v'[]'
def add_row(name, val, is_searchable=False, is_html=False, join=None):
if val is undefined or val is None:
return
def add_val(v):
if not v.appendChild:
v += ''
if is_searchable:
table.lastChild.lastChild.appendChild(E.a(
title=_('Click to see books with {0}: {1}').format(name, v), href=href_for_search(is_searchable, v), v))
else:
if v.appendChild:
table.lastChild.lastChild.appendChild(v)
else:
table.lastChild.lastChild.appendChild(document.createTextNode(v))
table.appendChild(E.tr(E.td(name + ':'), E.td()))
if is_html and /[<>]/.test(val + ''):
table.lastChild.lastChild.appendChild(adjusting_sandboxed_html(val + ''))
else:
if not join:
add_val(val)
else:
for v in val:
add_val(v)
if v is not val[-1]:
table.lastChild.lastChild.appendChild(document.createTextNode(join))
return table.lastChild.lastChild
def process_composite(field, fm, name, val):
if fm.display and fm.display.contains_html:
add_row(name, val, is_html=True)
return
if fm.is_multiple and fm.is_multiple.list_to_ui:
all_vals = filter(None, map(str.strip, val.split(fm.is_multiple.list_to_ui)))
add_row(name, all_vals, is_searchable=field, join=fm.is_multiple.list_to_ui)
else:
add_row(name, val, is_searchable=field)
def process_authors(field, fm, name, val):
add_row(name, val, is_searchable=field, join=' & ')
def process_publisher(field, fm, name, val):
add_row(name, val, is_searchable=field)
def process_formats(field, fm, name, val):
if val.length and book_id?:
table.appendChild(E.tr(E.td(name + ':'), E.td()))
td = table.lastChild.lastChild
for fmt in val:
fmt = fmt.toUpperCase()
td.appendChild(E.a(
fmt, class_='simple-link', href='javascript:void(0)',
title=_('Read or download this book in the {} format').format(fmt),
onclick=on_fmt_click.bind(v'[mi.title, mi.format_sizes[fmt] || 0]'),
data_format=fmt, data_book_id='' + book_id))
if fmt is not val[-1]:
td.appendChild(document.createTextNode(', '))
def process_rating(field, fm, name, val):
stars = E.span()
val = int(val or 0)
if val > 0:
add_stars_to(stars, val, fm.display?.allow_half_stars)
add_row(name, stars)
def process_identifiers(field, fm, name, val):
if val:
keys = Object.keys(val)
if keys.length:
td = E.td()
url_map = {k:v'[text, url]' for text, k, val, url in mi.urls_from_identifiers or v'[]'}
for k in keys:
idval = val[k]
x = url_map[k]
if x:
if td.childNodes.length:
td.appendChild(document.createTextNode(', '))
td.appendChild(E.a(title='{}:{}'.format(k, idval), target='_new', href=x[1], x[0]))
if td.childNodes.length:
table.appendChild(E.tr(E.td(name + ':'), td))
def process_languages(field, fm, name, val):
if val and val.length:
table.appendChild(E.tr(E.td(name + ':'), E.td()))
td = table.lastChild.lastChild
for k in val:
lang = mi.lang_names[k] or k
td.appendChild(E.a(lang,
title=_('Click to see books with language: {}').format(lang),
href=href_for_search(field, k)
))
if k is not val[-1]:
td.appendChild(document.createTextNode(', '))
def process_datetime(field, fm, name, val):
if val:
fmt = interface_data['gui_' + field + '_display_format'] or (fm['display'] or {}).date_format
val = format_date(val, fmt)
if val:
add_row(name, val)
def process_series(field, fm, name, val):
if val:
ifield = field + '_index'
try:
ival = float(mi[ifield])
except Exception:
ival = 1.0
ival = fmt_sidx(ival, use_roman=interface_data.use_roman_numerals_for_series_number)
table.appendChild(E.tr(E.td(name + ':'), E.td()))
s = safe_set_inner_html(E.span(), _('{0} of <a>{1}</a>').format(ival, val))
a = s.getElementsByTagName('a')
if a and a.length:
a = a[0]
a.setAttribute('href', href_for_search(field, val))
a.setAttribute('title', _('Click to see books with {0}: {1}').format(name, val))
else:
print("WARNING: Translation of series template is incorrect as it does not have an <a> tag")
table.lastChild.lastChild.appendChild(s)
def process_field(field, fm):
name = fm.name or field
datatype = fm.datatype
val = mi[field]
if field is 'comments' or datatype is 'comments':
if not val:
return
ias = fm.display?.interpret_as or 'html'
hp = fm.display?.heading_position or 'hide'
if ias is 'long-text':
if hp is 'side':
add_row(name, val).style.whiteSpace = 'pre-wrap'
return
val = E.pre(val, style='white-space:pre-wrap').outerHTML
elif ias is 'short-text':
if hp is 'side':
add_row(name, val)
return
val = E.span(val).outerHTML
if hp is 'side':
add_row(name, val, is_html=True)
return
comments.push(v'[field, val]')
return
func = None
if datatype is 'composite':
func = process_composite
elif field is 'formats':
func = process_formats
elif datatype is 'rating':
func = process_rating
elif field is 'identifiers':
func = process_identifiers
elif field is 'authors':
func = process_authors
elif field is 'publisher':
func = process_publisher
elif field is 'languages':
func = process_languages
elif datatype is 'datetime':
func = process_datetime
elif datatype is 'series':
func = process_series
if func:
func(field, fm, name, val)
else:
if datatype is 'text' or datatype is 'enumeration':
if val is not undefined and val is not None:
join = fm.is_multiple.list_to_ui if fm.is_multiple else None
add_row(name, val, join=join, is_searchable=field)
elif datatype is 'bool':
add_row(name, _('Yes') if val else _('No'))
elif datatype is 'int' or datatype is 'float':
if val is not undefined and val is not None:
fmt = (fm.display or {}).number_format
if fmt:
val = fmt.format(val)
else:
val += ''
add_row(name, val)
for field in fields:
fm = field_metadata[field]
if not fm:
continue
try:
process_field(field, fm)
except Exception:
print('Failed to render metadata field: ' + field)
traceback.print_exc()
all_html = ''
for field, comment in comments:
if comment:
fm = field_metadata[field]
if fm.display?.heading_position is 'above':
name = fm.name or field
all_html += f'<h3>{name}</h3>'
all_html += comment
iframe = adjusting_sandboxed_html(all_html)
iframe.style.marginTop = '2ex'
table.parentNode.appendChild(iframe)
# }}}
def basic_table_rules(sel):
style = ''
style += build_rule(sel + 'table.metadata td', vertical_align='top')
style += build_rule(sel + 'table.metadata td:first-of-type', font_weight='bold', padding_right='1em', white_space='nowrap')
style += build_rule(sel + 'table.metadata td:last-of-type', overflow_wrap='break-word')
return style
add_extra_css(def():
sel = '.' + CLASS_NAME + ' '
style = basic_table_rules(sel)
style += build_rule(sel + 'table.metadata a[href]', color='blue')
style += build_rule(sel + 'table.metadata a[href]:hover', color=get_color('window-hover-foreground'))
style += build_rule(sel + 'table.metadata a[href]:active', color=get_color('window-hover-foreground'), transform='scale(1.5)')
sel = '.' + SEARCH_INTERNET_CLASS
style += build_rule(sel, margin='1ex 1em')
style += build_rule(sel + ' ul > li', list_style_type='none')
style += build_rule(sel + ' ul > li > a', padding='2ex 1em', display='block', width='100%')
sel = '.' + COPY_TO_LIBRARY_CLASS
style += build_rule(sel, margin='1ex 1em')
return style
)
current_fetch = None
def no_book(container):
container.appendChild(E.div(
style='margin: 1ex 1em',
_('No book found')
))
def on_img_err(err):
img = err.target
if img.parentNode:
img.parentNode.style.display = 'none'
def preferred_format(book_id):
interface_data = get_interface_data()
return get_preferred_format(book_metadata(book_id), interface_data.output_format, interface_data.input_formats)
def read_format(book_id, fmt):
open_book(book_id, fmt)
def read_book(book_id):
fmt = preferred_format(book_id)
read_format(book_id, fmt)
def download_format(book_id, fmt):
window.location = download_url(book_id, fmt)
def download_book(book_id):
fmt = preferred_format(book_id)
download_format(book_id, fmt)
def render_book(container_id, book_id):
render_book.book_id = book_id
c = document.getElementById(container_id)
if not c:
return
metadata = book_metadata(book_id)
render_book.title = metadata.title
set_title(c, metadata.title)
authors = metadata.authors.join(' & ') if metadata.authors else _('Unknown')
alt = _('{} by {}\nClick to read').format(metadata.title, authors)
imgdiv = E.div(
E.img(
alt=alt, title=alt, data_title=metadata.title, data_authors=authors,
onclick=read_book.bind(None, book_id),
style='cursor: pointer; border-radius: 20px; max-width: calc(100vw - 2em); max-height: calc(100vh - 4ex - {}); display: block; width:auto; height:auto; border-radius: 20px'.format(get_font_size('title')
))
)
imgdiv.firstChild.onerror = on_img_err
imgdiv.firstChild.src = cover_url(book_id)
c = c.lastChild
c.appendChild(E.div(
style='display:flex; padding: 1ex 1em; align-items: flex-start; justify-content: flex-start; flex-wrap: wrap',
E.div(style='margin-right: 1em; flex-grow: 3; max-width: 500px', data_book_id='' + book_id),
imgdiv
))
container = c.lastChild.firstChild
read_button = create_button(_('Read'), 'book', read_book.bind(None, book_id), _('Read this book'))
fmt = preferred_format(book_id)
download_button = create_button(_('Download'), 'cloud-download', download_url(book_id, fmt),
_('Download this book in the {0} format ({1})').format(fmt, human_readable(metadata.format_sizes[fmt] or 0)),
download_filename=f'{metadata.title}.{fmt.toLowerCase()}')
row = E.div(read_button, '\xa0\xa0\xa0', download_button, style='margin-bottom: 1ex')
if not metadata.formats or not metadata.formats.length:
row.style.display = 'none'
container.appendChild(row)
md = E.div(style='margin-bottom: 2ex')
table = E.table(class_='metadata')
container.appendChild(md)
md.appendChild(table)
def next_book(delta):
next_book_id = book_after(book_id, delta)
if next_book_id:
q = parse_url_params()
q.book_id = next_book_id + ''
show_panel('book_details', query=q, replace=True)
md.appendChild(E.div(
style='margin-top: 1.5ex',
create_button(_('Previous'), 'arrow-left', next_book.bind(None, -1)),
'\xa0\xa0\xa0',
create_button(_('Next'), 'arrow-right', next_book.bind(None, 1)),
))
render_metadata(metadata, table, book_id)
def add_top_bar_buttons(container_id):
container = document.getElementById(container_id)
if container:
clear_buttons(container)
add_button(container, 'convert', action=convert_book, tooltip=_('Convert this book to another format'))
add_button(container, 'edit', action=edit_metadata, tooltip=_('Edit the metadata for this book'))
add_button(container, 'trash', action=delete_book, tooltip=_('Delete this book'))
book_id = parse_url_params().book_id
if book_id is '0':
add_button(container, 'random', def(): fetch_metadata(container_id, 0, proceed_after_succesful_fetch_metadata);)
add_button(container, 'ellipsis-v', action=show_subsequent_panel.bind(None, 'more_actions'), tooltip=_('More actions'))
def proceed_after_succesful_fetch_metadata(container_id, book_id):
render_book(container_id, book_id)
add_top_bar_buttons(container_id)
def metadata_fetched(container_id, book_id, proceed, end_type, xhr, event):
nonlocal current_fetch
if current_fetch is None or current_fetch is not xhr:
return # Fetching was aborted
current_fetch = None
c = document.getElementById(container_id)
if not c:
return
c = c.lastChild
if end_type is 'load':
try:
data = JSON.parse(xhr.responseText)
except Exception as err:
error_dialog(_('Could not fetch metadata for book'), _('Server returned an invalid response'), err.toString())
return
clear(c)
book_id = int(data['id'])
set_book_metadata(book_id, data)
proceed(container_id, book_id)
elif end_type is not 'abort':
clear(c)
c.appendChild(E.div(
style='margin: 1ex 1em',
_('Could not fetch metadata for book'),
E.div(style='margin: 1ex 1em')
))
safe_set_inner_html(c.lastChild.lastChild, xhr.error_html)
def fetch_metadata(container_id, book_id, proceed):
nonlocal current_fetch
container = document.getElementById(container_id)
if not container:
return
if current_fetch:
current_fetch.abort()
current_fetch = ajax('interface-data/book-metadata/' + book_id, metadata_fetched.bind(None, container_id, book_id, proceed),
query={'library_id':current_library_id(), 'vl':current_virtual_library()})
current_fetch.send()
container = container.lastChild
clear(container)
container.appendChild(E.div(
style='margin: 1ex 1em',
create_spinner(), '\xa0' + _('Fetching metadata for the book, please wait') + '…',
))
def create_book_details(container):
q = parse_url_params()
current_book_id = q.book_id
if current_book_id is undefined or current_book_id is None:
no_book(container)
return
current_book_id = int(current_book_id)
container_id = container.parentNode.id
if current_book_id is not 0 and book_metadata(current_book_id):
render_book(container_id, current_book_id)
add_top_bar_buttons(container_id)
else:
fetch_metadata(container_id, current_book_id, proceed_after_succesful_fetch_metadata)
def report_load_failure(container):
report_a_load_failure(
container, _('Failed to load books from calibre library, with error:'),
load_status.error_html)
def check_for_books_loaded():
container = this
if load_status.loading:
conditional_timeout(container.id, 5, check_for_books_loaded)
return
container = container.lastChild
clear(container)
if not load_status.ok:
report_load_failure(container)
return
create_book_details(container)
def init(container_id):
container = document.getElementById(container_id)
close_action, close_icon = back, 'close'
q = parse_url_params()
ca = q.close_action
if ca is 'home':
close_action, close_icon = def(): home();, 'home'
elif ca is 'book_list':
close_action = def():
show_panel('book_list', {'book_id':q.book_id})
create_top_bar(container, title=_('Book details'), action=close_action, icon=close_icon)
window.scrollTo(0, 0) # Ensure we are at the top of the window
container.appendChild(E.div(class_=CLASS_NAME))
container.lastChild.appendChild(E.div(_('Loading books from the calibre library, please wait...'), style='margin: 1ex 1em'))
conditional_timeout(container_id, 5, check_for_books_loaded)
def show_subsequent_panel(name, replace=False):
q = parse_url_params()
q.book_id = (read_book.book_id or q.book_id) + ''
show_panel('book_details^' + name, query=q, replace=replace)
def edit_metadata():
q = parse_url_params()
q.book_id = (read_book.book_id or q.book_id) + ''
show_panel('edit_metadata', query=q, replace=False)
def convert_book():
q = parse_url_params()
q.book_id = (read_book.book_id or q.book_id) + ''
show_panel('convert_book', query=q, replace=False)
def create_more_actions_panel(container_id):
container = document.getElementById(container_id)
create_top_bar(container, title=_('More actions…'), action=back, icon='close')
if get_session_data().get('show_all_metadata'):
title, subtitle = _('Show important metadata'), _('Show only the important metadata fields in the book details')
else:
title, subtitle = _('Show all metadata'), _('Show all metadata fields in the book details')
items = [
create_item(_('Search the internet'), subtitle=_('Search for this author or book on various websites'),
action=def():
show_subsequent_panel('search_internet', replace=True)
),
create_item(title, subtitle=subtitle, action=toggle_fields),
create_item(_('Copy to library'), subtitle=_('Copy or move this book to another calibre library'),
action=def():
show_subsequent_panel('copy_to_library', replace=True)
),
]
container.appendChild(E.div())
create_item_list(container.lastChild, items)
def return_to_book_details():
q = parse_url_params()
show_panel('book_details', query=q, replace=True)
def toggle_fields():
sd = get_session_data()
sd.set('show_all_metadata', False if sd.get('show_all_metadata') else True)
return_to_book_details()
def url_for(template, data):
def eqc(x):
return encode_query_component(x).replace(/%20/g, '+')
return template.format(title=eqc(data.title), author=eqc(data.author))
def search_internet(container_id):
if not render_book.book_id or not book_metadata(render_book.book_id):
return return_to_book_details()
container = document.getElementById(container_id)
create_top_bar(container, title=_('Search the internet'), action=back, icon='close')
mi = book_metadata(render_book.book_id)
data = {'title':mi.title, 'author':mi.authors[0] if mi.authors else _('Unknown')}
interface_data = get_interface_data()
def link_for(name, template):
return E.a(name, class_='simple-link', href=url_for(template, data), target="_blank")
author_links = E.ul()
book_links = E.ul()
if interface_data.search_the_net_urls:
for entry in interface_data.search_the_net_urls:
links = book_links if entry.type is 'book' else author_links
links.appendChild(E.li(link_for(entry.name, entry.url)))
for name, url in [
(_('Goodreads'), 'https://www.goodreads.com/book/author/{author}'),
(_('Wikipedia'), 'https://en.wikipedia.org/w/index.php?search={author}'),
(_('Google books'), 'https://www.google.com/search?tbm=bks&q=inauthor:%22{author}%22'),
]:
author_links.appendChild(E.li(link_for(name, url)))
for name, url in [
(_('Goodreads'), 'https://www.goodreads.com/search?q={author}+{title}&search%5Bsource%5D=goodreads&search_type=books&tab=books'),
(_('Google books'), 'https://www.google.com/search?tbm=bks&q=inauthor:%22{author}%22+intitle:%22{title}%22'),
(_('Amazon'), 'https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords={author}+{title}'),
]:
book_links.appendChild(E.li(link_for(name, url)))
container.appendChild(E.div(class_=SEARCH_INTERNET_CLASS,
safe_set_inner_html(E.h2(), _('Search for the author <i>{}</i> at:').format(data.author)),
author_links,
E.hr(),
safe_set_inner_html(E.h2(), _('Search for the book <i>{}</i> at:').format(data.title)),
book_links,
))
# Copy to library {{{
def do_copy_to_library(book_id, target_library_id, target_library_name):
def handle_result(move, close_func, end_type, xhr, ev):
close_func()
title = book_metadata(book_id).title
return_to_book_details()
if end_type is 'abort':
return
if end_type is not 'load':
error_dialog(_('Failed to copy book'), _(
'Failed to copy the book "{}" click "Show details" for more information.').format(title),
xhr.error_html)
return
try:
response = JSON.parse(xhr.responseText)[book_id]
if not response:
raise Exception('bad')
except:
error_dialog(_('Failed to copy book'), _(
'Failed to copy the book "{}" got an invalid response from calibre').format(title))
return
if not response.ok:
error_dialog(_('Failed to copy book'), _(
'Failed to copy the book "{}" click "Show details" for more information.').format(title),
response.payload
)
return
if response.action is 'duplicate':
warning_dialog(_('Book already exists'), _(
'Could not copy as a book with the same title and authors already exists in the {} library').format(target_library_name))
elif response.action is 'automerge':
warning_dialog(_('Book merged'), _(
'The files from the book were merged into a book with the same title and authors in the {} library').format(target_library_name))
if move:
refresh_after_delete(book_id, current_library_id())
def trigger_copy(container_id, move, close_func):
try:
choice = document.querySelector(f'#{dupes_id} input[name="dupes"]:checked').value
except:
choice = document.querySelector(f'#{dupes_id} input[name="dupes"]').value
sd.set('copy_to_library_dupes', choice)
duplicate_action, automerge_action = choice.split(';', 2)
data = {'book_ids':v'[book_id]', 'move': move, 'duplicate_action': duplicate_action, 'automerge_action': automerge_action}
container = document.getElementById(container_id)
clear(container)
container.appendChild(E.div(
_('Contacting calibre to copy book, please wait...')))
ajax_send(f'cdb/copy-to-library/{target_library_id}/{current_library_id()}',
data, handle_result.bind(None, move, close_func))
def radio(value, text):
return E.div(style='margin-top: 1rem', E.input(type='radio', name='dupes', value=value, checked=value is saved_value), '\xa0', E.span(text))
title = book_metadata(book_id).title
dupes_id = unique_id()
sd = get_session_data()
saved_value = sd.get('copy_to_library_dupes')
create_custom_dialog(_(
'Copy book to "{target_library_name}"').format(target_library_name=target_library_name),
def (container, close_func):
container_id = ensure_id(container)
container.appendChild(E.div(
E.div(_('Copying: {}').format(title)),
E.div(id=dupes_id,
E.p(_('If there are already existing books in "{}" with the same title and authors,'
' how would you like to handle them?').format(target_library_name)),
radio('add;overwrite', _('Copy anyway')),
radio('ignore;overwrite', _('Do not copy')),
radio('add_formats_to_existing;overwrite', _('Merge into existing books, overwriting existing files')),
radio('add_formats_to_existing;ignore', _('Merge into existing books, keeping existing files')),
radio('add_formats_to_existing;new record', _('Merge into existing books, putting conflicting files into a new book record')),
),
E.div(
class_='button-box',
create_button(_('Copy'), None, trigger_copy.bind(None, container_id, False, close_func), highlight=True),
'\xa0',
create_button(_('Move'), None, trigger_copy.bind(None, container_id, True, close_func)),
'\xa0',
create_button(_('Cancel'), None, close_func),
)
))
if not container.querySelector(f'#{dupes_id} input[name="dupes"]:checked'):
container.querySelector(f'#{dupes_id} input[name="dupes"]').checked = True
)
def copy_to_library(container_id):
if not render_book.book_id or not book_metadata(render_book.book_id):
return return_to_book_details()
container = document.getElementById(container_id)
create_top_bar(container, title=_('Copy to library'), action=back, icon='close')
libraries = all_libraries()
container.appendChild(E.div(class_=COPY_TO_LIBRARY_CLASS))
container = container.lastChild
if libraries.length < 2:
container.appendChild(E.div(_('There are no other calibre libraries available to copy the book to')))
return
container.appendChild(E.h2(_('Choose the library to copy to below')))
items = []
for library_id, library_name in libraries:
if library_id is current_library_id():
continue
items.push(create_item(library_name, action=do_copy_to_library.bind(None, render_book.book_id, library_id, library_name)))
container.appendChild(E.div())
create_item_list(container.lastChild, items)
# }}}
def delete_book():
start_delete_book(current_library_id(), render_book.book_id, render_book.title or _('Unknown'))
set_panel_handler('book_details', init)
set_panel_handler('book_details^more_actions', create_more_actions_panel)
set_panel_handler('book_details^search_internet', search_internet)
set_panel_handler('book_details^copy_to_library', copy_to_library)