From b884026d0806543b37f1a0dc07bb8154cec92acc Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Tue, 22 Mar 2022 22:52:31 +0000 Subject: [PATCH] Move inline panel JS into inline_panel.html --- .eslintignore | 1 - .prettierignore | 1 - tsconfig.json | 1 - wagtail/admin/panels.py | 28 +++---------------- .../wagtailadmin/panels/inline_panel.html | 21 ++++++++++++-- .../wagtailadmin/panels/inline_panel.js | 17 ----------- wagtail/admin/tests/test_edit_handlers.py | 4 +-- 7 files changed, 25 insertions(+), 48 deletions(-) delete mode 100644 wagtail/admin/templates/wagtailadmin/panels/inline_panel.js diff --git a/.eslintignore b/.eslintignore index 8179506385..e1f03e9518 100644 --- a/.eslintignore +++ b/.eslintignore @@ -13,5 +13,4 @@ wagtail/search/static wagtail/snippets/static wagtail/users/static wagtail/contrib/*/static -wagtail/admin/templates/wagtailadmin/panels/inline_panel.js wagtail/contrib/search_promotions/templates/wagtailsearchpromotions/includes/searchpromotions_formset.js diff --git a/.prettierignore b/.prettierignore index 9d771cd56e..88d7e45013 100644 --- a/.prettierignore +++ b/.prettierignore @@ -11,5 +11,4 @@ _build *.md # Files which contain incompatible syntax. *.html -wagtail/admin/templates/wagtailadmin/panels/inline_panel.js wagtail/contrib/search_promotions/templates/wagtailsearchpromotions/includes/searchpromotions_formset.js diff --git a/tsconfig.json b/tsconfig.json index b5ce60a8e6..88d6caa9ec 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,6 @@ "node_modules", "static", // Files with template syntax. - "wagtail/admin/templates/wagtailadmin/panels/inline_panel.js", "wagtail/contrib/search_promotions/templates/wagtailsearchpromotions/includes/searchpromotions_formset.js" ] } diff --git a/wagtail/admin/panels.py b/wagtail/admin/panels.py index 74b01165f7..4106f5edd5 100644 --- a/wagtail/admin/panels.py +++ b/wagtail/admin/panels.py @@ -42,10 +42,6 @@ from .forms.models import ( # NOQA from .forms.pages import WagtailAdminPageForm -def widget_with_script(widget, script): - return mark_safe("{0}".format(widget, script)) - - def get_form_for_model( model, form_class=WagtailAdminModelForm, @@ -845,7 +841,6 @@ class InlinePanel(Panel): class BoundPanel(Panel.BoundPanel): template_name = "wagtailadmin/panels/inline_panel.html" - js_template_name = "wagtailadmin/panels/inline_panel.js" def __init__(self, panel, instance, request, form): super().__init__(panel, instance, request, form) @@ -910,25 +905,10 @@ class InlinePanel(Panel): ) ] - def render_html(self): - formset = render_to_string( - self.template_name, - { - "self": self, - "can_order": self.formset.can_order, - }, - ) - js = self.render_js_init() - return widget_with_script(formset, js) - - def render_js_init(self): - return render_to_string( - self.js_template_name, - { - "self": self, - "can_order": self.formset.can_order, - }, - ) + def get_context_data(self, parent_context=None): + context = super().get_context_data(parent_context) + context["can_order"] = self.formset.can_order + return context # This allows users to include the publishing panel in their own per-model override diff --git a/wagtail/admin/templates/wagtailadmin/panels/inline_panel.html b/wagtail/admin/templates/wagtailadmin/panels/inline_panel.html index 5f8b32d213..10e3e3bff9 100644 --- a/wagtail/admin/templates/wagtailadmin/panels/inline_panel.html +++ b/wagtail/admin/templates/wagtailadmin/panels/inline_panel.html @@ -1,5 +1,4 @@ -{% load i18n %} -{% load wagtailadmin_tags %} +{% load i18n l10n wagtailadmin_tags %} {{ self.formset.management_form }} @@ -29,3 +28,21 @@ {% blocktrans trimmed with label=self.label|lower %}Add {{ label }}{% endblocktrans %}

+ + diff --git a/wagtail/admin/templates/wagtailadmin/panels/inline_panel.js b/wagtail/admin/templates/wagtailadmin/panels/inline_panel.js deleted file mode 100644 index 4f10d7b2c8..0000000000 --- a/wagtail/admin/templates/wagtailadmin/panels/inline_panel.js +++ /dev/null @@ -1,17 +0,0 @@ -{% load l10n %} -{% load wagtailadmin_tags %} -(function() { - var panel = InlinePanel({ - formsetPrefix: "id_{{ self.formset.prefix }}", - emptyChildFormPrefix: "{{ self.empty_child.form.prefix }}", - canOrder: {% if can_order %}true{% else %}false{% endif %}, - maxForms: {{ self.formset.max_num|unlocalize }} - }); - - {% for child in self.children %} - panel.initChildControls("{{ child.form.prefix }}"); - {% endfor %} - panel.setHasContent(); - panel.updateMoveButtonDisabledStates(); - panel.updateAddButtonState(); -})(); diff --git a/wagtail/admin/tests/test_edit_handlers.py b/wagtail/admin/tests/test_edit_handlers.py index a887b7c701..e25ddc598e 100644 --- a/wagtail/admin/tests/test_edit_handlers.py +++ b/wagtail/admin/tests/test_edit_handlers.py @@ -1187,7 +1187,7 @@ class TestInlinePanel(TestCase, WagtailTestUtils): ) # render_js_init must provide the JS initializer - self.assertIn("var panel = InlinePanel({", panel.render_js_init()) + self.assertIn("var panel = InlinePanel({", panel.render_html()) @override_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True) def test_no_thousand_separators_in_js(self): @@ -1217,7 +1217,7 @@ class TestInlinePanel(TestCase, WagtailTestUtils): instance=event_page, form=form, request=self.request ) - self.assertIn("maxForms: 1000", panel.render_js_init()) + self.assertIn("maxForms: 1000", panel.render_html()) def test_invalid_inlinepanel_declaration(self): with self.ignore_deprecation_warnings():