1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #30459 -- Delegated hide/show JS toggle to parent div.

Co-authored-by: Carlton Gibson <carlton.gibson@noumenal.es>
This commit is contained in:
Claude Paroz 2019-05-11 12:30:19 +02:00 committed by Mariusz Felisiak
parent 59ffafa1d2
commit e286987a27
5 changed files with 56 additions and 19 deletions

View File

@ -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);
}
});
})();

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",
"#");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)})})();
(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(")"))}}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
========
* ...
* 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 .models import (
Author, BinaryTree, CapoFamiglia, Chapter, ChildModel1, ChildModel2,
Author, BinaryTree, CapoFamiglia, Chapter, Child, ChildModel1, ChildModel2,
Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, Holder,
Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked,
Inner4Tabular, NonAutoPKBook, NonAutoPKBookChild, Novel,
NovelReadonlyChapter, ParentModelWithCustomPk, Poll, Profile,
ProfileCollection, Question, ReadOnlyInline, ShoppingWeakness, Sighting,
SomeChildModel, SomeParentModel, SottoCapo, Title, TitleCollection,
SomeChildModel, SomeParentModel, SottoCapo, Teacher, Title,
TitleCollection,
)
site = admin.AdminSite(name="admin")
@ -235,6 +236,18 @@ class SomeChildModelInline(admin.TabularInline):
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])
# Test bug #12561 and #12778
# only ModelAdmin media
@ -257,3 +270,4 @@ site.register(BinaryTree, inlines=[BinaryTreeAdmin])
site.register(ExtraTerrestrial, inlines=[SightingInline])
site.register(SomeParentModel, inlines=[SomeChildModelInline])
site.register([Question, Inner4Stacked, Inner4Tabular])
site.register(Teacher, TeacherAdmin)

View File

@ -1024,3 +1024,21 @@ class SeleniumTests(AdminSeleniumTestCase):
self.wait_until_visible(field_name)
hide_links[hide_index].click()
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)