Skip to content

Instantly share code, notes, and snippets.

@unexceptable
Created May 30, 2017 07:08
Show Gist options
  • Save unexceptable/8580b6e838b1c0fd29addbc80d7c6e81 to your computer and use it in GitHub Desktop.
Save unexceptable/8580b6e838b1c0fd29addbc80d7c6e81 to your computer and use it in GitHub Desktop.
OneToOne Wagtail InlinePanel
// stuff/templates/wagtailadmin/edit_handlers/inline_panel.js
(function() {
var opts = {
formsetPrefix: "id_{{ self.formset.prefix }}",
emptyChildFormPrefix: "{{ self.empty_child.form.prefix }}",
canOrder: {% if can_order %}true{% else %}false{% endif %},
maxForms: {{ self.formset.max_num }}
};
var panel = InlinePanel(opts);
{% for child in self.children %}
panel.initChildControls("{{ child.form.prefix }}");
{% endfor %}
panel.updateAddButtonState = function() {
if (opts.maxForms) {
var forms = panel.formsUl.children('li:visible');
var addButton = $('#' + opts.formsetPrefix + '-ADD');
if (forms.length >= opts.maxForms) {
addButton.hide();
} else {
addButton.show();
}
}
};
panel.setHasContent();
panel.updateMoveButtonDisabledStates();
panel.updateAddButtonState();
})();
# stuff/models.py
class SomeDocument(models.Model):
document = models.ForeignKey(
'wagtaildocs.Document',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
page = ParentalKey(
'stuff.SomePage',
related_name='my_one_document',
unique=True
)
class SomePage(Page):
body = RichTextField(blank=True)
content_panels = [
FieldPanel('body'),
InlinePanel('my_one_document', label="Document"),
]
@coredumperror
Copy link

There no longer seems to be any need to use javascript to hide the disabled + button. You can use some simple CSS, instead:

.classname-of-inline-panel:not(.empty) p.add {
  display: none;
}

Since it only hides p.add when the panel isn't empty, the + button is displayed properly when the panel is collapsed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment