Skip to content

Instantly share code, notes, and snippets.

@mkarajohn
Created January 11, 2021 20:24
Show Gist options
  • Save mkarajohn/44a35d6e3da383c0160fdf3e427666fd to your computer and use it in GitHub Desktop.
Save mkarajohn/44a35d6e3da383c0160fdf3e427666fd to your computer and use it in GitHub Desktop.
Django Collapsed Stacked Inlines
/* collapsed_stacked_inlines.js */
/* Created in May 2009 by Hannes Rydén */
/* Use, distribute and modify freely */
// Original script
// https://djangosnippets.org/snippets/1492/
// 2021 updates by Dimitris Karagiannis
// @MitchKarajohn
jQuery(function ($) {
var linkStyle =
'cursor: pointer; color: #fff; border-radius: 4px; font-weight: 400; padding: 5px 10px; background: #417690; border: none;';
// Only for stacked inlines
$('div.inline-group div.inline-related:not(.tabular)').each(function () {
const $h3 = $(this.querySelector('h3'));
const $fs = $(this.querySelector('fieldset'));
const fsErrorsExist = $fs.children('.errors').length;
const initialButtonText = fsErrorsExist ? gettext('Hide') : gettext('Show');
const $button = $(
$.parseHTML(
'<a role="button" style="' +
linkStyle +
'" class="stacked_collapse-toggle">' +
initialButtonText +
'</a> '
)
);
// Don't collapse initially if fieldset contains errors
if (fsErrorsExist) $fs.addClass('stacked_collapse');
else $fs.addClass('stacked_collapse collapsed');
// Add toggle link
$button.click(function () {
if (!$fs.hasClass('collapsed')) {
$fs.addClass('collapsed');
this.innerHTML = gettext('Show');
} else {
$fs.removeClass('collapsed');
this.innerHTML = gettext('Hide');
}
});
$h3.prepend($button);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment