Support media definitions on action menu items

This commit is contained in:
Matt Westcott 2018-09-19 13:06:18 +01:00 committed by Karl Hobley
parent 9b3e0c15e0
commit cb8148ede2
6 changed files with 20 additions and 1 deletions

View file

@ -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 <https://docs.djangoproject.com/en/stable/topics/forms/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:

View file

@ -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

View file

@ -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...)

View file

@ -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...)

View file

@ -665,6 +665,7 @@ class TestPageCreation(TestCase, WagtailTestUtils):
self.assertContains(response, '<a href="#tab-promote" class="">Promote</a>')
# test register_page_action_menu_item hook
self.assertContains(response, '<input type="submit" name="action-panic" value="Panic!" class="button" />')
self.assertContains(response, 'testapp/js/siren.js')
# test construct_page_action_menu hook
self.assertContains(response, '<input type="submit" name="action-relax" value="Relax." class="button" />')
@ -1280,6 +1281,7 @@ class TestPageEdit(TestCase, WagtailTestUtils):
# test register_page_action_menu_item hook
self.assertContains(response, '<input type="submit" name="action-panic" value="Panic!" class="button" />')
self.assertContains(response, 'testapp/js/siren.js')
# test construct_page_action_menu hook
self.assertContains(response, '<input type="submit" name="action-relax" value="Relax." class="button" />')

View file

@ -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():