Ensure metadata is loaded on the EM page

This commit is contained in:
Kovid Goyal 2018-02-28 13:54:07 +05:30
parent e877ef90e8
commit 4db8ce5f81
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 83 additions and 20 deletions

View File

@ -459,11 +459,16 @@ def add_top_bar_buttons(container_id):
add_button(container, 'trash', action=delete_book, tooltip=_('Delete this book')) add_button(container, 'trash', action=delete_book, tooltip=_('Delete this book'))
book_id = parse_url_params().book_id book_id = parse_url_params().book_id
if book_id is '0': if book_id is '0':
add_button(container, 'random', def(): fetch_metadata(container_id, 0);) add_button(container, 'random', def(): fetch_metadata(container_id, 0, proceed_after_succesful_fetch_metadata);)
add_button(container, 'ellipsis-v', action=show_subsequent_panel.bind(None, 'more_actions'), tooltip=_('More actions')) add_button(container, 'ellipsis-v', action=show_subsequent_panel.bind(None, 'more_actions'), tooltip=_('More actions'))
def metadata_fetched(container_id, book_id, end_type, xhr, event): def proceed_after_succesful_fetch_metadata(container_id, book_id):
render_book(container_id, book_id)
add_top_bar_buttons(container_id)
def metadata_fetched(container_id, book_id, proceed, end_type, xhr, event):
nonlocal current_fetch nonlocal current_fetch
if current_fetch is None or current_fetch is not xhr: if current_fetch is None or current_fetch is not xhr:
return # Fetching was aborted return # Fetching was aborted
@ -481,8 +486,7 @@ def metadata_fetched(container_id, book_id, end_type, xhr, event):
clear(c) clear(c)
book_id = int(data['id']) book_id = int(data['id'])
set_book_metadata(book_id, data) set_book_metadata(book_id, data)
render_book(container_id, book_id) proceed(container_id, book_id)
add_top_bar_buttons(container_id)
elif end_type is not 'abort': elif end_type is not 'abort':
clear(c) clear(c)
c.appendChild(E.div( c.appendChild(E.div(
@ -492,14 +496,14 @@ def metadata_fetched(container_id, book_id, end_type, xhr, event):
)) ))
safe_set_inner_html(c.lastChild.lastChild, xhr.error_html) safe_set_inner_html(c.lastChild.lastChild, xhr.error_html)
def fetch_metadata(container_id, book_id): def fetch_metadata(container_id, book_id, proceed):
nonlocal current_fetch nonlocal current_fetch
container = document.getElementById(container_id) container = document.getElementById(container_id)
if not container: if not container:
return return
if current_fetch: if current_fetch:
current_fetch.abort() current_fetch.abort()
current_fetch = ajax('interface-data/book-metadata/' + book_id, metadata_fetched.bind(None, container_id, book_id), current_fetch = ajax('interface-data/book-metadata/' + book_id, metadata_fetched.bind(None, container_id, book_id, proceed),
query={'library_id':current_library_id(), 'vl':current_virtual_library()}) query={'library_id':current_library_id(), 'vl':current_virtual_library()})
current_fetch.send() current_fetch.send()
container = container.lastChild container = container.lastChild
@ -522,7 +526,21 @@ def create_book_details(container):
render_book(container_id, current_book_id) render_book(container_id, current_book_id)
add_top_bar_buttons(container_id) add_top_bar_buttons(container_id)
else: else:
fetch_metadata(container_id, current_book_id) fetch_metadata(container_id, current_book_id, proceed_after_succesful_fetch_metadata)
def report_load_failure(container):
err = E.div()
safe_set_inner_html(err, load_status.error_html)
container.appendChild(E.div(
style='margin: 1ex 1em',
E.div(_('Failed to load books from calibre library, with error:')),
err,
E.div(
style='margin-top: 1em; border-top: solid 1px currentColor; padding-top: 1ex;',
E.a(onclick=def(): home(replace=True);, href='javascript: void(0)', style='color: blue', _('Go back to the home page')))
),
)
def check_for_books_loaded(): def check_for_books_loaded():
@ -533,17 +551,7 @@ def check_for_books_loaded():
container = container.lastChild container = container.lastChild
clear(container) clear(container)
if not load_status.ok: if not load_status.ok:
err = E.div() report_load_failure(container)
safe_set_inner_html(err, load_status.error_html)
container.appendChild(E.div(
style='margin: 1ex 1em',
E.div(_('Failed to load books from calibre library, with error:')),
err,
E.div(
style='margin-top: 1em; border-top: solid 1px currentColor; padding-top: 1ex;',
E.a(onclick=def(): home(replace=True);, href='javascript: void(0)', style='color: blue', _('Go back to the home page')))
),
)
return return
create_book_details(container) create_book_details(container)

View File

@ -2,11 +2,66 @@
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
from __python__ import bound_methods, hash_literals from __python__ import bound_methods, hash_literals
from book_list.ui import set_panel_handler from elementmaker import E
from gettext import gettext as _
from book_list.book_details import no_book, report_load_failure, fetch_metadata
from book_list.library_data import book_metadata, load_status
from book_list.router import back
from book_list.top_bar import create_top_bar
from book_list.ui import set_panel_handler, show_panel
from dom import clear
from utils import conditional_timeout, parse_url_params
current_book_id = None
current_fetch = None
CLASS_NAME = 'edit-metadata-panel'
def proceed_after_succesful_fetch_metadata(container_id, book_id):
print(book_metadata(book_id))
def create_edit_metadata(container):
q = parse_url_params()
current_book_id = q.book_id
if not current_book_id:
no_book(container)
return
current_book_id = int(current_book_id)
container_id = container.parentNode.id
if not book_metadata(current_book_id):
fetch_metadata(container_id, current_book_id, proceed_after_succesful_fetch_metadata)
else:
proceed_after_succesful_fetch_metadata(container_id, current_book_id)
def check_for_books_loaded():
container = this
if load_status.loading:
conditional_timeout(container.id, 5, check_for_books_loaded)
return
container = container.lastChild
clear(container)
if not load_status.ok:
report_load_failure(container)
return
create_edit_metadata(container)
def init(container_id): def init(container_id):
pass nonlocal current_book_id
container = document.getElementById(container_id)
container.appendChild(E.div(class_=CLASS_NAME))
container.lastChild.appendChild(E.div(_('Loading books from the calibre library, please wait...'), style='margin: 1ex 1em'))
conditional_timeout(container_id, 5, check_for_books_loaded)
q = parse_url_params()
current_book_id = int(q.book_id or 0)
mi = book_metadata(current_book_id)
if not mi or not container:
show_panel('book_details', query=q, replace=True)
return
create_top_bar(container, title=_('Edit metadata for {}').format(mi.title), action=back, icon='close')
set_panel_handler('edit_metadata', init) set_panel_handler('edit_metadata', init)