| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals, bound_methods |
| 4 |
|
| 5 |
import traceback |
| 6 |
from elementmaker import E |
| 7 |
from gettext import gettext as _, install |
| 8 |
|
| 9 |
from ajax import ajax |
| 10 |
from book_list.book_details import set_panel_handlers as book_details_init |
| 11 |
from book_list.constants import book_list_container_id, read_book_container_id, cs_top_bar_host_id, INIT_ENDPOINT |
| 12 |
from book_list.convert_book import set_panel_handlers as convert_book_init |
| 13 |
from book_list.edit_metadata import set_panel_handlers as edit_metadata_init |
| 14 |
from book_list.fts import set_panel_handlers as fts_init |
| 15 |
from book_list.globals import get_db, set_session_data |
| 16 |
from book_list.home import set_panel_handlers as home_init |
| 17 |
from book_list.home import update_recently_read_by_user |
| 18 |
from book_list.library_data import fetch_init_data, update_library_data, url_books_query |
| 19 |
from book_list.local_books import set_panel_handlers as local_books_init |
| 20 |
from book_list.nav_list import set_panel_handlers as nav_list_init |
| 21 |
from book_list.router import update_window_title, set_default_mode_handler, apply_url, set_mode_handler, on_pop_state |
| 22 |
from book_list.show_note import set_panel_handlers as show_note_init |
| 23 |
from book_list.sidebar import create_sidebar |
| 24 |
from book_list.theme import get_color, get_font_family, css_for_variables, apply_book_list_theme, bind_book_list_color_scheme_listener |
| 25 |
from book_list.ui import apply_url_state as book_list_mode_handler |
| 26 |
from book_list.ui_settings import set_panel_handlers as ui_settings_init |
| 27 |
from book_list.views import set_panel_handlers as views_init |
| 28 |
from dom import get_widget_css |
| 29 |
from modals import create_modal_container, error_dialog |
| 30 |
from popups import install_event_filters |
| 31 |
from read_book.ui import ReadUI |
| 32 |
from session import get_interface_data, UserSessionData, update_interface_data, get_translations |
| 33 |
from utils import safe_set_inner_html |
| 34 |
|
| 35 |
|
| 36 |
def remove_initial_progress_bar(): |
| 37 |
p = document.getElementById('page_load_progress') |
| 38 |
if p: |
| 39 |
p.parentNode.removeChild(p) |
| 40 |
|
| 41 |
|
| 42 |
source_map_set = False |
| 43 |
|
| 44 |
|
| 45 |
def source_map_received(data, end_type, xhr, ev): |
| 46 |
if end_type is 'abort': |
| 47 |
return |
| 48 |
if end_type is 'load' and xhr.responseType is 'json': |
| 49 |
traceback.set_source_map_data(xhr.response) |
| 50 |
error_object = traceback.unserialize_error_for_formatting(data.error) |
| 51 |
details = traceback.format_exception(error_object).join('') |
| 52 |
error_dialog(data.title, data.msg, details) |
| 53 |
|
| 54 |
|
| 55 |
def load_source_map(data): |
| 56 |
nonlocal source_map_set |
| 57 |
xhr = ajax('index.js.map', source_map_received.bind(None, data)) |
| 58 |
xhr.responseType = 'json' |
| 59 |
xhr.send() |
| 60 |
source_map_set = True |
| 61 |
|
| 62 |
|
| 63 |
def onerror(msg, script_url, line_number, column_number, error_object): |
| 64 |
if error_object is None: |
| 65 |
# This happens for cross-domain errors (probably javascript injected |
| 66 |
# into the browser via extensions/ userscripts and the like). It also |
| 67 |
# happens all the time when using Chrom on Safari, so ignore this |
| 68 |
# type of error |
| 69 |
console.log(f'Unhandled error from external javascript, ignoring: {msg} {script_url} {line_number}') |
| 70 |
return |
| 71 |
console.log(error_object) |
| 72 |
fname = script_url.rpartition('/')[-1] or script_url |
| 73 |
msg = msg + '<br><span style="font-size:smaller">' + 'Error at {}:{}:{}'.format(fname, line_number, column_number or '') + '</span>' |
| 74 |
title = _('Unhandled error') |
| 75 |
if source_map_set: |
| 76 |
details = traceback.format_exception(error_object).join('') |
| 77 |
error_dialog(title, msg, details) |
| 78 |
else: |
| 79 |
load_source_map({'title': title, 'msg': msg, 'error': traceback.serialize_error_for_formatting(error_object)}) |
| 80 |
|
| 81 |
|
| 82 |
def init_ui(): |
| 83 |
document.body.style.fontFamily = get_font_family() |
| 84 |
install_event_filters() |
| 85 |
set_default_mode_handler(book_list_mode_handler) |
| 86 |
window.onerror = onerror |
| 87 |
setTimeout(def (): |
| 88 |
window.onpopstate = on_pop_state |
| 89 |
, 0) # We do this after event loop ticks over to avoid catching popstate events that some browsers send on page load |
| 90 |
|
| 91 |
translations = get_translations() |
| 92 |
install(translations) |
| 93 |
remove_initial_progress_bar() |
| 94 |
document.head.appendChild(E.style(css_for_variables() + '\n\n' + get_widget_css())) |
| 95 |
apply_book_list_theme() |
| 96 |
bind_book_list_color_scheme_listener() |
| 97 |
root, left = create_sidebar() |
| 98 |
|
| 99 |
shell_top = E.div(class_='cs-shell-top') |
| 100 |
shell_top.appendChild(E.div(id=cs_top_bar_host_id)) |
| 101 |
main_row = E.div(class_='cs-main') |
| 102 |
main_body = E.div(class_='cs-main-body') |
| 103 |
main_body.appendChild(E.div(id=book_list_container_id, style='display: none')) |
| 104 |
main_body.appendChild(E.div(id=read_book_container_id, style='display: none')) |
| 105 |
main_row.appendChild(left) |
| 106 |
main_row.appendChild(main_body) |
| 107 |
root.appendChild(shell_top) |
| 108 |
root.appendChild(main_row) |
| 109 |
document.body.appendChild(root) |
| 110 |
|
| 111 |
create_modal_container() |
| 112 |
update_window_title() |
| 113 |
read_ui = ReadUI() |
| 114 |
get_db(read_ui.db) |
| 115 |
book_details_init() |
| 116 |
convert_book_init() |
| 117 |
local_books_init() |
| 118 |
edit_metadata_init() |
| 119 |
fts_init() |
| 120 |
home_init() |
| 121 |
nav_list_init() |
| 122 |
show_note_init() |
| 123 |
ui_settings_init() |
| 124 |
views_init() |
| 125 |
|
| 126 |
set_mode_handler('read_book', read_ui.apply_url_state.bind(read_ui)) |
| 127 |
|
| 128 |
apply_url() |
| 129 |
|
| 130 |
|
| 131 |
def install_data_and_init_ui(raw_data): |
| 132 |
data = JSON.parse(raw_data) |
| 133 |
update_interface_data(data) |
| 134 |
update_recently_read_by_user(data.recently_read_by_user) |
| 135 |
interface_data = get_interface_data() |
| 136 |
sd = UserSessionData(interface_data.username, interface_data.user_session_data) |
| 137 |
set_session_data(sd) |
| 138 |
update_library_data(data) |
| 139 |
if data.translations?: |
| 140 |
get_translations(data.translations) |
| 141 |
init_ui() |
| 142 |
|
| 143 |
|
| 144 |
def on_data_loaded(end_type, xhr, ev): |
| 145 |
remove_initial_progress_bar() |
| 146 |
if end_type is 'load': |
| 147 |
install_data_and_init_ui(xhr.responseText) |
| 148 |
else: |
| 149 |
p = E.p(style='color:{}; font-weight: bold; font-size:1.5em'.format(get_color('window-error-foreground'))) |
| 150 |
if xhr.status is 401: |
| 151 |
msg = _('You are not authorized to view this site') |
| 152 |
else: |
| 153 |
msg = xhr.error_html |
| 154 |
safe_set_inner_html(p, msg) |
| 155 |
document.body.appendChild(p) |
| 156 |
|
| 157 |
|
| 158 |
def on_data_load_progress(loaded, total): |
| 159 |
p = document.querySelector('#page_load_progress > progress') |
| 160 |
if p: |
| 161 |
p.max = total |
| 162 |
p.value = loaded |
| 163 |
|
| 164 |
|
| 165 |
def load_interface_data(): |
| 166 |
idata = get_interface_data() |
| 167 |
query = {} |
| 168 |
if not idata.is_default: |
| 169 |
temp = UserSessionData(None, {}) # So that settings for anonymous users are preserved |
| 170 |
query = url_books_query(temp) |
| 171 |
ajax(INIT_ENDPOINT, on_data_loaded, on_data_load_progress, query=query).send() |
| 172 |
|
| 173 |
|
| 174 |
def do_update_interface_data(): |
| 175 |
t = get_translations() |
| 176 |
thash = t?.hash |
| 177 |
url = 'interface-data/update' |
| 178 |
if thash: |
| 179 |
url += '/' + thash |
| 180 |
ajax(url, def (end_type, xhr, ev): |
| 181 |
if end_type is 'load': |
| 182 |
data = JSON.parse(xhr.responseText) |
| 183 |
update_interface_data(data) |
| 184 |
update_recently_read_by_user(data.recently_read_by_user) |
| 185 |
if data.translations?: |
| 186 |
get_translations(data.translations) |
| 187 |
install(data.translations) |
| 188 |
else: |
| 189 |
update_recently_read_by_user() |
| 190 |
).send() |
| 191 |
|
| 192 |
|
| 193 |
@no_prune |
| 194 |
def main(): |
| 195 |
if document.createElement('iframe').srcdoc is undefined: |
| 196 |
document.body.innerHTML = '<p style="margin:1.5em; max-width: 40em">' + _( |
| 197 |
'You are using a browser that does not have the iframe srcdoc attribute.' |
| 198 |
' This is not supported. Use a better browser such as <b>Google Chrome</b> or <b>Mozilla' |
| 199 |
' Firefox</b>, instead. You can also use the simplified <a href="mobile">/mobile</a>' |
| 200 |
' interface if you do not have access to a modern browser.') |
| 201 |
return |
| 202 |
interface_data = get_interface_data() |
| 203 |
if interface_data.is_default or not interface_data.library_map: |
| 204 |
load_interface_data() |
| 205 |
else: |
| 206 |
sd = UserSessionData(interface_data.username, interface_data.user_session_data) |
| 207 |
set_session_data(sd) |
| 208 |
do_update_interface_data() |
| 209 |
fetch_init_data() |
| 210 |
init_ui() |