def getMenuItems(self, object, request): """Return menu item entries in a TAL-friendly form.""" result = [] for name, item in getAdapters((object, request), self.getMenuItemType()): if item.available(): result.append(item) # 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. # (2) Sort unambigious entries by order and then by title. ifaces = list(providedBy(removeSecurityProxy(object)).__iro__) max_key = len(ifaces) def iface_index(item): iface = item._for if not iface: iface = Interface if 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': title, 'description': item.description, 'action': item.action, 'selected': (item.selected() and _u('selected')) or _u(''), 'icon': item.icon, 'extra': item.extra, 'submenu': (IBrowserSubMenuItem.providedBy(item) and getMenu(item.submenuId, object, request)) or None } for index, order, title, item in result] return result
def getMenuItems(self, object, request): """Return menu item entries in a TAL-friendly form.""" result = [] for name, item in getAdapters((object, request), self.getMenuItemType()): if item.available(): result.append(item) # 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. # (2) Sort unambigious entries by order and then by title. ifaces = list(providedBy(removeSecurityProxy(object)).__iro__) max_key = len(ifaces) def iface_index(item): iface = item._for if not iface: iface = Interface if 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": title, "description": item.description, "action": item.action, "selected": (item.selected() and u"selected") or u"", "icon": item.icon, "extra": item.extra, "submenu": (IBrowserSubMenuItem.providedBy(item) and getMenu(item.submenuId, object, request)) or None, } for index, order, title, item in result ] return result
def getMenuItems(self, obj, request): obj = removeSecurityProxy(obj) menu = tuple(zope.component.getAdapters( (obj, request), self.getMenuItemType())) # filter out all items which you do not have the permissions for result = [ item for name, item in menu if check_availability(item) ] # 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. # (2) Sort unambigious entries by order and then by title. ifaces = list(providedBy(removeSecurityProxy(obj)).__iro__) 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(obj), item._for): # directly specified for class, this goes first. return -1 # no idea. This goes last. return max_key result = [(item.order, iface_index(item), item.title, item) for item in result] result.sort() # !+ replace above with super getMenuItems() local_url = url.absoluteURL(obj, request) site_url = url.absoluteURL(getSite(), request) request_url = request.getURL() items = [] selected_index = None current_pos = -1 for index, (order, iface_index, title, item) in enumerate(result): # !+ for some reason, for the generic container listing views, the # context_actions menu comes out with a single item pointing to # "@@index" i.e. itself... we ignore it: if item.action=="@@index": continue extra = item.extra or {} # if no id has been explictly set, get one in some way or another... # !+ID-GENERATION(ah, 17-05-2011) - added call to action_to_id # around item.action as other-wise it returns an invalid @id # attribute (see comment above) extra.setdefault("id", # try "id", BrowserMenu, bungeni.ui.menu.BrowserSubMenuItem # (that picks an id value off item.submenuId) otherwise use # the "action" value, zope.browsermenu.BrowserMenuItem getattr(item, "id", None) or action_to_id(item.action) ) # !+CSS_ID(mr, may-2011) the CSS menu styling should NOT be based # on element id, it is unnecessarily brittle and limited e.g. what # happens if you wish to render a menu twice, on top and bottom of # a page? Styling selectors should be connected only via classes # (not even element tag names). The use of an element id should be # limited to only known-to-be-unique contrainers in the page e.g. # "content", "top-menu-bar", "footer", etc. if IBrowserSubMenuItem.providedBy(item): submenu = getMenu(item.submenuId, obj, request) else: submenu = None extra["hideChildren"] = True _url = make_absolute(item.action, local_url, site_url) if submenu: for menu in submenu: menu["url"] = make_absolute( menu["action"], local_url, site_url) pos = pos_action_in_url(item.action, request_url) if pos and pos > current_pos: # !+ should really only reset this only once, # and pos *should* always be len(base_url) current_pos = pos selected_index = index items.append({ "title": title, "description": item.description, "action": item.action, "url": _url, "selected": u"", # !+MENU_ICON(mr, aug-2010) remove, icon always managed via CSS "icon": item.icon, "extra": extra, "submenu": submenu}) if selected_index is not None: items[selected_index]["selected"] = u"selected" return items
def getMenuItems(self, obj, request): obj = removeSecurityProxy(obj) menu = tuple( zope.component.getAdapters((obj, request), self.getMenuItemType())) # filter out all items which you do not have the permissions for result = [item for name, item in menu if check_availability(item)] # 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. # (2) Sort unambigious entries by order and then by title. ifaces = list(providedBy(removeSecurityProxy(obj)).__iro__) 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(obj), item._for): # directly specified for class, this goes first. return -1 # no idea. This goes last. return max_key result = [(item.order, iface_index(item), item.title, item) for item in result] result.sort() # !+ replace above with super getMenuItems() local_url = url.absoluteURL(obj, request) site_url = url.absoluteURL(getSite(), request) request_url = request.getURL() items = [] selected_index = None current_pos = -1 for index, (order, iface_index, title, item) in enumerate(result): # !+ for some reason, for the generic container listing views, the # context_actions menu comes out with a single item pointing to # "@@index" i.e. itself... we ignore it: if item.action == "@@index": continue extra = item.extra or {} # if no id has been explictly set, get one in some way or another... # !+ID-GENERATION(ah, 17-05-2011) - added call to action_to_id # around item.action as other-wise it returns an invalid @id # attribute (see comment above) extra.setdefault( "id", # try "id", BrowserMenu, bungeni.ui.menu.BrowserSubMenuItem # (that picks an id value off item.submenuId) otherwise use # the "action" value, zope.browsermenu.BrowserMenuItem getattr(item, "id", None) or action_to_id(item.action)) # !+CSS_ID(mr, may-2011) the CSS menu styling should NOT be based # on element id, it is unnecessarily brittle and limited e.g. what # happens if you wish to render a menu twice, on top and bottom of # a page? Styling selectors should be connected only via classes # (not even element tag names). The use of an element id should be # limited to only known-to-be-unique contrainers in the page e.g. # "content", "top-menu-bar", "footer", etc. if IBrowserSubMenuItem.providedBy(item): submenu = getMenu(item.submenuId, obj, request) else: submenu = None extra["hideChildren"] = True _url = make_absolute(item.action, local_url, site_url) if submenu: for menu in submenu: menu["url"] = make_absolute(menu["action"], local_url, site_url) pos = pos_action_in_url(item.action, request_url) if pos and pos > current_pos: # !+ should really only reset this only once, # and pos *should* always be len(base_url) current_pos = pos selected_index = index items.append({ "title": title, "description": item.description, "action": item.action, "url": _url, "selected": u"", # !+MENU_ICON(mr, aug-2010) remove, icon always managed via CSS "icon": item.icon, "extra": extra, "submenu": submenu }) if selected_index is not None: items[selected_index]["selected"] = u"selected" return items