|
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 IParagraphSummary |
|
21 from pyams_content.component.paragraph.interfaces.header import IHeaderParagraph |
|
22 from pyams_content.component.paragraph.zmi.interfaces import IParagraphInnerEditor |
|
23 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION |
|
24 from pyams_content.shared.common.interfaces import IWfSharedContent |
|
25 from pyams_form.interfaces.form import IInnerForm, IEditFormButtons |
|
26 from pyams_i18n.interfaces import II18n |
|
27 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu |
|
28 from pyams_skin.layer import IPyAMSLayer |
|
29 from z3c.form.interfaces import INPUT_MODE |
|
30 |
|
31 # import packages |
|
32 from pyams_content.component.paragraph.header import HeaderParagraph |
|
33 from pyams_content.component.paragraph.zmi import BaseParagraphAJAXEditForm |
|
34 from pyams_content.component.paragraph.zmi.container import ParagraphContainerView |
|
35 from pyams_form.form import AJAXAddForm |
|
36 from pyams_form.security import ProtectedFormObjectMixin |
|
37 from pyams_pagelet.pagelet import pagelet_config |
|
38 from pyams_skin.viewlet.toolbar import ToolbarMenuItem |
|
39 from pyams_template.template import template_config, get_view_template |
|
40 from pyams_utils.adapter import adapter_config, ContextRequestAdapter |
|
41 from pyams_utils.traversing import get_parent |
|
42 from pyams_viewlet.viewlet import viewlet_config |
|
43 from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm |
|
44 from pyramid.view import view_config |
|
45 from z3c.form import field, button |
|
46 from zope.interface import implementer |
|
47 |
|
48 from pyams_content import _ |
|
49 |
|
50 |
|
51 @viewlet_config(name='add-header-paragraph.menu', context=IParagraphContainerTarget, view=ParagraphContainerView, |
|
52 layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=40) |
|
53 class HeaderParagraphAddMenu(ProtectedFormObjectMixin, ToolbarMenuItem): |
|
54 """Header paragraph add menu""" |
|
55 |
|
56 label = _("Add header paragraph...") |
|
57 label_css_class = 'fa fa-fw fa-header' |
|
58 url = 'add-header-paragraph.html' |
|
59 modal_target = True |
|
60 |
|
61 |
|
62 @pagelet_config(name='add-header-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer, |
|
63 permission=MANAGE_CONTENT_PERMISSION) |
|
64 class HeaderParagraphAddForm(AdminDialogAddForm): |
|
65 """Header paragraph add form""" |
|
66 |
|
67 legend = _("Add new header paragraph") |
|
68 icon_css_class = 'fa fa-fw fa-header' |
|
69 |
|
70 fields = field.Fields(IHeaderParagraph).select('header') |
|
71 ajax_handler = 'add-header-paragraph.json' |
|
72 edit_permission = MANAGE_CONTENT_PERMISSION |
|
73 |
|
74 def updateWidgets(self, prefix=None): |
|
75 super(HeaderParagraphAddForm, self).updateWidgets(prefix) |
|
76 if 'header' in self.widgets: |
|
77 self.widgets['header'].widget_css_class = 'textarea height-100' |
|
78 |
|
79 def create(self, data): |
|
80 return HeaderParagraph() |
|
81 |
|
82 def add(self, object): |
|
83 IParagraphContainer(self.context).append(object) |
|
84 |
|
85 |
|
86 @view_config(name='add-header-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer, |
|
87 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
88 class HeaderParagraphAJAXAddForm(AJAXAddForm, HeaderParagraphAddForm): |
|
89 """Header paragraph add form, JSON renderer""" |
|
90 |
|
91 def get_ajax_output(self, changes): |
|
92 return {'status': 'reload', |
|
93 'location': '#paragraphs.html'} |
|
94 |
|
95 |
|
96 @pagelet_config(name='properties.html', context=IHeaderParagraph, layer=IPyAMSLayer, |
|
97 permission=MANAGE_CONTENT_PERMISSION) |
|
98 class HeaderParagraphPropertiesEditForm(AdminDialogEditForm): |
|
99 """Header paragraph properties edit form""" |
|
100 |
|
101 @property |
|
102 def title(self): |
|
103 content = get_parent(self.context, IWfSharedContent) |
|
104 return II18n(content).query_attribute('title', request=self.request) |
|
105 |
|
106 legend = _("Edit header paragraph properties") |
|
107 icon_css_class = 'fa fa-fw fa-header' |
|
108 |
|
109 fields = field.Fields(IHeaderParagraph).select('header') |
|
110 ajax_handler = 'properties.json' |
|
111 edit_permission = MANAGE_CONTENT_PERMISSION |
|
112 |
|
113 def updateWidgets(self, prefix=None): |
|
114 super(HeaderParagraphPropertiesEditForm, self).updateWidgets(prefix) |
|
115 if 'header' in self.widgets: |
|
116 self.widgets['header'].widget_css_class = 'textarea height-100' |
|
117 |
|
118 |
|
119 @view_config(name='properties.json', context=IHeaderParagraph, request_type=IPyAMSLayer, |
|
120 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
121 class HeaderParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, HeaderParagraphPropertiesEditForm): |
|
122 """Header paragraph properties edit form, JSON renderer""" |
|
123 |
|
124 def get_ajax_output(self, changes): |
|
125 output = super(HeaderParagraphPropertiesAJAXEditForm, self).get_ajax_output(changes) |
|
126 if 'header' in changes.get(IHeaderParagraph, ()): |
|
127 output.setdefault('events', []).append({ |
|
128 'event': 'PyAMS_content.changed_item', |
|
129 'options': {'object_type': 'paragraph', |
|
130 'object_name': self.context.__name__, |
|
131 'title': II18n(self.context).query_attribute('title', request=self.request), |
|
132 'visible': self.context.visible} |
|
133 }) |
|
134 return output |
|
135 |
|
136 |
|
137 @adapter_config(context=(IHeaderParagraph, IPyAMSLayer), provides=IParagraphInnerEditor) |
|
138 @implementer(IInnerForm) |
|
139 class HeaderParagraphInnerEditForm(HeaderParagraphPropertiesEditForm): |
|
140 """Header paragraph inner edit form""" |
|
141 |
|
142 legend = None |
|
143 label_css_class = 'control-label col-md-2' |
|
144 input_css_class = 'col-md-10' |
|
145 |
|
146 @property |
|
147 def buttons(self): |
|
148 if self.mode == INPUT_MODE: |
|
149 return button.Buttons(IEditFormButtons) |
|
150 else: |
|
151 return button.Buttons() |
|
152 |
|
153 |
|
154 # |
|
155 # HTML paragraph summary |
|
156 # |
|
157 |
|
158 @adapter_config(context=(IHeaderParagraph, IPyAMSLayer), provides=IParagraphSummary) |
|
159 @template_config(template='templates/header-summary.pt', layer=IPyAMSLayer) |
|
160 class HeaderParagraphSummary(ContextRequestAdapter): |
|
161 """Header paragraph renderer""" |
|
162 |
|
163 language = None |
|
164 |
|
165 def update(self): |
|
166 i18n = II18n(self.context) |
|
167 if self.language: |
|
168 for attr in ('header', ): |
|
169 setattr(self, attr, i18n.get_attribute(attr, self.language, request=self.request)) |
|
170 else: |
|
171 for attr in ('header', ): |
|
172 setattr(self, attr, i18n.query_attribute(attr, request=self.request)) |
|
173 |
|
174 render = get_view_template() |