[Checkins] SVN: zope.app.publisher/trunk/ - <browser:menuItem> can now be registered for classes.

Christian Zagrodnick cz at gocept.com
Mon Aug 20 10:53:49 EDT 2007


Log message for revision 79035:
  - <browser:menuItem> can now be registered for classes.
  
  

Changed:
  U   zope.app.publisher/trunk/CHANGES.txt
  U   zope.app.publisher/trunk/src/zope/app/publisher/browser/menu.py
  U   zope.app.publisher/trunk/src/zope/app/publisher/browser/menu.txt
  U   zope.app.publisher/trunk/src/zope/app/publisher/browser/metadirectives.py

-=-
Modified: zope.app.publisher/trunk/CHANGES.txt
===================================================================
--- zope.app.publisher/trunk/CHANGES.txt	2007-08-20 14:51:58 UTC (rev 79034)
+++ zope.app.publisher/trunk/CHANGES.txt	2007-08-20 14:53:48 UTC (rev 79035)
@@ -5,6 +5,8 @@
 3.5
 ===
 
+- <browser:menuItem> can now be registered for classes.
+
 - Added a `layer` attribute to `xmlrpc:view`. This works just like layers for
   `browser:view` etc. but uses the `IXMLRPCSkinType`.
 

Modified: zope.app.publisher/trunk/src/zope/app/publisher/browser/menu.py
===================================================================
--- zope.app.publisher/trunk/src/zope/app/publisher/browser/menu.py	2007-08-20 14:51:58 UTC (rev 79034)
+++ zope.app.publisher/trunk/src/zope/app/publisher/browser/menu.py	2007-08-20 14:53:48 UTC (rev 79035)
@@ -19,6 +19,7 @@
 import sys
 
 import zope.component
+import zope.interface.interfaces
 from zope.interface import Interface, implements, providedBy
 from zope.security import checkPermission, canAccess
 from zope.security.interfaces import Unauthorized, Forbidden
@@ -47,6 +48,7 @@
 
     def getMenuItems(self, object, request):
         """Return menu item entries in a TAL-friendly form."""
+
         result = []
         for name, item in zope.component.getAdapters((object, request),
                                                      self.getMenuItemType()):
@@ -56,15 +58,27 @@
         # Now order the result. This is not as easy as it seems.
         #
         # (1) Look at the interfaces and put the more specific menu entries
-        #     to the front.
+        #     to the front. 
         # (2) Sort unambigious entries by order and then by title.
         ifaces = list(providedBy(removeSecurityProxy(object)).__iro__)
-        result = [(ifaces.index(item._for or Interface),
-            item.order, item.title, item) for item in result]
+        max_key = len(ifaces)
+        def iface_index(item):
+            iface = item._for
+            if not iface:
+                iface = Interface
+            if zope.interface.interfaces.IInterface.providedBy(iface):
+                return ifaces.index(iface)
+            if isinstance(removeSecurityProxy(object), item._for):
+                # directly specified for class, this goes first.
+                return -1
+            # no idea. This goes last.
+            return max_key
+        result = [(iface_index(item), item.order, item.title, item)
+                  for item in result]
         result.sort()
 
         result = [
-            {'title': item.title,
+            {'title': title,
              'description': item.description,
              'action': item.action,
              'selected': (item.selected() and u'selected') or u'',

Modified: zope.app.publisher/trunk/src/zope/app/publisher/browser/menu.txt
===================================================================
--- zope.app.publisher/trunk/src/zope/app/publisher/browser/menu.txt	2007-08-20 14:51:58 UTC (rev 79034)
+++ zope.app.publisher/trunk/src/zope/app/publisher/browser/menu.txt	2007-08-20 14:53:48 UTC (rev 79035)
@@ -281,6 +281,14 @@
   >>> ztapi.provideAdapter((IContent, IBrowserRequest), 
   ...                      SaveOptions, saveall, 'saveall')
 
+Note that we can also register menu items for classes:
+
+
+  >>> new = menumeta.MenuItemFactory(menu.BrowserMenuItem, title="New",
+  ...                                 action="new.html", _for=Content)
+  >>> ztapi.provideAdapter((Content, IBrowserRequest), EditMenu, new, 'new')
+
+
 The utility that is used to generate the menu into a TAL-friendly
 data-structure is ``getMenu()``::
 
@@ -290,9 +298,16 @@
 menu now:
 
   >>> pprint(menu.getMenu('edit', Content(), TestRequest()))
-  [{'action': 'redo.html',
+  [{'action': 'new.html',
     'description': u'',
     'extra': None,
+    'icon': None,
+    'selected': u'',
+    'submenu': None,
+    'title': 'New'},
+  {'action': 'redo.html',
+    'description': u'',
+    'extra': None,
     'icon': '/@@/redo.png',
     'selected': u'',
     'submenu': None,

Modified: zope.app.publisher/trunk/src/zope/app/publisher/browser/metadirectives.py
===================================================================
--- zope.app.publisher/trunk/src/zope/app/publisher/browser/metadirectives.py	2007-08-20 14:51:58 UTC (rev 79034)
+++ zope.app.publisher/trunk/src/zope/app/publisher/browser/metadirectives.py	2007-08-20 14:53:48 UTC (rev 79035)
@@ -412,7 +412,7 @@
         required=True,
         )
 
-    for_ = GlobalInterface(
+    for_ = GlobalObject(
         title=u"Interface",
         description=u"The interface the menu items are defined for",
         required=True



More information about the Checkins mailing list