Debian Codegraph @

src/pyj/book_list/router.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 from elementmaker import E
6 from gettext import gettext as _
7
8 from ajax import absolute_path, encode_query
9 from book_list.constants import book_list_container_id, read_book_container_id, cs_top_bar_host_id
10 from book_list.globals import get_current_query, get_session_data
11 from book_list.library_data import current_library_id
12 from dom import clear
13 from modals import close_all_modals
14 from read_book.globals import ui_operations
15 from utils import (
16 encode_query_with_path, parse_url_params, request_full_screen,
17 safe_set_inner_html
18 )
19
20 mode_handlers = {}
21 default_mode_handler = None
22 read_book_mode = 'read_book'
23
24
25 def set_mode_handler(mode, handler):
26 mode_handlers[mode] = handler
27
28
29 def set_default_mode_handler(handler):
30 nonlocal default_mode_handler
31 default_mode_handler = handler
32
33
34 def update_window_title(subtitle, title='calibre', sep=' :: '):
35 extra = (sep + subtitle) if subtitle else ''
36 document.title = title + extra
37
38
39 def is_reading_book():
40 cq = get_current_query()
41 return cq and cq.mode is read_book_mode
42
43
44 def apply_mode():
45 divid = read_book_container_id if is_reading_book() else book_list_container_id
46 for div in document.getElementById(divid).parentNode.childNodes:
47 div.style.display = 'block' if div.id is divid else 'none'
48 if is_reading_book():
49 host = document.getElementById(cs_top_bar_host_id)
50 if host:
51 clear(host)
52
53
54 def apply_url(ignore_handler):
55 # Ensure the current history entry has a state marker, so back() works
56 # reliably even after a page refresh.
57 try:
58 if not (window.history.state and window.history.state.cs_url_state?):
59 window.history.replaceState({'cs_url_state': True, 'cs_idx': 0}, '', window.location.href)
60 except Exception:
61 pass
62 close_all_modals() # needed to close any error dialogs, etc when clicking back or forward in the browser or using push_state() to go to a new page
63 prev = get_current_query()
64 data = parse_url_params()
65 data.mode = data.mode or 'book_list'
66 if prev?.mode is read_book_mode and data.mode is not read_book_mode:
67 # Ensure read-aloud/TTS is stopped when leaving reader mode from
68 # global navigation paths that do not explicitly call close_book().
69 try:
70 if ui_operations.close_book:
71 ui_operations.close_book()
72 except Exception:
73 pass
74 get_current_query(data)
75 apply_mode()
76 # Allow the main UI (e.g. sidebar) to update highlighting when URL changes
77 try:
78 cb = window.cs_update_sidebar_active
79 except Exception:
80 cb = None
81 if cb:
82 try:
83 cb()
84 except Exception:
85 pass
86 if not ignore_handler:
87 handler = mode_handlers[data.mode] or default_mode_handler
88 handler(data)
89
90
91 def request_full_screen_if_wanted():
92 opt = get_session_data().get('fullscreen_when_opening')
93 has_touch = v'"ontouchstart" in window'
94 at_left = window.screenLeft is 0
95 likely_wants_fullscreen = has_touch and at_left
96 if (opt is 'auto' and likely_wants_fullscreen) or opt is 'always':
97 # Note that full screen requests only succeed if they are in response to a
98 # user action like clicking/tapping a button
99 request_full_screen()
100
101
102 def open_book(book_id, fmt, library_id=None, replace=False, extra_query=None):
103 request_full_screen_if_wanted()
104 library_id = library_id or current_library_id()
105 q = {'book_id': book_id, 'fmt': fmt, 'library_id': library_id}
106 if extra_query and jstype(extra_query) is 'object':
107 Object.assign(q, extra_query)
108 push_state(q, replace=replace, mode=read_book_mode)
109
110
111 def open_book_url_in_library(library_id, book_id, fmt, extra_query):
112 ans = absolute_path('')
113 q = {'book_id': book_id, 'fmt': fmt, 'mode': read_book_mode}
114 if library_id:
115 q.library_id = library_id
116 if extra_query:
117 Object.assign(q, extra_query)
118 return ans + encode_query(q, '#')
119
120
121 def search_url_in_library(library_id, search, vl, sort):
122 ans = absolute_path('')
123 q = {'mode': 'book_list', 'search': search, 'panel': 'book_list'}
124 if library_id:
125 q.library_id = library_id
126 if vl:
127 q.vl = vl
128 if sort:
129 q.sort = sort
130 return ans + encode_query(q, '#')
131
132
133 def open_book_url(book_id, fmt, extra_query):
134 return open_book_url_in_library(current_library_id(), book_id, fmt, extra_query)
135
136
137 def show_note(field, item_id, item_value, replace=False, library_id=None, close_action='back', panel='show_note'):
138 lid = library_id or current_library_id()
139 q = {'field': field, 'item': item_value + '', 'item_id': (item_id or '') + '', 'panel': panel}
140 if panel is 'show_note':
141 q.close_action = close_action
142 if lid:
143 q.library_id = lid
144 push_state(q, replace=replace)
145
146
147 def push_state(query, replace=False, mode='book_list', call_handler=True):
148 query = {k: query[k] for k in query if query[k]}
149 if mode is not 'book_list':
150 query.mode = mode
151 query = encode_query_with_path(query)
152 try:
153 cur = window.history.state or {}
154 cur_idx = cur.cs_idx or 0
155 if replace:
156 window.history.replaceState({'cs_url_state': True, 'cs_idx': cur_idx}, '', query)
157 else:
158 window.history.pushState({'cs_url_state': True, 'cs_idx': cur_idx + 1}, '', query)
159 except Exception:
160 # Fallback to old behavior if history state is unavailable for some reason
161 if replace:
162 window.history.replaceState(None, '', query)
163 else:
164 window.history.pushState(None, '', query)
165 apply_url(not call_handler)
166
167
168 def on_pop_state(ev):
169 apply_url()
170
171
172 def back():
173 try:
174 idx = (window.history.state and window.history.state.cs_idx) or 0
175 except Exception:
176 idx = 0
177 if idx > 0:
178 window.history.back()
179 else:
180 cq = get_current_query()
181 handler = mode_handlers[cq.mode] or default_mode_handler
182 if handler.back_from_current:
183 q = handler.back_from_current(cq)
184 else:
185 q = {}
186 push_state(q, replace=True)
187
188
189 def home(replace=False):
190 push_state({})
191
192
193 def report_a_load_failure(container, msg, error_html):
194 err = E.div()
195 safe_set_inner_html(err, error_html)
196 container.appendChild(E.div(
197 style='margin: 1ex 1em',
198 E.div(msg),
199 err,
200 E.div(
201 style='margin-top: 1em; border-top: solid 1px currentColor; padding-top: 1ex;',
202 E.a(onclick=def (): home(replace=True);, href='javascript: void(0)', class_='blue-link', _('Go back to the home page')))
203 ),
204 )