Debian Codegraph @

src/pyj/popups.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import hash_literals, bound_methods
4
5 from elementmaker import E
6
7 from book_list.theme import get_color
8 from dom import set_css, ensure_id, clear, build_rule, add_extra_css
9
10 MODAL_Z_INDEX = 1000
11 POPUP_Z_INDEX = MODAL_Z_INDEX + 1
12 popup_count = 0
13
14 shown_popups = set()
15 associated_widgets = {}
16
17
18 def element_contains_click_event(element, event):
19 r = element.getBoundingClientRect()
20 return r.left <= event.clientX <= r.right and r.top <= event.clientY <= r.bottom
21
22
23 def click_in_popup(event):
24 for popup_id in shown_popups:
25 popup = document.getElementById(popup_id)
26 if popup and element_contains_click_event(popup, event):
27 return True
28 w = associated_widgets[popup_id]
29 if w and w.length:
30 for wid in w:
31 widget = document.getElementById(wid)
32 if widget and element_contains_click_event(widget, event):
33 return True
34 return False
35
36
37 def filter_clicks(event):
38 if shown_popups.length:
39 if not click_in_popup(event):
40 for popup_id in shown_popups:
41 hide_popup(popup_id)
42 shown_popups.clear()
43 event.stopPropagation(), event.preventDefault()
44
45
46 def install_event_filters():
47 window.addEventListener('click', filter_clicks, True)
48
49
50 def create_popup(parent, idprefix):
51 nonlocal popup_count
52 popup_count += 1
53 pid = (idprefix or 'popup') + '-' + popup_count
54 # Position has to be fixed so that setting style.top/style.bottom works in
55 # viewport coordinates
56 div = E.div(id=pid, style='display: none; position: fixed; z-index: {}'.format(POPUP_Z_INDEX))
57 parent = parent or document.body
58 parent.appendChild(div)
59 return div
60
61
62 def show_popup(popup_id, associated_widget_ids=None):
63 elem = document.getElementById(popup_id)
64 elem.style.display = 'block'
65 shown_popups.add(popup_id)
66 associated_widgets[popup_id] = associated_widget_ids
67
68
69 def hide_popup(popup_id):
70 elem = document.getElementById(popup_id)
71 if elem:
72 elem.style.display = 'none'
73 v'delete associated_widgets[popup_id]'
74
75
76 class CompletionPopup:
77 CLASS = 'popup-completion-items'
78 CURRENT_ITEM_CLASS = 'popup-completion-current-item'
79
80 def __init__(self, parent=None, max_items=25, onselect=None):
81 self.max_items = max_items
82 c = create_popup(parent)
83 set_css(c, user_select='none')
84 self.container_id = c.getAttribute('id')
85 self.onselect = onselect
86 self.items = []
87 self.matches = []
88 c.appendChild(E.div(class_=self.CLASS))
89 self.associated_widget_ids = set()
90 self.current_query, self.is_upwards = '', False
91 self.applied_query = self.current_query
92
93 @property
94 def container(self):
95 return document.getElementById(self.container_id)
96
97 @property
98 def is_visible(self):
99 return self.container.style.display is not 'none'
100
101 def set_all_items(self, items):
102 self.items = list(items)
103 self.matches = []
104 self.applied_query = None
105
106 def add_associated_widget(self, widget_or_id):
107 if jstype(widget_or_id) is not 'string':
108 widget_or_id = ensure_id(widget_or_id)
109 self.associated_widget_ids.add(widget_or_id)
110
111 def popup(self, widget):
112 if not self.is_visible:
113 if self.applied_query is not self.current_query:
114 self._apply_query()
115 if self.matches.length:
116 self.show_at_widget(widget)
117
118 def show_at_widget(self, w):
119 br = w.getBoundingClientRect()
120 if br.top > window.innerHeight - br.bottom:
121 y, upwards = br.top, True
122 else:
123 y, upwards = br.bottom, False
124 self._show_at(br.left, y, br.width, upwards)
125
126 def set_query(self, query):
127 self.current_query = query
128 if self.is_visible and self.applied_query is not self.current_query:
129 self._apply_query()
130 if not self.matches.length:
131 self.hide()
132
133 def hide(self):
134 self.container.style.display = 'none'
135 c = self.current_item
136 if c:
137 c.classList.remove(self.CURRENT_ITEM_CLASS)
138
139 def handle_keydown(self, event):
140 key = event.key
141 if key is 'Escape' or key is 'Esc':
142 self.hide()
143 return True
144 if key is 'ArrowUp':
145 self.move_highlight(True)
146 return True
147 if key is 'ArrowDown':
148 self.move_highlight(False)
149 return True
150 return False
151
152 @property
153 def current_item(self):
154 c = self.container
155 return c.querySelector('div.{} > div.{}'.format(self.CLASS, self.CURRENT_ITEM_CLASS))
156
157 @property
158 def current_text(self):
159 return self.current_item?.textContent
160
161 def move_highlight(self, up=None):
162 if up is None:
163 up = self.is_upwards
164 ans = None
165 div = self.current_item
166 if div:
167 div.classList.remove(self.CURRENT_ITEM_CLASS)
168 ans = div.previousSibling if up else div.nextSibling
169 if not ans:
170 c = self.container.firstChild
171 ans = c.lastChild if up else c.firstChild
172 if ans:
173 ans.classList.add(self.CURRENT_ITEM_CLASS)
174
175 def _show_at(self, x, y, width, upwards):
176 self.is_upwards = upwards
177 c = self.container
178 cs = c.style
179 cs.left = x + 'px'
180 cs.top = 'auto' if upwards else y + 'px'
181 cs.bottom = (window.innerHeight - y) + 'px' if upwards else 'auto'
182 cs.width = width + 'px'
183 cs.maxHeight = ((y if upwards else window.innerHeight - y) - 10) + 'px'
184 show_popup(self.container_id, self.associated_widget_ids)
185
186 def _apply_query(self):
187 q = self.current_query.toLowerCase()
188 self.matches.clear()
189 self.applied_query = self.current_query
190 if not q:
191 self.matches = list(self.items[:self.max_items + 1])
192 else:
193 i = 0
194 while self.matches.length < self.max_items and i < self.items.length:
195 if self.items[i].toLowerCase().startswith(q):
196 self.matches.push(self.items[i])
197 i += 1
198 self._render_matches()
199
200 def _render_matches(self):
201 c = self.container
202 clear(c.firstChild)
203 items = self.matches
204 if self.is_upwards:
205 items = reversed(items)
206 for m in items:
207 c.firstChild.appendChild(E.div(m, onmouseenter=self.onmouseenter, onclick=self.onclick))
208
209 def onmouseenter(self, event):
210 div = self.current_item
211 if div:
212 div.classList.remove(self.CURRENT_ITEM_CLASS)
213 event.currentTarget.classList.add(self.CURRENT_ITEM_CLASS)
214
215 def onclick(self, event):
216 self.onmouseenter(event)
217 try:
218 if self.onselect:
219 self.onselect(self.current_text)
220 finally:
221 self.hide()
222
223 add_extra_css(def ():
224 sel = 'div.' + CompletionPopup.CLASS
225 style = build_rule(
226 sel, overflow='hidden', text_align='left',
227 background_color=get_color('window-background'), border='solid 1px ' + get_color('window-foreground')
228 )
229 sel += ' > div'
230 style += build_rule(sel, cursor='pointer', padding='1ex 1rem', white_space='nowrap', text_overflow='ellipsis', overflow='hidden')
231 sel += '.' + CompletionPopup.CURRENT_ITEM_CLASS
232 style += build_rule(sel, color=get_color('list-hover-foreground'), background_color=get_color('list-hover-background'))
233 return style
234 )