Added framed text paragraph
authorThierry Florac <thierry.florac@onf.fr>
Wed, 07 Mar 2018 09:49:47 +0100
changeset 447 3665317b6009
parent 446 4ce676e4859f
child 448 1629885a2e95
Added framed text paragraph
src/pyams_content/component/paragraph/frame.py
src/pyams_content/component/paragraph/interfaces/frame.py
src/pyams_content/component/paragraph/interfaces/text.py
src/pyams_content/component/paragraph/text.py
src/pyams_content/component/paragraph/zmi/frame.py
src/pyams_content/component/paragraph/zmi/text.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/frame.py	Wed Mar 07 09:49:47 2018 +0100
@@ -0,0 +1,106 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.extfile.interfaces import IExtFileContainerTarget
+from pyams_content.component.illustration.interfaces import IIllustrationTarget
+from pyams_content.component.links.interfaces import ILinkContainerTarget
+from pyams_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.component.paragraph.interfaces.frame import IFrameParagraph, FRAME_PARAGRAPH_TYPE, \
+    FRAME_PARAGRAPH_RENDERERS
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
+from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
+
+# import packages
+from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
+from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
+from pyams_utils.adapter import adapter_config
+from pyams_utils.registry import utility_config, get_utility
+from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+from pyams_content import _
+
+
+#
+# Frame paragraph
+#
+
+@implementer(IFrameParagraph, IIllustrationTarget, IExtFileContainerTarget, ILinkContainerTarget)
+class FrameParagraph(RenderedContentMixin, BaseParagraph):
+    """Framed text paragraph"""
+
+    icon_class = 'fa-list-alt'
+    icon_hint = _("Framed text")
+
+    body = FieldProperty(IFrameParagraph['body'])
+    renderer = FieldProperty(IFrameParagraph['renderer'])
+
+
+@utility_config(name=FRAME_PARAGRAPH_TYPE, provides=IParagraphFactory)
+class FrameParagraphFactory(BaseParagraphFactory):
+    """Framed text paragraph factory"""
+
+    name = _("Framed text paragraph")
+    content_type = FrameParagraph
+    secondary_menu = True
+
+
+@adapter_config(context=IFrameParagraph, provides=IContentChecker)
+class FrameParagraphContentChecker(BaseParagraphContentChecker):
+    """Framed text paragraph content checker"""
+
+    def inner_check(self, request):
+        output = []
+        translate = request.localizer.translate
+        manager = get_parent(self.context, II18nManager)
+        if manager is not None:
+            langs = manager.get_languages()
+        else:
+            negotiator = get_utility(INegotiator)
+            langs = (negotiator.server_language, )
+        i18n = II18n(self.context)
+        for lang in langs:
+            for attr in ('title', 'body'):
+                value = i18n.get_attribute(attr, lang, request)
+                if not value:
+                    field_title = translate(IFrameParagraph[attr].title)
+                    if len(langs) == 1:
+                        output.append(translate(MISSING_VALUE).format(field=field_title))
+                    else:
+                        output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
+        return output
+
+
+@vocabulary_config(name=FRAME_PARAGRAPH_RENDERERS)
+class FrameParagraphRendererVocabulary(SimpleVocabulary):
+    """Framed text paragraph renderers vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        if not IFrameParagraph.providedBy(context):
+            context = FrameParagraph()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(FrameParagraphRendererVocabulary, self).__init__(terms)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/interfaces/frame.py	Wed Mar 07 09:49:47 2018 +0100
@@ -0,0 +1,46 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.paragraph.interfaces import IBaseParagraph
+from pyams_content.features.renderer import IRenderedContent
+
+# import packages
+from pyams_i18n.schema import I18nHTMLField
+from zope.schema import Choice
+
+from pyams_content import _
+
+
+#
+# Text paragraph
+#
+
+FRAME_PARAGRAPH_TYPE = 'FramedText'
+FRAME_PARAGRAPH_RENDERERS = 'PyAMS.paragraph.text.renderers'
+
+
+class IFrameParagraph(IRenderedContent, IBaseParagraph):
+    """Framed text paragraph interface"""
+
+    body = I18nHTMLField(title=_("Frame body"),
+                         required=False)
+
+    renderer = Choice(title=_("Text template"),
+                      description=_("Presentation template used for this paragraph"),
+                      vocabulary=FRAME_PARAGRAPH_RENDERERS,
+                      default='default')
--- a/src/pyams_content/component/paragraph/interfaces/text.py	Wed Mar 07 09:49:30 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-#
-# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-
-__docformat__ = 'restructuredtext'
-
-
-# import standard library
-
-# import interfaces
-from pyams_content.component.paragraph.interfaces import IBaseParagraph
-from pyams_content.features.renderer import IRenderedContent
-
-# import packages
-from pyams_i18n.schema import I18nHTMLField
-from zope.schema import Choice
-
-from pyams_content import _
-
-
-#
-# Text paragraph
-#
-
-TEXT_PARAGRAPH_TYPE = 'Text'
-TEXT_PARAGRAPH_RENDERERS = 'PyAMS.paragraph.text.renderers'
-
-
-class ITextParagraph(IRenderedContent, IBaseParagraph):
-    """Simple text paragraph interface"""
-
-    body = I18nHTMLField(title=_("Frame body"),
-                         required=False)
-
-    renderer = Choice(title=_("Text template"),
-                      description=_("Presentation template used for this paragraph"),
-                      vocabulary=TEXT_PARAGRAPH_RENDERERS,
-                      default='default')
--- a/src/pyams_content/component/paragraph/text.py	Wed Mar 07 09:49:30 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,106 +0,0 @@
-#
-# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-
-__docformat__ = 'restructuredtext'
-
-
-# import standard library
-
-# import interfaces
-from pyams_content.component.extfile.interfaces import IExtFileContainerTarget
-from pyams_content.component.illustration.interfaces import IIllustrationTarget
-from pyams_content.component.links.interfaces import ILinkContainerTarget
-from pyams_content.component.paragraph.interfaces import IParagraphFactory
-from pyams_content.component.paragraph.interfaces.text import ITextParagraph, TEXT_PARAGRAPH_TYPE, \
-    TEXT_PARAGRAPH_RENDERERS
-from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
-from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
-
-# import packages
-from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
-from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
-from pyams_utils.adapter import adapter_config
-from pyams_utils.registry import utility_config, get_utility
-from pyams_utils.request import check_request
-from pyams_utils.traversing import get_parent
-from pyams_utils.vocabulary import vocabulary_config
-from zope.interface import implementer
-from zope.schema.fieldproperty import FieldProperty
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
-
-from pyams_content import _
-
-
-#
-# Text paragraph
-#
-
-@implementer(ITextParagraph, IIllustrationTarget, IExtFileContainerTarget, ILinkContainerTarget)
-class TextParagraph(RenderedContentMixin, BaseParagraph):
-    """Framed text paragraph"""
-
-    icon_class = 'fa-list-alt'
-    icon_hint = _("Framed text")
-
-    body = FieldProperty(ITextParagraph['body'])
-    renderer = FieldProperty(ITextParagraph['renderer'])
-
-
-@utility_config(name=TEXT_PARAGRAPH_TYPE, provides=IParagraphFactory)
-class TextParagraphFactory(BaseParagraphFactory):
-    """Framed text paragraph factory"""
-
-    name = _("Framed text paragraph")
-    content_type = TextParagraph
-    secondary_menu = True
-
-
-@adapter_config(context=ITextParagraph, provides=IContentChecker)
-class TextParagraphContentChecker(BaseParagraphContentChecker):
-    """Framed text paragraph content checker"""
-
-    def inner_check(self, request):
-        output = []
-        translate = request.localizer.translate
-        manager = get_parent(self.context, II18nManager)
-        if manager is not None:
-            langs = manager.get_languages()
-        else:
-            negotiator = get_utility(INegotiator)
-            langs = (negotiator.server_language, )
-        i18n = II18n(self.context)
-        for lang in langs:
-            for attr in ('title', 'body'):
-                value = i18n.get_attribute(attr, lang, request)
-                if not value:
-                    field_title = translate(ITextParagraph[attr].title)
-                    if len(langs) == 1:
-                        output.append(translate(MISSING_VALUE).format(field=field_title))
-                    else:
-                        output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
-        return output
-
-
-@vocabulary_config(name=TEXT_PARAGRAPH_RENDERERS)
-class TextParagraphRendererVocabulary(SimpleVocabulary):
-    """Framed text paragraph renderers vocabulary"""
-
-    def __init__(self, context=None):
-        request = check_request()
-        translate = request.localizer.translate
-        registry = request.registry
-        if not ITextParagraph.providedBy(context):
-            context = TextParagraph()
-        terms = [SimpleTerm(name, title=translate(adapter.label))
-                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
-                                             key=lambda x: x[1].weight)]
-        super(TextParagraphRendererVocabulary, self).__init__(terms)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/zmi/frame.py	Wed Mar 07 09:49:47 2018 +0100
@@ -0,0 +1,232 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.association.interfaces import IAssociationTarget
+from pyams_content.component.association.zmi.interfaces import IAssociationsParentForm
+from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer, \
+    IParagraphPreview
+from pyams_content.component.paragraph.interfaces.frame import IFrameParagraph, FRAME_PARAGRAPH_TYPE
+from pyams_content.component.paragraph.zmi.interfaces import IParagraphInnerEditor, IParagraphContainerView
+from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
+from pyams_form.interfaces.form import IInnerForm, IEditFormButtons
+from pyams_skin.interfaces.tinymce import ITinyMCEConfiguration
+from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
+from pyams_skin.layer import IPyAMSLayer
+from pyams_zmi.interfaces import IPropertiesEditForm
+from z3c.form.interfaces import INPUT_MODE
+
+# import packages
+from pyams_content.component.association.zmi import AssociationsTable
+from pyams_content.component.paragraph.frame import FrameParagraph
+from pyams_content.component.paragraph.zmi import BaseParagraphAJAXAddForm, BaseParagraphAJAXEditForm, \
+    BaseParagraphAddMenu, BaseParagraphPropertiesEditForm
+from pyams_content.component.paragraph.zmi.container import ParagraphContainerTable, \
+    ParagraphTitleToolbarViewletManager
+from pyams_content.features.renderer.zmi import BaseRenderedContentPreview
+from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_utils.adapter import adapter_config, ContextRequestAdapter
+from pyams_utils.traversing import get_parent
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi.form import AdminDialogAddForm
+from pyramid.view import view_config
+from z3c.form import field, button
+from zope.interface import implementer, Interface
+
+from pyams_content import _
+
+
+#
+# Frame paragraph form interface and custom HTML edit widget
+#
+
+class IFrameParagraphForm(Interface):
+    """Marker interface for framed text paragraph form"""
+
+
+@adapter_config(context=(IFrameParagraphForm, IPyAMSLayer), provides=ITinyMCEConfiguration)
+class FrameParagraphBodyEditorConfiguration(ContextRequestAdapter):
+    """Custom configuration for 'body' widget editor"""
+
+    configuration = {
+        'ams-tinymce-menubar': False,
+        'ams-tinymce-plugins': ['lists', ],
+        'ams-tinymce-toolbar': 'undo redo | bold italic | bullist numlist',
+        'ams-tinymce-toolbar1': False,
+        'ams-tinymce-toolbar2': False
+    }
+
+
+#
+# Framed text paragraph
+#
+
+@viewlet_config(name='add-text-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
+                layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=580)
+class FrameParagraphAddMenu(BaseParagraphAddMenu):
+    """Framed text paragraph add menu"""
+
+    label = _("Framed text...")
+    label_css_class = 'fa fa-fw fa-list-alt'
+    url = 'add-text-paragraph.html'
+    paragraph_type = FRAME_PARAGRAPH_TYPE
+
+
+@pagelet_config(name='add-text-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+@implementer(IFrameParagraphForm)
+class FrameParagraphAddForm(AdminDialogAddForm):
+    """Framed text paragraph add form"""
+
+    legend = _("Add new framed text paragraph")
+    dialog_class = 'modal-large'
+    icon_css_class = 'fa fa-fw fa-list-alt'
+    label_css_class = 'control-label col-md-2'
+    input_css_class = 'col-md-10'
+
+    fields = field.Fields(IFrameParagraph).omit('__parent__', '__name__', 'visible')
+    ajax_handler = 'add-text-paragraph.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(FrameParagraphAddForm, self).updateWidgets(prefix)
+        if 'body' in self.widgets:
+            self.widgets['body'].widget_css_class = 'textarea'
+
+    def create(self, data):
+        return FrameParagraph()
+
+    def add(self, object):
+        IParagraphContainer(self.context).append(object)
+
+
+@view_config(name='add-text-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class FrameParagraphAJAXAddForm(BaseParagraphAJAXAddForm, FrameParagraphAddForm):
+    """Framed text paragraph add form, JSON renderer"""
+
+
+@pagelet_config(name='properties.html', context=IFrameParagraph, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+@implementer(IFrameParagraphForm)
+class FrameParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
+    """Framed text paragraph properties edit form"""
+
+    legend = _("Edit framed text paragraph properties")
+    dialog_class = 'modal-large'
+    icon_css_class = 'fa fa-fw fa-list-alt'
+    label_css_class = 'control-label col-md-2'
+    input_css_class = 'col-md-10'
+
+    fields = field.Fields(IFrameParagraph).omit('__parent__', '__name__', 'visible')
+    fields['renderer'].widgetFactory = RendererFieldWidget
+
+    ajax_handler = 'properties.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(FrameParagraphPropertiesEditForm, self).updateWidgets(prefix)
+        if 'body' in self.widgets:
+            body_widget = self.widgets['body']
+            for lang in body_widget.langs:
+                widget = body_widget.widgets[lang]
+                widget.id = '{id}_{name}'.format(id=widget.id, name=self.context.__name__)
+            body_widget.widget_css_class = 'textarea'
+
+
+@view_config(name='properties.json', context=IFrameParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class FrameParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, FrameParagraphPropertiesEditForm):
+    """Framed text paragraph properties edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(FrameParagraphPropertiesAJAXEditForm, self).get_ajax_output(changes)
+        if 'body' in changes.get(IFrameParagraph, ()):
+            # refresh associations count markers
+            parent = get_parent(self.context, IAssociationTarget)
+            table = ParagraphContainerTable(parent, self.request)
+            viewlet = ParagraphTitleToolbarViewletManager(parent, self.request, table)
+            viewlet.update()
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'PyAMS_content.paragraphs.updateToolbar',
+                    'object_name': self.context.__name__,
+                    'toolbar_tag': viewlet.render()
+                }
+            })
+            # refresh associations table
+            associations_table = AssociationsTable(self.context, self.request)
+            associations_table.update()
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'PyAMS_content.associations.refreshAssociations',
+                    'object_id': associations_table.id,
+                    'table': associations_table.render()
+                }
+            })
+        return output
+
+
+@adapter_config(context=(IFrameParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
+@implementer(IInnerForm, IPropertiesEditForm, IAssociationsParentForm)
+class FrameParagraphInnerEditForm(FrameParagraphPropertiesEditForm):
+    """Framed text paragraph inner edit form"""
+
+    legend = None
+    ajax_handler = 'inner-properties.json'
+
+    @property
+    def buttons(self):
+        if self.mode == INPUT_MODE:
+            return button.Buttons(IEditFormButtons)
+        else:
+            return button.Buttons()
+
+
+@view_config(name='inner-properties.json', context=IFrameParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class FrameParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, FrameParagraphInnerEditForm):
+    """Framed text paragraph inner edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(FrameParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
+        if 'renderer' in changes.get(IFrameParagraph, ()):
+            form = FrameParagraphInnerEditForm(self.context, self.request)
+            form.update()
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'object_id': '{0}_{1}_{2}'.format(
+                        self.context.__class__.__name__,
+                        getattr(form.getContent(), '__name__', 'noname').replace('++', ''),
+                        form.id),
+                    'content': form.render()
+                }
+            })
+        return output
+
+
+#
+# Framed text paragraph preview
+#
+
+@adapter_config(context=(IFrameParagraph, IPyAMSLayer), provides=IParagraphPreview)
+class FrameParagraphPreview(BaseRenderedContentPreview):
+    """Framed text paragraph preview"""
--- a/src/pyams_content/component/paragraph/zmi/text.py	Wed Mar 07 09:49:30 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,236 +0,0 @@
-#
-# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-from pyams_skin.interfaces.tinymce import ITinyMCEConfiguration
-
-__docformat__ = 'restructuredtext'
-
-
-# import standard library
-
-# import interfaces
-from pyams_content.component.association.interfaces import IAssociationTarget
-from pyams_content.component.association.zmi.interfaces import IAssociationsParentForm
-from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer, \
-    IParagraphPreview
-from pyams_content.component.paragraph.interfaces.text import ITextParagraph, TEXT_PARAGRAPH_TYPE
-from pyams_content.component.paragraph.zmi.interfaces import IParagraphInnerEditor, IParagraphContainerView
-from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
-from pyams_form.interfaces.form import IInnerForm, IEditFormButtons
-from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
-from pyams_skin.layer import IPyAMSLayer
-from pyams_zmi.interfaces import IPropertiesEditForm
-from z3c.form.interfaces import INPUT_MODE
-
-# import packages
-from pyams_content.component.association.zmi import AssociationsTable
-from pyams_content.component.paragraph.text import TextParagraph
-from pyams_content.component.paragraph.zmi import BaseParagraphAJAXAddForm, BaseParagraphAJAXEditForm, \
-    BaseParagraphAddMenu, BaseParagraphPropertiesEditForm
-from pyams_content.component.paragraph.zmi.container import ParagraphContainerTable, \
-    ParagraphTitleToolbarViewletManager
-from pyams_content.features.renderer.zmi import BaseRenderedContentPreview
-from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_utils.adapter import adapter_config, ContextRequestAdapter
-from pyams_utils.traversing import get_parent
-from pyams_viewlet.viewlet import viewlet_config
-from pyams_zmi.form import AdminDialogAddForm
-from pyramid.view import view_config
-from z3c.form import field, button
-from zope.interface import implementer, Interface
-
-from pyams_content import _
-
-
-#
-# Text paragraph form interface and custom HTML edit widget
-#
-
-class ITextParagraphForm(Interface):
-    """Marker interface for framed text paragraph form"""
-
-
-@adapter_config(context=(ITextParagraphForm, IPyAMSLayer), provides=ITinyMCEConfiguration)
-class TextParagraphBodyEditorConfiguration(ContextRequestAdapter):
-    """Custom configuration for 'body' widget editor"""
-
-    configuration = {
-        'ams-tinymce-menubar': False,
-        'ams-tinymce-plugins': ['lists', ],
-        'ams-tinymce-toolbar': 'undo redo | bold italic | bullist numlist',
-        'ams-tinymce-toolbar1': False,
-        'ams-tinymce-toolbar2': False
-    }
-
-
-#
-# Framed text paragraph
-#
-
-@viewlet_config(name='add-text-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
-                layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=580)
-class TextParagraphAddMenu(BaseParagraphAddMenu):
-    """Framed text paragraph add menu"""
-
-    label = _("Framed text...")
-    label_css_class = 'fa fa-fw fa-list-alt'
-    url = 'add-text-paragraph.html'
-    paragraph_type = TEXT_PARAGRAPH_TYPE
-
-
-@pagelet_config(name='add-text-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
-                permission=MANAGE_CONTENT_PERMISSION)
-@implementer(ITextParagraphForm)
-class TextParagraphAddForm(AdminDialogAddForm):
-    """Framed text paragraph add form"""
-
-    legend = _("Add new framed text paragraph")
-    dialog_class = 'modal-large'
-    icon_css_class = 'fa fa-fw fa-list-alt'
-    label_css_class = 'control-label col-md-2'
-    input_css_class = 'col-md-10'
-
-    fields = field.Fields(ITextParagraph).omit('__parent__', '__name__', 'visible')
-    ajax_handler = 'add-text-paragraph.json'
-    edit_permission = MANAGE_CONTENT_PERMISSION
-
-    def updateWidgets(self, prefix=None):
-        super(TextParagraphAddForm, self).updateWidgets(prefix)
-        if 'body' in self.widgets:
-            self.widgets['body'].widget_css_class = 'textarea'
-
-    def create(self, data):
-        return TextParagraph()
-
-    def add(self, object):
-        IParagraphContainer(self.context).append(object)
-
-
-@view_config(name='add-text-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
-             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
-class TextParagraphAJAXAddForm(BaseParagraphAJAXAddForm, TextParagraphAddForm):
-    """Framed text paragraph add form, JSON renderer"""
-
-
-@pagelet_config(name='properties.html', context=ITextParagraph, layer=IPyAMSLayer,
-                permission=MANAGE_CONTENT_PERMISSION)
-@implementer(ITextParagraphForm)
-class TextParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
-    """Framed text paragraph properties edit form"""
-
-    legend = _("Edit framed text paragraph properties")
-    dialog_class = 'modal-large'
-    icon_css_class = 'fa fa-fw fa-list-alt'
-    label_css_class = 'control-label col-md-2'
-    input_css_class = 'col-md-10'
-
-    fields = field.Fields(ITextParagraph).omit('__parent__', '__name__', 'visible')
-    fields['renderer'].widgetFactory = RendererFieldWidget
-
-    ajax_handler = 'properties.json'
-    edit_permission = MANAGE_CONTENT_PERMISSION
-
-    def updateWidgets(self, prefix=None):
-        super(TextParagraphPropertiesEditForm, self).updateWidgets(prefix)
-        if 'body' in self.widgets:
-            body_widget = self.widgets['body']
-            for lang in body_widget.langs:
-                widget = body_widget.widgets[lang]
-                widget.id = '{id}_{name}'.format(id=widget.id, name=self.context.__name__)
-            body_widget.widget_css_class = 'textarea'
-
-
-class ITextParagraphInnerEditForm(Interface):
-    """Marker interface for framed text paragraph inner form"""
-
-
-@view_config(name='properties.json', context=ITextParagraph, request_type=IPyAMSLayer,
-             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
-class TextParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, TextParagraphPropertiesEditForm):
-    """Framed text paragraph properties edit form, JSON renderer"""
-
-    def get_ajax_output(self, changes):
-        output = super(TextParagraphPropertiesAJAXEditForm, self).get_ajax_output(changes)
-        if 'body' in changes.get(ITextParagraph, ()):
-            # refresh associations count markers
-            parent = get_parent(self.context, IAssociationTarget)
-            table = ParagraphContainerTable(parent, self.request)
-            viewlet = ParagraphTitleToolbarViewletManager(parent, self.request, table)
-            viewlet.update()
-            output.setdefault('events', []).append({
-                'event': 'myams.refresh',
-                'options': {
-                    'handler': 'PyAMS_content.paragraphs.updateToolbar',
-                    'object_name': self.context.__name__,
-                    'toolbar_tag': viewlet.render()
-                }
-            })
-            # refresh associations table
-            associations_table = AssociationsTable(self.context, self.request)
-            associations_table.update()
-            output.setdefault('events', []).append({
-                'event': 'myams.refresh',
-                'options': {
-                    'handler': 'PyAMS_content.associations.refreshAssociations',
-                    'object_id': associations_table.id,
-                    'table': associations_table.render()
-                }
-            })
-        return output
-
-
-@adapter_config(context=(ITextParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
-@implementer(IInnerForm, IPropertiesEditForm, IAssociationsParentForm, ITextParagraphInnerEditForm)
-class TextParagraphInnerEditForm(TextParagraphPropertiesEditForm):
-    """Framed text paragraph inner edit form"""
-
-    legend = None
-    ajax_handler = 'inner-properties.json'
-
-    @property
-    def buttons(self):
-        if self.mode == INPUT_MODE:
-            return button.Buttons(IEditFormButtons)
-        else:
-            return button.Buttons()
-
-
-@view_config(name='inner-properties.json', context=ITextParagraph, request_type=IPyAMSLayer,
-             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
-class TextParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, TextParagraphInnerEditForm):
-    """Framed text paragraph inner edit form, JSON renderer"""
-
-    def get_ajax_output(self, changes):
-        output = super(TextParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
-        if 'renderer' in changes.get(ITextParagraph, ()):
-            form = TextParagraphInnerEditForm(self.context, self.request)
-            form.update()
-            output.setdefault('events', []).append({
-                'event': 'myams.refresh',
-                'options': {
-                    'object_id': '{0}_{1}_{2}'.format(
-                        self.context.__class__.__name__,
-                        getattr(form.getContent(), '__name__', 'noname').replace('++', ''),
-                        form.id),
-                    'content': form.render()
-                }
-            })
-        return output
-
-
-#
-# Framed text paragraph preview
-#
-
-@adapter_config(context=(ITextParagraph, IPyAMSLayer), provides=IParagraphPreview)
-class TextParagraphPreview(BaseRenderedContentPreview):
-    """Framed text paragraph preview"""