src/pyams_content/component/illustration/zmi/__init__.py
changeset 694 4541a452fd4c
parent 682 d093aba47072
child 842 7f0d72e427b1
equal deleted inserted replaced
693:26d51b558325 694:4541a452fd4c
    15 
    15 
    16 # import standard library
    16 # import standard library
    17 
    17 
    18 # import interfaces
    18 # import interfaces
    19 from pyams_content.component.illustration.interfaces import IBasicIllustration, IBasicIllustrationTarget, \
    19 from pyams_content.component.illustration.interfaces import IBasicIllustration, IBasicIllustrationTarget, \
    20     IIllustration, IIllustrationTarget
    20     IIllustration, IIllustrationTarget, ILinkIllustrationTarget
    21 from pyams_content.component.paragraph import IBaseParagraph
    21 from pyams_content.component.paragraph import IBaseParagraph
    22 from pyams_content.component.paragraph.zmi.interfaces import IParagraphContainerTable, IParagraphTitleToolbar
       
    23 from pyams_form.interfaces.form import IInnerSubForm, IWidgetsPrefixViewletsManager
    22 from pyams_form.interfaces.form import IInnerSubForm, IWidgetsPrefixViewletsManager
    24 from pyams_skin.layer import IPyAMSLayer
    23 from pyams_skin.layer import IPyAMSLayer
    25 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
       
    26 from pyams_zmi.interfaces import IPropertiesEditForm
    24 from pyams_zmi.interfaces import IPropertiesEditForm
    27 from transaction.interfaces import ITransactionManager
    25 from transaction.interfaces import ITransactionManager
    28 
    26 
    29 # import packages
    27 # import packages
       
    28 from pyams_content.component.illustration.zmi.paragraph import ParagraphContainerIllustrationMarker
    30 from pyams_content.component.paragraph.zmi import get_json_paragraph_markers_refresh_event
    29 from pyams_content.component.paragraph.zmi import get_json_paragraph_markers_refresh_event
    31 from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
    30 from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
    32 from pyams_skin.event import get_json_form_refresh_event, get_json_widget_refresh_event
    31 from pyams_skin.event import get_json_form_refresh_event, get_json_widget_refresh_event
    33 from pyams_template.template import template_config
    32 from pyams_template.template import template_config
    34 from pyams_utils.adapter import adapter_config
    33 from pyams_utils.adapter import adapter_config
    41 
    40 
    42 #
    41 #
    43 # Illustration properties inner edit form
    42 # Illustration properties inner edit form
    44 #
    43 #
    45 
    44 
    46 @viewlet_config(name='illustration', context=IIllustrationTarget, layer=IPyAMSLayer, view=IParagraphContainerTable,
       
    47                 manager=IParagraphTitleToolbar, permission=VIEW_SYSTEM_PERMISSION, weight=5)
       
    48 @template_config(template='templates/paragraph-illustration-icon.pt', layer=IPyAMSLayer)
       
    49 class ParagraphContainerIllustrationMarker(Viewlet):
       
    50     """Paragraph container illustration marker column"""
       
    51 
       
    52     weight = 5
       
    53     action_class = 'action illustration nowrap width-40'
       
    54     icon_class = 'fa fa-fw fa-picture-o'
       
    55     icon_hint = _("Illustration")
       
    56 
       
    57     marker_type = 'illustration'
       
    58 
       
    59     def render(self):
       
    60         illustration = IIllustration(self.context, None)
       
    61         if illustration and illustration.data:
       
    62             for value in illustration.data.values():
       
    63                 if value:
       
    64                     return super(ParagraphContainerIllustrationMarker, self).render()
       
    65         return ''
       
    66 
       
    67 
       
    68 @adapter_config(name='illustration', context=(IBasicIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
    45 @adapter_config(name='illustration', context=(IBasicIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
    69                 provides=IInnerSubForm)
    46                 provides=IInnerSubForm)
    70 class BasicIllustrationPropertiesInnerEditForm(InnerAdminEditForm):
    47 class BasicIllustrationPropertiesInnerEditForm(InnerAdminEditForm):
    71     """Basic illustration properties inner edit form"""
    48     """Basic illustration properties inner edit form"""
    72 
    49 
    73     prefix = 'illustration_form.'
    50     prefix = 'basic_illustration_form.'
    74 
    51 
    75     css_class = 'form-group'
    52     css_class = 'form-group'
    76     padding_class = ''
    53     padding_class = ''
    77     fieldset_class = 'margin-top-10 padding-y-5'
    54     fieldset_class = 'bordered margin-top-10 padding-y-5'
    78 
    55 
    79     legend = _("Illustration")
    56     legend = _("Illustration")
    80     legend_class = 'illustration switcher no-y-padding padding-right-10 pull-left width-auto'
    57     legend_class = 'illustration switcher no-y-padding padding-right-10'
    81 
    58 
    82     fields = field.Fields(IBasicIllustration).omit('__parent__', '__name__')
    59     fields = field.Fields(IBasicIllustration).omit('__parent__', '__name__')
    83 
    60 
    84     weight = 10
    61     weight = 10
    85 
    62 
    91             self.mode = self.parent_form.mode
    68             self.mode = self.parent_form.mode
    92 
    69 
    93     @property
    70     @property
    94     def switcher_state(self):
    71     def switcher_state(self):
    95         content = self.getContent()
    72         content = self.getContent()
    96         for value in (content.data or {}).values():
    73         if content.has_data():
    97             if value:
    74             return 'open'
    98                 return 'open'
       
    99 
    75 
   100     def get_ajax_output(self, changes):
    76     def get_ajax_output(self, changes):
   101         output = super(BasicIllustrationPropertiesInnerEditForm, self).get_ajax_output(changes)
    77         output = super(BasicIllustrationPropertiesInnerEditForm, self).get_ajax_output(changes)
   102         updated = changes.get(IIllustration, ())
    78         updated = changes.get(IBasicIllustration, ())
   103         events = output.setdefault('events', [])
       
   104         if 'data' in updated:
    79         if 'data' in updated:
   105             # we have to commit transaction to be able to handle blobs...
    80             # we have to commit transaction to be able to handle blobs...
   106             ITransactionManager(self.context).get().commit()
    81             ITransactionManager(self.context).get().commit()
   107             events.append(get_json_form_refresh_event(self.context, self.request, self.__class__))
    82             output.setdefault('events', []).append(
       
    83                 get_json_form_refresh_event(self.context, self.request, self.__class__))
   108         return output
    84         return output
   109 
    85 
   110 
    86 
   111 @adapter_config(name='illustration', context=(IIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
    87 @adapter_config(name='illustration', context=(IIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
   112                 provides=IInnerSubForm)
    88                 provides=IInnerSubForm)
   113 class IllustrationPropertiesInnerEditForm(BasicIllustrationPropertiesInnerEditForm):
    89 class IllustrationPropertiesInnerEditForm(BasicIllustrationPropertiesInnerEditForm):
   114     """Illustration properties inner edit form"""
    90     """Illustration properties inner edit form"""
       
    91 
       
    92     prefix = 'illustration_form.'
       
    93 
       
    94     fields = field.Fields(IIllustration).omit('__parent__', '__name__')
       
    95     fields['renderer'].widgetFactory = RendererFieldWidget
   115 
    96 
   116     @property
    97     @property
   117     def legend(self):
    98     def legend(self):
   118         if IBaseParagraph.providedBy(self.context):
    99         if IBaseParagraph.providedBy(self.context):
   119             return _("Illustration")
   100             return _("Illustration")
   123     @property
   104     @property
   124     def legend_class(self):
   105     def legend_class(self):
   125         if IBaseParagraph.providedBy(self.context):
   106         if IBaseParagraph.providedBy(self.context):
   126             return 'illustration switcher no-y-padding padding-right-10 pull-left width-auto'
   107             return 'illustration switcher no-y-padding padding-right-10 pull-left width-auto'
   127         else:
   108         else:
   128             return 'illustration no-y-padding'
   109             return 'illustration switcher no-y-padding'
   129 
   110 
   130     fields = field.Fields(IIllustration).omit('__parent__', '__name__')
   111     @property
   131     fields['renderer'].widgetFactory = RendererFieldWidget
   112     def fieldset_class(self):
       
   113         result = 'margin-top-10 padding-y-5'
       
   114         if not IBaseParagraph.providedBy(self.context):
       
   115             result += ' bordered'
       
   116         return result
   132 
   117 
   133     hide_widgets_prefix_div = True
   118     hide_widgets_prefix_div = True
       
   119 
       
   120     @property
       
   121     def switcher_state(self):
       
   122         if not IBaseParagraph.providedBy(self.context):
       
   123             return 'open'
   134 
   124 
   135     def updateWidgets(self, prefix=None):
   125     def updateWidgets(self, prefix=None):
   136         super(IllustrationPropertiesInnerEditForm, self).updateWidgets(prefix)
   126         super(IllustrationPropertiesInnerEditForm, self).updateWidgets(prefix)
   137         if 'description' in self.widgets:
   127         if 'description' in self.widgets:
   138             self.widgets['description'].widget_css_class = 'textarea'
   128             self.widgets['description'].widget_css_class = 'textarea'
   139 
   129 
   140     def get_ajax_output(self, changes):
   130     def get_ajax_output(self, changes):
   141         output = super(IllustrationPropertiesInnerEditForm, self).get_ajax_output(changes)
   131         output = super(IllustrationPropertiesInnerEditForm, self).get_ajax_output(changes)
   142         updated = changes.get(IIllustration, ())
       
   143         events = output.setdefault('events', [])
   132         events = output.setdefault('events', [])
   144         if 'data' in updated:
   133         if 'data' in changes.get(IBasicIllustration, ()):
   145             if IBaseParagraph.providedBy(self.context):
   134             if IBaseParagraph.providedBy(self.context):
   146                 if self.getContent().data:
   135                 if self.getContent().data:
   147                     events.append(get_json_paragraph_markers_refresh_event(self.context, self.request, self,
   136                     events.append(get_json_paragraph_markers_refresh_event(self.context, self.request, self,
   148                                                                            ParagraphContainerIllustrationMarker))
   137                                                                            ParagraphContainerIllustrationMarker))
   149                 else:
   138                 else:
   150                     events.append(get_json_paragraph_markers_refresh_event(self.context, self.request, self,
   139                     events.append(get_json_paragraph_markers_refresh_event(self.context, self.request, self,
   151                                                                            EmptyViewlet,
   140                                                                            EmptyViewlet,
   152                                                                            ParagraphContainerIllustrationMarker.marker_type))
   141                                                                            ParagraphContainerIllustrationMarker.marker_type))
   153         elif 'renderer' in updated:
   142         elif 'renderer' in changes.get(IIllustration, ()):
   154             events.append(get_json_widget_refresh_event(self.context, self.request,
   143             events.append(get_json_widget_refresh_event(self.context, self.request,
   155                                                         IllustrationPropertiesInnerEditForm, 'renderer'))
   144                                                         IllustrationPropertiesInnerEditForm, 'renderer'))
   156         return output
   145         return output
       
   146 
       
   147 
       
   148 @adapter_config(name='link-illustration', context=(ILinkIllustrationTarget, IPyAMSLayer, IPropertiesEditForm),
       
   149                 provides=IInnerSubForm)
       
   150 class LinkIllustrationPropertiesInnerEditForm(BasicIllustrationPropertiesInnerEditForm):
       
   151     """Link illustration properties inner edit form"""
       
   152 
       
   153     prefix = 'link_illustration_form.'
       
   154 
       
   155     legend = _("Navigation link illustration")
       
   156     legend_class = 'illustration switcher no-y-padding'
       
   157 
       
   158     weight = 11
       
   159 
       
   160     def getContent(self):
       
   161         registry = self.request.registry
       
   162         return registry.getAdapter(self.context, IIllustration, name='link')
   157 
   163 
   158 
   164 
   159 @viewlet_config(name='illustration-thumbnail', context=IBasicIllustrationTarget, layer=IPyAMSLayer,
   165 @viewlet_config(name='illustration-thumbnail', context=IBasicIllustrationTarget, layer=IPyAMSLayer,
   160                 view=BasicIllustrationPropertiesInnerEditForm, manager=IWidgetsPrefixViewletsManager)
   166                 view=BasicIllustrationPropertiesInnerEditForm, manager=IWidgetsPrefixViewletsManager)
   161 @template_config(template='templates/illustration-thumbnail.pt', layer=IPyAMSLayer)
   167 @template_config(template='templates/illustration-thumbnail.pt', layer=IPyAMSLayer)