From a5abcf15ce6c6345fd3296302bcb4c9e5fabc915 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 23 Oct 2023 09:23:34 +0530 Subject: [PATCH] Content server: Allow editing notes from tag browser --- src/calibre/srv/content.py | 4 +++- src/pyj/book_list/book_details.pyj | 2 +- src/pyj/book_list/router.pyj | 4 ++-- src/pyj/book_list/search.pyj | 36 +++++++++++++++++++++--------- src/pyj/book_list/show_note.pyj | 6 ++--- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/calibre/srv/content.py b/src/calibre/srv/content.py index 1424a1f926..90c37a4a69 100644 --- a/src/calibre/srv/content.py +++ b/src/calibre/srv/content.py @@ -368,7 +368,9 @@ def resource_hash_to_url(ctx, scheme, digest, library_id): def _get_note(ctx, rd, db, field, item_id, library_id): note_data = db.notes_data_for(field, item_id) if not note_data: - raise HTTPNotFound(f'Note for {field!r}:{item_id!r} not found') + if db.get_item_name(field, item_id): + return '' + raise HTTPNotFound(f'Item {field!r}:{item_id!r} not found') note_data.pop('searchable_text', None) resources = note_data.pop('resource_hashes', None) if resources: diff --git a/src/pyj/book_list/book_details.pyj b/src/pyj/book_list/book_details.pyj index e66871ea65..92781571b4 100644 --- a/src/pyj/book_list/book_details.pyj +++ b/src/pyj/book_list/book_details.pyj @@ -193,7 +193,7 @@ def render_metadata(mi, table, book_id, iframe_css): # {{{ link_maps = mi.link_maps or v'{}' def show_note_action(field, item_id, item_val): - show_note(book_id, field, item_id, item_val) + show_note(field, item_id, item_val) def add_note_link(field, name, val, parent): if mi.items_with_notes[field] and mi.items_with_notes[field][val]: diff --git a/src/pyj/book_list/router.pyj b/src/pyj/book_list/router.pyj index 4971b622af..d976375f47 100644 --- a/src/pyj/book_list/router.pyj +++ b/src/pyj/book_list/router.pyj @@ -87,9 +87,9 @@ def open_book_url(book_id, fmt, extra_query): return ans + encode_query(q, '#') -def show_note(book_id, field, item_id, item_value, replace=False, library_id=None, close_action='back', panel='show_note'): +def show_note(field, item_id, item_value, replace=False, library_id=None, close_action='back', panel='show_note'): lid = library_id or current_library_id() - q = {'book_id':book_id + '', 'field': field, 'item':item_value + '', 'item_id': (item_id or '')+ '', 'panel': panel} + q = {'field': field, 'item':item_value + '', 'item_id': (item_id or '')+ '', 'panel': panel} if panel is 'show_note': q.close_action = close_action if lid: diff --git a/src/pyj/book_list/search.pyj b/src/pyj/book_list/search.pyj index 7f243e38eb..f8eeb557f9 100644 --- a/src/pyj/book_list/search.pyj +++ b/src/pyj/book_list/search.pyj @@ -14,7 +14,7 @@ from session import get_interface_data from book_list.library_data import library_data, current_library_id, current_virtual_library from book_list.ui import show_panel -from book_list.router import back +from book_list.router import back, show_note from book_list.top_bar import create_top_bar, add_button from book_list.globals import get_session_data from book_list.theme import get_color, get_font_size @@ -222,20 +222,21 @@ def menu_clicked(i): node = node_for_path().children[i] data = node.data name = data.original_name or data.name or data.sort - items = [] - if data.count is not undefined: - items.append(_('Count: ') + data.count) - if data.avg_rating is not undefined: - items.append(_('Rating: {:.1f}').format(data.avg_rating)) - suffix = '' - if items.length: - suffix = ' [' + items.join(' ') + ']' - title = E.h2( style='display:flex; align-items: center; border-bottom: solid 1px currentColor; font-weight:bold; font-size:' + get_font_size('title'), E.img(src=icon_for_node(node), style='height:2ex; margin-right: 0.5rem'), - E.span(name + suffix) + E.span(name) ) + def edit_note(field, item_name): + hide_modal() + show_note(field, 0, item_name, panel='edit_note') + + if data.category and data.name and library_data.fields_that_support_notes.indexOf(data.category): + title.appendChild(E.a(svgicon('pencil'), + class_='blue-link', style='margin-left: 0.5em', href='javascript:void(0)', + onclick=edit_note.bind(None, data.category, data.name), + title=_('Edit or add notes for {}').format(data.name), + )) container.appendChild(title) container.appendChild(E.div( style='margin-top:1ex; margin-bottom: 1ex', @@ -280,6 +281,19 @@ def menu_clicked(i): f.firstChild.nextSibling.addEventListener('change', def(ev): get_session_data().set('and_search_terms', not ev.target.checked) ) + about_items = v'[]' + if data.count is not undefined: + about_items.push(_('Number of books: {}').format(data.count)) + if data.avg_rating is not undefined: + about_items.push(_('Average rating of books: {:.1f}').format(data.avg_rating)) + footer = E.div( + style='text-align:left; border-top: solid 1px currentColor; padding-top:1ex; margin-top:0.5ex;', + ) + if about_items.length: + footer.appendChild(E.div(style='font-size: smaller', ' '.join(about_items))) + if footer.firstChild: + container.appendChild(footer) + show_modal(create_details) diff --git a/src/pyj/book_list/show_note.pyj b/src/pyj/book_list/show_note.pyj index 7807142307..2a67a40239 100644 --- a/src/pyj/book_list/show_note.pyj +++ b/src/pyj/book_list/show_note.pyj @@ -69,7 +69,7 @@ def on_item_val_based_notes_fetched(load_type, xhr, ev): q.html = data.html q.item_id = str(data.item_id) current_note_markup = q - show_note(q.book_id, q.field, q.item_id, q.item, replace=True, library_id=q.library_id or None, close_action=q.close_action, panel=q.panel) + show_note(q.field, q.item_id, q.item, replace=True, library_id=q.library_id or None, close_action=q.close_action, panel=q.panel) return old = container.querySelector('div.loading') p = old.parentNode @@ -106,7 +106,7 @@ def on_notes_fetched(load_type, xhr, ev): def edit_note(): q = parse_url_params() - show_note(q.book_id, q.field, q.item_id, q.item, library_id=q.library_id, panel='edit_note') + show_note(q.field, q.item_id, q.item, library_id=q.library_id, panel='edit_note') def onkeydown(container_id, close_action, ev): @@ -215,7 +215,7 @@ def init_edit(container_id): container = document.getElementById(container_id) q = parse_url_params() close_action, close_icon = get_close_action() - create_top_bar(container, title=_('Edit notes for:') + ' ' + q.item, action=close_action, icon=close_icon, tooltip=_('Discard any changes')) + create_top_bar(container, title=_('Edit {}').format(q.item), action=close_action, icon=close_icon, tooltip=_('Discard any changes')) setup_container(container, close_action) if q.item_id is '0' or q.item_id is 0 or not q.item_id: return reroute_with_item_id(container, q)