Debian Codegraph @

src/pyj/book_list/sidebar.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
3
4 from elementmaker import E
5 from gettext import gettext as _
6
7 from book_list.globals import get_session_data
8 from book_list.library_data import browse_fields, current_library_id, current_virtual_library
9 from book_list.router import home
10 from book_list.saved_virtual_libraries import (
11 DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON, get_saved_virtual_libraries, normalize_saved_virtual_library_icon,
12 remove_saved_virtual_library, replace_saved_virtual_library, saved_virtual_library_nav_initial,
13 saved_virtual_library_query, save_saved_virtual_library
14 )
15 from book_list.saved_virtual_library_dialog import show_saved_virtual_library_dialog
16 from book_list.ui import show_panel
17 from dom import add_extra_css, clear, svgicon
18 from utils import parse_url_params
19
20
21 add_extra_css(def ():
22 return '''
23 .cs-left-nav {
24 width: var(--cs-sidebar-width); flex: 0 0 var(--cs-sidebar-width);
25 box-sizing: border-box;
26 background: var(--calibre-color-window-background);
27 color: var(--calibre-color-window-foreground);
28 padding: 0.9rem 0.65rem;
29 border-right: var(--calibre-border-style-subtle);
30 min-height: 0;
31 overflow-x: hidden;
32 overflow-y: auto;
33 display: flex;
34 flex-direction: column;
35 transition: width var(--cs-dur-3) var(--cs-ease), flex-basis var(--cs-dur-3) var(--cs-ease), padding var(--cs-dur-2) var(--cs-ease-soft);
36
37 /* Sidebar title removed to avoid duplicate header */
38 & a {
39 display: flex;
40 align-items: center;
41 justify-content: space-between;
42 color: inherit;
43 /* Reserve left gutter for active indicator to keep alignment */
44 padding: 0.55rem 0.65rem 0.55rem calc(0.65rem + 10px);
45 border-radius: var(--calibre-radius-small);
46 text-decoration: none;
47 cursor: pointer;
48 user-select: none;
49 transition: background var(--cs-transition), transform var(--cs-transition), opacity var(--cs-transition);
50
51 & .cs-nav-left {
52 display: inline-flex;
53 align-items: center;
54 gap: 0.55rem;
55 min-width: 0;
56 }
57
58 & .cs-nav-icon {
59 opacity: 0.9;
60 flex: 0 0 auto;
61 width: 1.6rem;
62 display: inline-flex;
63 justify-content: center;
64 align-items: center;
65 }
66
67 & .cs-nav-icon-emoji {
68 text-align: center;
69 overflow: hidden;
70 white-space: nowrap;
71 }
72
73 & .cs-nav-icon-image {
74 height: 1.35rem;
75 max-width: 1.6rem;
76 object-fit: contain;
77 }
78
79 & .cs-nav-collapsed-initial {
80 display: none !important;
81 font-size: 1.15rem;
82 line-height: 1;
83 font-weight: 700;
84 width: 1.6rem;
85 text-align: center;
86 }
87
88 & .cs-nav-label {
89 overflow: hidden;
90 text-overflow: ellipsis;
91 white-space: nowrap;
92 }
93 &:hover { background: var(--calibre-color-primary-light); }
94 &:active { transform: none; opacity: 0.92; }
95 &:focus { outline: none; box-shadow: var(--calibre-focus-ring); }
96 &.cs-nav-item:hover { background: var(--calibre-color-primary-light-2); }
97 &[data-active="1"] {
98 background: var(--calibre-color-primary-light-2);
99 position: relative;
100 font-weight: 600;
101 }
102 &[data-active="1"]::before {
103 content: "";
104 position: absolute;
105 left: 10px;
106 top: 8px;
107 bottom: 8px;
108 width: 3px;
109 border-radius: 999px;
110 background: var(--calibre-color-primary);
111 opacity: 0.95;
112 }
113 }
114
115 & .cs-vl-row {
116 display: flex;
117 align-items: center;
118 margin: 0;
119 }
120
121 & .cs-nav-chevron { opacity: 0.7; margin-left: 0.75rem; flex: 0 0 auto; }
122 & .cs-nav-section-contents {
123 overflow: auto;
124 padding: 0.25rem 0.25rem 0.75rem 0.25rem;
125 border-radius: var(--calibre-radius);
126 border: var(--calibre-border-style-subtle);
127 background: linear-gradient(135deg, var(--calibre-color-surface-2) 0%, var(--calibre-color-surface-3) 100%);
128 }
129 & .cs-nav-item { padding-left: 0.9rem; }
130 & .cs-nav-group { display: flex; flex-direction: column; gap: 0.25rem; }
131 & .cs-nav-group > a { margin: 0; }
132 /* Active state should not shift layout (no padding changes) */
133 & .cs-nav-bottom { margin-top: auto; padding-top: 0.25rem; }
134 & .cs-nav-divider {
135 height: 1px;
136 background: var(--cs-border);
137 margin: 0.75rem 0.65rem;
138 border-radius: 999px;
139 opacity: 0.9;
140 }
141 & .cs-nav-group-title {
142 margin: 0.75rem 0.65rem 0.25rem 0.65rem;
143 font-size: 0.85em;
144 letter-spacing: 0.08em;
145 text-transform: uppercase;
146 opacity: 0.65;
147 user-select: none;
148 display: flex;
149 align-items: center;
150 gap: 0.5rem;
151 }
152 & .cs-nav-group-title svg { opacity: 0.8; }
153 & .cs-sidebar-toggle {
154 margin: 0 0 0.25rem 0;
155 }
156 & .cs-sidebar-toggle-expand {
157 display: none;
158 }
159 & .cs-hamburger {
160 font-size: 1.25rem;
161 line-height: 1;
162 letter-spacing: -0.06em;
163 }
164 }
165
166 /* Collapsed sidebar */
167 :root.cs-sidebar-collapsed .cs-left-nav {
168 padding-left: 0.35rem; padding-right: 0.35rem;
169
170 & .cs-sidebar-toggle-expand { display: inline }
171 & .cs-sidebar-toggle-collapse { display: none }
172 & a {
173 justify-content: center;
174 padding: 0.55rem 0.5rem;
175 }
176 & a .cs-nav-left {
177 width: 100%;
178 justify-content: center;
179 }
180 & a[data-active="1"]::before {
181 display: none;
182 }
183 & .cs-nav-chevron,
184 & .cs-nav-label {
185 display: none !important;
186 }
187 & .cs-nav-group-title > span {
188 display: none !important;
189 }
190 & .cs-nav-group-title svg {
191 display: none !important;
192 }
193 & .cs-nav-group-title {
194 justify-content: center;
195 }
196 & a .cs-nav-collapsed-initial {
197 display: inline-flex !important;
198 justify-content: center;
199 align-items: center;
200 }
201 & .cs-vl-row {
202 margin: 0;
203 & a:last-of-type { display: none; }
204 }
205 }
206 '''
207 )
208
209
210 def create_sidebar():
211 # Left navigation bar (content server)
212 sidebar_width = '240px'
213 document.documentElement.style.setProperty('--cs-sidebar-width', sidebar_width)
214
215 def nav_svg_icon(icon):
216 if icon:
217 return E.span(svgicon(icon), class_='cs-nav-icon')
218 return E.span('', class_='cs-nav-icon')
219
220 def nav_image_icon(icon_url, fallback_icon):
221 img = E.img(src=icon_url, alt='', class_='cs-nav-icon cs-nav-icon-image')
222 img.onerror = def ():
223 img.onerror = None
224 if img.parentNode:
225 img.parentNode.replaceChild(nav_svg_icon(fallback_icon), img)
226 return img
227
228 def nav_link(label, action, title='', icon=None, icon_url=None, icon_emoji=None, chevron=False, collapsed_initial=None):
229 left = E.span(class_='cs-nav-left')
230 if icon_url is not None and (icon_url + ''):
231 left.appendChild(nav_image_icon(icon_url, icon))
232 elif icon_emoji is not None and (icon_emoji + ''):
233 left.appendChild(E.span(icon_emoji + '', class_='cs-nav-icon cs-nav-icon-emoji'))
234 elif collapsed_initial is not None and (collapsed_initial + ''):
235 left.appendChild(E.span(collapsed_initial + '', class_='cs-nav-icon cs-nav-collapsed-initial'))
236 else:
237 left.appendChild(nav_svg_icon(icon))
238 left.appendChild(E.span(label, class_='cs-nav-label'))
239 a = E.a(left, href='#', title=title)
240 if chevron:
241 a.appendChild(E.span('›', class_='cs-nav-chevron'))
242 a.addEventListener('click', def (evt):
243 evt.preventDefault()
244 action()
245 )
246 return a
247
248 root = E.div(class_='cs-app-root')
249 left = E.nav(class_='cs-left-nav')
250 # Sidebar collapse/expand state
251 collapsed_width = '64px'
252 expanded_width = sidebar_width
253
254 def apply_sidebar_state(collapsed):
255 de = document.documentElement
256 if collapsed:
257 de.classList.add('cs-sidebar-collapsed')
258 de.style.setProperty('--cs-sidebar-width', collapsed_width)
259 else:
260 de.classList.remove('cs-sidebar-collapsed')
261 de.style.setProperty('--cs-sidebar-width', expanded_width)
262 try:
263 window.localStorage.setItem('cs_sidebar_collapsed', '1' if collapsed else '0')
264 except Exception:
265 pass
266
267 def toggle_sidebar():
268 de = document.documentElement
269 apply_sidebar_state(not de.classList.contains('cs-sidebar-collapsed'))
270
271 # Restore previous state, default to collapsed on small screens
272 try:
273 stored = window.localStorage.getItem('cs_sidebar_collapsed')
274 except Exception:
275 stored = None
276 if stored is None or stored is '':
277 # No user preference yet
278 try:
279 small = window.matchMedia('(max-width: 720px)').matches
280 except Exception:
281 small = False
282 apply_sidebar_state(bool(small))
283 else:
284 apply_sidebar_state(stored is '1')
285
286 collapse = nav_link(_('Collapse'), toggle_sidebar, _('Toggle sidebar'), icon='chevron-left')
287 collapse.classList.add('cs-sidebar-toggle', 'cs-sidebar-toggle-collapse')
288 expand = nav_link('', toggle_sidebar, _('Toggle sidebar'), icon='chevron-right')
289 expand.classList.add('cs-sidebar-toggle', 'cs-sidebar-toggle-expand')
290 left.appendChild(collapse)
291 left.appendChild(expand)
292
293 # Group 1: primary navigation
294 left.appendChild(E.div(svgicon('home'), E.span(_('Navigation')), class_='cs-nav-group-title'))
295 g1 = E.div(class_='cs-nav-group')
296
297 home_link = nav_link(_('Home'), home, _('Go to Home'), icon='home')
298 g1.appendChild(home_link)
299 go_search = def ():
300 show_panel('book_list^search')
301 search_link = nav_link(_('Search'), go_search, _('Search for books'), icon='search')
302 g1.appendChild(search_link)
303 go_reader = def ():
304 show_panel('local_books')
305 reader_link = nav_link(_('Downloaded books'), go_reader, _('Read previously downloaded books'), icon='cloud-download')
306 g1.appendChild(reader_link)
307 left.appendChild(g1)
308
309 left.appendChild(E.div(class_='cs-nav-divider'))
310
311 # Group 2: browse lists
312 left.appendChild(E.div(svgicon('toc'), E.span(_('Browse')), class_='cs-nav-group-title'))
313 g2 = E.div(class_='cs-nav-group')
314
315 def nav_section(label, field, tooltip='', icon='toc', icon_url=''):
316 action = def ():
317 show_panel('nav_list', {'field': field})
318 t = nav_link(label, action, tooltip, icon=icon, icon_url=icon_url, chevron=True)
319 return t
320
321 section_elems = {}
322
323 def render_browse_fields():
324 clear(g2)
325 for k in Object.keys(section_elems):
326 v'delete section_elems[k]'
327 for field in browse_fields():
328 key = (field.key or '') + ''
329 if not key:
330 continue
331 section_elems[key] = nav_section(field.name or key, key, field.description or '', icon=field.icon or 'toc', icon_url=field.custom_icon_url or '')
332 g2.appendChild(section_elems[key])
333
334 left.appendChild(g2)
335
336 left.appendChild(E.div(class_='cs-nav-divider'))
337
338 # Group: virtual libraries
339 left.appendChild(E.div(svgicon('star'), E.span(_('Virtual libraries')), class_='cs-nav-group-title'))
340 saved_virtual_libraries_group = E.div(class_='cs-nav-group')
341 saved_virtual_libraries_library_id = None
342 left.appendChild(saved_virtual_libraries_group)
343
344 left.appendChild(E.div(class_='cs-nav-divider'))
345
346 # Bottom: settings
347 settings_link = nav_link(_('Settings'), def (): show_panel('book_list^prefs');, _('Configure web interface'), icon='cog')
348 settings_link.classList.add('cs-sidebar-toggle')
349 left.appendChild(settings_link)
350
351 def clear_active(elem):
352 if elem and elem.dataset and elem.dataset.active:
353 elem.dataset.active = ''
354
355 def edit_saved_virtual_library(item):
356 item = item or v'{}'
357
358 def save_changes(updated_item):
359 if not replace_saved_virtual_library(item, updated_item):
360 return False
361 render_saved_virtual_libraries()
362 return True
363
364 def delete_item():
365 if not remove_saved_virtual_library(item):
366 return False
367 render_saved_virtual_libraries()
368 return True
369
370 show_saved_virtual_library_dialog(
371 _('Edit virtual library'), _('Save'), save_changes,
372 (item.library_id or '') + '',
373 (item.vl or '') + '',
374 initial_name=(item.name or '') + '',
375 initial_search=(item.search or '') + '',
376 initial_icon=(item.icon or DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON) + '',
377 initial_use_icon=item.use_icon is not False,
378 on_delete=delete_item,
379 )
380
381 def create_saved_virtual_library():
382 def create_item(item):
383 if not save_saved_virtual_library(item):
384 return False
385 render_saved_virtual_libraries()
386 return True
387
388 show_saved_virtual_library_dialog(
389 _('New virtual library'), _('Create'), create_item,
390 current_library_id(), current_virtual_library(), save_icon='plus'
391 )
392
393 def render_saved_virtual_libraries():
394 nonlocal saved_virtual_libraries_library_id
395 library_id = (current_library_id() or '') + ''
396 saved_virtual_libraries_library_id = library_id
397 clear(saved_virtual_libraries_group)
398 for item in get_saved_virtual_libraries(library_id):
399 name = (item.name or '') + ''
400 search_expr = (item.search or '') + ''
401 icon_name = (item.icon or DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON) + ''
402 if not name or not search_expr:
403 continue
404 action = def (saved_item = item):
405 show_panel('book_list', saved_virtual_library_query(saved_item), replace=True)
406 row = E.div(class_='cs-vl-row')
407 show_icon = item.use_icon is not False
408 if show_icon:
409 link = nav_link(name, action, search_expr, icon_emoji=normalize_saved_virtual_library_icon(icon_name), chevron=True)
410 else:
411 link = nav_link(name, action, search_expr, collapsed_initial=saved_virtual_library_nav_initial(name), chevron=True)
412 link.style.margin = '0'
413 link.style.flex = '1 1 auto'
414 link.style.minWidth = '0'
415 cfg = E.a(svgicon('cog'), href='#', title=_('Configure virtual library'), style='padding: 0.2rem; border-radius:10px; flex:0 0 auto;')
416 cfg.addEventListener('click', def (ev, saved_item = item):
417 ev.preventDefault()
418 ev.stopPropagation()
419 edit_saved_virtual_library(saved_item)
420 )
421 row.appendChild(link)
422 row.appendChild(cfg)
423 saved_virtual_libraries_group.appendChild(row)
424 add_link = nav_link(_('New virtual library'), create_saved_virtual_library, _('Create a new virtual library'), icon='plus')
425 saved_virtual_libraries_group.appendChild(add_link)
426
427 def sync_saved_virtual_libraries():
428 if saved_virtual_libraries_library_id is not ((current_library_id() or '') + ''):
429 render_saved_virtual_libraries()
430
431 def update_sidebar_visibility(current):
432 sd = get_session_data()
433 show_in_viewer = sd.get('show_sidebar_in_book_viewer')
434 mode = (current.mode or 'book_list') if current else 'book_list'
435 left.style.display = 'flex' if (mode is not 'read_book' or show_in_viewer) else 'none'
436
437 def update_sidebar_active():
438 current = parse_url_params()
439 update_sidebar_visibility(current)
440 sync_saved_virtual_libraries()
441 active_panel = current.panel or ''
442 active_field = current.field or ''
443 clear_active(home_link)
444 clear_active(search_link)
445 clear_active(reader_link)
446 clear_active(settings_link)
447 for k in Object.keys(section_elems):
448 clear_active(section_elems[k])
449 if active_panel is 'book_list^search':
450 search_link.dataset.active = '1'
451 elif active_panel is 'local_books':
452 reader_link.dataset.active = '1'
453 elif active_panel is 'book_list^prefs':
454 settings_link.dataset.active = '1'
455 elif active_panel is 'nav_list' and active_field and section_elems[active_field]:
456 section_elems[active_field].dataset.active = '1'
457 elif not active_panel:
458 home_link.dataset.active = '1'
459
460 def render_sidebar_navigation():
461 render_browse_fields()
462 render_saved_virtual_libraries()
463 update_sidebar_active()
464
465 window.cs_update_sidebar_active = update_sidebar_active
466 window.cs_refresh_sidebar_navigation = render_sidebar_navigation
467 render_sidebar_navigation()
468 return root, left