Debian Codegraph @

src/pyj/read_book/profiles.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2024, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 from elementmaker import E
6 from gettext import gettext as _
7
8 from book_list.globals import get_session_data
9 from book_list.item_list import create_item, create_item_list, create_side_action
10 from book_list.top_bar import create_top_bar
11 from dom import clear, unique_id
12 from modals import question_dialog
13 from read_book.globals import ui_operations
14 from session import apply_reader_profile, settings_for_reader_profile
15 from widgets import create_button
16
17
18 def save_profile(container_id, hide_panel):
19 container = document.getElementById(container_id)
20 if not container:
21 return
22 name = container.querySelector('[name=profile_name]').value
23 sd = get_session_data()
24 settings = settings_for_reader_profile(sd)
25 ui_operations.save_profile(name, settings, def ():
26 container = document.getElementById(container_id)
27 if container:
28 r = container.querySelector('.saved_msg')
29 if r:
30 clear(r)
31 r.appendChild(E.div(E.i(_('Saved: ') + name), style='margin-top: 1rem; margin-bottom: 1rem'))
32 load_profiles(container_id)
33 )
34
35
36 def use_profile(container_id, profile_name, profile_data):
37 container = document.getElementById(container_id)
38 if not container:
39 return
40 sd = get_session_data()
41 apply_reader_profile(sd, profile_data)
42 if ui_operations.apply_profile:
43 ui_operations.apply_profile(profile_data)
44 container.dispatchEvent(new Event('settings_changed'))
45 container.dispatchEvent(new Event('close_panel'))
46
47
48 def delete_profile(container_id, profile_name):
49 question_dialog(_('Are you sure?'), _('Are you sure you want to delete the saved profile named: {}?').format(profile_name),
50 def (ok):
51 if ok:
52 ui_operations.save_profile(profile_name, None, def ():
53 load_profiles(container_id)
54 )
55 )
56
57
58 def got_all_profiles(container_id, all_profiles):
59 container = document.getElementById(container_id)
60 if not container:
61 return
62 all_profiles['__default__'] = {}
63 names = list_wrap(Object.keys(all_profiles))
64 names.pysort(def (x): return '' if x is '__default__' else x.toLowerCase();)
65 pl = container.querySelector('.profiles_list_container')
66 clear(pl)
67 pl.appendChild(E.div(
68 _('Load settings from one of the previously saved profiles below…')
69 ))
70 pl.appendChild(E.div())
71 items = []
72 for name in names:
73 display_name = name
74 if name is '__default__':
75 display_name = _('Restore settings to default values')
76 side_actions = []
77 else:
78 side_actions = [create_side_action('trash', delete_profile.bind(None, container_id, name), _('Delete this profile'))]
79 items.push(create_item(display_name, action=use_profile.bind(None, container_id, name, all_profiles[name]), side_actions=side_actions))
80 create_item_list(pl.lastChild, items)
81
82
83 def load_profiles(container_id):
84 container = document.getElementById(container_id)
85 if not container:
86 return
87 pl = container.querySelector('.profiles_list_container')
88 clear(pl)
89 pl.appendChild(E.div(_('Loading saved profiles, please wait…'), style='padding-bottom: 1rem'))
90 ui_operations.get_all_profiles(got_all_profiles.bind(None, container_id))
91
92
93 def create_profiles_panel(container, hide_panel, on_change):
94 create_top_bar(container, title=_('Profiles'), icon='close', action=hide_panel)
95 c = E.div(style='margin: 1rem', id=unique_id('placeholder-prefs'))
96 c.addEventListener('close_panel', def (): hide_panel();, False)
97 c.addEventListener('settings_changed', def (): on_change();, False)
98 container_id = c.id
99 a = E.div(
100 style='display:flex; flex-wrap: wrap; align-items: center; gap: 0.65rem',
101 E.label(_('Save current settings as profile:') + ' '), E.input(
102 name='profile_name', type='text', placeholder=_('Name of profile'), onkeydown=def (e):
103 if e.key is 'Enter':
104 save_profile(container_id, hide_panel)
105 ),
106 create_button(_('Save'), 'bookmark', save_profile.bind(None, container_id, hide_panel))
107 )
108 container.appendChild(c)
109 c.appendChild(a)
110 c.appendChild(E.div(class_='saved_msg'))
111 c.appendChild(E.hr(style='margin-top: 1ex; margin-bottom: 1ex'))
112 c.appendChild(E.div(class_='profiles_list_container'))
113 load_profiles(container_id)