# HG changeset patch # User souliane # Date 1408630731 -7200 # Node ID 35ccb3ff82454150e0404cb33d38f0c89ef81d01 # Parent 1d41cc5b57b1f0785e9d2ebb37ff363ba6a693a1 browser_side: add method GenericMenuBar.addCategory + fixes MenuNode.addMenuItem when callback argument is a sub-menu diff -r 1d41cc5b57b1 -r 35ccb3ff8245 src/browser/sat_browser/base_menu.py --- a/src/browser/sat_browser/base_menu.py Thu Aug 21 11:21:47 2014 +0200 +++ b/src/browser/sat_browser/base_menu.py Thu Aug 21 16:18:51 2014 +0200 @@ -83,7 +83,7 @@ self.flat_level = max(flat_level, -1) self.children = [] - def _getOrCreateCategory(self, path, path_i18n=None, types=None, create=False): + def _getOrCreateCategory(self, path, path_i18n=None, types=None, create=False, sub_menu=None): """Return the requested category. If create is True, path_i18n and types are specified, recursively create the category and its parent. @@ -91,19 +91,21 @@ @param path_i18n (list[str]): internationalized path to the category @param types (list[str]): types of the category and its parents @param create (bool): if True, create the category + @param sub_menu (GenericMenuBar): instance to popup as the category + sub-menu, if it is created. Otherwise keep the previous sub-menu. @return: MenuNode or None """ assert(len(path) > 0 and len(path) == len(path_i18n) == len(types)) - if isinstance(path, list) and len(path) > 1: + if len(path) > 1: cat = self._getOrCreateCategory(path[:1], path_i18n[:1], types[:1], create) - return cat._getOrCreateCategory(path[1:], path_i18n[1:], types[1:], create) if cat else None + return cat._getOrCreateCategory(path[1:], path_i18n[1:], types[1:], create, sub_menu) if cat else None cats = [child for child in self.children if child.menu and child.name == path[0]] if len(cats) == 1: return cats[0] assert(cats == []) # there should not be more than 1 category with the same name if create: html = self.menu.getCategoryHTML(path_i18n[0], types[0]) - sub_menu = GenericMenuBar(self.menu.host, vertical=True) + sub_menu = sub_menu if sub_menu else GenericMenuBar(self.menu.host, vertical=True) return self.addItem(html, True, sub_menu, name=path[0]) return None @@ -128,12 +130,12 @@ @param path_i18n (list[str], str): internationalized path to the item @param types (list[str], str): types of the item and its parents @param menu_cmd (MenuCmd, PluginMenuCmd or GenericMenuBar): instance to - execute as a leaf's callback or to popup as a category's sub-menu. + execute as a leaf's callback or to popup as a category sub-menu. """ log.info("addMenuItem: %s %s %s %s" % (path, path_i18n, types, callback)) leaf_node = hasattr(callback, "execute") - category = hasattr(callback, "onShow") + category = isinstance(callback, GenericMenuBar) assert(not leaf_node or not category) path = [path] if isinstance(path, str) else path @@ -141,9 +143,7 @@ types = [types for dummy in range(len(path_i18n))] if isinstance(types, str) else types if category: - cat = self._getOrCreateCategory(path, path_i18n, types, True) - cat.item.setSubMenu(callback) - return cat + return self._getOrCreateCategory(path, path_i18n, types, True, callback) if len(path) == len(path_i18n) - 1: path.append(None) # dummy name for a leaf node @@ -151,6 +151,20 @@ parent = self._getOrCreateCategory(path[:-1], path_i18n[:-1], types[:-1], True) return parent.addItem(path_i18n[-1], callback) + def addCategory(self, path, path_i18n, types, menu_bar=None): + """Recursively add a new category. + + @param path (list[str], str): path to the category + @param path_i18n (list[str], str): internationalized path to the category + @param types (list[str], str): types of the category and its parents + @param menu_bar (GenericMenuBar): instance to popup as the category sub-menu. + """ + if menu_bar: + assert(isinstance(menu_bar, GenericMenuBar)) + else: + menu_bar = GenericMenuBar(self.menu.host, vertical=True) + return self.addMenuItem(path, path_i18n, types, menu_bar) + def addItem(self, item, asHTML=None, popup=None, name=None): """Add a single child to the current node. @@ -276,7 +290,10 @@ return [cat.item for cat in self.node.getCategories(parent_path)] def addMenuItem(self, path, path_i18n, types, menu_cmd): - self.node.addMenuItem(path, path_i18n, types, menu_cmd) + return self.node.addMenuItem(path, path_i18n, types, menu_cmd).item + + def addCategory(self, path, path_i18n, types, menu_bar): + return self.node.addCategory(path, path_i18n, types, menu_bar).item def addItem(self, item, asHTML=None, popup=None): return self.node.addItem(item, asHTML, popup).item diff -r 1d41cc5b57b1 -r 35ccb3ff8245 src/browser/sat_browser/menu.py --- a/src/browser/sat_browser/menu.py Thu Aug 21 11:21:47 2014 +0200 +++ b/src/browser/sat_browser/menu.py Thu Aug 21 16:18:51 2014 +0200 @@ -65,10 +65,13 @@ def addMenuItem(self, *args): self.menu_bar.addMenuItem(*args) + def addCategory(self, *args): + self.menu_bar.addCategory(*args) + def createMenus(self): self.addMenuItem("General", [_("General"), _("Web widget")], 'home', MenuCmd(self, "onWebWidget")) self.addMenuItem("General", [_("General"), _("Disconnect")], 'home', MenuCmd(self, "onDisconnect")) - self.addMenuItem("Contacts", [_("Contacts"), None], 'social') # save the position for this category + self.addCategory("Contacts", _("Contacts"), 'social') # save the position for this category self.addMenuItem("Groups", [_("Groups"), _("Discussion")], 'social', MenuCmd(self, "onJoinRoom")) self.addMenuItem("Groups", [_("Groups"), _("Collective radio")], 'social', MenuCmd(self, "onCollectiveRadio")) self.addMenuItem("Games", [_("Games"), _("Tarot")], 'games', MenuCmd(self, "onTarotGame"))