Updated paragraphs container portlet renderer
authorThierry Florac <thierry.florac@onf.fr>
Thu, 29 Nov 2018 15:37:11 +0100
changeset 266 588256bd26ee
parent 265 914dd6e8cf6d
child 267 cf4776387bfd
Updated paragraphs container portlet renderer
src/pyams_default_theme/component/paragraph/portlet/__init__.py
src/pyams_default_theme/component/paragraph/portlet/templates/content.pt
--- a/src/pyams_default_theme/component/paragraph/portlet/__init__.py	Thu Nov 29 11:19:23 2018 +0100
+++ b/src/pyams_default_theme/component/paragraph/portlet/__init__.py	Thu Nov 29 15:37:11 2018 +0100
@@ -12,24 +12,23 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
 from zope.interface import Interface
 
-# import interfaces
 from pyams_content.component.paragraph.interfaces import IParagraphContainer
 from pyams_content.component.paragraph.portlet.interfaces import IParagraphContainerPortletSettings, \
     IParagraphNavigationPortletSettings
 from pyams_content.features.renderer.interfaces import ISharedContentRenderer
-from pyams_default_theme import _
+from pyams_content.shared.site.interfaces import ISiteContainer
+from pyams_default_theme.interfaces import IContentSummaryInfo
 from pyams_portal.interfaces import IPortalContext, IPortletRenderer
-# import packages
 from pyams_portal.portlet import PortletRenderer
+from pyams_sequence.interfaces import IInternalReference, ISequentialIdInfo
 from pyams_skin.layer import IPyAMSLayer
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 
+from pyams_default_theme import _
+
 
 @adapter_config(context=(IPortalContext, IPyAMSLayer, Interface, IParagraphContainerPortletSettings),
                 provides=IPortletRenderer)
@@ -57,6 +56,36 @@
         self.renderers = list(filter(lambda x: x is not None, renderers))
         [renderer.update() for renderer in self.renderers]
 
+    def get_navigation_links(self):
+
+        def test_item(nav_item):
+            item_sequence = ISequentialIdInfo(nav_item, None)
+            if (item_sequence is not None) and (item_sequence.oid == context_sequence.oid):
+                return True
+            if IInternalReference.providedBy(nav_item) and (nav_item.reference == context_sequence.hex_oid):
+                return True
+
+        prev_nav, next_nav = None, None
+        context_sequence = ISequentialIdInfo(self.context, None)
+        if context_sequence is not None:
+            display_context = self.request.display_context
+            if ISiteContainer.providedBy(display_context):
+                registry = self.request.registry
+                previous_item = None
+                items = display_context.get_visible_items(self.request)
+                for item in items:
+                    if test_item(item):
+                        prev_nav = registry.queryMultiAdapter((previous_item, self.request), IContentSummaryInfo)
+                        break
+                    previous_item = item
+                try:
+                    next_item = next(items)
+                except StopIteration:
+                    pass
+                else:
+                    next_nav = registry.queryMultiAdapter((next_item, self.request), IContentSummaryInfo)
+        return prev_nav, next_nav
+
 
 @adapter_config(context=(IPortalContext, IPyAMSLayer, Interface, IParagraphNavigationPortletSettings),
                 provides=IPortletRenderer)
--- a/src/pyams_default_theme/component/paragraph/portlet/templates/content.pt	Thu Nov 29 11:19:23 2018 +0100
+++ b/src/pyams_default_theme/component/paragraph/portlet/templates/content.pt	Thu Nov 29 15:37:11 2018 +0100
@@ -6,4 +6,28 @@
 			${structure:renderer.render()}
 		</tal:if>
 	</tal:loop>
+	<tal:if define="settings view.settings"
+			condition="settings.display_navigation_links">
+		<tal:var define="(previous, next) view.get_navigation_links()"
+				 condition="(previous is not None) or (next is not None)">
+			<nav aria-label="Previous and next topics" i18n:attributes="aria-label">
+				<ul class="pager">
+					<li class="previous"
+						tal:condition="previous is not None">
+						<a href="${tales:relative_url(previous.context)}">
+							<span aria-hidden="true">&larr;</span>
+							${i18n:previous.title}
+						</a>
+					</li>
+					<li class="next"
+						tal:condition="next is not None">
+						<a href="${tales:relative_url(next.context)}">
+							${i18n:next.title}
+							<span aria-hidden="true">&rarr;</span>
+						</a>
+					</li>
+				</ul>
+			</nav>
+		</tal:var>
+	</tal:if>
 </div>