src/pyams_default_theme/component/paragraph/frame.py
changeset 9 e81e39878694
child 22 87eb62630b15
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/component/paragraph/frame.py	Wed Mar 07 16:32:24 2018 +0100
@@ -0,0 +1,157 @@
+#
+# Copyright (c) 2008-2017 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
+from persistent import Persistent
+
+# import interfaces
+from pyams_content.component.association.interfaces import IAssociationContainer
+from pyams_content.component.illustration.interfaces import IIllustration
+from pyams_content.component.paragraph.interfaces.frame import IFrameParagraph
+from pyams_content.features.renderer.interfaces import IContentRenderer
+from pyams_default_theme.component.paragraph.interfaces.frame import IFrameParagraphRendererSettings, \
+    ILateralFrameParagraphRendererSettings, IDefaultFrameParagraphRendererSettings
+from pyams_i18n.interfaces import II18n
+from pyams_skin.layer import IPyAMSLayer
+from zope.annotation.interfaces import IAnnotations
+
+# import packages
+from pyams_content.features.renderer.zmi import BaseContentRenderer
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config
+from zope.interface import implementer
+from zope.location import locate, Location
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_default_theme import _
+
+
+#
+# Framed text paragraph default renderer settings
+#
+
+FRAME_PARAGRAPH_RENDERER_SETTINGS_KEY = 'pyams_content.frame.renderer:default'
+
+
+@implementer(IFrameParagraphRendererSettings)
+class BaseFrameParagraphRendererSettings(Persistent, Location):
+    """Base frame text paragraph renderer settings"""
+
+    display_illustration = FieldProperty(IFrameParagraphRendererSettings['display_illustration'])
+    display_associations = FieldProperty(IFrameParagraphRendererSettings['display_associations'])
+
+    def can_display_illustration(self):
+        if not self.display_illustration:
+            return False
+        frame = IFrameParagraph(self.__parent__)
+        illustration = IIllustration(frame, None)
+        return (illustration is not None) and bool(II18n(illustration).query_attribute('data'))
+
+    def can_display_associations(self):
+        if not self.display_associations:
+            return False
+        frame = IFrameParagraph(self.__parent__)
+        associations = IAssociationContainer(frame, None)
+        return (associations is not None) and (len(associations.get_visible_items()) > 0)
+
+
+@implementer(IDefaultFrameParagraphRendererSettings)
+class DefaultFrameParagraphRendererSettings(BaseFrameParagraphRendererSettings):
+    """Framed text paragraph lateral renderer settings"""
+
+
+@adapter_config(context=IFrameParagraph, provides=IDefaultFrameParagraphRendererSettings)
+def DefaultFrameParagraphRendererSettingsFactory(context):
+    """Frame paragraph default renderer settings factory"""
+    annotations = IAnnotations(context)
+    settings = annotations.get(FRAME_PARAGRAPH_RENDERER_SETTINGS_KEY)
+    if settings is None:
+        settings = annotations[FRAME_PARAGRAPH_RENDERER_SETTINGS_KEY] = DefaultFrameParagraphRendererSettings()
+        locate(settings, context)
+    return settings
+
+
+@implementer(ILateralFrameParagraphRendererSettings)
+class LateralFrameParagraphRendererSettings(BaseFrameParagraphRendererSettings):
+    """Framed text paragraph lateral renderer settings"""
+
+    relative_width = FieldProperty(ILateralFrameParagraphRendererSettings['relative_width'])
+
+
+LATERAL_FRAME_PARAGRAPH_RENDERER_SETTINGS_KEY = 'pyams_content.frame.renderer:lateral'
+
+
+@adapter_config(context=IFrameParagraph, provides=ILateralFrameParagraphRendererSettings)
+def LateralFrameParagraphRendererSettingsFactory(context):
+    """Frame text paragraph lateral renderer settings factory"""
+    annotations = IAnnotations(context)
+    settings = annotations.get(LATERAL_FRAME_PARAGRAPH_RENDERER_SETTINGS_KEY)
+    if settings is None:
+        settings = annotations[LATERAL_FRAME_PARAGRAPH_RENDERER_SETTINGS_KEY] = LateralFrameParagraphRendererSettings()
+        locate(settings, context)
+    return settings
+
+
+#
+# Framed text paragraph default renderer
+#
+
+class BaseFrameParagraphRenderer(BaseContentRenderer):
+    """Base frame paragraph renderer"""
+
+    i18n_context_attrs = ('title', 'body')
+    illustration_renderer = None
+
+    def update(self):
+        super(BaseFrameParagraphRenderer, self).update()
+        if self.settings.can_display_illustration():
+            illustration = IIllustration(self.context, None)
+            renderer = illustration.get_renderer(self.request)
+            if renderer is not None:
+                renderer.update()
+                self.illustration_renderer = renderer
+
+
+@adapter_config(name='default', context=(IFrameParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/frame-default.pt', layer=IPyAMSLayer)
+class DefaultFrameParagraphRenderer(BaseFrameParagraphRenderer):
+    """Framed text paragraph default renderer"""
+
+    label = _("Default frame renderer")
+    weight = 1
+
+    settings_interface = IDefaultFrameParagraphRendererSettings
+
+
+@adapter_config(name='left', context=(IFrameParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/frame-left.pt', layer=IPyAMSLayer)
+class LeftFrameParagraphRenderer(BaseFrameParagraphRenderer):
+    """Framed text paragraph renderer displayed on the left"""
+
+    label = _("Small frame on the left")
+    weight = 2
+
+    settings_interface = ILateralFrameParagraphRendererSettings
+
+
+@adapter_config(name='right', context=(IFrameParagraph, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/frame-right.pt', layer=IPyAMSLayer)
+class RightFrameParagraphRenderer(BaseFrameParagraphRenderer):
+    """Framed text paragraph renderer displayed on the right"""
+
+    label = _("Small frame on the right")
+    weight = 3
+
+    settings_interface = ILateralFrameParagraphRendererSettings