src/pyams_content/component/paragraph/zmi/keypoint.py
changeset 416 c848b7ceefb7
parent 415 43f42d74cdb8
child 421 20a2b671ade1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/zmi/keypoint.py	Fri Mar 02 11:22:56 2018 +0100
@@ -0,0 +1,164 @@
+#
+# 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 IParagraphContainerTarget, IParagraphContainer, \
+    IParagraphPreview
+from pyams_content.component.paragraph.interfaces.keypoint import IKeypointsParagraph, KEYPOINTS_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_i18n.interfaces import II18n
+from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
+from pyams_skin.layer import IPyAMSLayer
+from z3c.form.interfaces import INPUT_MODE
+
+# import packages
+from pyams_content.component.paragraph.keypoint import KeypointsParagraph
+from pyams_content.component.paragraph.zmi import BaseParagraphAJAXAddForm, BaseParagraphAJAXEditForm, \
+    BaseParagraphAddMenu, BaseParagraphPropertiesEditForm
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config
+from pyams_viewlet.viewlet import viewlet_config, BaseContentProvider
+from pyams_zmi.form import AdminDialogAddForm
+from pyramid.view import view_config
+from z3c.form import field, button
+from zope.interface import implementer
+
+from pyams_content import _
+
+
+@viewlet_config(name='add-keypoints-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
+                layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=570)
+class KeypointsParagraphAddMenu(BaseParagraphAddMenu):
+    """Key points paragraph add menu"""
+
+    label = _("Key points...")
+    label_css_class = 'fa fa-fw fa-key'
+    url = 'add-keypoints-paragraph.html'
+    paragraph_type = KEYPOINTS_PARAGRAPH_TYPE
+
+
+@pagelet_config(name='add-keypoints-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+class KeypointsParagraphAddForm(AdminDialogAddForm):
+    """Key points paragraph add form"""
+
+    legend = _("Add new key points paragraph")
+    icon_css_class = 'fa fa-fw fa-key'
+
+    fields = field.Fields(IKeypointsParagraph).select('body')
+    ajax_handler = 'add-keypoints-paragraph.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(KeypointsParagraphAddForm, self).updateWidgets(prefix)
+        if 'body' in self.widgets:
+            self.widgets['body'].widget_css_class = 'textarea height-100'
+
+    def create(self, data):
+        return KeypointsParagraph()
+
+    def add(self, object):
+        IParagraphContainer(self.context).append(object)
+
+
+@view_config(name='add-keypoints-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeypointsParagraphAJAXAddForm(BaseParagraphAJAXAddForm, KeypointsParagraphAddForm):
+    """Key points paragraph add form, JSON renderer"""
+
+
+@pagelet_config(name='properties.html', context=IKeypointsParagraph, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+class KeypointsParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
+    """Key points paragraph properties edit form"""
+
+    legend = _("Edit key points paragraph properties")
+    icon_css_class = 'fa fa-fw fa-key'
+
+    fields = field.Fields(IKeypointsParagraph).select('body')
+    ajax_handler = 'properties.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(KeypointsParagraphPropertiesEditForm, self).updateWidgets(prefix)
+        if 'body' in self.widgets:
+            self.widgets['body'].widget_css_class = 'textarea height-100'
+
+
+@view_config(name='properties.json', context=IKeypointsParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeypointsParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, KeypointsParagraphPropertiesEditForm):
+    """Key points paragraph properties edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(KeypointsParagraphPropertiesAJAXEditForm, self).get_ajax_output(changes)
+        if 'body' in changes.get(IKeypointsParagraph, ()):
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'PyAMS_content.paragraphs.refreshParagraph',
+                    'object_name': self.context.__name__,
+                    'title': II18n(self.context).query_attribute('title', request=self.request),
+                    'visible': self.context.visible
+                }
+            })
+        return output
+
+
+@adapter_config(context=(IKeypointsParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
+@implementer(IInnerForm)
+class KeypointsParagraphInnerEditForm(KeypointsParagraphPropertiesEditForm):
+    """Key points paragraph inner edit form"""
+
+    legend = None
+    label_css_class = 'control-label col-md-2'
+    input_css_class = 'col-md-10'
+
+    @property
+    def buttons(self):
+        if self.mode == INPUT_MODE:
+            return button.Buttons(IEditFormButtons)
+        else:
+            return button.Buttons()
+
+
+#
+# Key points paragraph preview
+#
+
+@adapter_config(context=(IKeypointsParagraph, IPyAMSLayer), provides=IParagraphPreview)
+@template_config(template='templates/keypoints-preview.pt', layer=IPyAMSLayer)
+class KeypointsParagraphPreview(BaseContentProvider):
+    """Key points paragraph preview"""
+
+    language = None
+
+    def update(self):
+        i18n = II18n(self.context)
+        if self.language:
+            for attr in ('body', ):
+                setattr(self, attr, i18n.get_attribute(attr, self.language, request=self.request))
+        else:
+            for attr in ('body', ):
+                setattr(self, attr, i18n.query_attribute(attr, request=self.request))
+
+    @property
+    def keypoints(self):
+        return (self.body or '').split('\n')
\ No newline at end of file