Make the category links on the book details page middle-clickable

This commit is contained in:
Kovid Goyal 2017-05-11 13:35:29 +05:30
parent d8dae7a7f2
commit 496b0ce0f9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 38 additions and 30 deletions

View File

@ -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.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.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.top_bar import create_top_bar, set_title, add_button
from book_list.ui import set_panel_handler from book_list.ui import set_panel_handler, query_as_href
from book_list.views import search from book_list.views import search_query_for
bd_counter = 0 bd_counter = 0
@ -55,10 +55,10 @@ def field_sorter(field_metadata):
return lvl + (fm.name or 'zzzzz') return lvl + (fm.name or 'zzzzz')
def execute_search(ev): def href_for_search(name, val):
name, val = JSON.parse(ev.currentTarget.dataset.search)
query = '{}:"={}"'.format(name, str.replace(val, '"', r'\"')) query = '{}:"={}"'.format(name, str.replace(val, '"', r'\"'))
search(query) q = search_query_for(query)
return query_as_href(q)
def on_fmt_click(ev): def on_fmt_click(ev):
@ -106,8 +106,7 @@ def render_metadata(mi, table, book_id, field_list=None): # {{{
v += '' v += ''
if is_searchable: if is_searchable:
table.lastChild.lastChild.appendChild(E.a( 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=href_for_search(is_searchable, v), v))
title=_('Click to see books with {0}: {1}').format(name, v), href='javascript: void(0)', v))
else: else:
if v.appendChild: if v.appendChild:
table.lastChild.lastChild.appendChild(v) table.lastChild.lastChild.appendChild(v)
@ -187,8 +186,8 @@ def render_metadata(mi, table, book_id, field_list=None): # {{{
for k in val: for k in val:
lang = mi.lang_names[k] or k lang = mi.lang_names[k] or k
td.appendChild(E.a(lang, td.appendChild(E.a(lang,
title=_('Click to see books with language: {}').format(lang), href='javascript: void(0)', title=_('Click to see books with language: {}').format(lang),
data_search=JSON.stringify([field, k]), onclick=execute_search href=href_for_search(field, k)
)) ))
if k is not val[-1]: if k is not val[-1]:
td.appendChild(document.createTextNode(', ')) 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) ival = fmt_sidx(ival, use_roman=interface_data.use_roman_numerals_for_series_number)
table.appendChild(E.tr(E.td(name + ':'), E.td())) table.appendChild(E.tr(E.td(name + ':'), E.td()))
table.lastChild.lastChild.appendChild(E.span(ival, _(' of '), E.a( table.lastChild.lastChild.appendChild(E.span(ival, _(' of '), E.a(
data_search=JSON.stringify([field, val]), onclick=execute_search, href=href_for_search(field, val),
title=_('Click to see books with {0}: {1}').format(name, val), href='javascript: void(0)', val))) title=_('Click to see books with {0}: {1}').format(name, val), val)))
def process_field(field, fm): def process_field(field, fm):
name = fm.name or field name = fm.name or field

View File

@ -9,6 +9,7 @@ from book_list.constants import book_list_container_id
from book_list.globals import get_current_query from book_list.globals import get_current_query
from book_list.router import push_state from book_list.router import push_state
from book_list.library_data import current_library_id, current_virtual_library from book_list.library_data import current_library_id, current_virtual_library
from utils import encode_query_with_path
panel_handlers = {} panel_handlers = {}
@ -46,26 +47,29 @@ def currently_showing_panel():
def add_library_info(query): def add_library_info(query):
if not query.library_id: if not query.library_id:
query.library_id = current_library_id() query.library_id = current_library_id()
if not query.vl: if not query.vl:
if query.vl is None: if query.vl is None:
v'delete query.vl' v'delete query.vl'
else: else:
vlid = current_virtual_library() vlid = current_virtual_library()
if vlid: if vlid:
query.vl = 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): def show_panel(panel, query=None, replace=False):
if query is None: push_state(prepare_query(query, panel), replace=replace)
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)
def apply_url_state(state): def apply_url_state(state):

View File

@ -332,13 +332,18 @@ def create_sort_panel(container_id):
# Searching {{{ # Searching {{{
def search(query, replace=False):
def search_query_for(query):
q = loaded_books_query() q = loaded_books_query()
if query: if query:
q.search = query q.search = query
else: else:
v'delete q.search' 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);) set_apply_search(def(query): search(query, True);)