Browse Source

Move inline panel JS into inline_panel.html

Matt Westcott 3 years ago
parent
commit
b884026d08

+ 0 - 1
.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

+ 0 - 1
.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

+ 0 - 1
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"
   ]
 }

+ 4 - 24
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}<script>{1}</script>".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

+ 19 - 2
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 %}
     </button>
 </p>
+
+<script>
+    (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();
+    })();
+</script>

+ 0 - 17
wagtail/admin/templates/wagtailadmin/panels/inline_panel.js

@@ -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();
-})();

+ 2 - 2
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():