Added inner table row actions column
authorThierry Florac <tflorac@ulthar.net>
Wed, 04 Mar 2015 00:05:22 +0100
changeset 25 23d78e06822d
parent 24 21ae3534db9b
child 26 8c05bb60a7ca
Added inner table row actions column
src/pyams_skin/interfaces/container.py
src/pyams_skin/interfaces/viewlet.py
src/pyams_skin/table.py
src/pyams_skin/viewlet/toolbar/__init__.py
src/pyams_skin/viewlet/toolbar/menu-item.pt
src/pyams_skin/viewlet/toolbar/menu.pt
--- a/src/pyams_skin/interfaces/container.py	Wed Mar 04 00:04:11 2015 +0100
+++ b/src/pyams_skin/interfaces/container.py	Wed Mar 04 00:05:22 2015 +0100
@@ -40,6 +40,10 @@
     click_handler = TextLine(title="Default elements click handler")
 
 
+class ITableWithActions(ITable):
+    """Marker interface for table with inner actions menus"""
+
+
 class ITableElementEditor(Interface):
     """Table element editor interface"""
 
--- a/src/pyams_skin/interfaces/viewlet.py	Wed Mar 04 00:04:11 2015 +0100
+++ b/src/pyams_skin/interfaces/viewlet.py	Wed Mar 04 00:05:22 2015 +0100
@@ -291,17 +291,28 @@
     css_class = TextLine(title="Button CSS class",
                          default='btn btn-xs btn-primary dropdown-toggle')
 
+    menu_css_class = TextLine(title="Menu CSS class",
+                              default='dropdown-menu pull-right')
+
 
 class IToolbarAddingMenu(IToolbarMenu):
     """Toolbar adding actions menu"""
 
 
+class ITableItemColumnActionsMenu(IViewletManager, IToolbarMenu):
+    """Table item actions viewlet manager"""
+
+
 class IToolbarMenuItem(IToolbarAction):
     """Toolbar actions menu item"""
 
     css_class = TextLine(title="Menu CSS class",
                          default='')
 
+    stop_propagation = Bool(title="Stop click event propagation",
+                            required=True,
+                            default=False)
+
 
 class IContextActions(IViewletManager, IToolbarMenu):
     """Context actions viewlet manager"""
--- a/src/pyams_skin/table.py	Wed Mar 04 00:04:11 2015 +0100
+++ b/src/pyams_skin/table.py	Wed Mar 04 00:05:22 2015 +0100
@@ -12,11 +12,13 @@
 
 __docformat__ = 'restructuredtext'
 
+
 # import standard library
 
 # import interfaces
-from pyams_skin.interfaces.container import ITable, ITableElementEditor
+from pyams_skin.interfaces.container import ITable, ITableElementEditor, ITableWithActions
 from pyams_skin.layer import IPyAMSLayer
+from pyams_viewlet.interfaces import IViewletManager
 from z3c.table.interfaces import IColumn
 from zope.container.interfaces import IContained
 
@@ -179,6 +181,26 @@
         return absolute_url(item, self.request, self.url)
 
 
+@adapter_config(name='actions', context=(Interface, IPyAMSLayer, ITableWithActions), provides=IColumn)
+class MenuActionsColumn(I18nColumn, Column):
+    """Menu actions column"""
+
+    _header = _("Actions")
+    cssClasses = {'th': 'actions-menu',
+                  'td': 'nowrap'}
+    weight = 9000
+
+    def renderCell(self, item):
+        registry = self.request.registry
+        viewlet = registry.queryMultiAdapter((item, self.request, self.table), IViewletManager,
+                                             name='pyams.table-item.actions')
+        if viewlet is not None:
+            viewlet.update()
+            return viewlet.render()
+        else:
+            return ''
+
+
 class TrashColumn(ActionColumn):
     """Trash action icon column"""
 
