From 0eb7bcb34976c32bbe7e3d33da55f54153dcefbf Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 27 Jun 2023 16:23:56 +0300 Subject: [PATCH 1/2] feat: if select has display:none save style --- assets/javascripts/searchable_selectbox.js | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/assets/javascripts/searchable_selectbox.js b/assets/javascripts/searchable_selectbox.js index dedb790..3839580 100644 --- a/assets/javascripts/searchable_selectbox.js +++ b/assets/javascripts/searchable_selectbox.js @@ -34,6 +34,8 @@ $(function() { initAssignToMeLink(); + observeHiidenSelect(); + // Fix Select2 search broken inside jQuery UI modal Dialog( https://github.com/select2/select2/issues/1246 ) if ($.ui && $.ui.dialog && $.ui.dialog.prototype._allowInteraction) { var ui_dialog_interaction = $.ui.dialog.prototype._allowInteraction; @@ -111,4 +113,26 @@ function retriggerChangeIfNativeEventExists(element) { if (element.data('use-add-change-event-listener') && typeof Rails != 'undefined') { Rails.fire(element[0], 'change') } -} \ No newline at end of file +} + +function observeHiidenSelect() { + const $targetNode = $('select.select2-hidden-accessible'); + const config = { attributes: true, childList: false, subtree: false }; + + const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.attributeName === 'style'){ + const targetDisplayValue = window.getComputedStyle(mutation.target).display; + $(mutation.target).next('span.select2').css({display: targetDisplayValue === 'block' ? 'inline-block' : targetDisplayValue}); + } + }); + }); + + $targetNode.each(function(_, $target) { + const targetDisplayValue = window.getComputedStyle($target).display; + if (targetDisplayValue === 'none') { + $($target).next('span.select2').css({display: targetDisplayValue}); + observer.observe($target, config); + } + }); +} From 365b884061c881c688d8fb4a209430975b35da3b Mon Sep 17 00:00:00 2001 From: "v.polyvyanov" Date: Thu, 29 Jun 2023 17:44:42 +0300 Subject: [PATCH 2/2] fix: bug with all hiddent selects --- assets/javascripts/searchable_selectbox.js | 33 +++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/assets/javascripts/searchable_selectbox.js b/assets/javascripts/searchable_selectbox.js index 3839580..7302de9 100644 --- a/assets/javascripts/searchable_selectbox.js +++ b/assets/javascripts/searchable_selectbox.js @@ -3,6 +3,7 @@ $(document).ajaxSuccess(function() { replaceSelect2(); initAssignToMeLink(); + restartObserver(); }); // Replace with select2 when the HTTP status of data-remote request is a success. @@ -10,6 +11,7 @@ $(document).ajaxSuccess(function() { $(document).on('ajax:success', function() { replaceSelect2(); initAssignToMeLink(); + restartObserver(); }); // Fix a problem with focus not working in Redmine 5.0 or later. @@ -31,10 +33,8 @@ EventTarget.prototype.addEventListener = function(type, listener, options) { $(function() { // Replace with select2 when loading page. replaceSelect2(); - initAssignToMeLink(); - - observeHiidenSelect(); + observeHidenSelect(); // Fix Select2 search broken inside jQuery UI modal Dialog( https://github.com/select2/select2/issues/1246 ) if ($.ui && $.ui.dialog && $.ui.dialog.prototype._allowInteraction) { @@ -115,24 +115,29 @@ function retriggerChangeIfNativeEventExists(element) { } } -function observeHiidenSelect() { +const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.attributeName === 'style'){ + const targetDisplayValue = window.getComputedStyle(mutation.target).display; + $(mutation.target).next('span.select2').css({display: targetDisplayValue === 'block' ? 'inline-block' : targetDisplayValue}); + } + }); +}); + +function observeHidenSelect() { const $targetNode = $('select.select2-hidden-accessible'); const config = { attributes: true, childList: false, subtree: false }; - const observer = new MutationObserver(function(mutations) { - mutations.forEach(function(mutation) { - if (mutation.attributeName === 'style'){ - const targetDisplayValue = window.getComputedStyle(mutation.target).display; - $(mutation.target).next('span.select2').css({display: targetDisplayValue === 'block' ? 'inline-block' : targetDisplayValue}); - } - }); - }); - $targetNode.each(function(_, $target) { - const targetDisplayValue = window.getComputedStyle($target).display; + const targetDisplayValue = $target.style.display; if (targetDisplayValue === 'none') { $($target).next('span.select2').css({display: targetDisplayValue}); observer.observe($target, config); } }); } + +function restartObserver() { + observer.disconnect(); + observeHidenSelect(); +}