From 4db8ce5f8185e31aa05111b09bd9b9936c90204b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 28 Feb 2018 13:54:07 +0530 Subject: [PATCH] Ensure metadata is loaded on the EM page --- src/pyj/book_list/book_details.pyj | 44 ++++++++++++--------- src/pyj/book_list/edit_metadata.pyj | 59 ++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 20 deletions(-) diff --git a/src/pyj/book_list/book_details.pyj b/src/pyj/book_list/book_details.pyj index 7d1c133ead..c9ea329e69 100644 --- a/src/pyj/book_list/book_details.pyj +++ b/src/pyj/book_list/book_details.pyj @@ -459,11 +459,16 @@ def add_top_bar_buttons(container_id): add_button(container, 'trash', action=delete_book, tooltip=_('Delete this book')) book_id = parse_url_params().book_id 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')) -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 if current_fetch is None or current_fetch is not xhr: return # Fetching was aborted @@ -481,8 +486,7 @@ def metadata_fetched(container_id, book_id, end_type, xhr, event): clear(c) book_id = int(data['id']) set_book_metadata(book_id, data) - render_book(container_id, book_id) - add_top_bar_buttons(container_id) + proceed(container_id, book_id) elif end_type is not 'abort': clear(c) 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) -def fetch_metadata(container_id, book_id): +def fetch_metadata(container_id, book_id, proceed): nonlocal current_fetch container = document.getElementById(container_id) if not container: return if current_fetch: 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()}) current_fetch.send() container = container.lastChild @@ -522,7 +526,21 @@ def create_book_details(container): render_book(container_id, current_book_id) add_top_bar_buttons(container_id) 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(): @@ -533,17 +551,7 @@ def check_for_books_loaded(): container = container.lastChild clear(container) if not load_status.ok: - 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'))) - ), - ) + report_load_failure(container) return create_book_details(container) diff --git a/src/pyj/book_list/edit_metadata.pyj b/src/pyj/book_list/edit_metadata.pyj index f3e2e33238..47c11cf463 100644 --- a/src/pyj/book_list/edit_metadata.pyj +++ b/src/pyj/book_list/edit_metadata.pyj @@ -2,11 +2,66 @@ # License: GPL v3 Copyright: 2018, Kovid Goyal 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): - 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)