| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, 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 |
| 10 |
from complete import create_search_bar |
| 11 |
from dom import clear, unique_id |
| 12 |
from read_book.shortcuts import ( |
| 13 |
key_as_text, keyevent_as_shortcut, shortcut_differs, shortcuts_definition, |
| 14 |
shortcuts_group_desc |
| 15 |
) |
| 16 |
from widgets import create_button |
| 17 |
|
| 18 |
|
| 19 |
def get_container(): |
| 20 |
return document.getElementById(get_container.id) |
| 21 |
|
| 22 |
|
| 23 |
def restore_defaults(close_func): |
| 24 |
get_container().dataset.changed = 'true' |
| 25 |
for item in get_container().querySelectorAll('[data-user-data]'): |
| 26 |
q = JSON.parse(item.dataset.userData) |
| 27 |
q.shortcuts = shortcuts_definition()[q.name].shortcuts |
| 28 |
item.dataset.userData = JSON.stringify(q) |
| 29 |
close_func() |
| 30 |
|
| 31 |
|
| 32 |
def as_groups(shortcuts): |
| 33 |
ans = {} |
| 34 |
for sc_name in Object.keys(shortcuts): |
| 35 |
sc = shortcuts[sc_name] |
| 36 |
if not ans[sc.group]: |
| 37 |
ans[sc.group] = {} |
| 38 |
ans[sc.group][sc_name] = sc |
| 39 |
return ans |
| 40 |
|
| 41 |
|
| 42 |
def sort_group_key(group, sc_name): |
| 43 |
return group[sc_name].short.toLowerCase() |
| 44 |
|
| 45 |
|
| 46 |
def sc_as_item(sc_name, sc, shortcuts): |
| 47 |
cuts = shortcuts or sc.shortcuts |
| 48 |
return create_item(sc.short, action=customize_shortcut.bind(None, sc_name), subtitle=sc.long, data=JSON.stringify({'name': sc_name, 'shortcuts': cuts})) |
| 49 |
|
| 50 |
|
| 51 |
def remove_key(evt): |
| 52 |
key_container = evt.currentTarget.parentNode.parentNode |
| 53 |
key_container.parentNode.removeChild(key_container) |
| 54 |
|
| 55 |
|
| 56 |
def key_widget(key): |
| 57 |
return E.tr( |
| 58 |
data_shortcut=JSON.stringify(keyevent_as_shortcut(key)), |
| 59 |
E.td(style='padding: 0.5rem; border-right: solid 1px; margin-right: 0.5rem; margin-top: 0.5rem; vertical-align: middle', key_as_text(key)), |
| 60 |
E.td(style='padding: 0.5rem; margin-top: 0.5rem; vertical-align: bottom', create_button(_('Remove'), 'remove', action=remove_key)), |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
def close_customize_shortcut(apply_changes): |
| 65 |
container = get_container() |
| 66 |
container.firstChild.style.display = 'flex' |
| 67 |
container.firstChild.nextSibling.style.display = 'block' |
| 68 |
container.lastChild.style.display = 'none' |
| 69 |
if close_customize_shortcut.shortcut_being_customized: |
| 70 |
for item in container.firstChild.querySelectorAll('[data-user-data]'): |
| 71 |
q = JSON.parse(item.dataset.userData) |
| 72 |
if q.name is close_customize_shortcut.shortcut_being_customized: |
| 73 |
item.scrollIntoView() |
| 74 |
break |
| 75 |
if apply_changes: |
| 76 |
shortcuts = [] |
| 77 |
for x in container.lastChild.querySelectorAll('[data-shortcut]'): |
| 78 |
sc = JSON.parse(x.dataset.shortcut) |
| 79 |
shortcuts.push(sc) |
| 80 |
sc_name = container.lastChild.dataset.scName |
| 81 |
for item in container.querySelectorAll('[data-user-data]'): |
| 82 |
q = JSON.parse(item.dataset.userData) |
| 83 |
if q.name is sc_name: |
| 84 |
q.shortcuts = shortcuts |
| 85 |
item.dataset.userData = JSON.stringify(q) |
| 86 |
break |
| 87 |
get_container().dataset.changed = 'true' |
| 88 |
commit_keyboard(create_keyboard_panel.onchange, None) |
| 89 |
|
| 90 |
|
| 91 |
def add_key_widget(): |
| 92 |
uid = unique_id('add-new-shortcut') |
| 93 |
return E.div(style='margin-top: 1ex; margin-bottom: 1ex', |
| 94 |
id=uid, |
| 95 |
E.div(create_button(_('Add a new shortcut'), icon='plus', action=def (evt): |
| 96 |
div = document.getElementById(uid) |
| 97 |
div.firstChild.style.display = 'none' |
| 98 |
div.lastChild.style.display = 'block' |
| 99 |
div.lastChild.querySelector('input').focus() |
| 100 |
)), |
| 101 |
E.div(style='display:none', |
| 102 |
E.input(readonly='readonly', |
| 103 |
value=_('Press the key combination to use as a shortcut'), |
| 104 |
style='width: 90vw', |
| 105 |
onkeydown=def (evt): |
| 106 |
evt.preventDefault() |
| 107 |
evt.stopPropagation() |
| 108 |
if evt.key not in ['Control', 'Meta', 'Alt', 'Shift']: |
| 109 |
key_con = get_container().querySelector('.key-container') |
| 110 |
key_con.appendChild(key_widget(evt)) |
| 111 |
div = document.getElementById(uid) |
| 112 |
div.firstChild.style.display = 'block' |
| 113 |
div.lastChild.style.display = 'none' |
| 114 |
)) |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
def customize_shortcut(sc_name): |
| 119 |
container = get_container() |
| 120 |
container.firstChild.style.display = 'none' |
| 121 |
container.firstChild.nextSibling.style.display = 'none' |
| 122 |
container.lastChild.style.display = 'block' |
| 123 |
close_customize_shortcut.shortcut_being_customized = sc_name |
| 124 |
shortcuts = [] |
| 125 |
for item in container.querySelectorAll('[data-user-data]'): |
| 126 |
q = JSON.parse(item.dataset.userData) |
| 127 |
if q.name is sc_name: |
| 128 |
shortcuts = q.shortcuts |
| 129 |
break |
| 130 |
container = container.lastChild |
| 131 |
clear(container) |
| 132 |
container.dataset.scName = sc_name |
| 133 |
sc = shortcuts_definition()[sc_name] |
| 134 |
container.appendChild(E.h4(sc.short)) |
| 135 |
if sc.long: |
| 136 |
container.appendChild(E.div(sc.long, style='font-style: italic; font-size: smaller; margin-top: 1ex')) |
| 137 |
container.appendChild(E.div(style='margin-top: 1rem', _('Existing shortcuts:'))) |
| 138 |
key_con = container.appendChild(E.table(class_='key-container')) |
| 139 |
for key in shortcuts: |
| 140 |
key_con.appendChild(key_widget(key)) |
| 141 |
container.appendChild(E.div(style='margin-top:1ex;', add_key_widget())) |
| 142 |
container.appendChild(E.div(style='margin-top:1ex; display:flex; justify-content: flex-end', |
| 143 |
create_button(_('OK'), action=close_customize_shortcut.bind(None, True)), |
| 144 |
E.span('\xa0'), |
| 145 |
create_button(_('Cancel'), action=close_customize_shortcut.bind(None, False)), |
| 146 |
)) |
| 147 |
|
| 148 |
|
| 149 |
def run_search(): |
| 150 |
container = get_container() |
| 151 |
query = container.querySelector(f'[name=search-for-sc]').value or '' |
| 152 |
query = query.toLowerCase() |
| 153 |
for item in get_container().querySelectorAll('[data-user-data]'): |
| 154 |
q = item.textContent.toLowerCase() |
| 155 |
matches = not query or q.indexOf(query) > -1 |
| 156 |
item.style.display = 'list-item' if matches else 'none' |
| 157 |
|
| 158 |
|
| 159 |
def create_keyboard_panel(container, apply_func, cancel_func, onchange): |
| 160 |
create_keyboard_panel.onchange = onchange |
| 161 |
container.appendChild(E.div(id=unique_id('keyboard-settings'), style='margin: 1rem')) |
| 162 |
container = container.lastChild |
| 163 |
container.dataset.changed = 'false' |
| 164 |
get_container.id = container.id |
| 165 |
search_button = create_button(_('Search'), icon='search') |
| 166 |
sb = create_search_bar( |
| 167 |
run_search, 'search-for-sc', placeholder=_('Search for shortcut'), button=search_button) |
| 168 |
container.appendChild(E.div( |
| 169 |
style='margin-bottom: 1ex; display: flex; width: 100%; justify-content: space-between', sb, E.span('\xa0\xa0'), search_button |
| 170 |
)) |
| 171 |
container.firstChild.firstChild.style.flexGrow = '100' |
| 172 |
container.appendChild(E.div()) |
| 173 |
container.appendChild(E.div(style='display: none')) |
| 174 |
container = container.firstChild.nextSibling |
| 175 |
sd = get_session_data() |
| 176 |
custom_shortcuts = sd.get('keyboard_shortcuts') |
| 177 |
groups = as_groups(shortcuts_definition()) |
| 178 |
for group_name in Object.keys(groups): |
| 179 |
container.appendChild(E.h3(style='margin-top: 1ex', shortcuts_group_desc()[group_name])) |
| 180 |
group = groups[group_name] |
| 181 |
items = [] |
| 182 |
for sc_name in sorted(Object.keys(group), key=sort_group_key.bind(None, group)): |
| 183 |
sc = group[sc_name] |
| 184 |
items.push(sc_as_item(sc_name, sc, custom_shortcuts[sc_name])) |
| 185 |
container.appendChild(E.div()) |
| 186 |
create_item_list(container.lastChild, items) |
| 187 |
|
| 188 |
container.appendChild(E.div( |
| 189 |
style='margin-top: 1rem', create_button(_('Restore defaults'), action=restore_defaults.bind(None, apply_func)) |
| 190 |
)) |
| 191 |
|
| 192 |
|
| 193 |
develop = create_keyboard_panel |
| 194 |
|
| 195 |
|
| 196 |
def shortcuts_differ(a, b): |
| 197 |
if a.length is not b.length: |
| 198 |
return True |
| 199 |
for x, y in zip(a, b): |
| 200 |
if shortcut_differs(x, y): |
| 201 |
return True |
| 202 |
return False |
| 203 |
|
| 204 |
|
| 205 |
def commit_keyboard(onchange, container): |
| 206 |
sd = get_session_data() |
| 207 |
vals = {} |
| 208 |
for item in get_container().querySelectorAll('[data-user-data]'): |
| 209 |
q = JSON.parse(item.dataset.userData) |
| 210 |
if shortcuts_differ(q.shortcuts, shortcuts_definition()[q.name].shortcuts): |
| 211 |
vals[q.name] = q.shortcuts |
| 212 |
sd.set('keyboard_shortcuts', vals) |
| 213 |
if container is not None: |
| 214 |
create_keyboard_panel.onchange = None |
| 215 |
if get_container().dataset.changed is 'true': |
| 216 |
onchange() |