From 9628560d649552deb034b2dfd30d8ca3a765e671 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 4 Oct 2021 14:39:53 +0200 Subject: [PATCH] Improve entry search with a debouncer Signed-off-by: Yohann D'ANELLO --- apps/activity/templates/activity/activity_entry.html | 7 ++++++- note_kfet/static/js/base.js | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/apps/activity/templates/activity/activity_entry.html b/apps/activity/templates/activity/activity_entry.html index 0a0e8015..0286bdb7 100644 --- a/apps/activity/templates/activity/activity_entry.html +++ b/apps/activity/templates/activity/activity_entry.html @@ -63,7 +63,12 @@ SPDX-License-Identifier: GPL-3.0-or-later refreshBalance(); } - alias_obj.keyup(reloadTable); + alias_obj.keyup(function(event) { + let code = event.originalEvent.keyCode + if (65 <= code <= 122 || code === 13) { + debounce(reloadTable)() + } + }); $(document).ready(init); diff --git a/note_kfet/static/js/base.js b/note_kfet/static/js/base.js index 39b2216d..4b1b7c78 100644 --- a/note_kfet/static/js/base.js +++ b/note_kfet/static/js/base.js @@ -381,11 +381,11 @@ function de_validate (id, validated, resourcetype) { * @param callback Function to call * @param wait Debounced milliseconds */ -function debounce (callback, wait) { - let timeout +let debounce_timeout +function debounce (callback, wait=500) { return (...args) => { const context = this - clearTimeout(timeout) - timeout = setTimeout(() => callback.apply(context, args), wait) + clearTimeout(debounce_timeout) + debounce_timeout = setTimeout(() => callback.apply(context, args), wait) } }