Created
May 30, 2017 07:08
-
-
Save unexceptable/8580b6e838b1c0fd29addbc80d7c6e81 to your computer and use it in GitHub Desktop.
OneToOne Wagtail InlinePanel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There no longer seems to be any need to use javascript to hide the disabled + button. You can use some simple CSS, instead:
Since it only hides p.add when the panel isn't empty, the + button is displayed properly when the panel is collapsed.