Added breadcrumbs management interface and viewlet
authorThierry Florac <thierry.florac@onf.fr>
Mon, 18 Jan 2016 18:36:55 +0100
changeset 109 12d6e8ef5284
parent 108 e49d9bb000ae
child 110 e575b6ab5745
Added breadcrumbs management interface and viewlet
src/pyams_skin/interfaces/viewlet.py
src/pyams_skin/viewlet/breadcrumb/__init__.py
src/pyams_skin/viewlet/breadcrumb/breadcrumbs.pt
--- a/src/pyams_skin/interfaces/viewlet.py	Mon Jan 04 13:30:37 2016 +0100
+++ b/src/pyams_skin/interfaces/viewlet.py	Mon Jan 18 18:36:55 2016 +0100
@@ -18,7 +18,7 @@
 from pyams_viewlet.interfaces import IViewlet, IViewletManager
 
 # import packages
-from zope.interface import Interface
+from zope.interface import Interface, Attribute
 from zope.schema import Text, TextLine, List, Object, Dict, Bool
 
 from pyams_skin import _
@@ -140,7 +140,11 @@
 #
 
 class IMainMenusViewletManager(IViewletManager):
-    """Main menu viewlet manager"""
+    """Main menus viewlet manager"""
+
+
+class IUserMenusViewletManager(IViewletManager):
+    """User menus viewlet manager"""
 
 
 class IMenu(IViewlet):
@@ -209,6 +213,23 @@
 
 
 #
+# Breadcrumbs interfaces
+#
+
+class IBreadcrumbItem(Interface):
+    """Breadcrumb item interface"""
+
+    label = TextLine(title="Item label")
+
+    view_name = TextLine(title="Link view name",
+                         default='admin.html')
+
+    css_class = TextLine(title="CSS class")
+
+    url = Attribute("Link absolute target URL")
+
+
+#
 # Extensions viewlet manager
 #
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_skin/viewlet/breadcrumb/__init__.py	Mon Jan 18 18:36:55 2016 +0100
@@ -0,0 +1,62 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_skin.interfaces.viewlet import IBreadcrumbItem
+from pyams_skin.layer import IPyAMSLayer
+
+# import packages
+from pyams_template.template import template_config
+from pyams_utils.adapter import ContextRequestAdapter
+from pyams_utils.url import absolute_url
+from pyams_viewlet.viewlet import contentprovider_config, ContentProvider
+from pyramid.location import lineage
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+
+@implementer(IBreadcrumbItem)
+class BreadcrumbItem(ContextRequestAdapter):
+    """Breadcrumb item"""
+
+    label = FieldProperty(IBreadcrumbItem['label'])
+    css_class = FieldProperty(IBreadcrumbItem['css_class'])
+    view_name = FieldProperty(IBreadcrumbItem['view_name'])
+
+    @property
+    def url(self):
+        return absolute_url(self.context, self.request, self.view_name)
+
+
+@contentprovider_config(name='pyams.breadcrumbs', layer=IPyAMSLayer)
+@template_config(template='breadcrumbs.pt', layer=IPyAMSLayer)
+class BreadcrumbsContentProvider(ContentProvider):
+    """Breadcrumbs content provider"""
+
+    @property
+    def items(self):
+        result = []
+        registry = self.request.registry
+        for context in lineage(self.context):
+            item = registry.queryMultiAdapter((context, self.request, self.view), IBreadcrumbItem)
+            if item is None:
+                item = registry.queryMultiAdapter((context, self.request), IBreadcrumbItem)
+            if item is None:
+                item = registry.queryAdapter(context, IBreadcrumbItem)
+            if item is not None:
+                result.insert(0, item)
+        return result
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_skin/viewlet/breadcrumb/breadcrumbs.pt	Mon Jan 18 18:36:55 2016 +0100
@@ -0,0 +1,5 @@
+<li tal:repeat="item view.items" i18n:domain="pyams_skin"
+	tal:attributes="class string:parent ${item.css_class}">
+	<a tal:attributes="href item.url"
+	   tal:content="item.label" i18n:translate="">Label</a>
+</li>