--- a/src/pyams_skin/viewlet/toolbar/__init__.py	Wed Mar 04 00:04:11 2015 +0100
+++ b/src/pyams_skin/viewlet/toolbar/__init__.py	Wed Mar 04 00:05:22 2015 +0100
@@ -16,9 +16,9 @@
 # import standard library
 
 # import interfaces
-from pyams_skin.interfaces.container import ITable
+from pyams_skin.interfaces.container import ITable, ITableWithActions
 from pyams_skin.interfaces.viewlet import IToolbarViewletManager, IToolbarAction, IToolbarMenu, IToolbarMenuItem, \
-    IContextActions, IToolbarAddingMenu
+    IContextActions, IToolbarAddingMenu, ITableItemColumnActionsMenu
 from pyams_skin.layer import IPyAMSLayer
 from pyams_utils.interfaces.tales import ITALESExtension
 from zope.contentprovider.interfaces import IContentProvider
@@ -65,6 +65,7 @@
     label = FieldProperty(IToolbarMenu['label'])
     label_css_class = FieldProperty(IToolbarMenu['label_css_class'])
     css_class = FieldProperty(IToolbarMenu['css_class'])
+    menu_css_class = FieldProperty(IToolbarMenu['menu_css_class'])
     click_handler = FieldProperty(IToolbarMenu['click_handler'])
     url = FieldProperty(IToolbarMenu['url'])
     modal_target = FieldProperty(IToolbarMenu['modal_target'])
@@ -92,6 +93,16 @@
     label = _("Add...")
 
 
+@viewletmanager_config(name='pyams.table-item.actions', view=ITableWithActions, layer=IPyAMSLayer,
+                       provides=ITableItemColumnActionsMenu)
+@implementer(ITableItemColumnActionsMenu)
+class TableItemColumnActionsMenu(ToolbarMenu):
+    """Table item actions menu"""
+
+    label = _("Actions...")
+    css_class = 'btn btn-xs btn-info dropdown-toggle'
+
+
 @template_config(template='menu-item.pt', layer=IPyAMSLayer)
 @implementer(IToolbarMenuItem)
 class ToolbarMenuItem(Viewlet):
@@ -103,6 +114,7 @@
     click_handler = FieldProperty(IToolbarMenuItem['click_handler'])
     url = FieldProperty(IToolbarMenuItem['url'])
     modal_target = FieldProperty(IToolbarMenuItem['modal_target'])
+    stop_propagation = FieldProperty(IToolbarMenuItem['stop_propagation'])
 
     def get_url(self):
         return absolute_url(self.context, self.request, self.url)
--- a/src/pyams_skin/viewlet/toolbar/menu-item.pt	Wed Mar 04 00:04:11 2015 +0100
+++ b/src/pyams_skin/viewlet/toolbar/menu-item.pt	Wed Mar 04 00:05:22 2015 +0100
@@ -1,5 +1,6 @@
 <li class="small">
 	<a tal:attributes="data-ams-click-handler view.click_handler;
+					   data-ams-click-stop-propagation 'true' if view.stop_propagation else '';
 					   data-toggle 'modal' if view.modal_target else '';
 					   data-ams-url view.get_url();">
 		<i tal:attributes="class view.label_css_class"></i>&nbsp;
--- a/src/pyams_skin/viewlet/toolbar/menu.pt	Wed Mar 04 00:04:11 2015 +0100
+++ b/src/pyams_skin/viewlet/toolbar/menu.pt	Wed Mar 04 00:05:22 2015 +0100
@@ -4,7 +4,7 @@
 		<tal:var content="view.label" />
 		&nbsp;<i tal:attributes="class view.label_css_class"></i>
 	</button>
-	<ul class="dropdown-menu pull-right">
+	<ul tal:attributes="class view.menu_css_class">
 		<tal:loop repeat="viewlet view.viewlets"
 				  content="structure viewlet.render()" />
 	</ul>