| 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 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from dom import ensure_id, unique_id, clear, svgicon |
| 9 |
from popups import CompletionPopup |
| 10 |
from session import local_storage |
| 11 |
from utils import uniq |
| 12 |
|
| 13 |
|
| 14 |
class EditWithComplete: |
| 15 |
def __init__(self, name, placeholder=None, tooltip=None, parent=None, input_type='text', onenterkey=None): |
| 16 |
inpt = E.input( |
| 17 |
type=input_type, name=name, title=tooltip or '', placeholder=placeholder or '', autocomplete='off', |
| 18 |
style='width: 0; flex-grow: 1; min-width: 6em', |
| 19 |
) |
| 20 |
self.input_id = ensure_id(inpt) |
| 21 |
self.onenterkey = onenterkey |
| 22 |
self.completion_popup = CompletionPopup(parent=parent, onselect=self.apply_completion) |
| 23 |
inpt.addEventListener('keydown', self.onkeydown) |
| 24 |
inpt.addEventListener('input', self.oninput) |
| 25 |
self.completion_popup.add_associated_widget(inpt) |
| 26 |
if parent: |
| 27 |
parent.appendChild(inpt) |
| 28 |
|
| 29 |
@property |
| 30 |
def text_input(self): |
| 31 |
return document.getElementById(self.input_id) |
| 32 |
|
| 33 |
def apply_completion(self, text): |
| 34 |
if text: |
| 35 |
ti = self.text_input |
| 36 |
ti.value = text |
| 37 |
ti.focus() |
| 38 |
return True |
| 39 |
|
| 40 |
def onkeydown(self, event): |
| 41 |
if self.completion_popup.is_visible and self.completion_popup.handle_keydown(event): |
| 42 |
event.preventDefault(), event.stopPropagation() |
| 43 |
return |
| 44 |
if event.key is 'Enter': |
| 45 |
if self.completion_popup.is_visible: |
| 46 |
if self.apply_completion(self.completion_popup.current_text): |
| 47 |
self.completion_popup.hide() |
| 48 |
event.preventDefault(), event.stopPropagation() |
| 49 |
return |
| 50 |
if self.onenterkey: |
| 51 |
event.preventDefault(), event.stopPropagation() |
| 52 |
self.onenterkey() |
| 53 |
elif event.key is 'Tab': |
| 54 |
if self.completion_popup.is_visible: |
| 55 |
if self.apply_completion(self.completion_popup.current_text): |
| 56 |
self.completion_popup.hide() |
| 57 |
else: |
| 58 |
self.completion_popup.move_highlight() |
| 59 |
event.preventDefault(), event.stopPropagation() |
| 60 |
elif event.key is 'ArrowDown': |
| 61 |
ti = self.text_input |
| 62 |
if not ti.value: |
| 63 |
self.completion_popup.set_query('') |
| 64 |
self.completion_popup.popup(ti) |
| 65 |
event.preventDefault(), event.stopPropagation() |
| 66 |
|
| 67 |
def oninput(self, event): |
| 68 |
ti = self.text_input |
| 69 |
self.completion_popup.set_query(ti.value or '') |
| 70 |
self.completion_popup.popup(ti) |
| 71 |
|
| 72 |
def hide_completion_popup(self): |
| 73 |
self.completion_popup.hide() |
| 74 |
|
| 75 |
def add_associated_widget(self, widget_or_id): |
| 76 |
self.completion_popup.add_associated_widget(widget_or_id) |
| 77 |
|
| 78 |
def set_all_items(self, items): |
| 79 |
self.completion_popup.set_all_items(items) |
| 80 |
|
| 81 |
|
| 82 |
def create_search_bar(action, name, tooltip=None, placeholder=None, button=None, history_size=100, associated_widgets=None): |
| 83 |
parent = E.div( |
| 84 |
style='display:flex; gap: 0.65rem; align-items: center; justify-content: space-between', |
| 85 |
data_component='search_box') |
| 86 |
ewc = EditWithComplete( |
| 87 |
name, parent=parent, tooltip=tooltip or placeholder, placeholder=placeholder, input_type='search') |
| 88 |
parent.lastChild.style.width = '100%' |
| 89 |
history_name = 'search-bar-history-' + name |
| 90 |
|
| 91 |
def update_completion_items(): |
| 92 |
items = local_storage().get(history_name) |
| 93 |
if items?.length: |
| 94 |
ewc.set_all_items(items) |
| 95 |
update_completion_items() |
| 96 |
|
| 97 |
def trigger(): |
| 98 |
text = ewc.text_input.value |
| 99 |
ewc.hide_completion_popup() |
| 100 |
action(text) |
| 101 |
if text and text.strip(): |
| 102 |
items = local_storage().get(history_name) or [] |
| 103 |
idx = items.indexOf(text) |
| 104 |
if idx > -1: |
| 105 |
items.splice(idx, 1) |
| 106 |
items.unshift(text) |
| 107 |
local_storage().set(history_name, uniq(items[:history_size])) |
| 108 |
update_completion_items() |
| 109 |
ewc.onenterkey = trigger |
| 110 |
inp = parent.querySelector('input') |
| 111 |
inp.addEventListener('search', trigger) |
| 112 |
if button is 'icon-button': |
| 113 |
button = E.a( |
| 114 |
svgicon('search'), |
| 115 |
class_='calibre-icon-button', |
| 116 |
style='margin-left: auto', |
| 117 |
href='javascript:void(0)', |
| 118 |
title=_('Search'), |
| 119 |
onmousedown=def (evt): evt.preventDefault();, |
| 120 |
) |
| 121 |
parent.appendChild(button) |
| 122 |
if button: |
| 123 |
ewc.add_associated_widget(button) |
| 124 |
button.addEventListener('click', trigger) |
| 125 |
if associated_widgets is not None: |
| 126 |
for w in associated_widgets: |
| 127 |
ewc.add_associated_widget(w) |
| 128 |
return parent |
| 129 |
|
| 130 |
|
| 131 |
def create_simple_input_with_history_native(name, tooltip=None, placeholder=None, history_size=100, input_type='text'): |
| 132 |
# cannot use in standalone viewer because of: https://bugreports.qt.io/browse/QTBUG-54433 |
| 133 |
# fixed in Qt 6.4.0 |
| 134 |
dl_id = unique_id() |
| 135 |
dl = document.createElement('datalist') |
| 136 |
dl.id = dl_id |
| 137 |
history_name = f'simple_input_history_{name}' |
| 138 |
|
| 139 |
def update_completion_items(x): |
| 140 |
items = local_storage().get(history_name) |
| 141 |
dl = x or document.getElementById(dl_id) |
| 142 |
if dl: |
| 143 |
clear(dl) |
| 144 |
if items?.length: |
| 145 |
for item in items: |
| 146 |
dl.appendChild(E.option(value=item)) |
| 147 |
|
| 148 |
def save_completion_items(): |
| 149 |
text = document.getElementById(dl_id).nextSibling.value |
| 150 |
if text and text.strip(): |
| 151 |
items = local_storage().get(history_name) or [] |
| 152 |
idx = items.indexOf(text) |
| 153 |
if idx > -1: |
| 154 |
items.splice(idx, 1) |
| 155 |
items.unshift(text) |
| 156 |
local_storage().set(history_name, uniq(items[:history_size])) |
| 157 |
update_completion_items() |
| 158 |
|
| 159 |
update_completion_items(dl) |
| 160 |
ans = E.span( |
| 161 |
data_calibre_history_input='1', |
| 162 |
dl, |
| 163 |
E.input(type=input_type, name=name, list=dl_id, title=tooltip or '', placeholder=placeholder or ''), |
| 164 |
) |
| 165 |
ans.addEventListener('save_history', save_completion_items) |
| 166 |
return ans |
| 167 |
|
| 168 |
|
| 169 |
def create_simple_input_with_history(name, tooltip=None, placeholder=None, history_size=100, input_type='text'): |
| 170 |
# to use simply add the returned element to the DOM and when you want to |
| 171 |
# save a new completion item, use dispatchEvent to send a 'save_history' |
| 172 |
# event to it. |
| 173 |
parent = E.span(data_calibre_history_input='1') |
| 174 |
ewc = EditWithComplete(name, parent=parent, tooltip=tooltip, placeholder=placeholder, input_type=input_type) |
| 175 |
history_name = f'simple_input_history_{name}' |
| 176 |
|
| 177 |
def update_completion_items(): |
| 178 |
items = local_storage().get(history_name) |
| 179 |
if items?.length: |
| 180 |
ewc.set_all_items(items) |
| 181 |
update_completion_items() |
| 182 |
|
| 183 |
def save_completion_items(): |
| 184 |
text = ewc.text_input.value |
| 185 |
if text and text.strip(): |
| 186 |
items = local_storage().get(history_name) or [] |
| 187 |
idx = items.indexOf(text) |
| 188 |
if idx > -1: |
| 189 |
items.splice(idx, 1) |
| 190 |
items.unshift(text) |
| 191 |
local_storage().set(history_name, uniq(items[:history_size])) |
| 192 |
update_completion_items() |
| 193 |
|
| 194 |
parent.addEventListener('save_history', save_completion_items) |
| 195 |
return parent |
| 196 |
# }}} |
| 197 |
|
| 198 |
|
| 199 |
def main(container): |
| 200 |
container.appendChild(create_search_bar(print, 'test-search-bar', placeholder='Testing search bar')) |
| 201 |
container.firstChild.lastChild.focus() |
| 202 |
# ewc = EditWithComplete(parent=container, placeholder='Testing edit with complete') |
| 203 |
# ewc.set_all_items('a a1 a11 a12 a13 b b1 b2 b3'.split(' ')) |
| 204 |
# ewc.text_input.focus() |