From 496b0ce0f96f7b584afd0920faac377e17ac03e5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 11 May 2017 13:35:29 +0530 Subject: [PATCH] Make the category links on the book details page middle-clickable --- src/pyj/book_list/book_details.pyj | 21 ++++++++--------- src/pyj/book_list/ui.pyj | 38 +++++++++++++++++------------- src/pyj/book_list/views.pyj | 9 +++++-- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/pyj/book_list/book_details.pyj b/src/pyj/book_list/book_details.pyj index dcfb622dd4..a790412faa 100644 --- a/src/pyj/book_list/book_details.pyj +++ b/src/pyj/book_list/book_details.pyj @@ -17,8 +17,8 @@ from utils import fmt_sidx, parse_url_params, conditional_timeout from book_list.router import back, open_book, home from book_list.library_data import book_metadata, cover_url, set_book_metadata, current_library_id, library_data, download_url, load_status, current_virtual_library from book_list.top_bar import create_top_bar, set_title, add_button -from book_list.ui import set_panel_handler -from book_list.views import search +from book_list.ui import set_panel_handler, query_as_href +from book_list.views import search_query_for bd_counter = 0 @@ -55,10 +55,10 @@ def field_sorter(field_metadata): return lvl + (fm.name or 'zzzzz') -def execute_search(ev): - name, val = JSON.parse(ev.currentTarget.dataset.search) +def href_for_search(name, val): query = '{}:"={}"'.format(name, str.replace(val, '"', r'\"')) - search(query) + q = search_query_for(query) + return query_as_href(q) def on_fmt_click(ev): @@ -106,8 +106,7 @@ def render_metadata(mi, table, book_id, field_list=None): # {{{ v += '' if is_searchable: table.lastChild.lastChild.appendChild(E.a( - data_search=JSON.stringify([is_searchable, v]), onclick=execute_search, - title=_('Click to see books with {0}: {1}').format(name, v), href='javascript: void(0)', v)) + title=_('Click to see books with {0}: {1}').format(name, v), href=href_for_search(is_searchable, v), v)) else: if v.appendChild: table.lastChild.lastChild.appendChild(v) @@ -187,8 +186,8 @@ def render_metadata(mi, table, book_id, field_list=None): # {{{ for k in val: lang = mi.lang_names[k] or k td.appendChild(E.a(lang, - title=_('Click to see books with language: {}').format(lang), href='javascript: void(0)', - data_search=JSON.stringify([field, k]), onclick=execute_search + title=_('Click to see books with language: {}').format(lang), + href=href_for_search(field, k) )) if k is not val[-1]: td.appendChild(document.createTextNode(', ')) @@ -208,8 +207,8 @@ def render_metadata(mi, table, book_id, field_list=None): # {{{ ival = fmt_sidx(ival, use_roman=interface_data.use_roman_numerals_for_series_number) table.appendChild(E.tr(E.td(name + ':'), E.td())) table.lastChild.lastChild.appendChild(E.span(ival, _(' of '), E.a( - data_search=JSON.stringify([field, val]), onclick=execute_search, - title=_('Click to see books with {0}: {1}').format(name, val), href='javascript: void(0)', val))) + href=href_for_search(field, val), + title=_('Click to see books with {0}: {1}').format(name, val), val))) def process_field(field, fm): name = fm.name or field diff --git a/src/pyj/book_list/ui.pyj b/src/pyj/book_list/ui.pyj index c6fb8271a5..007339b2c7 100644 --- a/src/pyj/book_list/ui.pyj +++ b/src/pyj/book_list/ui.pyj @@ -9,6 +9,7 @@ from book_list.constants import book_list_container_id from book_list.globals import get_current_query from book_list.router import push_state from book_list.library_data import current_library_id, current_virtual_library +from utils import encode_query_with_path panel_handlers = {} @@ -46,26 +47,29 @@ def currently_showing_panel(): def add_library_info(query): - if not query.library_id: - query.library_id = current_library_id() - if not query.vl: - if query.vl is None: - v'delete query.vl' - else: - vlid = current_virtual_library() - if vlid: - query.vl = vlid + if not query.library_id: + query.library_id = current_library_id() + if not query.vl: + if query.vl is None: + v'delete query.vl' + else: + vlid = current_virtual_library() + if vlid: + query.vl = vlid +def prepare_query(query, panel): + q = {k:query[k] for k in (query or {}) if k is not 'panel'} + if panel is not 'home': + q.panel = panel + add_library_info(q) + return q + +def query_as_href(query, panel): + q = prepare_query(query, panel or 'book_list') + return encode_query_with_path(q) def show_panel(panel, query=None, replace=False): - if query is None: - query = {} - else: - query = {k:query[k] for k in query if k is not 'panel'} - if panel is not 'home': - query.panel = panel - add_library_info(query) - push_state(query, replace=replace) + push_state(prepare_query(query, panel), replace=replace) def apply_url_state(state): diff --git a/src/pyj/book_list/views.pyj b/src/pyj/book_list/views.pyj index 33b17b23b7..fb9b3be258 100644 --- a/src/pyj/book_list/views.pyj +++ b/src/pyj/book_list/views.pyj @@ -332,13 +332,18 @@ def create_sort_panel(container_id): # Searching {{{ -def search(query, replace=False): + +def search_query_for(query): q = loaded_books_query() if query: q.search = query else: v'delete q.search' - show_panel('book_list', query=q, replace=replace) + return q + + +def search(query, replace=False): + show_panel('book_list', query=search_query_for(query), replace=replace) set_apply_search(def(query): search(query, True);)