Debian Codegraph @

src/pyj/read_book/word_actions.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2018, 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 dom import clear, ensure_id
11 from modals import error_dialog
12 from read_book.globals import ui_operations
13 from widgets import create_button
14
15
16 def lookup(close_panel_func, url):
17 close_panel_func()
18 ui_operations.open_url(url)
19
20
21 def toggle_custom_container(container_id, visible):
22 container = document.getElementById(container_id)
23 if container:
24 c = container.lastChild
25 list_container = c.previousSibling
26 list_container.style.display = 'none' if visible else 'block'
27 c.style.display = 'block' if visible else 'none'
28 return c
29
30
31 def add(container_id):
32 container = document.getElementById(container_id)
33 if not container:
34 return
35 c = container.lastChild
36 name = c.querySelector('input[name="name"]').value
37 url = c.querySelector('input[name="url"]').value
38 if not name or not url:
39 error_dialog(_('Required fields missing'), _(
40 'You must specify both a name and a URL.'))
41 return
42 sd = get_session_data()
43 actions = sd.get('word_actions', [])
44 actions.push({'title': name, 'url': url})
45 sd.set('word_actions', actions)
46 toggle_custom_container(container_id, False)
47 populate_list(container)
48
49
50 def add_custom(container_id):
51 container = toggle_custom_container(container_id, True)
52 if not container:
53 return
54 clear(container)
55 container.appendChild(E.div(style='margin: 1rem',
56 _('Enter the name and URL for a custom lookup source. The URL must contain '
57 ' {0} in it, which will be replaced by the word being looked up. For example: '
58 ' {1}').format('{word}', 'https://google.com/search?q={word}')
59 ))
60 do_add = add.bind(None, container_id)
61 container.appendChild(E.form(E.button(style='display:none', onclick=do_add), E.table(style='margin: 1rem',
62 E.tr(E.td(_('Name:') + '\xa0'), E.td(E.input(type='text', name='name'))),
63 E.tr(E.td('\xa0'), E.td('\xa0')),
64 E.tr(E.td(_('URL:') + '\xa0'), E.td(E.input(type='url', name='url'))),
65 )))
66 container.append(E.div(style='margin: 1rem; display: flex',
67 create_button(_('Add'), 'plus', add.bind(None, container_id), highlight=True),
68 '\xa0',
69 create_button(_('Cancel'), action=toggle_custom_container.bind(None, container_id, False)),
70 ))
71 container.querySelector('input').focus()
72
73
74 def remove(container_id, num):
75 container = document.getElementById(container_id)
76 if not container:
77 return
78 sd = get_session_data()
79 actions = sd.get('word_actions', [])
80 actions.splice(num, 1)
81 sd.set('word_actions', actions)
82 toggle_custom_container(container_id, False)
83 populate_list(container)
84
85
86 def remove_custom(container_id):
87 container = toggle_custom_container(container_id, True)
88 if not container:
89 return
90 clear(container)
91 container.appendChild(E.div(style='margin: 1rem', _('Choose a custom lookup to remove')))
92 items = [create_item(_('Cancel (no removals)'), toggle_custom_container.bind(None, container_id, False))]
93 sd = get_session_data()
94 actions = sd.get('word_actions', [])
95 for i, item in enumerate(actions):
96 items.push(create_item(item.title, remove.bind(None, container_id, i)))
97 container.appendChild(E.div())
98 create_item_list(container.lastChild, items)
99
100
101 def lookup_items(word, close_panel_func, container_id):
102 eword = encodeURIComponent(word)
103 items = []
104
105 def a(title, url):
106 items.push(create_item(title, lookup.bind(word, close_panel_func, url.format(word=eword))))
107
108 custom_actions = get_session_data().get('word_actions', [])
109 has_custom = custom_actions and custom_actions.length
110 if has_custom:
111 for entry in custom_actions:
112 if entry.title and entry.url:
113 a(entry.title, entry.url)
114 a(_('Google dictionary'), 'https://google.com/search?q=define:{word}')
115 a(_('Wordnik'), 'https://www.wordnik.com/words/{word}')
116 a(_('Search the internet'), 'https://google.com/search?q={word}')
117 items.push(create_item(_('Add a custom lookup'), add_custom.bind(None, container_id)))
118 if has_custom:
119 items.push(create_item(_('Remove a custom lookup'), remove_custom.bind(None, container_id)))
120
121 return items
122
123
124 current_data = None
125
126
127 def populate_list(container):
128 word, close_panel_func = current_data
129 list_container = container.lastChild.previousSibling
130 clear(list_container)
131 create_item_list(list_container, lookup_items(word, close_panel_func, ensure_id(container)))
132
133
134 def create_word_actions_panel(container, word, close_panel_func):
135 nonlocal current_data
136 word = str.replace(word, '\u00ad', '')
137 current_data = word, close_panel_func
138 container.appendChild(E.div())
139 container.appendChild(E.div(style='display:none'))
140 populate_list(container)
141
142
143 def develop(container):
144 container.appendChild(E.div())
145 create_word_actions_panel(container, 'develop', def (): pass;)