summaryrefslogtreecommitdiff
path: root/web/js/search.js
diff options
context:
space:
mode:
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);
+}