Implement Continue reading section on home page

This commit is contained in:
Kovid Goyal 2017-02-17 21:38:16 +05:30
parent e76d85ccc5
commit e59891a60f
3 changed files with 54 additions and 10 deletions

View File

@ -2,7 +2,7 @@
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
from __python__ import hash_literals, bound_methods from __python__ import hash_literals, bound_methods
from dom import ensure_id from dom import ensure_id, add_extra_css, build_rule
from elementmaker import E from elementmaker import E
from session import get_interface_data from session import get_interface_data
from gettext import gettext as _ from gettext import gettext as _
@ -11,15 +11,56 @@ from utils import conditional_timeout
from book_list.globals import get_db from book_list.globals import get_db
from book_list.top_bar import create_top_bar from book_list.top_bar import create_top_bar
from book_list.ui import set_default_panel_handler, show_panel from book_list.ui import set_default_panel_handler, show_panel
from book_list.router import update_window_title from book_list.router import update_window_title, open_book
CLASS_NAME = 'home-page'
add_extra_css(def():
ans = ''
sel = '.' + CLASS_NAME + ' '
ans += build_rule(sel + 'h2', padding='1em')
sel += '.recently-read img'
ans += build_rule(sel, max_width='25vw', height='auto')
return ans
)
def show_cover(blob, name, mt, book):
img = document.getElementById(this)
if not img:
return
img.onload = def():
window.URL.revokeObjectURL(this.src)
img.src = window.URL.createObjectURL(blob)
def read_book(library_id, book_id, fmt):
open_book(book_id, fmt, library_id)
def show_recent_stage2(books): def show_recent_stage2(books):
container = document.getElementById(this) container = document.getElementById(this)
if not container: if not container or not books.length:
return return
container.style.display = 'block'
container.appendChild(E.h2(_(
'Continue reading')))
container.appendChild(E.div(style='display:flex'))
images = container.lastChild
db = get_db()
for book in books: for book in books:
print(book.key, book.metadata.title, book.last_read) img = E.img(
alt=_('{} by {}').format(book.metadata.title, book.metadata.authors.join(' & '))
)
img_id = ensure_id(img)
images.appendChild(E.div(style='margin: 0 1em',
E.a(img, href='javascript: void(0)', title=img.alt,
onclick=read_book.bind(None, book.key[0], book.key[1], book.key[2])
),
))
if book.cover_name:
db.get_file(book, book.cover_name, show_cover.bind(img_id))
def show_recent(): def show_recent():
@ -34,11 +75,12 @@ def show_recent():
def init(container_id): def init(container_id):
update_window_title() update_window_title()
container = document.getElementById(container_id) container = document.getElementById(container_id)
container.classList.add(CLASS_NAME)
create_top_bar(container, run_animation=True) create_top_bar(container, run_animation=True)
interface_data = get_interface_data() interface_data = get_interface_data()
# Recent books # Recent books
recent = E.div(style='display:none') recent = E.div(style='display:none', class_='recently-read')
recent_container_id = ensure_id(recent) recent_container_id = ensure_id(recent)
container.appendChild(recent) container.appendChild(recent)
conditional_timeout(recent_container_id, 5, show_recent) conditional_timeout(recent_container_id, 5, show_recent)

View File

@ -6,6 +6,7 @@ from ajax import encode_query
from book_list.constants import read_book_container_id, book_list_container_id from book_list.constants import read_book_container_id, book_list_container_id
from book_list.globals import get_current_query from book_list.globals import get_current_query
from book_list.library_data import current_library_id
from utils import parse_url_params from utils import parse_url_params
mode_handlers = {} mode_handlers = {}
@ -48,8 +49,9 @@ def apply_url(ignore_handler):
handler(data) handler(data)
def open_book(book_id, fmt, replace=False): def open_book(book_id, fmt, library_id=None, replace=False):
push_state({'book_id':book_id, 'fmt':fmt}, replace=replace, mode=read_book_mode) library_id = library_id or current_library_id()
push_state({'book_id':book_id, 'fmt':fmt, 'library_id':library_id}, replace=replace, mode=read_book_mode)
def push_state(query, replace=False, mode='book_list', call_handler=True): def push_state(query, replace=False, mode='book_list', call_handler=True):

View File

@ -146,7 +146,7 @@ class DB:
'manifest': None, 'manifest': None,
'cover_width': None, 'cover_width': None,
'cover_height': None, 'cover_height': None,
'cover_fname': None, 'cover_name': None,
'last_read_position': {}, 'last_read_position': {},
}) })
) )
@ -181,7 +181,7 @@ class DB:
blob = Blob([data], {'type':'image/jpeg'}) blob = Blob([data], {'type':'image/jpeg'})
url = window.URL.createObjectURL(blob) url = window.URL.createObjectURL(blob)
img = new Image() img = new Image()
book.cover_fname = fname book.cover_name = name
proceeded = False proceeded = False
def done(): def done():
@ -238,7 +238,7 @@ class DB:
'Failed to read the file {0} for the book {1} from the database').format(name, book.metadata.title) 'Failed to read the file {0} for the book {1} from the database').format(name, book.metadata.title)
self.do_op(['files'], key, err, def (result): self.do_op(['files'], key, err, def (result):
if not result: if not result:
self.show_error(_('Cannot read book'), err) self.show_error(_('Cannot read file from book'), err)
return return
fdata = book.stored_files[key] fdata = book.stored_files[key]
mt = fdata.mimetype or 'application/octet-stream' mt = fdata.mimetype or 'application/octet-stream'