From 5b03d668ec9396a2ed1ab8cc12cc7441bef1d426 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 15 Feb 2017 18:04:01 +0530 Subject: [PATCH] Start work on porting book reader --- src/pyj/book_list/book_details.pyj | 4 ++-- src/pyj/book_list/globals.pyj | 6 ++++++ src/pyj/book_list/main.pyj | 5 ++--- src/pyj/book_list/router.pyj | 13 ++++++------- src/pyj/read_book/db.pyj | 5 ++++- src/pyj/read_book/ui.pyj | 19 +++++++++++++++---- src/pyj/read_book/view.pyj | 7 ++++--- 7 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/pyj/book_list/book_details.pyj b/src/pyj/book_list/book_details.pyj index 0da81afc19..6416cafe69 100644 --- a/src/pyj/book_list/book_details.pyj +++ b/src/pyj/book_list/book_details.pyj @@ -14,7 +14,7 @@ from date import format_date from session import get_interface_data from utils import fmt_sidx, parse_url_params, conditional_timeout -from book_list.router import back +from book_list.router import back, open_book from book_list.library_data import book_metadata, cover_url, set_book_metadata, current_library_id, library_data, download_url, load_status from book_list.top_bar import create_top_bar, set_title, add_button from book_list.ui import set_panel_handler @@ -311,7 +311,7 @@ def preferred_format(book_id): def read_format(book_id, fmt): - get_boss().read_book(self.current_book_id, fmt, self.interface_data.metadata[self.current_book_id]) + open_book(book_id, fmt) def read_book(book_id): diff --git a/src/pyj/book_list/globals.pyj b/src/pyj/book_list/globals.pyj index b21d79b246..b65fc9ce4f 100644 --- a/src/pyj/book_list/globals.pyj +++ b/src/pyj/book_list/globals.pyj @@ -33,3 +33,9 @@ def get_translations(val): if val: get_translations.ans = val return get_translations.ans + + +def get_read_ui(val): + if val: + get_read_ui.ans = val + return get_read_ui.ans diff --git a/src/pyj/book_list/main.pyj b/src/pyj/book_list/main.pyj index 0c83399e60..438de52214 100644 --- a/src/pyj/book_list/main.pyj +++ b/src/pyj/book_list/main.pyj @@ -15,7 +15,7 @@ from book_list.constants import book_list_container_id, read_book_container_id from book_list.library_data import fetch_init_data, update_library_data, url_books_query from book_list.theme import get_color from book_list.router import update_window_title, set_default_mode_handler, apply_url, set_mode_handler, on_pop_state -from book_list.globals import get_db, set_session_data +from book_list.globals import get_db, set_session_data, get_read_ui from book_list.ui import apply_url_state as book_list_mode_handler from read_book.ui import ReadUI @@ -43,10 +43,8 @@ def onerror(msg, script_url, line_number, column_number, error_object): except: console.log('There was an error in the unhandled exception handler') -read_ui = None def init_ui(): - nonlocal read_ui install_event_filters() set_default_mode_handler(book_list_mode_handler) window.onerror = onerror @@ -69,6 +67,7 @@ def init_ui(): read_ui = ReadUI() get_db(read_ui.db) set_mode_handler('read_book', read_ui.apply_url_state.bind(read_ui)) + get_read_ui(read_ui) apply_url() diff --git a/src/pyj/book_list/router.pyj b/src/pyj/book_list/router.pyj index ce55879d00..d2eb2953c7 100644 --- a/src/pyj/book_list/router.pyj +++ b/src/pyj/book_list/router.pyj @@ -48,11 +48,11 @@ def apply_url(ignore_handler): handler(data) -history_count = 0 +def open_book(book_id, fmt, replace=False): + push_state({'book_id':book_id, 'fmt':fmt}, replace=replace, mode=read_book_mode) def push_state(query, replace=False, mode='book_list', call_handler=True): - nonlocal history_count query = {k:query[k] for k in query} if mode is not 'book_list': query.mode = mode @@ -61,19 +61,18 @@ def push_state(query, replace=False, mode='book_list', call_handler=True): window.history.replaceState(None, '', query) else: window.history.pushState(None, '', query) - history_count += 1 + push_state.history_count += 1 apply_url(not call_handler) +push_state.history_count = 0 def on_pop_state(ev): - nonlocal history_count - history_count = max(0, history_count - 1) + push_state.history_count = max(0, push_state.history_count - 1) apply_url() def back(): - nonlocal history_count - if history_count > 0: + if push_state.history_count > 0: window.history.back() else: cq = get_current_query() diff --git a/src/pyj/read_book/db.pyj b/src/pyj/read_book/db.pyj index 82ac33fcdf..6901a8f991 100644 --- a/src/pyj/read_book/db.pyj +++ b/src/pyj/read_book/db.pyj @@ -6,8 +6,11 @@ from gettext import gettext as _ from encodings import base64encode, base64decode from modals import error_dialog +from book_list.library_data import current_library_id from book_list.router import is_reading_book +current_library_id + def upgrade_schema(idb, old_version, new_version): print('upgrade_schema:', old_version, new_version) if not idb.objectStoreNames.contains('books'): @@ -128,7 +131,7 @@ class DB: # The key has to be a JavaScript array as otherwise it cannot be stored # into indexed db, because the RapydScript list has properties that # refer to non-serializable objects like functions. - key = v'[self.interface_data.library_id, book_id, fmt]' + key = v'[current_library_id(), book_id, fmt]' self.do_op(['books'], key, _('Failed to read from the books database'), def(result): proceed(result or { 'key':key, diff --git a/src/pyj/read_book/ui.pyj b/src/pyj/read_book/ui.pyj index 6975acac3a..66c1644b76 100644 --- a/src/pyj/read_book/ui.pyj +++ b/src/pyj/read_book/ui.pyj @@ -13,6 +13,7 @@ from utils import human_readable, debounce from book_list.constants import read_book_container_id from read_book.db import get_db from book_list.router import update_window_title +from book_list.library_data import library_data, current_library_id from read_book.view import View RENDER_VERSION = __RENDER_VERSION__ @@ -21,6 +22,7 @@ MATHJAX_VERSION = "__MATHJAX_VERSION__" class ReadUI: def __init__(self): + self.base_url_data = {} self.current_metadata = {'title': _('Unknown book')} self.current_book_id = None self.manifest_xhr = None @@ -72,7 +74,7 @@ class ReadUI: a = div.lastChild.firstChild if self.current_book_id: a.setAttribute('href', encode_query({ - 'library_id':self.interface_data.library_id, + 'library_id':current_library_id(), 'book-id': (self.current_book_id + ''), 'panel':'book-details' })) @@ -106,7 +108,7 @@ class ReadUI: def reload_book(self): book_id = self.base_url_data.book_id - metadata = self.metadata or self.interface_data.metadata[book_id] + metadata = self.metadata or library_data.metadata[book_id] self.load_book(book_id, self.base_url_data.fmt, metadata, True) def redisplay_book(self): @@ -136,7 +138,7 @@ class ReadUI: def start_load(self, book_id, fmt, metadata, force_reload): self.current_book_id = book_id - metadata = metadata or self.interface_data.metadata[book_id] + metadata = metadata or library_data.metadata[book_id] self.current_metadata = metadata or {'title':_('Book id #') + book_id} update_window_title('', self.current_metadata.title) self.init_ui() @@ -406,4 +408,13 @@ class ReadUI: self.view.display_book(book) def apply_url_state(self, current_query): - pass + same = True + current_state = self.url_data + for k in v"['book_id', 'fmt']": + if str(current_query[k]) is not str(current_state[k]): + same = False + break + if same: + pass # TODO: Implement this to move book position if different from current + else: + self.load_book(int(current_query.book_id), current_query.fmt, library_data.metadata[current_query.book_id]) diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index f846a2b82c..926cc2c24f 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -6,6 +6,7 @@ from dom import set_css, add_extra_css, build_rule, svgicon from elementmaker import E from gettext import gettext as _ from utils import html_escape +from session import get_interface_data from modals import error_dialog, warning_dialog from book_list.globals import get_session_data, main_js, get_translations @@ -269,7 +270,7 @@ class View: self.ui.db.update_last_read_time(book) self.loaded_resources = {} pos = {'replace_history':True} - unkey = username_key(self.ui.interface_data.username) + unkey = username_key(get_interface_data().username) name = book.manifest.spine[0] cfi = None q = parse_url_params() @@ -355,8 +356,8 @@ class View: def on_update_cfi(self, data): self.currently_showing.bookpos = data.cfi - push_state(self.url_data, replace=data.replace_history, mode=read_book_mode) - unkey = username_key(self.ui.interface_data.username) + push_state(self.ui.url_data, replace=data.replace_history, mode=read_book_mode) + unkey = username_key(get_interface_data().username) if not self.book.last_read_position: self.book.last_read_position = {} self.book.last_read_position[unkey] = data.cfi