Added flags and search viewlets
authorThierry Florac <tflorac@ulthar.net>
Tue, 03 Jun 2014 23:31:47 +0200
changeset 29 d4270d6b448a
parent 28 4ec2f5fdb47e
child 30 3c2c5114bc5f
Added flags and search viewlets
src/ztfy/myams/viewlet/flags/__init__.py
src/ztfy/myams/viewlet/flags/configure.zcml
src/ztfy/myams/viewlet/flags/flags.pt
src/ztfy/myams/viewlet/search/__init__.py
src/ztfy/myams/viewlet/search/configure.zcml
src/ztfy/myams/viewlet/search/interfaces.py
src/ztfy/myams/viewlet/search/mobile-search.pt
src/ztfy/myams/viewlet/search/site-search.pt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/myams/viewlet/flags/__init__.py	Tue Jun 03 23:31:47 2014 +0200
@@ -0,0 +1,51 @@
+#
+# Copyright (c) 2012 Thierry Florac <tflorac AT onf.fr>
+# All Rights Reserved.
+#
+
+
+# import standard packages
+
+# import Zope3 interfaces
+from z3c.language.negotiator.interfaces import INegotiatorManager
+from zope.i18n.interfaces import INegotiator
+
+# import local interfaces
+
+# import Zope3 packages
+from zope.component import adapts, queryUtility
+from zope.i18n import translate
+from zope.interface import Interface
+
+# import local packages
+from ztfy.baseskin.viewlet import ContentProviderBase
+from ztfy.myams.layer import MyAMSLayer
+
+
+class FlagsContentProvider(ContentProviderBase):
+    """Flags content provider"""
+
+    adapts(Interface, MyAMSLayer, Interface)
+
+    @property
+    def langs(self):
+        manager = queryUtility(INegotiatorManager)
+        if manager is not None:
+            return manager.offeredLanguages
+        return ()
+
+    def getLabel(self, lang):
+        try:
+            from ztfy.i18n.languages import BASE_LANGUAGES
+        except ImportError:
+            return lang
+        else:
+            return translate(BASE_LANGUAGES.get(lang), context=self.request)
+
+    @property
+    def current(self):
+        manager = queryUtility(INegotiatorManager)
+        negotiator = queryUtility(INegotiator)
+        if (manager is not None) and (negotiator is not None):
+            return negotiator.getLanguage(manager.offeredLanguages, self.request)
+        return None
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/myams/viewlet/flags/configure.zcml	Tue Jun 03 23:31:47 2014 +0200
@@ -0,0 +1,16 @@
+<configure
+	xmlns="http://namespaces.zope.org/zope"
+	xmlns:z3c="http://namespaces.zope.org/z3c"
+	i18n_domain="ztfy.myams">
+
+	<adapter
+		name="myams.flags"
+		factory=".FlagsContentProvider"
+		permission="zope.View" />
+
+	<z3c:template
+		template="flags.pt"
+		for=".FlagsContentProvider"
+		layer="ztfy.myams.layer.MyAMSLayer" />
+
+</configure>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/myams/viewlet/flags/flags.pt	Tue Jun 03 23:31:47 2014 +0200
@@ -0,0 +1,19 @@
+<tal:var define="langs view/langs; current view/current;" condition="langs" i18n:domain="ztfy.myams">
+	<a href="#" class="dropdown-toggle" data-toggle="dropdown">
+		<img alt=""
+			 tal:attributes="src string:/--static--/ztfy.i18n/img/flags/${current}.png">
+		<i class="fa fa-angle-down"></i>
+	</a>
+	<ul class="dropdown-menu pull-right">
+		<tal:loop repeat="lang langs">
+			<li tal:attributes="class python:'active' if lang == current else ''">
+				<a href="javascript:void(0);"
+				   data-ams-click-handler="MyAMS.skin.setLanguage"
+				   tal:attributes='data-ams-click-handler-options string:{"lang": "${lang}"}'>
+					<img alt="" tal:attributes="src string:/--static--/ztfy.i18n/img/flags/${lang}.png">
+					<span tal:content="python:view.getLabel(lang)" />
+				</a>
+			</li>
+		</tal:loop>
+	</ul>
+</tal:var>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/myams/viewlet/search/__init__.py	Tue Jun 03 23:31:47 2014 +0200
@@ -0,0 +1,72 @@
+#
+# Copyright (c) 2014 Thierry Florac <tflorac AT onf.fr>
+# All Rights Reserved.
+#
+
+
+# import standard packages
+
+# import Zope3 interfaces
+
+# import local interfaces
+from ztfy.myams.interfaces import IMyAMSApplication
+from ztfy.myams.interfaces.search import IMyAMSApplicationSearch
+from ztfy.myams.viewlet.search.interfaces import ISearchViewlet
+
+# import Zope3 packages
+from zope.component import adapts
+from zope.i18n import translate
+from zope.interface import implements, Interface
+
+# import local packages
+from ztfy.baseskin.viewlet import ContentProviderBase
+from ztfy.myams.layer import MyAMSLayer
+from ztfy.utils.traversing import getParent
+
+
+class SiteSearchViewlet(ContentProviderBase):
+    """Site search content provider"""
+
+    adapts(Interface, MyAMSLayer, Interface)
+    implements(ISearchViewlet)
+
+    search_config = None
+
+    def update(self):
+        app = getParent(self.context, IMyAMSApplication)
+        if app is not None:
+            self.search_config = IMyAMSApplicationSearch(app, None)
+
+    @property
+    def placeholder(self):
+        if self.search_config is not None:
+            return translate(self.search_config.site_search_placeholder, context=self.request)
+
+    @property
+    def handler(self):
+        if self.search_config is not None:
+            return self.search_config.site_search_handler
+
+
+class MobileSearchViewlet(ContentProviderBase):
+    """Mobile search content provider"""
+
+    adapts(Interface, MyAMSLayer, Interface)
+    implements(ISearchViewlet)
+
+    search_config = None
+
+    def update(self):
+        app = getParent(self.context, IMyAMSApplication)
+        if app is not None:
+            self.search_config = IMyAMSApplicationSearch(app, None)
+
+    @property
+    def placeholder(self):
+        if self.search_config is not None:
+            return translate(self.search_config.mobile_search_placeholder, context=self.request)
+
+    @property
+    def handler(self):
+        if self.search_config is not None:
+            return self.search_config.mobile_search_handler
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/myams/viewlet/search/configure.zcml	Tue Jun 03 23:31:47 2014 +0200
@@ -0,0 +1,28 @@
+<configure
+	xmlns="http://namespaces.zope.org/zope"
+	xmlns:z3c="http://namespaces.zope.org/z3c"
+	i18n_domain="ztfy.myams">
+
+	<adapter
+		name="myams.site_search"
+		factory=".SiteSearchViewlet"
+		provides="zope.contentprovider.interfaces.IContentProvider"
+		permission="zope.View" />
+
+	<z3c:template
+		template="site-search.pt"
+		for=".SiteSearchViewlet"
+		layer="ztfy.myams.layer.MyAMSLayer" />
+
+	<adapter
+		name="myams.mobile_search"
+		factory=".MobileSearchViewlet"
+		provides="zope.contentprovider.interfaces.IContentProvider"
+		permission="zope.View" />
+
+	<z3c:template
+		template="mobile-search.pt"
+		for=".MobileSearchViewlet"
+		layer="ztfy.myams.layer.MyAMSLayer" />
+
+</configure>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/myams/viewlet/search/interfaces.py	Tue Jun 03 23:31:47 2014 +0200
@@ -0,0 +1,26 @@
+#
+# Copyright (c) 2012 Thierry Florac <tflorac AT onf.fr>
+# All Rights Reserved.
+#
+
+
+# import standard packages
+
+# import Zope3 interfaces
+
+# import local interfaces
+
+# import Zope3 packages
+from zope.interface import Interface, Attribute
+
+# import local packages
+
+from ztfy.myams import _
+
+
+class ISearchViewlet(Interface):
+    """Site search viewlet interface"""
+
+    placeholder = Attribute(_("Search input placeholder"))
+
+    handler = Attribute(_("Search handler"))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/myams/viewlet/search/mobile-search.pt	Tue Jun 03 23:31:47 2014 +0200
@@ -0,0 +1,5 @@
+<div id="search-mobile" class="btn-header pull-right" i18n:domain="ztfy.myams">
+	<span>
+		<a href="javascript:void(0)" title="Search" i18n:attributes="title"><i class="fa fa-search"></i></a>
+	</span>
+</div>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/myams/viewlet/search/site-search.pt	Tue Jun 03 23:31:47 2014 +0200
@@ -0,0 +1,11 @@
+<form method="post" action="#search.html" class="header-search pull-right" data-async="" i18n:domain="ztfy.myams"
+	  tal:attributes="action view/handler">
+	<input type="text" placeholder="Find reports and more" id="search-field" name="search-field"
+		   tal:attributes="placeholder view/placeholder">
+	<button type="submit">
+		<i class="fa fa-search"></i>
+	</button>
+	<a href="javascript:void(0);" id="cancel-search-js" title="Cancel Search" i18n:attributes="title">
+		<i class="fa fa-times"></i>
+	</a>
+</form>