| 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 book_list.theme import get_color |
| 9 |
from dom import build_rule, clear, svgicon, create_keyframes, set_css, change_icon_image, add_extra_css, ensure_id |
| 10 |
|
| 11 |
# Button {{{ |
| 12 |
|
| 13 |
|
| 14 |
def create_button(text, icon=None, action=None, tooltip=None, highlight=False, download_filename=None, class_=''): |
| 15 |
ic = '' |
| 16 |
if icon: |
| 17 |
ic = svgicon(icon) |
| 18 |
if text: |
| 19 |
text = '\xa0' + text |
| 20 |
cls = ('calibre-push-button ' + (class_ or '')).strip() |
| 21 |
if highlight: |
| 22 |
cls = (cls + ' calibre-primary-button').strip() |
| 23 |
ans = E.a(ic, E.span(text), class_=cls, href='javascript: void(0)', role='button', title=tooltip or '') |
| 24 |
if download_filename and v'"download" in ans': |
| 25 |
ans.setAttribute('download', download_filename) |
| 26 |
if action is not None: |
| 27 |
if jstype(action) is 'string': |
| 28 |
ans.setAttribute('href', action) |
| 29 |
else: |
| 30 |
ans.addEventListener('click', def (event): event.preventDefault(), action(event);) |
| 31 |
return ans |
| 32 |
|
| 33 |
create_button.style += r''' |
| 34 |
@media (max-width: 420px) { |
| 35 |
#modal-container .button-box { flex-direction: column; align-items: stretch; } |
| 36 |
#modal-container .button-box > a.calibre-push-button { width: 100%; flex: 1 1 auto; justify-content: center; } |
| 37 |
#modal-container .button-box > a.calibre-push-button:only-of-type { margin-left: 0; } |
| 38 |
} |
| 39 |
''' |
| 40 |
# }}} |
| 41 |
|
| 42 |
# Pager Widget {{{ |
| 43 |
|
| 44 |
|
| 45 |
def create_pager_widget(on_prev, on_next): |
| 46 |
def create_pager_button(is_prev, label, icon, onclick): |
| 47 |
a = E.a(svgicon(icon), E.span(label), href='javascript:void(0)', class_='calibre-push-button') |
| 48 |
if not is_prev: # next button should have icon on the right |
| 49 |
a.insertBefore(a.firstChild.nextSibling, a.firstChild) |
| 50 |
a.addEventListener('click', def (evt): |
| 51 |
evt.preventDefault() |
| 52 |
onclick() |
| 53 |
) |
| 54 |
return a |
| 55 |
|
| 56 |
prev_btn = create_pager_button(True, _('Prev'), 'chevron-left', on_prev) |
| 57 |
next_btn = create_pager_button(False, _('Next'), 'chevron-right', on_next) |
| 58 |
buttons_container = E.div(prev_btn, E.div(), next_btn, class_='bottom-row') |
| 59 |
top_row = E.div(class_='top-row hidden') |
| 60 |
middle_row = E.div(class_='middle-row hidden') |
| 61 |
|
| 62 |
container = E.div( |
| 63 |
top_row, |
| 64 |
E.hr(), |
| 65 |
middle_row, |
| 66 |
E.hr(), |
| 67 |
buttons_container, |
| 68 |
class_='calibre-pager-widget calibre-group-box', |
| 69 |
) |
| 70 |
|
| 71 |
def update_container_visibility(): |
| 72 |
container.style.display = 'none' if ( |
| 73 |
buttons_container.classList.contains('hidden') |
| 74 |
and top_row.classList.contains('hidden') |
| 75 |
and middle_row.classList.contains('hidden') |
| 76 |
) else '' |
| 77 |
|
| 78 |
def set_enabled(has_prev, has_next): |
| 79 |
prev_btn.setAttribute('aria-disabled', 'false' if has_prev else 'true') |
| 80 |
next_btn.setAttribute('aria-disabled', 'false' if has_next else 'true') |
| 81 |
if has_prev or has_next: |
| 82 |
buttons_container.classList.remove('hidden') |
| 83 |
else: |
| 84 |
buttons_container.classList.add('hidden') |
| 85 |
update_container_visibility() |
| 86 |
|
| 87 |
def set_visible(which, visible): |
| 88 |
if visible: |
| 89 |
which.classList.remove('hidden') |
| 90 |
else: |
| 91 |
which.classList.add('hidden') |
| 92 |
update_container_visibility() |
| 93 |
|
| 94 |
return { |
| 95 |
'container': container, 'count_msg': buttons_container.firstChild.nextSibling, |
| 96 |
'prev_btn': prev_btn, 'next_btn': next_btn, 'set_enabled': set_enabled, |
| 97 |
'top_row': top_row, 'middle_row': middle_row, |
| 98 |
'set_top_row_visibility': def (visible): set_visible(top_row, visible);, |
| 99 |
'set_middle_row_visibility': def (visible): set_visible(middle_row, visible);, |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
create_pager_widget.style = ''' |
| 104 |
div.calibre-pager-widget { |
| 105 |
display: flex; |
| 106 |
flex-direction: column; |
| 107 |
|
| 108 |
& > hr { |
| 109 |
margin-top: 0.9ex; margin-bottom: 0.9ex; |
| 110 |
} |
| 111 |
|
| 112 |
& > .top-row { |
| 113 |
display: flex; |
| 114 |
overflow: hidden; |
| 115 |
gap: 0.75rem; |
| 116 |
} |
| 117 |
|
| 118 |
& > .middle-row { |
| 119 |
display: flex; |
| 120 |
overflow: hidden; |
| 121 |
flex-wrap: wrap; |
| 122 |
gap: 0.75rem; |
| 123 |
} |
| 124 |
|
| 125 |
& > .bottom-row { |
| 126 |
display: flex; |
| 127 |
align-items: center; |
| 128 |
justify-content: space-between; |
| 129 |
gap: 0.75rem; |
| 130 |
flex-wrap: wrap; |
| 131 |
min-width: 0; |
| 132 |
width: 100%; |
| 133 |
box-sizing: border-box; |
| 134 |
|
| 135 |
& > :first-child, & > :last-child { |
| 136 |
display: flex; |
| 137 |
align-items: center; |
| 138 |
gap: 0.5rem; |
| 139 |
flex: 1 1 auto; |
| 140 |
min-width: 0; |
| 141 |
max-width: 100%; |
| 142 |
} |
| 143 |
|
| 144 |
& > :first-child { justify-content: flex-start; } |
| 145 |
& > :last-child { justify-content: flex-end; } |
| 146 |
& > :nth-child(2) { |
| 147 |
text-align: center; |
| 148 |
display: block; |
| 149 |
text-overflow: ellipsis; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
& > .hidden { |
| 154 |
display: none; |
| 155 |
& + hr { display: none; } |
| 156 |
} |
| 157 |
& > hr:has(+ .hidden) { display: none; } |
| 158 |
|
| 159 |
} |
| 160 |
''' |
| 161 |
|
| 162 |
# }}} |
| 163 |
|
| 164 |
|
| 165 |
# Search {{{ |
| 166 |
def create_search_widget(onsearch, value='', placeholder='', tooltip='', live_searching=False): |
| 167 |
box = E.div( |
| 168 |
data_component='search_box', |
| 169 |
style='width: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 0.65rem' |
| 170 |
) |
| 171 |
search = E.input( |
| 172 |
type='search', value=value, placeholder=placeholder, title=tooltip or placeholder, autocomplete='off', |
| 173 |
style='width: 0; flex-grow: 1; min-width: 6em', |
| 174 |
) |
| 175 |
search.addEventListener('input' if live_searching else 'search', def (): onsearch(this.value);) |
| 176 |
box.appendChild(search) |
| 177 |
sb = E.a( |
| 178 |
svgicon('search'), |
| 179 |
class_='calibre-icon-button', |
| 180 |
style='margin-left: auto', |
| 181 |
href='javascript:void(0)', |
| 182 |
title=_('Search'), |
| 183 |
onmousedown=def (evt): evt.preventDefault();, |
| 184 |
onclick=def (evt): |
| 185 |
evt.preventDefault() |
| 186 |
onsearch(this.previousSibling.value) |
| 187 |
) |
| 188 |
box.appendChild(sb) |
| 189 |
return box |
| 190 |
# }}} |
| 191 |
|
| 192 |
|
| 193 |
# Select {{{ |
| 194 |
def create_select(title, current_value, **options): |
| 195 |
sel = E.select(title=title) |
| 196 |
for value, text in Object.entries(options): |
| 197 |
sel.appendChild(E.option(text, value=value)) |
| 198 |
sel.value = current_value |
| 199 |
|
| 200 |
container = E.div( |
| 201 |
sel, E.span(svgicon('angle-down')), |
| 202 |
data_component='select', |
| 203 |
) |
| 204 |
return container, sel |
| 205 |
|
| 206 |
create_select.style = ''' |
| 207 |
div[data-component=select] { |
| 208 |
display: inline-flex; |
| 209 |
position: relative; |
| 210 |
align-items: center; |
| 211 |
flex-shrink: 0; |
| 212 |
|
| 213 |
& > select { |
| 214 |
appearance: none; |
| 215 |
margin: 0; |
| 216 |
padding: 0.35rem 2.0em 0.35rem 0.65rem; |
| 217 |
border-radius: 999px; |
| 218 |
border: var(--calibre-border-style-subtle); |
| 219 |
background-color: var(--calibre-color-window-background); |
| 220 |
color: var(--calibre-color-window-foreground); |
| 221 |
align-self: center; |
| 222 |
box-sizing: border-box; |
| 223 |
|
| 224 |
&:hover { |
| 225 |
background-color: var(--calibre-color-primary-light); |
| 226 |
border: var(--calibre-border-style); |
| 227 |
} |
| 228 |
&:focus { |
| 229 |
outline: none; |
| 230 |
border: var(--calibre-border-style-subtle); |
| 231 |
} |
| 232 |
|
| 233 |
& > option { |
| 234 |
background-color: var(--calibre-color-window-background); |
| 235 |
color: var(--calibre-color-input-foreground); |
| 236 |
&:checked { outline-color: var(--calibre-color-primary); } |
| 237 |
&:hover { |
| 238 |
background-color: var(--calibre-color-window-background); |
| 239 |
color: var(--calibre-color-primary); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
& > span { |
| 244 |
position: absolute; |
| 245 |
right: 0.65em; |
| 246 |
top: 50%; |
| 247 |
transform: translateY(-50%); |
| 248 |
pointer-events: none; |
| 249 |
opacity: 0.75; |
| 250 |
color: currentColor; |
| 251 |
display: inline-flex; |
| 252 |
align-items: center; |
| 253 |
justify-content: center; |
| 254 |
|
| 255 |
& > svg { |
| 256 |
width: 1em; |
| 257 |
height: 1em; |
| 258 |
flex-shrink: 0; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
''' |
| 263 |
# }}} |
| 264 |
|
| 265 |
|
| 266 |
# Spinner {{{ |
| 267 |
def create_spinner(height, width): |
| 268 |
ans = svgicon('cog', height, width) |
| 269 |
ans.classList.add('spin') |
| 270 |
return ans |
| 271 |
|
| 272 |
create_spinner.style = build_rule('.spin', animation='spin 2s infinite linear') |
| 273 |
create_spinner.style += create_keyframes('spin', 'from { transform: rotate(0deg); } to { transform: rotate(359deg);}') |
| 274 |
|
| 275 |
# }}} |
| 276 |
|
| 277 |
# Breadcrumbs {{{ |
| 278 |
|
| 279 |
|
| 280 |
class Breadcrumbs: |
| 281 |
STYLE_RULES = build_rule( |
| 282 |
'.calibre-breadcrumbs', |
| 283 |
user_select='none', white_space='nowrap', background_color=get_color('window-background2'), |
| 284 |
z_index='-1', border_radius='10px', margin='1ex 1em', margin_bottom='0' |
| 285 |
) |
| 286 |
|
| 287 |
STYLE_RULES += build_rule( |
| 288 |
'.calibre-breadcrumbs > li', |
| 289 |
cursor='pointer', display='inline-block', line_height='26px', margin='0 9px 0 -10px', |
| 290 |
padding='0.5ex 1rem', position='relative' |
| 291 |
) |
| 292 |
|
| 293 |
STYLE_RULES += build_rule('.calibre-breadcrumbs > li:hover', color=get_color('window-hover-foreground')) |
| 294 |
|
| 295 |
STYLE_RULES += build_rule('.calibre-breadcrumbs > li:active', color=get_color('window-hover-foreground'), transform='scale(1.5)') |
| 296 |
|
| 297 |
STYLE_RULES += build_rule( |
| 298 |
'.calibre-breadcrumbs > li:before, .calibre-breadcrumbs > li:after', |
| 299 |
border_right='2px solid currentColor', content='""', display='block', height='50%', |
| 300 |
position='absolute', left='0', top='0', right='0', transform='skewX(45deg)' |
| 301 |
) |
| 302 |
|
| 303 |
STYLE_RULES += build_rule( |
| 304 |
'.calibre-breadcrumbs > li:after', |
| 305 |
bottom='0', top='auto', transform='skewX(-45deg)' |
| 306 |
) |
| 307 |
|
| 308 |
STYLE_RULES += build_rule( |
| 309 |
'.calibre-breadcrumbs > li:last-of-type:before, .calibre-breadcrumbs > li:last-of-type:after', |
| 310 |
display='none') |
| 311 |
|
| 312 |
def __init__(self, container): |
| 313 |
self.container_id = ensure_id(container, 'calibre-breadcrumbs-') |
| 314 |
container.classList.add('calibre-breadcrumbs') |
| 315 |
clear(container) |
| 316 |
|
| 317 |
@property |
| 318 |
def container(self): |
| 319 |
return document.getElementById(self.container_id) |
| 320 |
|
| 321 |
def reset(self): |
| 322 |
clear(self.container) |
| 323 |
|
| 324 |
def add_crumb(self, callback): |
| 325 |
li = E.li() |
| 326 |
if callback: |
| 327 |
li.addEventListener('click', callback) |
| 328 |
self.container.appendChild(li) |
| 329 |
return li |
| 330 |
# }}} |
| 331 |
|
| 332 |
# Simple Tree {{{ |
| 333 |
|
| 334 |
|
| 335 |
def create_tree(root, populate_data, onclick): |
| 336 |
container = E.div(class_='simple-tree') |
| 337 |
set_css(container, overflow='auto') |
| 338 |
|
| 339 |
def toggle_node(li): |
| 340 |
if li.dataset.treeState is 'closed': |
| 341 |
li.dataset.treeState = 'open' |
| 342 |
li.lastChild.style.display = 'block' |
| 343 |
change_icon_image(li.firstChild.firstChild, 'caret-down') |
| 344 |
else: |
| 345 |
li.dataset.treeState = 'closed' |
| 346 |
li.lastChild.style.display = 'none' |
| 347 |
change_icon_image(li.firstChild.firstChild, 'caret-right') |
| 348 |
|
| 349 |
def bullet(icon): |
| 350 |
ans = svgicon(icon, '2ex', '2ex') |
| 351 |
ans.style.minWidth = '2ex' |
| 352 |
ans.style.minHeight = '2ex' |
| 353 |
return ans |
| 354 |
|
| 355 |
def process_node(node, parent_container, level): |
| 356 |
if node.children?.length: |
| 357 |
ul = E.div() |
| 358 |
parent_container.appendChild(ul) |
| 359 |
for child in node.children: |
| 360 |
icon = 'caret-right' if child.children?.length else None |
| 361 |
li = E.div(style='display:flex; flex-direction:column; margin: 1ex 1em; margin-left: {}em'.format(level + 1), |
| 362 |
E.div(style='display:flex; align-items: center', |
| 363 |
bullet(icon), |
| 364 |
E.span('\xa0'), |
| 365 |
E.a( |
| 366 |
href='javascript: void(0)', |
| 367 |
class_='tree-item-title', |
| 368 |
onclick=def (event): |
| 369 |
if onclick: |
| 370 |
if event.button is 0: |
| 371 |
event.preventDefault(), event.stopPropagation() |
| 372 |
onclick(event, event.currentTarget.parentNode.parentNode) |
| 373 |
), |
| 374 |
), |
| 375 |
E.div(style='display:none', data_tree_subtree_container='1'), |
| 376 |
data_tree_state='closed', |
| 377 |
) |
| 378 |
ul.appendChild(li) |
| 379 |
populate_data(child, li, li.firstChild.lastChild) |
| 380 |
if icon: |
| 381 |
set_css(li.firstChild.firstChild, cursor='pointer') |
| 382 |
li.firstChild.firstChild.addEventListener('click', def (event): |
| 383 |
toggle_node(event.currentTarget.parentNode.parentNode) |
| 384 |
) |
| 385 |
process_node(child, li.lastChild, level + 1) |
| 386 |
|
| 387 |
if root: |
| 388 |
process_node(root, container, 0) |
| 389 |
return container |
| 390 |
|
| 391 |
|
| 392 |
def find_text_in_tree(container, q): |
| 393 |
q = q.lower() |
| 394 |
last_match = container.querySelector('a[data-tree-last-match]') |
| 395 |
if last_match: |
| 396 |
last_match.parentNode.style.backgroundColor = 'transparent' |
| 397 |
last_match.parentNode.style.borderRadius = '0' |
| 398 |
lm = last_match.getAttribute('data-tree-last-match') |
| 399 |
last_match.removeAttribute('data-tree-last-match') |
| 400 |
if lm is not q: |
| 401 |
last_match = None |
| 402 |
if not q: |
| 403 |
return |
| 404 |
before = [] |
| 405 |
seen = False |
| 406 |
ans = None |
| 407 |
|
| 408 |
for a in container.querySelectorAll('a.tree-item-title'): |
| 409 |
if a is last_match: |
| 410 |
seen = True |
| 411 |
else: |
| 412 |
if a.textContent.lower().indexOf(q) != -1: |
| 413 |
if seen: |
| 414 |
ans = a |
| 415 |
break |
| 416 |
if last_match is None: |
| 417 |
ans = a |
| 418 |
break |
| 419 |
before.push(a) |
| 420 |
if not ans and before.length: |
| 421 |
ans = before[0] |
| 422 |
ans = ans or last_match |
| 423 |
if ans: |
| 424 |
ans.dataset.treeLastMatch = q |
| 425 |
if ans: |
| 426 |
ans.parentNode.style.backgroundColor = get_color('tree-highlight-item') |
| 427 |
ans.parentNode.style.borderRadius = '5px' |
| 428 |
ans = ans.parentNode.parentNode |
| 429 |
return ans |
| 430 |
|
| 431 |
|
| 432 |
def scroll_tree_item_into_view(item): |
| 433 |
p = item.parentNode?.parentNode |
| 434 |
while p and p.getAttribute('data-tree-subtree-container'): |
| 435 |
p.style.display = 'block' |
| 436 |
p = p.parentNode?.parentNode?.parentNode |
| 437 |
item.scrollIntoView() |
| 438 |
|
| 439 |
# }}} |
| 440 |
|
| 441 |
add_extra_css(def (): |
| 442 |
ans = 'a, button:focus { outline: none }; a, button::-moz-focus-inner { border: 0 }\n' |
| 443 |
ans += ( |
| 444 |
'.simple-link { cursor: pointer } .simple-link:hover { color: HC }' |
| 445 |
' .simple-link:active { transform: scale(1.5) }\n' |
| 446 |
).replace('HC', get_color('window-hover-foreground')) |
| 447 |
ans += ''' |
| 448 |
a.tree-item-title { |
| 449 |
color: currentColor; |
| 450 |
cursor: pointer; |
| 451 |
text-decoration: none; |
| 452 |
&:hover, &:active { |
| 453 |
color: var(--calibre-color-primary); |
| 454 |
text-decoration: none; |
| 455 |
} |
| 456 |
} |
| 457 |
''' |
| 458 |
# Use theme primary color for all "blue-link" links. |
| 459 |
ans += ( |
| 460 |
'.blue-link { cursor: pointer; color: var(--calibre-color-primary) }' |
| 461 |
' .blue-link:visited { color: var(--calibre-color-primary)}' |
| 462 |
' .blue-link:hover { color: var(--calibre-color-primary) }' |
| 463 |
' .blue-link:active { transform: scale(1.5) }\n' |
| 464 |
) |
| 465 |
ans += create_button.style + '\n' |
| 466 |
ans += create_pager_widget.style + '\n' |
| 467 |
ans += create_spinner.style + '\n' |
| 468 |
ans += Breadcrumbs.STYLE_RULES + '\n' |
| 469 |
ans += create_select.style + '\n' |
| 470 |
# Chrome hardcodes the size of these to 13.3px. In safari on some devices |
| 471 |
# the font size is smaller than the body font size and small enough to |
| 472 |
# trigger page zooms. So set it to 1em so they scale with body font size. |
| 473 |
ans += ''' |
| 474 |
select, textarea, input[type="text"], input[type="password"], |
| 475 |
input[type="datetime"], input[type="datetime-local"], |
| 476 |
input[type="date"], input[type="month"], input[type="time"], |
| 477 |
input[type="week"], input[type="number"], input[type="email"], |
| 478 |
input[type="url"], input[type="search"] { font-size: 1em } |
| 479 |
''' |
| 480 |
return ans |
| 481 |
) |
| 482 |
|
| 483 |
|
| 484 |
def enable_escape_key(container, action): |
| 485 |
container.setAttribute('tabindex', '0') |
| 486 |
container.addEventListener('keydown', def (ev): |
| 487 |
if ev.key is 'Escape': |
| 488 |
ev.stopPropagation(), ev.preventDefault() |
| 489 |
action() |
| 490 |
, {'passive': False}) |
| 491 |
container.focus() |