From 64fdd4d2904849a9652d1be8532793a3e1d26193 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Wed, 20 Oct 2021 13:16:03 +0200 Subject: [PATCH] fix: [settings] Correctly detect value changes for select[multiple] --- webroot/js/settings.js | 9 ++++++++- webroot/js/utils.js | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/webroot/js/settings.js b/webroot/js/settings.js index 5247dc5..622f9a0 100644 --- a/webroot/js/settings.js +++ b/webroot/js/settings.js @@ -52,6 +52,9 @@ $(document).ready(function () { oldValue = oldValue !== undefined ? oldValue : '' } $input.val(oldValue) + if ($input.is('select') && $input.prop('multiple')) { + $input.trigger('change') + } handleSettingValueChange($input) }) @@ -85,7 +88,11 @@ function handleSettingValueChange($input) { if ($input.attr('type') == 'checkbox') { oldValue = oldValue == true } - if (newValue == oldValue || (newValue == '' && oldValue == undefined)) { + let hasChanged = newValue != oldValue + if ($input.is('select') && $input.prop('multiple')) { + hasChanged = !arrayEqual(oldValue, newValue) + } + if (!hasChanged || (newValue == '' && oldValue == undefined)) { restoreWarnings($input) } else { removeWarnings($input) diff --git a/webroot/js/utils.js b/webroot/js/utils.js index 7acdb43..7fa910d 100644 --- a/webroot/js/utils.js +++ b/webroot/js/utils.js @@ -145,4 +145,10 @@ function debounce(func, wait, options) { debounced.flush = flush debounced.pending = pending return debounced +} + +function arrayEqual(array1, array2) { + const array2Sorted = array2.slice().sort(); + return array1.length === array2.length && array1.slice().sort().every((value, index) => value === array2Sorted[index]) + } \ No newline at end of file