Refactor page permission formset on group edit page to not rely on edit-handler logic

This commit is contained in:
Matt Westcott 2015-08-17 00:57:25 +02:00
parent 73dee9898c
commit d180116ea0
4 changed files with 26 additions and 11 deletions

View file

@ -8,6 +8,12 @@ function buildExpandingFormset(prefix, opts) {
var totalFormsInput = $('#' + prefix + '-TOTAL_FORMS');
var formCount = parseInt(totalFormsInput.val(), 10);
if (opts.onInit) {
for (var i = 0; i < formCount; i++) {
opts.onInit(i);
}
}
var emptyFormTemplate = document.getElementById(prefix + '-EMPTY_FORM_TEMPLATE');
if (emptyFormTemplate.innerText) {
emptyFormTemplate = emptyFormTemplate.innerText;
@ -20,9 +26,8 @@ function buildExpandingFormset(prefix, opts) {
.replace(/__prefix__/g, formCount)
.replace(/<-(-*)\/script>/g, '<$1/script>');
formContainer.append(newFormHtml);
if (opts.onAdd) {
opts.onAdd(formCount);
}
if (opts.onAdd) opts.onAdd(formCount);
if (opts.onInit) opts.onInit(formCount);
formCount++;
totalFormsInput.val(formCount);

View file

@ -44,6 +44,18 @@
{% include "wagtailadmin/pages/_editor_js.html" %}
<script>
{% include "wagtailusers/groups/includes/page_permissions_formset.js" with formset=formset only %}
$(function() {
buildExpandingFormset('id_page_permissions', {
'onInit': function(index) {
var deleteInputId = 'id_page_permissions-' + index + '-DELETE';
var childId = 'inline_child_page_permissions-' + index;
$('#' + deleteInputId + '-button').click(function() {
/* set 'deleted' form field to true */
$('#' + deleteInputId).val('1');
$('#' + childId).fadeOut();
});
}
});
});
</script>
{% endblock %}

View file

@ -1,17 +1,15 @@
{% load i18n %}
<td>
{% include "wagtailadmin/edit_handlers/field_panel_field.html" with field=form.page only %}
{% include "wagtailadmin/shared/field.html" with field=form.page only %}
</td>
<td>
{% include "wagtailadmin/edit_handlers/field_panel_field.html" with field=form.permission_type only %}
{% include "wagtailadmin/shared/field.html" with field=form.permission_type only %}
{{ form.id }}
{{ form.ORDER }}
{{ form.DELETE }}
</td>
<td>
<button class="button-secondary button-small no" id="{{ form.DELETE.id_for_label }}-button">{% trans "Delete" %}</button>
<button class="button-secondary button-small no" type="button" id="{{ form.DELETE.id_for_label }}-button">{% trans "Delete" %}</button>
</td>

View file

@ -1,7 +1,7 @@
{% load i18n wagtailadmin_tags %}
<h2>{% trans "Page permissions" %}</h2>
{{ formset.management_form }}{# what is this? #}
{{ formset.management_form }}
<table class="listing">
<col width="40%"/>
@ -32,7 +32,7 @@
<script type="text/django-form-template" id="id_{{ formset.prefix }}-EMPTY_FORM_TEMPLATE">
{% escapescript %}
<tr>
<tr id="inline_child_{{ formset.empty_form.prefix }}">
{% include "wagtailusers/groups/includes/page_permissions_form.html" with form=formset.empty_form only %}
</tr>
{% endescapescript %}