|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer, \ |
|
20 IParagraphPreview |
|
21 from pyams_content.component.paragraph.interfaces.keypoint import IKeypointsParagraph, KEYPOINTS_PARAGRAPH_TYPE |
|
22 from pyams_content.component.paragraph.zmi.interfaces import IParagraphInnerEditor, IParagraphContainerView |
|
23 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION |
|
24 from pyams_form.interfaces.form import IInnerForm, IEditFormButtons |
|
25 from pyams_i18n.interfaces import II18n |
|
26 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu |
|
27 from pyams_skin.layer import IPyAMSLayer |
|
28 from z3c.form.interfaces import INPUT_MODE |
|
29 |
|
30 # import packages |
|
31 from pyams_content.component.paragraph.keypoint import KeypointsParagraph |
|
32 from pyams_content.component.paragraph.zmi import BaseParagraphAJAXAddForm, BaseParagraphAJAXEditForm, \ |
|
33 BaseParagraphAddMenu, BaseParagraphPropertiesEditForm |
|
34 from pyams_pagelet.pagelet import pagelet_config |
|
35 from pyams_template.template import template_config |
|
36 from pyams_utils.adapter import adapter_config |
|
37 from pyams_viewlet.viewlet import viewlet_config, BaseContentProvider |
|
38 from pyams_zmi.form import AdminDialogAddForm |
|
39 from pyramid.view import view_config |
|
40 from z3c.form import field, button |
|
41 from zope.interface import implementer |
|
42 |
|
43 from pyams_content import _ |
|
44 |
|
45 |
|
46 @viewlet_config(name='add-keypoints-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView, |
|
47 layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=570) |
|
48 class KeypointsParagraphAddMenu(BaseParagraphAddMenu): |
|
49 """Key points paragraph add menu""" |
|
50 |
|
51 label = _("Key points...") |
|
52 label_css_class = 'fa fa-fw fa-key' |
|
53 url = 'add-keypoints-paragraph.html' |
|
54 paragraph_type = KEYPOINTS_PARAGRAPH_TYPE |
|
55 |
|
56 |
|
57 @pagelet_config(name='add-keypoints-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer, |
|
58 permission=MANAGE_CONTENT_PERMISSION) |
|
59 class KeypointsParagraphAddForm(AdminDialogAddForm): |
|
60 """Key points paragraph add form""" |
|
61 |
|
62 legend = _("Add new key points paragraph") |
|
63 icon_css_class = 'fa fa-fw fa-key' |
|
64 |
|
65 fields = field.Fields(IKeypointsParagraph).select('body') |
|
66 ajax_handler = 'add-keypoints-paragraph.json' |
|
67 edit_permission = MANAGE_CONTENT_PERMISSION |
|
68 |
|
69 def updateWidgets(self, prefix=None): |
|
70 super(KeypointsParagraphAddForm, self).updateWidgets(prefix) |
|
71 if 'body' in self.widgets: |
|
72 self.widgets['body'].widget_css_class = 'textarea height-100' |
|
73 |
|
74 def create(self, data): |
|
75 return KeypointsParagraph() |
|
76 |
|
77 def add(self, object): |
|
78 IParagraphContainer(self.context).append(object) |
|
79 |
|
80 |
|
81 @view_config(name='add-keypoints-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer, |
|
82 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
83 class KeypointsParagraphAJAXAddForm(BaseParagraphAJAXAddForm, KeypointsParagraphAddForm): |
|
84 """Key points paragraph add form, JSON renderer""" |
|
85 |
|
86 |
|
87 @pagelet_config(name='properties.html', context=IKeypointsParagraph, layer=IPyAMSLayer, |
|
88 permission=MANAGE_CONTENT_PERMISSION) |
|
89 class KeypointsParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm): |
|
90 """Key points paragraph properties edit form""" |
|
91 |
|
92 legend = _("Edit key points paragraph properties") |
|
93 icon_css_class = 'fa fa-fw fa-key' |
|
94 |
|
95 fields = field.Fields(IKeypointsParagraph).select('body') |
|
96 ajax_handler = 'properties.json' |
|
97 edit_permission = MANAGE_CONTENT_PERMISSION |
|
98 |
|
99 def updateWidgets(self, prefix=None): |
|
100 super(KeypointsParagraphPropertiesEditForm, self).updateWidgets(prefix) |
|
101 if 'body' in self.widgets: |
|
102 self.widgets['body'].widget_css_class = 'textarea height-100' |
|
103 |
|
104 |
|
105 @view_config(name='properties.json', context=IKeypointsParagraph, request_type=IPyAMSLayer, |
|
106 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
107 class KeypointsParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, KeypointsParagraphPropertiesEditForm): |
|
108 """Key points paragraph properties edit form, JSON renderer""" |
|
109 |
|
110 def get_ajax_output(self, changes): |
|
111 output = super(KeypointsParagraphPropertiesAJAXEditForm, self).get_ajax_output(changes) |
|
112 if 'body' in changes.get(IKeypointsParagraph, ()): |
|
113 output.setdefault('events', []).append({ |
|
114 'event': 'myams.refresh', |
|
115 'options': { |
|
116 'handler': 'PyAMS_content.paragraphs.refreshParagraph', |
|
117 'object_name': self.context.__name__, |
|
118 'title': II18n(self.context).query_attribute('title', request=self.request), |
|
119 'visible': self.context.visible |
|
120 } |
|
121 }) |
|
122 return output |
|
123 |
|
124 |
|
125 @adapter_config(context=(IKeypointsParagraph, IPyAMSLayer), provides=IParagraphInnerEditor) |
|
126 @implementer(IInnerForm) |
|
127 class KeypointsParagraphInnerEditForm(KeypointsParagraphPropertiesEditForm): |
|
128 """Key points paragraph inner edit form""" |
|
129 |
|
130 legend = None |
|
131 label_css_class = 'control-label col-md-2' |
|
132 input_css_class = 'col-md-10' |
|
133 |
|
134 @property |
|
135 def buttons(self): |
|
136 if self.mode == INPUT_MODE: |
|
137 return button.Buttons(IEditFormButtons) |
|
138 else: |
|
139 return button.Buttons() |
|
140 |
|
141 |
|
142 # |
|
143 # Key points paragraph preview |
|
144 # |
|
145 |
|
146 @adapter_config(context=(IKeypointsParagraph, IPyAMSLayer), provides=IParagraphPreview) |
|
147 @template_config(template='templates/keypoints-preview.pt', layer=IPyAMSLayer) |
|
148 class KeypointsParagraphPreview(BaseContentProvider): |
|
149 """Key points paragraph preview""" |
|
150 |
|
151 language = None |
|
152 |
|
153 def update(self): |
|
154 i18n = II18n(self.context) |
|
155 if self.language: |
|
156 for attr in ('body', ): |
|
157 setattr(self, attr, i18n.get_attribute(attr, self.language, request=self.request)) |
|
158 else: |
|
159 for attr in ('body', ): |
|
160 setattr(self, attr, i18n.query_attribute(attr, request=self.request)) |
|
161 |
|
162 @property |
|
163 def keypoints(self): |
|
164 return (self.body or '').split('\n') |