src/pyams_content/component/illustration/zmi/__init__.py
changeset 682 d093aba47072
parent 535 d01efcb4d332
child 694 4541a452fd4c
--- a/src/pyams_content/component/illustration/zmi/__init__.py	Thu Jun 14 10:40:46 2018 +0200
+++ b/src/pyams_content/component/illustration/zmi/__init__.py	Thu Jun 14 14:03:31 2018 +0200
@@ -16,7 +16,8 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.illustration.interfaces import IIllustration, IIllustrationTarget
+from pyams_content.component.illustration.interfaces import IBasicIllustration, IBasicIllustrationTarget, \
+    IIllustration, IIllustrationTarget
 from pyams_content.component.paragraph import IBaseParagraph
 from pyams_content.component.paragraph.zmi.interfaces import IParagraphContainerTable, IParagraphTitleToolbar
 from pyams_form.interfaces.form import IInnerSubForm, IWidgetsPrefixViewletsManager
@@ -64,10 +65,10 @@
         return ''
 
 
-@adapter_config(name='illustration', context=(IIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
+@adapter_config(name='illustration', context=(IBasicIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
                 provides=IInnerSubForm)
-class IllustrationPropertiesInnerEditForm(InnerAdminEditForm):
-    """Illustration properties inner edit form"""
+class BasicIllustrationPropertiesInnerEditForm(InnerAdminEditForm):
+    """Basic illustration properties inner edit form"""
 
     prefix = 'illustration_form.'
 
@@ -75,6 +76,43 @@
     padding_class = ''
     fieldset_class = 'margin-top-10 padding-y-5'
 
+    legend = _("Illustration")
+    legend_class = 'illustration switcher no-y-padding padding-right-10 pull-left width-auto'
+
+    fields = field.Fields(IBasicIllustration).omit('__parent__', '__name__')
+
+    weight = 10
+
+    def getContent(self):
+        return IIllustration(self.context)
+
+    def check_mode(self):
+        if self.parent_form is not None:
+            self.mode = self.parent_form.mode
+
+    @property
+    def switcher_state(self):
+        content = self.getContent()
+        for value in (content.data or {}).values():
+            if value:
+                return 'open'
+
+    def get_ajax_output(self, changes):
+        output = super(BasicIllustrationPropertiesInnerEditForm, self).get_ajax_output(changes)
+        updated = changes.get(IIllustration, ())
+        events = output.setdefault('events', [])
+        if 'data' in updated:
+            # we have to commit transaction to be able to handle blobs...
+            ITransactionManager(self.context).get().commit()
+            events.append(get_json_form_refresh_event(self.context, self.request, self.__class__))
+        return output
+
+
+@adapter_config(name='illustration', context=(IIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
+                provides=IInnerSubForm)
+class IllustrationPropertiesInnerEditForm(BasicIllustrationPropertiesInnerEditForm):
+    """Illustration properties inner edit form"""
+
     @property
     def legend(self):
         if IBaseParagraph.providedBy(self.context):
@@ -93,14 +131,6 @@
     fields['renderer'].widgetFactory = RendererFieldWidget
 
     hide_widgets_prefix_div = True
-    weight = 10
-
-    def getContent(self):
-        return IIllustration(self.context)
-
-    def check_mode(self):
-        if self.parent_form is not None:
-            self.mode = self.parent_form.mode
 
     def updateWidgets(self, prefix=None):
         super(IllustrationPropertiesInnerEditForm, self).updateWidgets(prefix)
@@ -112,10 +142,6 @@
         updated = changes.get(IIllustration, ())
         events = output.setdefault('events', [])
         if 'data' in updated:
-            # we have to commit transaction to be able to handle blobs...
-            ITransactionManager(self.context).get().commit()
-            events.append(get_json_form_refresh_event(self.context, self.request,
-                                                      IllustrationPropertiesInnerEditForm))
             if IBaseParagraph.providedBy(self.context):
                 if self.getContent().data:
                     events.append(get_json_paragraph_markers_refresh_event(self.context, self.request, self,
@@ -130,8 +156,8 @@
         return output
 
 
-@viewlet_config(name='illustration-thumbnail', context=IIllustrationTarget, layer=IPyAMSLayer,
-                view=IllustrationPropertiesInnerEditForm, manager=IWidgetsPrefixViewletsManager)
+@viewlet_config(name='illustration-thumbnail', context=IBasicIllustrationTarget, layer=IPyAMSLayer,
+                view=BasicIllustrationPropertiesInnerEditForm, manager=IWidgetsPrefixViewletsManager)
 @template_config(template='templates/illustration-thumbnail.pt', layer=IPyAMSLayer)
 class IllustrationThumbnail(Viewlet):
     """Paragraph illustration thumbnail"""