Debian Codegraph @

src/pyj/read_book/goto.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 import traceback
6 from elementmaker import E
7 from gettext import gettext as _
8
9 from ajax import encode_query_component
10 from book_list.item_list import build_list, create_item
11 from dom import ensure_id, set_css
12 from modals import error_dialog
13 from read_book.globals import current_book, runtime, ui_operations
14 from read_book.prefs.head_foot import format_pos
15 from read_book.toc import get_border_nodes, get_toc_maps
16 from widgets import create_button
17
18
19 def create_goto_list(onclick, current_position_data, page_list, show_sub_panel=None):
20 ans = E.div()
21 items = []
22 location_text = format_pos(current_position_data.progress_frac, current_position_data.book_length) + ' :: '
23 if current_position_data.cfi:
24 location_text += current_position_data.cfi
25 landmarks = current_book().manifest.landmarks
26 toc = current_book().manifest.toc
27 id_map = get_toc_maps(toc)[1]
28 before, after = get_border_nodes(toc, id_map)
29 if after:
30 items.push(create_item(_('Next section'), icon='caret-right', subtitle=after.title, action=onclick.bind(None, after.dest, after.frag)))
31 if before:
32 items.push(create_item(_('Previous section'), icon='caret-left', subtitle=before.title, action=onclick.bind(None, before.dest, before.frag)))
33 items.push(create_item(_('Book start'), action=onclick.bind(None, def (view): view.goto_doc_boundary(True);)))
34 items.push(create_item(_('Book end'), action=onclick.bind(None, def (view): view.goto_doc_boundary(False);)))
35
36 def panel_action(name, fallback_fn):
37 # Use show_sub_panel when available so the goto panel stays in the
38 # stack and closing the sub-panel returns here; otherwise fall back
39 # to the regular onclick which closes the overlay first.
40 if show_sub_panel:
41 return def (): show_sub_panel(name)
42 return onclick.bind(None, fallback_fn)
43
44 items.push(create_item(_('Metadata'), subtitle=_('Details about this book'),
45 action=panel_action('metadata', def (view): view.overlay.show_metadata();)))
46 if not runtime.is_standalone_viewer:
47 items.push(create_item(_('Book page in library'), subtitle=_('The page for this book in the calibre library'), action=onclick.bind(None, def (view):
48 view.open_book_page()
49 )))
50 items.push(create_item(_('Location'), subtitle=location_text,
51 action=panel_action('location', def (view): view.overlay.show_ask_for_location();)))
52 if page_list and page_list.length > 0:
53 items.push(create_item(_('Page number'), subtitle=_('Typically the page number from a paper edition of this book'),
54 action=panel_action('page_list', def (view): view.overlay.show_page_list(page_list);)))
55 for l in landmarks:
56 items.push(create_item(l.title, action=onclick.bind(None, l.dest, l.frag)))
57 build_list(ans, items)
58 return ans
59
60
61 def get_next_section(forward):
62 toc = current_book().manifest.toc
63 id_map = get_toc_maps(toc)[1]
64 before, after = get_border_nodes(toc, id_map)
65 return after if forward else before
66
67
68 def create_goto_panel(current_position_data, book, container, onclick, show_sub_panel=None):
69 panel = create_goto_list(onclick, current_position_data, book.manifest.page_list, show_sub_panel)
70 set_css(container, display='flex', flex_direction='column')
71 set_css(panel, flex_grow='10')
72 container.appendChild(panel)
73
74
75 def create_page_list_overlay(book, overlay, container):
76 list_container = E.div()
77 pl = overlay.view.current_pagelist_items
78
79 def goto(x):
80 overlay.view.goto_pagelist_item(x)
81 overlay.hide()
82
83 def goto_matching_page(text):
84 for x in page_list:
85 if x.pagenum is text:
86 goto(x)
87 return
88 q = text.toLowerCase()
89 for x in page_list:
90 if x.pagenum.toLowerCase() is q:
91 goto(x)
92 return
93 error_dialog(_('No such page'), _('No page {!r} exists').format(text))
94
95 if pl and pl.length > 0:
96 if pl.length is 1:
97 text = _('Currently on page: {}').format(pl[0].pagenum)
98 else:
99 text = _('Currently on pages: {}').format(pl[0].pagenum + ' - ' + pl[1].pagenum)
100 container.appendChild(E.div(style='margin: 1em', text))
101 search_input = E.div(
102 E.div(
103 E.input(type='text', placeholder=_('Enter a page number'), onkeydown=def (ev):
104 if ev.key is 'Enter':
105 text = ev.target.value
106 if text:
107 goto_matching_page(text)
108 ),
109 ), style='margin: 1em',
110 )
111 container.appendChild(search_input)
112 container.appendChild(list_container)
113 page_list = book.manifest.page_list or []
114 items = []
115
116 for x in page_list:
117 items.push(create_item(x.pagenum, action=goto.bind(None, x),
118 ))
119 build_list(list_container, items)
120
121
122 def create_location_overlay(current_position_data, book, overlay, container):
123 container_id = ensure_id(container)
124 container.appendChild(E.div(style='margin: 0 1rem'))
125 container = container.lastChild
126 current_cfi = current_position_data.cfi
127 calibre_book_url = book?.calibre_book_url
128
129 def copy_button(text_to_copy):
130 return create_button(_('Copy'), action=def ():
131 src = document.querySelector(f'#{container_id} input')
132 orig = src.value
133 src.value = text_to_copy
134 src.focus()
135 src.select()
136 try:
137 document.execCommand('copy')
138 finally:
139 src.value = orig
140 )
141
142 def display_and_copy(label, text):
143 container.appendChild(E.div(
144 style='margin: 1rem; margin-bottom: calc(1rem - 1ex); display: flex; align-items: baseline; flex-wrap: wrap',
145 E.div(style='flex-grow: 10; text-overflow: ellipsis; margin-bottom: 1ex',
146 label, ' ', E.span(text, style='font-size: smaller; font-family: monospace; overflow-wrap: anywhere')),
147 copy_button(text)
148 ))
149
150 if current_cfi:
151 display_and_copy(_('Current location:'), current_cfi)
152
153 def goto_cfi(cfi):
154 if ui_operations.goto_cfi(cfi):
155 overlay.hide()
156 else:
157 error_dialog(_('No such location'), _(
158 'No location {} found').format(cfi))
159
160 def goto_ref(ref):
161 ref = ref.replace(/,/g, '.')
162 if ui_operations.goto_reference(ref):
163 overlay.hide()
164 else:
165 error_dialog(_('No such reference'), _(
166 'No reference {} found').format(ref))
167
168 if current_position_data.book_length > 0:
169 container.appendChild(
170 E.div(style='margin: 1rem', _('Current position: {}').format(
171 format_pos(current_position_data.progress_frac, current_position_data.book_length))))
172
173 container.appendChild(
174 E.div(style='margin: 1rem', _(
175 'Type the position, location or reference below. For a reference type ref: followed by the reference:')))
176
177 def goto_pos():
178 src = document.querySelector(f'#{container_id} [name=newpos]').value
179 if not src:
180 return
181 if src.indexOf('epubcfi(') is 0:
182 return goto_cfi(src)
183 if src.indexOf('ref:') is 0:
184 return goto_ref(src[len('ref:'):])
185 try:
186 ui_operations.goto_book_position(float(src))
187 except:
188 error_dialog(_('Not a valid book position'), _(
189 '{} is not a valid book position').format(src), traceback.format_exc())
190 else:
191 overlay.hide()
192
193 container.appendChild(E.div(
194 style='margin: 1rem;',
195 E.div(
196 style='display: flex; align-items: baseline; flex-wrap: wrap',
197 E.label(_('Go to:'), style='margin-right: 1rem'),
198 E.input(name='newpos', type='text', style='flex-grow: 10; margin-right: 1rem', onkeydown=def (ev):
199 if ev.key is 'Enter':
200 goto_pos()
201 ), E.span(' '),
202 create_button(_('Go'), action=goto_pos)
203 )
204 ))
205 container.appendChild(
206 E.div(style='margin: 1rem', _(
207 'Note that using a position is not very robust, it is best to use either the location or reference.')))
208
209 if calibre_book_url:
210 if current_cfi:
211 calibre_book_url += '?open_at=' + encode_query_component(current_cfi)
212 display_and_copy(_('URL for this position:'), calibre_book_url)
213 elif not runtime.is_standalone_viewer:
214 display_and_copy(_('URL for this position:'), window.top.location.toString())
215 window.setTimeout(def ():
216 container = document.getElementById(container_id)
217 if container:
218 container.querySelector('[name=newpos]').focus()
219 , 10)