src/pyams_content/component/illustration/zmi/renderer.py
changeset 395 2a39b333a585
child 397 fe989328a54f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/illustration/zmi/renderer.py	Thu Feb 15 15:08:29 2018 +0100
@@ -0,0 +1,119 @@
+#
+# 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
+from persistent import Persistent
+
+# import interfaces
+from pyams_content.component.illustration.interfaces import IIllustration
+from pyams_content.component.illustration.zmi.interfaces import IIllustrationWithZoomSettings
+from pyams_i18n.interfaces import II18n
+from pyams_content.features.renderer.interfaces import IContentRenderer
+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_content import _
+
+
+#
+# Illustration renderers
+#
+
+class BaseIllustrationRenderer(BaseContentRenderer):
+    """Base illustration renderer"""
+
+    language = None
+
+    def update(self):
+        i18n = II18n(self.context)
+        if self.language:
+            self.legend = i18n.get_attribute('alt_title', self.language, request=self.request)
+        else:
+            self.legend = i18n.query_attribute('alt_title', request=self.request)
+
+
+@adapter_config(name='hidden', context=(IIllustration, IPyAMSLayer), provides=IContentRenderer)
+class HiddenIllustrationRenderer(BaseIllustrationRenderer):
+    """Hidden illustration renderer"""
+
+    label = _("Hidden illustration")
+    weight = -999
+
+    def render(self):
+        return ''
+
+
+@adapter_config(name='default', context=(IIllustration, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/renderer-default.pt', layer=IPyAMSLayer)
+class DefaultIllustrationRenderer(BaseIllustrationRenderer):
+    """Default illustration renderer"""
+
+    label = _("Centered illustration")
+    weight = 1
+
+
+@adapter_config(name='left+zoom', context=(IIllustration, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/renderer-left.pt', layer=IPyAMSLayer)
+class LeftIllustrationWithZoomRenderer(BaseIllustrationRenderer):
+    """Illustration renderer with small image and zoom"""
+
+    label = _("Small illustration on the left with zoom")
+    weight = 2
+
+    target_interface = IIllustrationWithZoomSettings
+
+
+@adapter_config(name='right+zoom', context=(IIllustration, IPyAMSLayer), provides=IContentRenderer)
+@template_config(template='templates/renderer-right.pt', layer=IPyAMSLayer)
+class RightIllustrationWithZoomRenderer(BaseIllustrationRenderer):
+    """Illustration renderer with small image and zoom"""
+
+    label = _("Small illustration on the right with zoom")
+    weight = 3
+
+    target_interface = IIllustrationWithZoomSettings
+
+
+#
+# Illustration renderer with zoom settings
+#
+
+ILLUSTRATION_ZOOM_RENDERER_SETTINGS_KEY = 'pyams_content.illustration.renderer:zoom'
+
+
+@implementer(IIllustrationWithZoomSettings)
+class IllustrationZoomSettings(Persistent, Location):
+    """Illustration zoom renderer settings"""
+
+    zoom_on_click = FieldProperty(IIllustrationWithZoomSettings['zoom_on_click'])
+
+
+@adapter_config(context=IIllustration, provides=IIllustrationWithZoomSettings)
+def IllustrationWithZoomSettingsFactory(context):
+    """Illustration zoom renderer settings factory"""
+    annotations = IAnnotations(context)
+    settings = annotations.get(ILLUSTRATION_ZOOM_RENDERER_SETTINGS_KEY)
+    if settings is None:
+        settings = annotations[ILLUSTRATION_ZOOM_RENDERER_SETTINGS_KEY] = IllustrationZoomSettings()
+        locate(settings, context)
+    return settings