diff --git a/docs/reference/hooks.rst b/docs/reference/hooks.rst index e5fbf675d..3223b987b 100644 --- a/docs/reference/hooks.rst +++ b/docs/reference/hooks.rst @@ -508,6 +508,7 @@ Hooks for customising the way users are directed through the process of creating :template: path to a template to render to produce the menu item HTML :get_context: a method that returns a context dictionary to pass to the template :render_html: a method that returns the menu item HTML; by default, renders ``template`` with the context returned from ``get_context`` + :Media: an inner class defining Javascript and CSS to import when this menu item is shown - see `Django form media `_ The ``get_url``, ``is_shown``, ``get_context`` and ``render_html`` methods all accept a request object and a context dictionary containing the following fields: diff --git a/wagtail/admin/action_menu.py b/wagtail/admin/action_menu.py index 02424c288..952eed728 100644 --- a/wagtail/admin/action_menu.py +++ b/wagtail/admin/action_menu.py @@ -1,14 +1,16 @@ """Handles rendering of the list of actions in the footer of the page create/edit views.""" +from django.forms import Media, MediaDefiningClass from django.template.loader import render_to_string from django.urls import reverse +from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ from wagtail.core import hooks from wagtail.core.models import UserPagePermissionsProxy -class ActionMenuItem: +class ActionMenuItem(metaclass=MediaDefiningClass): """Defines an item in the actions drop-up on the page creation/edit view""" order = 100 # default order index if one is not specified on init template = 'wagtailadmin/pages/action_menu/menu_item.html' @@ -162,3 +164,10 @@ class PageActionMenu: for menu_item in self.menu_items ] }, request=self.request) + + @cached_property + def media(self): + media = Media() + for item in self.menu_items: + media += item.media + return media diff --git a/wagtail/admin/templates/wagtailadmin/pages/create.html b/wagtail/admin/templates/wagtailadmin/pages/create.html index bc7a506ae..b83f50a59 100644 --- a/wagtail/admin/templates/wagtailadmin/pages/create.html +++ b/wagtail/admin/templates/wagtailadmin/pages/create.html @@ -62,6 +62,7 @@ {{ block.super }} {% include "wagtailadmin/pages/_editor_css.html" %} {{ edit_handler.form.media.css }} + {{ action_menu.media.css }} {% endblock %} {% block extra_js %} @@ -72,6 +73,7 @@ Additional js from widgets media. Allows for custom widgets in admin panel. {% endcomment %} {{ edit_handler.form.media.js }} + {{ action_menu.media.js }} {% comment %} Additional HTML code that edit handlers define through 'html_declarations'. (Technically this isn't Javascript, but it will generally be data that exists for Javascript to work with...) diff --git a/wagtail/admin/templates/wagtailadmin/pages/edit.html b/wagtail/admin/templates/wagtailadmin/pages/edit.html index 749261454..0ff58558a 100644 --- a/wagtail/admin/templates/wagtailadmin/pages/edit.html +++ b/wagtail/admin/templates/wagtailadmin/pages/edit.html @@ -87,6 +87,7 @@ {{ block.super }} {% include "wagtailadmin/pages/_editor_css.html" %} {{ edit_handler.form.media.css }} + {{ action_menu.media.css }} {% endblock %} {% block extra_js %} @@ -97,6 +98,7 @@ Additional js from widgets media. Allows for custom widgets in admin panel. {% endcomment %} {{ edit_handler.form.media.js }} + {{ action_menu.media.js }} {% comment %} Additional HTML code that edit handlers define through 'html_declarations'. (Technically this isn't Javascript, but it will generally be data that exists for Javascript to work with...) diff --git a/wagtail/admin/tests/test_pages_views.py b/wagtail/admin/tests/test_pages_views.py index dff377ed5..7629497fb 100644 --- a/wagtail/admin/tests/test_pages_views.py +++ b/wagtail/admin/tests/test_pages_views.py @@ -665,6 +665,7 @@ class TestPageCreation(TestCase, WagtailTestUtils): self.assertContains(response, 'Promote') # test register_page_action_menu_item hook self.assertContains(response, '') + self.assertContains(response, 'testapp/js/siren.js') # test construct_page_action_menu hook self.assertContains(response, '') @@ -1280,6 +1281,7 @@ class TestPageEdit(TestCase, WagtailTestUtils): # test register_page_action_menu_item hook self.assertContains(response, '') + self.assertContains(response, 'testapp/js/siren.js') # test construct_page_action_menu hook self.assertContains(response, '') diff --git a/wagtail/tests/testapp/wagtail_hooks.py b/wagtail/tests/testapp/wagtail_hooks.py index 8d5b2a791..2491df199 100644 --- a/wagtail/tests/testapp/wagtail_hooks.py +++ b/wagtail/tests/testapp/wagtail_hooks.py @@ -111,6 +111,9 @@ class PanicMenuItem(ActionMenuItem): label = "Panic!" name = 'action-panic' + class Media: + js = ['testapp/js/siren.js'] + @hooks.register('register_page_action_menu_item') def register_panic_menu_item():