From 261fd732f8a17186aeaa647b665f0335ef60709f Mon Sep 17 00:00:00 2001 From: Nikunj Ladava Date: Mon, 16 May 2022 12:40:20 +0000 Subject: [PATCH] [IMP] mail: add a node option for custom onchange debounce delay This commit introduces a node option 'keydown_debounce_delay' on the character field and on emoji mixin, so that use can define the custom debounce delay (in milliseconds) instead of fixed 2000 ms for triggering an onchange on the field. Note that this delay will be applied only when 'onchange_on_keydown' node option is also provided. Also, we've utilized the odoo's debounce instead of the one provided by underscore js, for 2 reasons: 1 - odoo's debounce is already well tested (see the file /web/static/tests/core/utils/timing_tests.js) 2 - to take a step forward for reducing external lib dependency task-2821978 closes odoo/odoo#94486 X-original-commit: ab80cdc0809e1347d201f21ac9474e66e9270f94 Related: odoo/enterprise#28822 Signed-off-by: Thibault Delavallee (tde) --- addons/mail/static/src/js/field_char.js | 11 ++++++++--- addons/mail/static/src/js/field_emojis_common.js | 8 ++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/addons/mail/static/src/js/field_char.js b/addons/mail/static/src/js/field_char.js index 7fb99f89e8f..d176eae6e57 100644 --- a/addons/mail/static/src/js/field_char.js +++ b/addons/mail/static/src/js/field_char.js @@ -1,5 +1,6 @@ /** @odoo-module **/ +import { debounce } from "@web/core/utils/timing"; import { FieldChar } from 'web.basic_fields'; FieldChar.include({ @@ -10,12 +11,16 @@ FieldChar.include({ /** * Support a key-based onchange in text field. - * The _triggerOnchange method is debounced to run 2 seconds after typing ends. + * The _triggerOnchange method is debounced to run after given debouce delay + * (or 2 seconds by default) on typing ends. * */ init: function () { this._super.apply(this, arguments); - this._triggerOnchange = _.debounce(this._triggerOnchange, 2000); + if (this.nodeOptions.keydown_debounce_delay === undefined) { + this.nodeOptions.keydown_debounce_delay = 2000; + } + this._triggerOnchange = debounce(this._triggerOnchange, this.nodeOptions.keydown_debounce_delay); }, @@ -44,7 +49,7 @@ FieldChar.include({ /** * Triggers the 'change' event to refresh the value. - * This method is debounced to run 2 seconds after typing ends. + * This method is debounced to run after given debouce delay on typing ends. * (to avoid spamming the server while the user is typing their message) * * @private diff --git a/addons/mail/static/src/js/field_emojis_common.js b/addons/mail/static/src/js/field_emojis_common.js index fae1c3dca1e..b1498c5ae05 100644 --- a/addons/mail/static/src/js/field_emojis_common.js +++ b/addons/mail/static/src/js/field_emojis_common.js @@ -1,5 +1,6 @@ /** @odoo-module **/ +import { debounce } from "@web/core/utils/timing"; import emojis from '@mail/js/emojis'; import MailEmojisMixin from '@mail/js/emojis_mixin'; import core from 'web.core'; @@ -17,7 +18,10 @@ var FieldEmojiCommon = { */ init: function () { this._super.apply(this, arguments); - this._triggerOnchange = _.debounce(this._triggerOnchange, 2000); + if (this.nodeOptions.keydown_debounce_delay === undefined) { + this.nodeOptions.keydown_debounce_delay = 2000; + } + this._triggerOnchange = debounce(this._triggerOnchange, this.nodeOptions.keydown_debounce_delay); this.emojis = emojis; }, @@ -89,7 +93,7 @@ var FieldEmojiCommon = { /** * Triggers the 'change' event to refresh the value. - * This method is debounced to run 2 seconds after typing ends. + * This method is debounced to run after given debouce delay on typing ends. * (to avoid spamming the server while the user is typing their message) * * @private