[2.2.x] Fixed #30459 -- Delegated hide/show JS toggle to parent div.

Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>

Backport of e286987a27 from master
This commit is contained in:
Claude Paroz 2019-05-11 12:30:19 +02:00 committed by Mariusz Felisiak
parent 08c8838727
commit 34a357d519
5 changed files with 56 additions and 19 deletions

View File

@ -30,23 +30,26 @@
h2.appendChild(document.createTextNode(')')); h2.appendChild(document.createTextNode(')'));
} }
} }
// Add toggle to anchor tag // Add toggle to hide/show anchor tag
var toggles = document.querySelectorAll('fieldset.collapse a.collapse-toggle');
var toggleFunc = function(ev) { var toggleFunc = function(ev) {
ev.preventDefault(); if (ev.target.matches('.collapse-toggle')) {
var fieldset = closestElem(this, 'fieldset'); ev.preventDefault();
if (fieldset.classList.contains('collapsed')) { ev.stopPropagation();
// Show var fieldset = closestElem(ev.target, 'fieldset');
this.textContent = gettext('Hide'); if (fieldset.classList.contains('collapsed')) {
fieldset.classList.remove('collapsed'); // Show
} else { ev.target.textContent = gettext('Hide');
// Hide fieldset.classList.remove('collapsed');
this.textContent = gettext('Show'); } else {
fieldset.classList.add('collapsed'); // Hide
ev.target.textContent = gettext('Show');
fieldset.classList.add('collapsed');
}
} }
}; };
for (i = 0; i < toggles.length; i++) { var inlineDivs = document.querySelectorAll('fieldset.module');
toggles[i].addEventListener('click', toggleFunc); for (i = 0; i < inlineDivs.length; i++) {
inlineDivs[i].addEventListener('click', toggleFunc);
} }
}); });
})(); })();

View File

@ -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<a.length;b++){var c=a[b];if(0===c.querySelectorAll("div.errors").length){c.classList.add("collapsed");c=c.querySelector("h2");var d=document.createElement("a");d.setAttribute("id","fieldsetcollapser"+b);d.setAttribute("class","collapse-toggle");d.setAttribute("href", (function(){var e=function(b,a){return b.nodeName===a.toUpperCase()?b:"BODY"===b.parentNode.nodeName?null:b.parentNode&&e(b.parentNode,a)};window.addEventListener("load",function(){for(var b=document.querySelectorAll("fieldset.collapse"),a=0;a<b.length;a++){var c=b[a];if(0===c.querySelectorAll("div.errors").length){c.classList.add("collapsed");c=c.querySelector("h2");var d=document.createElement("a");d.setAttribute("id","fieldsetcollapser"+a);d.setAttribute("class","collapse-toggle");d.setAttribute("href",
"#");d.textContent=gettext("Show");c.appendChild(document.createTextNode(" ("));c.appendChild(d);c.appendChild(document.createTextNode(")"))}}a=document.querySelectorAll("fieldset.collapse a.collapse-toggle");c=function(a){a.preventDefault();a=e(this,"fieldset");a.classList.contains("collapsed")?(this.textContent=gettext("Hide"),a.classList.remove("collapsed")):(this.textContent=gettext("Show"),a.classList.add("collapsed"))};for(b=0;b<a.length;b++)a[b].addEventListener("click",c)})})(); "#");d.textContent=gettext("Show");c.appendChild(document.createTextNode(" ("));c.appendChild(d);c.appendChild(document.createTextNode(")"))}}b=function(a){if(a.target.matches(".collapse-toggle")){a.preventDefault();a.stopPropagation();var b=e(a.target,"fieldset");b.classList.contains("collapsed")?(a.target.textContent=gettext("Hide"),b.classList.remove("collapsed")):(a.target.textContent=gettext("Show"),b.classList.add("collapsed"))}};c=document.querySelectorAll("fieldset.module");for(a=0;a<c.length;a++)c[a].addEventListener("click",
b)})})();

View File

