| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from ajax import ajax, absolute_path |
| 9 |
from book_list.globals import get_session_data |
| 10 |
from book_list.library_data import library_data, current_library_id, current_virtual_library |
| 11 |
from book_list.prefs import prefs_panel_handler |
| 12 |
from book_list.router import back, show_note |
| 13 |
from book_list.theme import get_color, get_font_size |
| 14 |
from book_list.top_bar import create_top_bar |
| 15 |
from book_list.ui import show_panel |
| 16 |
from complete import create_search_bar |
| 17 |
from dom import clear, set_css, build_rule, svgicon, add_extra_css, ensure_id |
| 18 |
from modals import show_modal, create_custom_dialog |
| 19 |
from session import get_interface_data |
| 20 |
from utils import rating_to_stars, safe_set_inner_html, is_ios |
| 21 |
from widgets import create_button, create_spinner, Breadcrumbs |
| 22 |
|
| 23 |
apply_search = None |
| 24 |
|
| 25 |
|
| 26 |
def set_apply_search(func): |
| 27 |
nonlocal apply_search |
| 28 |
apply_search = func |
| 29 |
|
| 30 |
sp_counter = 0 |
| 31 |
CLASS_NAME = 'book-search-panel' |
| 32 |
add_extra_css(def (): |
| 33 |
sel = '.' + CLASS_NAME + ' ' |
| 34 |
card_bg = 'var(--calibre-surface-1)' |
| 35 |
card_border = 'var(--calibre-border-style-subtle)' |
| 36 |
style = build_rule(sel, |
| 37 |
padding='0.75rem 1rem 1rem 1rem', |
| 38 |
max_width='980px', |
| 39 |
margin_top='0.75rem', |
| 40 |
margin_bottom='0.75rem', |
| 41 |
margin_left='auto', |
| 42 |
margin_right='auto', |
| 43 |
box_sizing='border-box', |
| 44 |
display='flex', |
| 45 |
flex_direction='column', |
| 46 |
gap='1rem', |
| 47 |
) |
| 48 |
style += r''' |
| 49 |
@media (max-width: 720px) { |
| 50 |
.''' + CLASS_NAME + r''' { padding-left: 0.75rem; padding-right: 0.75rem; } |
| 51 |
} |
| 52 |
''' |
| 53 |
style += build_rule(sel + ' .cs-search-body', margin_top='0', display='flex', flex_direction='column', gap='0.95rem') |
| 54 |
style += build_rule(sel + ' div.tag-name > span', word_break='break-all', hyphens='auto', padding_left='1ex') |
| 55 |
style += build_rule(sel + ' div.tag-name:hover', color='inherit', background_color='var(--calibre-color-primary-light)') |
| 56 |
style += build_rule(sel + ' div.tag-menu:hover', color='inherit', background_color='var(--calibre-color-primary-light)') |
| 57 |
style += build_rule(sel + ' div.tag-name:active', transform='none', opacity='0.92') |
| 58 |
style += build_rule(sel + ' div.tag-menu:active', transform='none', opacity='0.92') |
| 59 |
# Active search terms chips (match details-list badges) |
| 60 |
style += build_rule(sel + ' ul.search-items', |
| 61 |
margin_top='0.65rem', |
| 62 |
margin_bottom='0', |
| 63 |
list_style_type='none', |
| 64 |
text_align='left', |
| 65 |
display='flex', |
| 66 |
flex_wrap='wrap', |
| 67 |
gap='0.35rem', |
| 68 |
padding='0', |
| 69 |
) |
| 70 |
style += build_rule(sel + ' ul.search-items > li', |
| 71 |
display='inline-flex', |
| 72 |
align_items='center', |
| 73 |
gap='0.25rem', |
| 74 |
cursor='pointer', |
| 75 |
padding='0.15rem 0.5rem', |
| 76 |
border_radius='999px', |
| 77 |
border='var(--calibre-border-style-subtle)', |
| 78 |
background='var(--calibre-color-surface-3)', |
| 79 |
font_size='0.92em', |
| 80 |
) |
| 81 |
style += build_rule(sel + ' ul.search-items > li:hover', |
| 82 |
background='var(--calibre-color-primary-light)', |
| 83 |
border_color='var(--calibre-color-border)', |
| 84 |
) |
| 85 |
style += build_rule(sel + ' ul.search-items > li:active', |
| 86 |
background='var(--calibre-color-primary-light-2)', |
| 87 |
transform='none', |
| 88 |
) |
| 89 |
# Tag browser card + breadcrumbs |
| 90 |
style += build_rule(sel + ' .cs-search-tag-card', |
| 91 |
padding='1rem', |
| 92 |
border_radius='18px', |
| 93 |
border=card_border, |
| 94 |
background=card_bg, |
| 95 |
box_shadow='none', |
| 96 |
) |
| 97 |
style += build_rule(sel + ' .cs-search-breadcrumbs.calibre-breadcrumbs', |
| 98 |
background='var(--calibre-color-surface-2)', |
| 99 |
border='var(--calibre-border-style-subtle)', |
| 100 |
border_radius='14px', |
| 101 |
margin='0 0 0.85rem 0', |
| 102 |
padding='0.25rem 0', |
| 103 |
z_index='auto', |
| 104 |
) |
| 105 |
style += build_rule(sel + ' .cs-search-breadcrumbs.calibre-breadcrumbs > li:hover', |
| 106 |
color='inherit', |
| 107 |
) |
| 108 |
style += build_rule(sel + ' .cs-search-tags', |
| 109 |
padding='0', |
| 110 |
display='flex', |
| 111 |
flex_direction='column', |
| 112 |
gap='0.65rem', |
| 113 |
margin='0', |
| 114 |
width='100%', |
| 115 |
) |
| 116 |
style += build_rule(sel + ' .cs-search-tag-row', |
| 117 |
display='flex', |
| 118 |
align_items='stretch', |
| 119 |
max_width='100%', |
| 120 |
border='var(--calibre-border-style-subtle)', |
| 121 |
border_radius='14px', |
| 122 |
background='var(--calibre-color-surface-2)', |
| 123 |
box_shadow='none', |
| 124 |
cursor='pointer', |
| 125 |
overflow='hidden', |
| 126 |
user_select='none', |
| 127 |
transition='background-color 160ms ease, border-color 160ms ease', |
| 128 |
) |
| 129 |
style += build_rule(sel + ' .cs-search-tag-row:hover', |
| 130 |
background_color='var(--calibre-color-primary-light)', |
| 131 |
border_color='var(--calibre-color-border)', |
| 132 |
box_shadow='none', |
| 133 |
) |
| 134 |
style += build_rule(sel + ' .cs-search-tag-row:active', |
| 135 |
background_color='var(--calibre-color-primary-light-2)', |
| 136 |
transform='translateY(0.5px)', |
| 137 |
) |
| 138 |
style += build_rule(sel + ' .cs-search-tag-row .tag-name', |
| 139 |
border_right='var(--calibre-border-style-subtle)', |
| 140 |
padding='0.65rem 0.85rem', |
| 141 |
display='flex', |
| 142 |
align_items='center', |
| 143 |
flex='1 1 auto', |
| 144 |
min_width='0', |
| 145 |
) |
| 146 |
style += build_rule(sel + ' .cs-search-tag-row .tag-menu', |
| 147 |
padding='0.65rem 0.75rem', |
| 148 |
display='flex', |
| 149 |
align_items='center', |
| 150 |
flex='0 0 auto', |
| 151 |
background='var(--calibre-color-surface-3)', |
| 152 |
) |
| 153 |
style += build_rule(sel + ' .cs-search-loading', |
| 154 |
padding='2.5rem 1rem', |
| 155 |
text_align='center', |
| 156 |
border_radius='18px', |
| 157 |
border=card_border, |
| 158 |
background=card_bg, |
| 159 |
box_shadow='none', |
| 160 |
) |
| 161 |
# Actions popup |
| 162 |
style += build_rule( |
| 163 |
'#modal-container ul.tb-action-list > li:hover', |
| 164 |
color=get_color('list-hover-foreground'), background_color=get_color('list-hover-background') |
| 165 |
) |
| 166 |
style += build_rule( |
| 167 |
'#modal-container ul.tb-action-list > li:active', |
| 168 |
color=get_color('window-hover-foreground'), color=get_color('list-hover-foreground'), |
| 169 |
background_color=get_color('list-hover-background') |
| 170 |
) |
| 171 |
return style |
| 172 |
) |
| 173 |
|
| 174 |
state = {} |
| 175 |
last_accepted_search = None |
| 176 |
|
| 177 |
|
| 178 |
def component(container, name): |
| 179 |
return container.querySelector(f'[data-component="{name}"]') |
| 180 |
|
| 181 |
|
| 182 |
def icon_for_node(node): |
| 183 |
interface_data = get_interface_data() |
| 184 |
fallback = '' |
| 185 |
if node.data.category is 'formats' and not node.data.is_category: |
| 186 |
fmt = node.data.name.lower() |
| 187 |
if fmt.startswith('original_'): |
| 188 |
fmt = fmt.partition('_')[2] |
| 189 |
ans = f'mimetypes/{fmt}.png' |
| 190 |
fallback = interface_data.icon_map[node.data.category] |
| 191 |
else: |
| 192 |
ans = interface_data.icon_map[node.data.category] or 'column.png' |
| 193 |
if node.data.value_icon: |
| 194 |
fallback = ans |
| 195 |
ans = '_' + node.data.value_icon |
| 196 |
if fallback: |
| 197 |
fallback = absolute_path(interface_data.icon_path + '/' + fallback) |
| 198 |
return absolute_path(interface_data.icon_path + '/' + ans), fallback |
| 199 |
|
| 200 |
|
| 201 |
def node_for_path(path): |
| 202 |
path = path or state.tag_path |
| 203 |
ans = state.tag_browser_data |
| 204 |
for child_index in path: |
| 205 |
ans = ans.children[child_index] |
| 206 |
return ans |
| 207 |
|
| 208 |
|
| 209 |
def execute_search(text): |
| 210 |
nonlocal last_accepted_search |
| 211 |
if state.tag_path and state.tag_path.length: |
| 212 |
last_accepted_search = {'library_id': current_library_id(), 'vl': current_virtual_library(), 'tag_path': list(state.tag_path)} |
| 213 |
apply_search(text) |
| 214 |
|
| 215 |
|
| 216 |
def ask_about_smart_quotes(text): |
| 217 |
create_custom_dialog(_('Search contains smart quotes'), def (parent, close_modal): |
| 218 |
def action(replace): |
| 219 |
nonlocal text |
| 220 |
close_modal() |
| 221 |
if replace: |
| 222 |
text = text.replace(/[“”«»„]/g, '"') |
| 223 |
execute_search(text) |
| 224 |
parent.appendChild(E.div( |
| 225 |
E.div(_( |
| 226 |
'The search expression {} contains smart quotes. By default, Apple inserts smart' |
| 227 |
' quotes instead of normal quotes when tapping the quote button. This will prevent' |
| 228 |
' the search from working, if you intended to use normal quotes. You can type' |
| 229 |
' normal quotes by long-tapping the quotes button. Do you want to keep the smart' |
| 230 |
' quotes or replace them with normal quotes?' |
| 231 |
).format(text)), |
| 232 |
E.div(class_='button-box', |
| 233 |
create_button(_('Keep'), None, action.bind(None, False)), |
| 234 |
create_button(_('Replace'), None, action.bind(None, True), highlight=True), |
| 235 |
) |
| 236 |
)) |
| 237 |
) |
| 238 |
|
| 239 |
|
| 240 |
def execute_search_interactive(text): |
| 241 |
if not text: |
| 242 |
container = document.getElementById(state.container_id) |
| 243 |
search_control = container.querySelector('input[name="search-books"]') |
| 244 |
text = search_control.value or '' |
| 245 |
if is_ios and /[“”«»„]/.test(text): |
| 246 |
ask_about_smart_quotes(text) |
| 247 |
else: |
| 248 |
execute_search(text) |
| 249 |
|
| 250 |
|
| 251 |
def search_expression_for_item(node, node_state): |
| 252 |
item = node.data |
| 253 |
if item.is_searchable is False or not node_state or node_state is 'clear': |
| 254 |
return '' |
| 255 |
|
| 256 |
search_state = {'plus': 'true', 'plusplus': '.true', 'minus': 'false', 'minusminus': '.false'}[node_state] |
| 257 |
stars = rating_to_stars(3, True) |
| 258 |
|
| 259 |
if item.is_category: |
| 260 |
category = item.category |
| 261 |
|
| 262 |
if item.is_first_letter: |
| 263 |
letters_seen = {} |
| 264 |
for child in node.children: |
| 265 |
if child.data.sort: |
| 266 |
letters_seen[child.data.sort[0]] = True |
| 267 |
letters_seen = Object.keys(letters_seen) |
| 268 |
if letters_seen.length: |
| 269 |
charclass = letters_seen.join('') |
| 270 |
if category is 'authors': |
| 271 |
expr = r'author_sort:"~(^[{0}])|(&\s*[{0}])"'.format(charclass) |
| 272 |
elif category is 'series': |
| 273 |
expr = r'series_sort:"~^[{0}]"'.format(charclass) |
| 274 |
else: |
| 275 |
expr = r'{0}:"~^[{1}]"'.format(category, charclass) |
| 276 |
else: |
| 277 |
expr = '{}:false'.format(category) |
| 278 |
|
| 279 |
elif category is 'news': |
| 280 |
expr = 'tags:"={}"'.format(item.name) |
| 281 |
|
| 282 |
else: |
| 283 |
return '{}:{}'.format(category, search_state) |
| 284 |
|
| 285 |
if 'false' in search_state: |
| 286 |
expr = '(not ' + expr + ')' |
| 287 |
return expr |
| 288 |
|
| 289 |
category = 'tags' if item.category is 'news' else item.category |
| 290 |
if item.name and item.name[0] in stars: |
| 291 |
# Assume ratings |
| 292 |
rnum = item.name.length |
| 293 |
if item.name.endswith(stars[-1]): |
| 294 |
rnum = '{}.5'.format(rnum - 1) |
| 295 |
expr = '{}:{}'.format(category, rnum) |
| 296 |
else: |
| 297 |
fm = library_data.field_metadata[item.category] |
| 298 |
suffix = ':' if fm and fm.is_csp else '' |
| 299 |
name = item.original_name or item.name or item.sort |
| 300 |
if not name: |
| 301 |
return '' |
| 302 |
name = str.replace(name, '"', r'\"') |
| 303 |
if name[0] is '.': |
| 304 |
name = '.' + name |
| 305 |
if search_state is '.true' or search_state is '.false': |
| 306 |
name = '.' + name |
| 307 |
expr = '{}:"={}{}"'.format(category, name, suffix) |
| 308 |
|
| 309 |
if 'false' in search_state: |
| 310 |
expr = '(not ' + expr + ')' |
| 311 |
return expr |
| 312 |
|
| 313 |
|
| 314 |
def node_clicked(i): |
| 315 |
node = node_for_path().children[i] |
| 316 |
if node.children and node.children.length: |
| 317 |
state.tag_path.append(i) |
| 318 |
render_tag_browser() |
| 319 |
else: |
| 320 |
expr = search_expression_for_item(node, 'plus') |
| 321 |
execute_search(expr) |
| 322 |
|
| 323 |
|
| 324 |
def add_to_search(node, search_type): |
| 325 |
anded = get_session_data().get('and_search_terms') |
| 326 |
state.active_nodes[node.id] = [search_type, anded] |
| 327 |
render_search_expression() |
| 328 |
|
| 329 |
|
| 330 |
def remove_expression(node_id): |
| 331 |
v'delete state.active_nodes[node_id]' |
| 332 |
render_search_expression() |
| 333 |
|
| 334 |
|
| 335 |
def render_search_expression(): |
| 336 |
parts = [] |
| 337 |
container = document.getElementById(state.container_id) |
| 338 |
sic = component(container, 'search_expression') |
| 339 |
clear(sic) |
| 340 |
for node_id in Object.keys(state.active_nodes): |
| 341 |
search_type, anded = state.active_nodes[node_id] |
| 342 |
node = state.node_id_map[node_id] |
| 343 |
expr = search_expression_for_item(node, search_type) |
| 344 |
name = node.data.original_name or node.data.name or node.data.sort or '' |
| 345 |
if expr: |
| 346 |
c = E.li(svgicon('remove'), '\xa0' + name) |
| 347 |
sic.appendChild(c) |
| 348 |
c.addEventListener('click', remove_expression.bind(None, node_id)) |
| 349 |
if parts.length: |
| 350 |
expr = ('and' if anded else 'or') + ' ' + expr |
| 351 |
parts.push(expr) |
| 352 |
search_control = container.querySelector('input[name="search-books"]') |
| 353 |
search_control.value = parts.join(' ') |
| 354 |
|
| 355 |
|
| 356 |
def show_fallback(fallback, ev): |
| 357 |
if fallback: |
| 358 |
if ev.target.src is not fallback: |
| 359 |
ev.target.src = fallback |
| 360 |
|
| 361 |
|
| 362 |
def menu_clicked(i): |
| 363 |
def create_details(container, hide_modal): |
| 364 |
node = node_for_path().children[i] |
| 365 |
data = node.data |
| 366 |
name = data.original_name or data.name or data.sort |
| 367 |
icon, fallback = icon_for_node(node) |
| 368 |
title = E.h2( |
| 369 |
style='display:flex; align-items: center; border-bottom: solid 1px currentColor; font-weight:bold; font-size:' + get_font_size('title'), |
| 370 |
E.img(src=icon, onerror=show_fallback.bind(None, fallback), style='height:2ex; margin-right: 0.5rem'), |
| 371 |
E.span(name) |
| 372 |
) |
| 373 |
|
| 374 |
def edit_note(field, item_name): |
| 375 |
hide_modal() |
| 376 |
show_note(field, 0, item_name, panel='edit_note') |
| 377 |
|
| 378 |
if data.category and data.name and library_data.fields_that_support_notes.indexOf(data.category): |
| 379 |
title.appendChild(E.a(svgicon('pencil'), |
| 380 |
class_='blue-link', style='margin-left: 0.5em', href='javascript:void(0)', |
| 381 |
onclick=edit_note.bind(None, data.category, data.name), |
| 382 |
title=_('Edit or add notes for {}').format(data.name), |
| 383 |
)) |
| 384 |
container.appendChild(title) |
| 385 |
container.appendChild(E.div( |
| 386 |
style='margin-top:1ex; margin-bottom: 1ex', |
| 387 |
_('Search for books based on this category (a search term will be added to the search field)') |
| 388 |
)) |
| 389 |
|
| 390 |
ul = E.ul(style='list-style:none; overflow-x:auto; overflow-y:visible', class_='tb-action-list') |
| 391 |
container.appendChild(ul) |
| 392 |
items = [ |
| 393 |
(_('Books matching this category'), 'plus'), |
| 394 |
(_('Books that do not match this category'), 'minus'), |
| 395 |
] |
| 396 |
if node.data.is_hierarchical is 5: |
| 397 |
items.extend([ |
| 398 |
(_('Books that match this category and all sub-categories'), 'plusplus'), |
| 399 |
(_('Books that do not match this category or any of its sub-categories'), 'minusminus'), |
| 400 |
]) |
| 401 |
interface_data = get_interface_data() |
| 402 |
for text, search_type in items: |
| 403 |
li = E.li( |
| 404 |
style='display:flex; align-items: center; margin-bottom:0.5ex; padding: 0.5ex; cursor:pointer', |
| 405 |
E.img(src=absolute_path('{}/{}.png'.format(interface_data.icon_path, search_type)), style='max-height: 2.5ex; margin-right:0.5rem'), |
| 406 |
E.span(text) |
| 407 |
) |
| 408 |
li.addEventListener('click', add_to_search.bind(None, node, search_type)) |
| 409 |
li.addEventListener('click', hide_modal) |
| 410 |
ul.appendChild(li) |
| 411 |
f = E.form( |
| 412 |
style='text-align:left; border-top: solid 1px currentColor; padding-top:1ex; margin-top:0.5ex; display:flex; align-items:center', |
| 413 |
E.span(_('Add to the search expression with:')), |
| 414 |
E.input(type='radio', name='expr_join', value='OR', checked=''), |
| 415 |
E.span('\xa0OR\xa0'), |
| 416 |
E.input(type='radio', name='expr_join', value='AND'), |
| 417 |
E.span('\xa0AND') |
| 418 |
) |
| 419 |
and_control = f.lastChild.previousSibling |
| 420 |
and_control.checked = get_session_data().get('and_search_terms') |
| 421 |
container.appendChild(f) |
| 422 |
and_control.addEventListener('change', def (ev): |
| 423 |
get_session_data().set('and_search_terms', bool(ev.target.checked)) |
| 424 |
) |
| 425 |
f.firstChild.nextSibling.addEventListener('change', def (ev): |
| 426 |
get_session_data().set('and_search_terms', not ev.target.checked) |
| 427 |
) |
| 428 |
about_items = [] |
| 429 |
if data.count is not undefined: |
| 430 |
about_items.push(_('Number of books: {}').format(data.count)) |
| 431 |
if data.avg_rating is not undefined: |
| 432 |
about_items.push(_('Average rating of books: {:.1f}').format(data.avg_rating)) |
| 433 |
footer = E.div( |
| 434 |
style='text-align:left; border-top: solid 1px currentColor; padding-top:1ex; margin-top:0.5ex;', |
| 435 |
) |
| 436 |
if about_items.length: |
| 437 |
footer.appendChild(E.div(style='font-size: smaller', ' '.join(about_items))) |
| 438 |
if footer.firstChild: |
| 439 |
container.appendChild(footer) |
| 440 |
|
| 441 |
show_modal(create_details) |
| 442 |
|
| 443 |
|
| 444 |
def render_children(container, children): |
| 445 |
for i, node in enumerate(children): |
| 446 |
data = node.data |
| 447 |
tooltip = '' |
| 448 |
if data.count is not undefined: |
| 449 |
tooltip += '\n' + _('Number of books in this category: {}').format(data.count) |
| 450 |
if data.avg_rating is not undefined: |
| 451 |
tooltip += '\n' + _('Average rating for books in this category: {:.1f}').format(data.avg_rating) |
| 452 |
icon, fallback = icon_for_node(node) |
| 453 |
div = E.div( |
| 454 |
title=tooltip.lstrip(), |
| 455 |
class_='cs-search-tag-row', |
| 456 |
E.div( |
| 457 |
class_='tag-name', |
| 458 |
E.img(src=icon, onerror=show_fallback.bind(None, fallback), |
| 459 |
style='display:inline-block; max-height:2.5ex; margin-right:0.5rem; flex-shrink:0'), |
| 460 |
E.span(data.name), |
| 461 |
), |
| 462 |
E.div(class_='tag-menu', E.div(svgicon('angle-down'))) |
| 463 |
) |
| 464 |
div.firstChild.addEventListener('click', node_clicked.bind(None, i)) |
| 465 |
div.lastChild.addEventListener('click', menu_clicked.bind(None, i)) |
| 466 |
container.appendChild(div) |
| 467 |
|
| 468 |
|
| 469 |
def render_breadcrumbs(): |
| 470 |
container = state.breadcrumbs.container |
| 471 |
if not state.tag_path.length: |
| 472 |
container.style.display = 'none' |
| 473 |
return |
| 474 |
container.style.display = 'inline-block' |
| 475 |
state.breadcrumbs.reset() |
| 476 |
|
| 477 |
def onclick(i): |
| 478 |
return def (ev): |
| 479 |
state.tag_path = state.tag_path[:i + 1] |
| 480 |
render_tag_browser() |
| 481 |
ev.preventDefault() |
| 482 |
return True |
| 483 |
|
| 484 |
def create_breadcrumb(index=-1, item=None): |
| 485 |
li = state.breadcrumbs.add_crumb(onclick(index)) |
| 486 |
if item: |
| 487 |
li.appendChild(E.span(item.name)) |
| 488 |
else: |
| 489 |
li.appendChild(svgicon('home', '2.2ex', '2.2ex')) |
| 490 |
|
| 491 |
create_breadcrumb() |
| 492 |
parent = state.tag_browser_data |
| 493 |
for i, index in enumerate(state.tag_path): |
| 494 |
parent = parent.children[index] |
| 495 |
create_breadcrumb(i, parent.data) |
| 496 |
|
| 497 |
|
| 498 |
def render_tag_browser(): |
| 499 |
container = component(document.getElementById(state.container_id), 'tag_browser') |
| 500 |
clear(container) |
| 501 |
set_css(container, padding='0', display='flex', flex_direction='column', gap='0.65rem', width='100%') |
| 502 |
try: |
| 503 |
node = node_for_path() |
| 504 |
except: |
| 505 |
# If a saved tag_path is no longer valid |
| 506 |
state.tag_path = [] |
| 507 |
node = node_for_path() |
| 508 |
render_children(container, node.children) |
| 509 |
render_breadcrumbs() |
| 510 |
|
| 511 |
|
| 512 |
def on_data_fetched(end_type, xhr, ev): |
| 513 |
state.currently_loading = None |
| 514 |
if end_type is 'abort': |
| 515 |
return |
| 516 |
container = document.getElementById(state.container_id) |
| 517 |
if not container: |
| 518 |
return |
| 519 |
|
| 520 |
loading_panel = component(container, 'loading') |
| 521 |
loading_panel.style.display = 'none' |
| 522 |
tc = component(container, 'tag_card') |
| 523 |
if tc: |
| 524 |
tc.style.display = 'block' |
| 525 |
container = component(container, 'tag_browser') |
| 526 |
container.style.display = 'flex' |
| 527 |
clear(container) |
| 528 |
|
| 529 |
def show_error(error_html): |
| 530 |
ediv = E.div() |
| 531 |
container.appendChild(ediv) |
| 532 |
safe_set_inner_html(ediv, '<h3>' + _('Failed to load Tag browser data') + '</h3>' + error_html) |
| 533 |
|
| 534 |
def process_node(node, item_map): |
| 535 |
state.node_id_map[node.id] = node |
| 536 |
node.data = item_map[node.id] |
| 537 |
for child in node.children: |
| 538 |
child.parent = node |
| 539 |
process_node(child, item_map) |
| 540 |
|
| 541 |
if end_type is 'load': |
| 542 |
try: |
| 543 |
tag_browser_data = JSON.parse(xhr.responseText) |
| 544 |
except Exception as err: |
| 545 |
show_error(err + '') |
| 546 |
return |
| 547 |
state.tag_browser_data = tag_browser_data.root |
| 548 |
state.node_id_map = {} |
| 549 |
state.active_nodes = {} |
| 550 |
process_node(state.tag_browser_data, tag_browser_data.item_map) |
| 551 |
if last_accepted_search and last_accepted_search.library_id is current_library_id() and last_accepted_search.vl is current_virtual_library(): |
| 552 |
if last_accepted_search.tag_path.length: |
| 553 |
state.tag_path = list(last_accepted_search.tag_path) |
| 554 |
render_tag_browser() |
| 555 |
else: |
| 556 |
show_error(xhr.error_html) |
| 557 |
|
| 558 |
|
| 559 |
def refresh(): |
| 560 |
root = document.getElementById(state.container_id) |
| 561 |
if root: |
| 562 |
lp = component(root, 'loading') |
| 563 |
if lp: |
| 564 |
lp.style.display = 'block' |
| 565 |
tc = component(root, 'tag_card') |
| 566 |
if tc: |
| 567 |
tc.style.display = 'none' |
| 568 |
if state.currently_loading is not None: |
| 569 |
state.currently_loading.abort() |
| 570 |
state.currently_loading = None |
| 571 |
sd = get_session_data() |
| 572 |
query = {'library_id': current_library_id(), 'vl': current_virtual_library()} |
| 573 |
for k in 'sort_tags_by partition_method collapse_at dont_collapse hide_empty_categories'.split(' '): |
| 574 |
query[k] = sd.get(k) + '' |
| 575 |
xhr = ajax('interface-data/tag-browser', on_data_fetched, query=query, bypass_cache=False) |
| 576 |
xhr.send() |
| 577 |
state.currently_loading = xhr |
| 578 |
|
| 579 |
|
| 580 |
def create_search_panel(container): |
| 581 |
nonlocal state |
| 582 |
state = {} |
| 583 |
container.classList.add(CLASS_NAME) |
| 584 |
# search input container |
| 585 |
search_container = E.div(class_='calibre-group-box', data_component='search') |
| 586 |
container.appendChild(search_container) |
| 587 |
container.appendChild(E.div(class_='cs-search-body', |
| 588 |
E.div(data_component='loading', class_='cs-search-loading'), |
| 589 |
E.div(data_component='tag_card', class_='cs-search-tag-card', style='display:none', |
| 590 |
E.ol(style='display:none', data_component='breadcrumbs', class_='cs-search-breadcrumbs'), |
| 591 |
E.div(style='display:none', data_component='tag_browser', class_='cs-search-tags'), |
| 592 |
), |
| 593 |
)) |
| 594 |
|
| 595 |
# Build search input |
| 596 |
# We don't focus the search box because on mobile that will cause the |
| 597 |
# keyboard to popup and obscure the rest of the page |
| 598 |
|
| 599 |
search_bar = create_search_bar( |
| 600 |
execute_search_interactive, 'search-books', tooltip=_('Search for books'), |
| 601 |
placeholder=_('Enter the search query'), button='icon-button') |
| 602 |
search_container.appendChild(search_bar) |
| 603 |
search_container.appendChild(E.div( |
| 604 |
E.a(class_='blue-link', svgicon('fts'), '\xa0', _('Search the full text of books instead'), |
| 605 |
href='javascript: void(0)', onclick=show_panel.bind(None, 'fts')), |
| 606 |
style='margin-top: 1rem' |
| 607 |
)) |
| 608 |
search_container.appendChild(E.ul(class_='search-items', data_component='search_expression')) |
| 609 |
|
| 610 |
# Build loading panel |
| 611 |
loading_panel = component(container, 'loading') |
| 612 |
loading_panel.appendChild(E.div( |
| 613 |
create_spinner(), '\xa0' + _('Fetching data for the Tag browser, please wait') + '…', |
| 614 |
style='font-size: 1.15rem; font-weight: 700; opacity: 0.9') |
| 615 |
) |
| 616 |
|
| 617 |
# Build breadcrumbs |
| 618 |
state.breadcrumbs = Breadcrumbs(component(container, 'breadcrumbs')) |
| 619 |
|
| 620 |
# Init state |
| 621 |
state.currently_loading = None |
| 622 |
state.tag_browser_data = None |
| 623 |
state.node_id_map = {} |
| 624 |
state.active_nodes = {} |
| 625 |
state.tag_path = [] |
| 626 |
state.container_id = ensure_id(container) |
| 627 |
refresh() |
| 628 |
|
| 629 |
|
| 630 |
# Tag browser prefrences {{{ |
| 631 |
|
| 632 |
|
| 633 |
def get_prefs(): |
| 634 |
return [ |
| 635 |
{ |
| 636 |
'name': 'sort_tags_by', |
| 637 |
'text': _('Sort tags by'), |
| 638 |
'choices': [('name', _('Name')), ('popularity', _('Popularity (number of books)')), ('rating', _('Average rating'))], |
| 639 |
'tooltip': _('Change how the tags/authors/etc. are sorted in the "Tag browser"'), |
| 640 |
}, |
| 641 |
{ |
| 642 |
'name': 'partition_method', |
| 643 |
'text': _('Category partitioning method'), |
| 644 |
'choices': [('first letter', _('First Letter')), ('disable', _('Disable')), ('partition', _('Partition'))], |
| 645 |
'tooltip': _('Choose how Tag browser subcategories are displayed when' |
| 646 |
' there are more items than the limit. Select by first' |
| 647 |
' letter to see an A, B, C list. Choose partitioned to' |
| 648 |
' have a list of fixed-sized groups. Set to disabled' |
| 649 |
' if you never want subcategories.'), |
| 650 |
}, |
| 651 |
{ |
| 652 |
'name': 'collapse_at', |
| 653 |
'text': _('Collapse when more items than'), |
| 654 |
'min': 5, 'max': 10000, 'step': 5, |
| 655 |
'from_storage': int, 'to_storage': int, |
| 656 |
'tooltip': _('If a "Tag browser" category has more than this number of items, it is divided' |
| 657 |
' up into subcategories. If the partition method is set to disable, this value is ignored.'), |
| 658 |
}, |
| 659 |
{ |
| 660 |
'name': 'dont_collapse', |
| 661 |
'text': _('Categories not to partition'), |
| 662 |
'tooltip': _('A comma-separated list of categories in which items containing' |
| 663 |
' periods are displayed in the Tag browser trees. For example, if' |
| 664 |
" this box contains 'tags' then tags of the form 'Mystery.English'" |
| 665 |
" and 'Mystery.Thriller' will be displayed with English and Thriller" |
| 666 |
" both under 'Mystery'. If 'tags' is not in this box," |
| 667 |
' then the tags will be displayed each on their own line.'), |
| 668 |
}, |
| 669 |
{ |
| 670 |
'name': 'hide_empty_categories', |
| 671 |
'text': _('Hide empty categories (columns)'), |
| 672 |
'from_storage': def (x): return x.toLowerCase() is 'yes';, |
| 673 |
'to_storage': def (x): return 'yes' if x else 'no';, |
| 674 |
'tooltip': _('When checked, calibre will automatically hide any category' |
| 675 |
' (a column, custom or standard) that has no items to show. For example, some' |
| 676 |
' categories might not have values when using Virtual libraries. Checking this' |
| 677 |
' box will cause these empty categories to be hidden.'), |
| 678 |
}, |
| 679 |
] |
| 680 |
|
| 681 |
|
| 682 |
def tb_config_panel_handler(): |
| 683 |
return prefs_panel_handler(def (): return _('Configure Tag browser');, get_prefs) |
| 684 |
# }}} |
| 685 |
|
| 686 |
|
| 687 |
def onkeydown(container_id, close_action, ev): |
| 688 |
if ev.key is 'Escape': |
| 689 |
ev.preventDefault(), ev.stopPropagation() |
| 690 |
close_action() |
| 691 |
|
| 692 |
|
| 693 |
def init(container_id): |
| 694 |
if not library_data.sortable_fields: |
| 695 |
show_panel('book_list', replace=True) |
| 696 |
return |
| 697 |
container = document.getElementById(container_id) |
| 698 |
container.setAttribute('tabindex', '0') |
| 699 |
container.addEventListener('keydown', onkeydown.bind(None, container_id, back), {'passive': False, 'capture': True}) |
| 700 |
create_top_bar(container, title=_('Search for books'), icon='back') |
| 701 |
container.appendChild(E.div(class_=CLASS_NAME)) |
| 702 |
create_search_panel(container.lastChild) |
| 703 |
container.focus() |