summaryrefslogtreecommitdiff
path: root/web/js/search.js
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-29 20:19:34 +0300
committerPaul Buetow <paul@buetow.org>2026-04-29 20:19:34 +0300
commit4a70c0ea083306cb79d0fa325d331ffa4333154a (patch)
tree84df7e2f781974ef45ac8c34f1a59e80fdd5c41d /web/js/search.js
parentcf30414c2a0696cc75c615f4da66ea85d0522c42 (diff)
feat: create web PWA frontend
Diffstat (limited to 'web/js/search.js')
-rw-r--r--web/js/search.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/web/js/search.js b/web/js/search.js
new file mode 100644
index 0000000..dfb698a
--- /dev/null
+++ b/web/js/search.js
@@ -0,0 +1,35 @@
+let _onChange = () => {};
+let _debounceTimer = null;
+
+export function initSearch({ onChange, input, clearBtn }) {
+ _onChange = onChange;
+ if (!input) return;
+ input.addEventListener('keydown', (e) => {
+ if (e.key === 'Escape') {
+ input.value = '';
+ input.blur();
+ trigger('');
+ }
+ });
+ input.addEventListener('input', () => {
+ clearTimeout(_debounceTimer);
+ _debounceTimer = setTimeout(() => trigger(input.value.trim()), 300);
+ });
+ if (clearBtn) {
+ clearBtn.addEventListener('click', () => {
+ input.value = '';
+ input.focus();
+ trigger('');
+ });
+ }
+}
+
+export function focusSearch() {
+ const input = document.getElementById('search-input');
+ if (input) input.focus();
+}
+
+export function trigger(query) {
+ clearTimeout(_debounceTimer);
+ _onChange(query);
+}