@ -9,4 +9,5 @@ Django 2.2.2 fixes several bugs in 2.2.1.
Bugfixes Bugfixes
======== ========
* ... * Fixed a regression in Django 2.2 that stopped Show/Hide toggles working on
dynamically added admin inlines (:ticket:`30459`).

View File

@ -3,13 +3,14 @@ from django.contrib import admin
from django.db import models from django.db import models
from .models import ( from .models import (
Author, BinaryTree, CapoFamiglia, Chapter, ChildModel1, ChildModel2, Author, BinaryTree, CapoFamiglia, Chapter, Child, ChildModel1, ChildModel2,
Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, Holder, Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, Holder,
Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked, Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked,
Inner4Tabular, NonAutoPKBook, NonAutoPKBookChild, Novel, Inner4Tabular, NonAutoPKBook, NonAutoPKBookChild, Novel,
NovelReadonlyChapter, ParentModelWithCustomPk, Poll, Profile, NovelReadonlyChapter, ParentModelWithCustomPk, Poll, Profile,
ProfileCollection, Question, ReadOnlyInline, ShoppingWeakness, Sighting, ProfileCollection, Question, ReadOnlyInline, ShoppingWeakness, Sighting,
SomeChildModel, SomeParentModel, SottoCapo, Title, TitleCollection, SomeChildModel, SomeParentModel, SottoCapo, Teacher, Title,
TitleCollection,
) )
site = admin.AdminSite(name="admin") site = admin.AdminSite(name="admin")
@ -235,6 +236,18 @@ class SomeChildModelInline(admin.TabularInline):
readonly_fields = ('readonly_field',) readonly_fields = ('readonly_field',)
class StudentInline(admin.StackedInline):
model = Child
extra = 1
fieldsets = [
('Name', {'fields': ('name',), 'classes': ('collapse',)}),
]
class TeacherAdmin(admin.ModelAdmin):
inlines = [StudentInline]
site.register(TitleCollection, inlines=[TitleInline]) site.register(TitleCollection, inlines=[TitleInline])
# Test bug #12561 and #12778 # Test bug #12561 and #12778
# only ModelAdmin media # only ModelAdmin media
@ -257,3 +270,4 @@ site.register(BinaryTree, inlines=[BinaryTreeAdmin])
site.register(ExtraTerrestrial, inlines=[SightingInline]) site.register(ExtraTerrestrial, inlines=[SightingInline])
site.register(SomeParentModel, inlines=[SomeChildModelInline]) site.register(SomeParentModel, inlines=[SomeChildModelInline])
site.register([Question, Inner4Stacked, Inner4Tabular]) site.register([Question, Inner4Stacked, Inner4Tabular])
site.register(Teacher, TeacherAdmin)

View File

@ -1024,3 +1024,21 @@ class SeleniumTests(AdminSeleniumTestCase):
self.wait_until_visible(field_name) self.wait_until_visible(field_name)
hide_links[hide_index].click() hide_links[hide_index].click()
self.wait_until_invisible(field_name) self.wait_until_invisible(field_name)
def test_added_stacked_inline_with_collapsed_fields(self):
self.admin_login(username='super', password='secret')
self.selenium.get(self.live_server_url + reverse('admin:admin_inlines_teacher_add'))
self.selenium.find_element_by_link_text('Add another Child').click()
test_fields = ['#id_child_set-0-name', '#id_child_set-1-name']
show_links = self.selenium.find_elements_by_link_text('SHOW')
self.assertEqual(len(show_links), 2)
for show_index, field_name in enumerate(test_fields, 0):
self.wait_until_invisible(field_name)
show_links[show_index].click()
self.wait_until_visible(field_name)
hide_links = self.selenium.find_elements_by_link_text('HIDE')
self.assertEqual(len(hide_links), 2)
for hide_index, field_name in enumerate(test_fields, 0):
self.wait_until_visible(field_name)
hide_links[hide_index].click()
self.wait_until_invisible(field_name)