From 34a357d519a6ff37516c963181af912f776ea80d Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 11 May 2019 12:30:19 +0200 Subject: [PATCH] [2.2.x] Fixed #30459 -- Delegated hide/show JS toggle to parent div. Co-authored-by: Carlton Gibson Backport of e286987a27271c8ee7eb6e4d4332b563c4e6094b from master --- .../contrib/admin/static/admin/js/collapse.js | 31 ++++++++++--------- .../admin/static/admin/js/collapse.min.js | 5 +-- docs/releases/2.2.2.txt | 3 +- tests/admin_inlines/admin.py | 18 +++++++++-- tests/admin_inlines/tests.py | 18 +++++++++++ 5 files changed, 56 insertions(+), 19 deletions(-) diff --git a/django/contrib/admin/static/admin/js/collapse.js b/django/contrib/admin/static/admin/js/collapse.js index 3123878e10..20e7030e7e 100644 --- a/django/contrib/admin/static/admin/js/collapse.js +++ b/django/contrib/admin/static/admin/js/collapse.js @@ -30,23 +30,26 @@ h2.appendChild(document.createTextNode(')')); } } - // Add toggle to anchor tag - var toggles = document.querySelectorAll('fieldset.collapse a.collapse-toggle'); + // Add toggle to hide/show anchor tag var toggleFunc = function(ev) { - ev.preventDefault(); - var fieldset = closestElem(this, 'fieldset'); - if (fieldset.classList.contains('collapsed')) { - // Show - this.textContent = gettext('Hide'); - fieldset.classList.remove('collapsed'); - } else { - // Hide - this.textContent = gettext('Show'); - fieldset.classList.add('collapsed'); + if (ev.target.matches('.collapse-toggle')) { + ev.preventDefault(); + ev.stopPropagation(); + var fieldset = closestElem(ev.target, 'fieldset'); + if (fieldset.classList.contains('collapsed')) { + // Show + ev.target.textContent = gettext('Hide'); + fieldset.classList.remove('collapsed'); + } else { + // Hide + ev.target.textContent = gettext('Show'); + fieldset.classList.add('collapsed'); + } } }; - for (i = 0; i < toggles.length; i++) { - toggles[i].addEventListener('click', toggleFunc); + var inlineDivs = document.querySelectorAll('fieldset.module'); + for (i = 0; i < inlineDivs.length; i++) { + inlineDivs[i].addEventListener('click', toggleFunc); } }); })(); diff --git a/django/contrib/admin/static/admin/js/collapse.min.js b/django/contrib/admin/static/admin/js/collapse.min.js index 6f59280e5a..9e16a21eb9 100644 --- a/django/contrib/admin/static/admin/js/collapse.min.js +++ b/django/contrib/admin/static/admin/js/collapse.min.js @@ -1,2 +1,3 @@ -(function(){var e=function(a,b){return a.nodeName===b.toUpperCase()?a:"BODY"===a.parentNode.nodeName?null:a.parentNode&&e(a.parentNode,b)};window.addEventListener("load",function(){for(var a=document.querySelectorAll("fieldset.collapse"),b=0;b