From db94ab575610859cb42851f0908fa02c7b051a80 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Fri, 9 Jan 2015 11:34:23 +0000 Subject: [PATCH] add a mechanism for edit_handlers to output html declarations, and use that to output the html declarations for StreamField --- wagtail/wagtailadmin/edit_handlers.py | 19 +++++++++++++++++-- .../wagtailadmin/pages/_editor_js.html | 5 +++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/wagtail/wagtailadmin/edit_handlers.py b/wagtail/wagtailadmin/edit_handlers.py index 45aa871bc..d4eac9fe3 100644 --- a/wagtail/wagtailadmin/edit_handlers.py +++ b/wagtail/wagtailadmin/edit_handlers.py @@ -159,6 +159,13 @@ class EditHandler(object): def required_formsets(cls): return [] + # return any HTML that needs to be output on the edit page once per edit handler definition. + # Typically this will be used to define snippets of HTML within blocks + # for Javascript code to work with. + @classmethod + def html_declarations(cls): + return '' + # the top-level edit handler is responsible for providing a form class that can produce forms # acceptable to the edit handler _form_class = None @@ -280,6 +287,10 @@ class BaseCompositeEditHandler(EditHandler): return cls._required_formsets + @classmethod + def html_declarations(cls): + return mark_safe(''.join([c.html_declarations() for c in cls.children])) + def __init__(self, instance=None, form=None): super(BaseCompositeEditHandler, self).__init__(instance=instance, form=form) @@ -623,10 +634,14 @@ from wagtail.wagtailadmin.blocks import StreamBlock class BaseStreamFieldPanel(BaseFieldPanel): @classmethod def widget_overrides(cls): - return {cls.field_name: widgets.StreamWidget(block_def=StreamBlock(cls.block_types))} + return {cls.field_name: widgets.StreamWidget(block_def=cls.block_def)} + + @classmethod + def html_declarations(cls): + return cls.block_def.all_html_declarations() def StreamFieldPanel(field_name, block_types): return type(str('_StreamFieldPanel'), (BaseStreamFieldPanel,), { 'field_name': field_name, - 'block_types': block_types, + 'block_def': StreamBlock(block_types), }) diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/pages/_editor_js.html b/wagtail/wagtailadmin/templates/wagtailadmin/pages/_editor_js.html index b83ca3d6a..a15ca2b67 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/pages/_editor_js.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/pages/_editor_js.html @@ -32,3 +32,8 @@ Additional js from widgets media. Allows for custom widgets in admin panel. {% endcomment %} {{ edit_handler.form.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...) +{% endcomment %} +{{ edit_handler.html_declarations }}