From b0a2f557bbc38c4fbceccf52ba37aec8323f9f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Thu, 13 Oct 2022 16:17:47 +0200 Subject: [PATCH] rename atk.debounce to atk.createDebouncedFx and fix wrong usage --- js/src/plugins/conditional-form.plugin.js | 10 +++--- js/src/plugins/js-search.plugin.js | 2 +- js/src/services/panel.service.js | 4 +-- js/src/setup-utils.js | 20 +++++------ .../vue-components/item-search.component.js | 19 +++++----- .../multiline/multiline.component.js | 11 +++--- js/src/vue-components/share/atk-lookup.js | 22 +++++++----- public/js/atk-vue-item-search.js | 20 ++++++----- public/js/atk-vue-item-search.min.js | 2 +- public/js/atk-vue-item-search.min.js.map | 2 +- public/js/atk-vue-multiline.js | 36 +++++++++++-------- public/js/atk-vue-multiline.min.js | 2 +- public/js/atk-vue-multiline.min.js.map | 2 +- public/js/atk-vue-query-builder.js | 23 ++++++------ public/js/atk-vue-query-builder.min.js | 2 +- public/js/atkjs-ui.js | 36 +++++++++---------- public/js/atkjs-ui.min.js | 2 +- public/js/atkjs-ui.min.js.map | 2 +- 18 files changed, 121 insertions(+), 96 deletions(-) diff --git a/js/src/plugins/conditional-form.plugin.js b/js/src/plugins/conditional-form.plugin.js index 828a32037d..60c38549b1 100644 --- a/js/src/plugins/conditional-form.plugin.js +++ b/js/src/plugins/conditional-form.plugin.js @@ -55,15 +55,15 @@ export default class AtkConditionalFormPlugin extends AtkPlugin { } // add change listener to inputs according to selector this.$el.find(':checkbox') - .on('change', this, atk.debounce(this.onInputChange, 100, true)); + .on('change', this, atk.createDebouncedFx(this.onInputChange, 100, true)); this.$el.find(':radio') - .on('change', this, atk.debounce(this.onInputChange, 100, true)); + .on('change', this, atk.createDebouncedFx(this.onInputChange, 100, true)); this.$el.find('input[type="hidden"]') - .on('change', this, atk.debounce(this.onInputChange, 100, true)); + .on('change', this, atk.createDebouncedFx(this.onInputChange, 100, true)); this.$el.find('input') - .on(this.settings.validateEvent, this, atk.debounce(this.onInputChange, 250)); + .on(this.settings.validateEvent, this, atk.createDebouncedFx(this.onInputChange, 250)); this.$el.find('select') - .on('change', this, atk.debounce(this.onInputChange, 100)); + .on('change', this, atk.createDebouncedFx(this.onInputChange, 100)); this.initialize(); } diff --git a/js/src/plugins/js-search.plugin.js b/js/src/plugins/js-search.plugin.js index 325d38696d..66a970ceb4 100644 --- a/js/src/plugins/js-search.plugin.js +++ b/js/src/plugins/js-search.plugin.js @@ -38,7 +38,7 @@ export default class AtkJsSearchPlugin extends AtkPlugin { * Query server on each keystroke after proper timeout. */ onAutoQueryAction() { - this.textInput.on('keyup', atk.debounce((e) => { + this.textInput.on('keyup', atk.createDebouncedFx((e) => { const options = $.extend({}, this.urlArgs, this.settings.urlOptions); if (e.target.value === '' || e.keyCode === 27) { this.doSearch(this.settings.url, null, options, () => { diff --git a/js/src/services/panel.service.js b/js/src/services/panel.service.js index 80be38ab83..6e8323c404 100644 --- a/js/src/services/panel.service.js +++ b/js/src/services/panel.service.js @@ -280,7 +280,7 @@ class PanelService { */ addClickAwayEvent(id) { // clicking anywhere in main tag will close panel. - $('main').on('click.atkPanel', atk.debounce((evt) => { + $('main').on('click.atkPanel', atk.createDebouncedFx((evt) => { this.closePanel(id); }, 250)); } @@ -290,7 +290,7 @@ class PanelService { */ addEscAwayEvent(id) { // pressing esc key will close panel. - $(document).on('keyup.atkPanel', atk.debounce((evt) => { + $(document).on('keyup.atkPanel', atk.createDebouncedFx((evt) => { if (evt.keyCode === 27) { this.closePanel(id); } diff --git a/js/src/setup-utils.js b/js/src/setup-utils.js index 20d8761a77..ab7e23e544 100644 --- a/js/src/setup-utils.js +++ b/js/src/setup-utils.js @@ -39,13 +39,13 @@ atk.eventBus = (function () { }; }()); -atk.debounce = function (func, wait, options) { +atk.createDebouncedFx = function (func, wait, options) { let timerId = null; - let debouncedInner; + let lodashDebouncedFx; function createTimer() { timerId = setInterval(() => { - if (!debouncedInner.pending()) { + if (!lodashDebouncedFx.pending()) { clearInterval(timerId); timerId = null; $.active--; @@ -54,20 +54,20 @@ atk.debounce = function (func, wait, options) { $.active++; } - debouncedInner = lodashDebounce(func, wait, options); + lodashDebouncedFx = lodashDebounce(func, wait, options); - function debounced(...args) { + function debouncedFx(...args) { if (timerId === null) { createTimer(); } - return debouncedInner(...args); + return lodashDebouncedFx(...args); } - debounced.cancel = debouncedInner.cancel; - debounced.flush = debouncedInner.flush; - debounced.pending = debouncedInner.pending; + debouncedFx.cancel = lodashDebouncedFx.cancel; + debouncedFx.flush = lodashDebouncedFx.flush; + debouncedFx.pending = lodashDebouncedFx.pending; - return debounced; + return debouncedFx; }; /* diff --git a/js/src/vue-components/item-search.component.js b/js/src/vue-components/item-search.component.js index 5ab3e4dd32..7739fdbce1 100644 --- a/js/src/vue-components/item-search.component.js +++ b/js/src/vue-components/item-search.component.js @@ -55,15 +55,18 @@ export default { }, methods: { onKeyup: function () { - atk.debounce((e) => { - if (this.query !== this.temp) { - if (this.query === '') { - this.query = null; + if (typeof this.onKeyup.debouncedFx === 'undefined') { + this.onKeyup.debouncedFx = atk.createDebouncedFx((e) => { + if (this.query !== this.temp) { + if (this.query === '') { + this.query = null; + } + this.sendQuery(); + this.temp = this.query; } - this.sendQuery(); - this.temp = this.query; - } - }, this.options.inputTimeOut).call(this); + }, this.options.inputTimeOut); + } + this.onKeyup.debouncedFx.call(this); }, onEscape: function () { if (this.query !== null) { diff --git a/js/src/vue-components/multiline/multiline.component.js b/js/src/vue-components/multiline/multiline.component.js index 31f5001561..4c0cf42258 100644 --- a/js/src/vue-components/multiline/multiline.component.js +++ b/js/src/vue-components/multiline/multiline.component.js @@ -115,10 +115,13 @@ export default { this.clearError(atkmlId, fieldName); this.updateInputValue(); - atk.debounce(() => { - this.fetchExpression(atkmlId); - this.fetchOnChangeAction(fieldName); - }, 300).call(this); + if (typeof this.onUpdate.debouncedFx === 'undefined') { + this.onUpdate.debouncedFx = atk.createDebouncedFx(() => { + this.fetchExpression(atkmlId); + this.fetchOnChangeAction(fieldName); + }, 300); + } + this.onUpdate.debouncedFx.call(this); }, /** * Creates a new row of data and diff --git a/js/src/vue-components/share/atk-lookup.js b/js/src/vue-components/share/atk-lookup.js index a1c9d4be9e..abd3e3237a 100644 --- a/js/src/vue-components/share/atk-lookup.js +++ b/js/src/vue-components/share/atk-lookup.js @@ -60,15 +60,19 @@ export default { if (inputValue) { this.isLoading = true; } - this.temp = inputValue; - atk.debounce(() => { - if (this.query !== this.temp) { - this.query = this.temp; - if (this.query) { - this.fetchItems(this.query); + + if (typeof this.onFiltered.debouncedFx === 'undefined') { + this.onFiltered.debouncedFx = atk.createDebouncedFx(() => { + if (this.query !== this.temp) { + this.query = this.temp; + if (this.query) { + this.fetchItems(this.query); + } } - } - }, 300).call(this); + }, 300); + } + this.temp = inputValue; + this.onFiltered.debouncedFx(this); }, /** * Fetch new data from server. @@ -80,9 +84,9 @@ export default { if (response.success) { this.dropdownProps.options = response.results; } - this.isLoading = false; } catch (e) { console.error(e); + } finally { this.isLoading = false; } }, diff --git a/public/js/atk-vue-item-search.js b/public/js/atk-vue-item-search.js index 66807ff912..545c76477d 100644 --- a/public/js/atk-vue-item-search.js +++ b/public/js/atk-vue-item-search.js @@ -69,16 +69,20 @@ const template = `