diff src/browser/sat_browser/menu.py @ 494:5d8632a7bfde

browser_side: refactorisation of menus and LiberviaWidget's header
author souliane <souliane@mailoo.org>
date Tue, 15 Jul 2014 18:43:55 +0200
parents a7f5448a1bc3
children 60be99de3808
line wrap: on
line diff
--- a/src/browser/sat_browser/menu.py	Tue Jul 15 13:39:36 2014 +0200
+++ b/src/browser/sat_browser/menu.py	Tue Jul 15 18:43:55 2014 +0200
@@ -24,142 +24,78 @@
 from sat.core.i18n import _
 
 from pyjamas.ui.SimplePanel import SimplePanel
-from pyjamas.ui.MenuBar import MenuBar
-from pyjamas.ui.MenuItem import MenuItem
 from pyjamas.ui.HTML import HTML
 from pyjamas.ui.Frame import Frame
 from pyjamas import Window
 
+from constants import Const as C
 import jid
-
 import file_tools
 import xmlui
 import panels
 import dialog
 import contact_group
-
-
-class MenuCmd:
-
-    def __init__(self, object_, handler):
-        self._object = object_
-        self._handler = handler
-
-    def execute(self):
-        handler = getattr(self._object, self._handler)
-        handler()
-
-
-class PluginMenuCmd:
-
-    def __init__(self, host, action_id):
-        self.host = host
-        self.action_id = action_id
-
-    def execute(self):
-        self.host.launchAction(self.action_id, None)
+import base_menu
+from base_menu import MenuCmd
 
 
-class LiberviaMenuBar(MenuBar):
+class MainMenuBar(base_menu.GenericMenuBar):
+    """The main menu bar which is displayed on top of the document"""
 
-    def __init__(self):
-        MenuBar.__init__(self, vertical=False)
+    ITEM_TPL = "<img src='media/icons/menu/%s_menu_red.png' />%s"
 
-    def doItemAction(self, item, fireCommand):
-        MenuBar.doItemAction(self, item, fireCommand)
-        if item == self.items[-1] and self.popup:
-            self.popup.setPopupPosition(Window.getClientWidth() -
-                                        self.popup.getOffsetWidth() - 22,
-                                        self.getAbsoluteTop() +
-                                        self.getOffsetHeight() - 1)
-            self.popup.addStyleName('menuLastPopup')
+    def __init__(self, host):
+        base_menu.GenericMenuBar.__init__(self, host, vertical=False)
+        self.moved_popup_style = 'menuLastPopup'
+
+    @classmethod
+    def getCategoryHTML(cls, type_, menu_name_i18n):
+        return cls.ITEM_TPL % (type_, menu_name_i18n)
 
 
-class AvatarUpload(file_tools.FileUploadPanel):
-    def __init__(self):
-        texts = {'ok_button': 'Upload avatar',
-                 'body': 'Please select an image to show as your avatar...<br>Your picture must be a square and will be resized to 64x64 pixels if necessary.',
-                 'errback': "Can't open image... did you actually submit an image?",
-                 'body_errback': 'Please select another image file.',
-                 'callback': "Your new profile picture has been set!"}
-        file_tools.FileUploadPanel.__init__(self, 'upload_avatar', 'avatar_path', 2, texts)
-
-
-class Menu(SimplePanel):
+class MainMenuPanel(SimplePanel):
+    """Container for the main menu bar"""
 
     def __init__(self, host):
         self.host = host
         SimplePanel.__init__(self)
         self.setStyleName('menuContainer')
+        self.menu_bar = MainMenuBar(self.host)
 
-    def createMenus(self, add_menus):
-        _item_tpl = "<img src='media/icons/menu/%s_menu_red.png' />%s"
-        menus_dict = {}
-        menus_order = []
+    def addMenu(self, *args):
+        self.menu_bar.addMenu(*args)
+
+    def addCachedMenus(self, *args):
+        self.menu_bar.addCachedMenus(*args)
 
-        def addMenu(menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd):
-            """ add a menu to menu_dict """
-            log.info("addMenu: %s %s %s %s %s" % (menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd))
-            try:
-                menu_bar = menus_dict[menu_name]
-            except KeyError:
-                menu_bar = menus_dict[menu_name] = MenuBar(vertical=True)
-                menus_order.append((menu_name, menu_name_i18n, icon))
-            if item_name_i18n and menu_cmd:
-                menu_bar.addItem(item_name_i18n, menu_cmd)
-
-        addMenu("General", _("General"), _("Web widget"), 'home', MenuCmd(self, "onWebWidget"))
-        addMenu("General", _("General"), _("Disconnect"), 'home', MenuCmd(self, "onDisconnect"))
-        addMenu("Contacts", _("Contacts"), None, 'social', None)
-        addMenu("Groups", _("Groups"), _("Discussion"), 'social', MenuCmd(self, "onJoinRoom"))
-        addMenu("Groups", _("Groups"), _("Collective radio"), 'social', MenuCmd(self, "onCollectiveRadio"))
-        addMenu("Games", _("Games"), _("Tarot"), 'games', MenuCmd(self, "onTarotGame"))
-        addMenu("Games", _("Games"), _("Xiangqi"), 'games', MenuCmd(self, "onXiangqiGame"))
+    def createMenus(self):
+        self.addMenu("General", _("General"), _("Web widget"), 'home', MenuCmd(self, "onWebWidget"))
+        self.addMenu("General", _("General"), _("Disconnect"), 'home', MenuCmd(self, "onDisconnect"))
+        self.addMenu("Contacts", _("Contacts"), None, 'social', None)
+        self.addMenu("Groups", _("Groups"), _("Discussion"), 'social', MenuCmd(self, "onJoinRoom"))
+        self.addMenu("Groups", _("Groups"), _("Collective radio"), 'social', MenuCmd(self, "onCollectiveRadio"))
+        self.addMenu("Games", _("Games"), _("Tarot"), 'games', MenuCmd(self, "onTarotGame"))
+        self.addMenu("Games", _("Games"), _("Xiangqi"), 'games', MenuCmd(self, "onXiangqiGame"))
 
         # additional menus
-        for action_id, type_, path, path_i18n in add_menus:
-            if not path:
-                log.warning("skipping menu without path")
-                continue
-            if len(path) != len(path_i18n):
-                log.error("inconsistency between menu paths")
-                continue
-            menu_name = path[0]
-            menu_name_i18n = path_i18n[0]
-            item_name = path[1:]
-            if not item_name:
-                log.warning("skipping menu with a path of lenght 1 [%s]" % path[0])
-                continue
-            item_name_i18n = ' | '.join(path_i18n[1:])
-            addMenu(menu_name, menu_name_i18n, item_name_i18n, 'plugins', PluginMenuCmd(self.host, action_id))
+        self.addCachedMenus(C.MENU_GLOBAL)
 
         # menu items that should be displayed after the automatically added ones
-        addMenu("Contacts", _("Contacts"), _("Manage groups"), 'social', MenuCmd(self, "onManageContactGroups"))
+        self.addMenu("Contacts", _("Contacts"), _("Manage groups"), 'social', MenuCmd(self, "onManageContactGroups"))
 
-        menus_order.append(None)  # we add separator
+        self.menu_bar.addSeparator()
 
-        addMenu("Help", _("Help"), _("Social contract"), 'help', MenuCmd(self, "onSocialContract"))
-        addMenu("Help", _("Help"), _("About"), 'help', MenuCmd(self, "onAbout"))
-        addMenu("Settings", _("Settings"), _("Account"), 'settings', MenuCmd(self, "onAccount"))
-        addMenu("Settings", _("Settings"), _("Parameters"), 'settings', MenuCmd(self, "onParameters"))
+        self.addMenu("Help", _("Help"), _("Social contract"), 'help', MenuCmd(self, "onSocialContract"))
+        self.addMenu("Help", _("Help"), _("About"), 'help', MenuCmd(self, "onAbout"))
+        self.addMenu("Settings", _("Settings"), _("Account"), 'settings', MenuCmd(self, "onAccount"))
+        self.addMenu("Settings", _("Settings"), _("Parameters"), 'settings', MenuCmd(self, "onParameters"))
 
         # XXX: temporary, will change when a full profile will be managed in SàT
-        addMenu("Settings", _("Settings"), _("Upload avatar"), 'settings', MenuCmd(self, "onAvatarUpload"))
-
-        menubar = LiberviaMenuBar()
+        self.addMenu("Settings", _("Settings"), _("Upload avatar"), 'settings', MenuCmd(self, "onAvatarUpload"))
 
-        for menu_data in menus_order:
-            if menu_data is None:
-                _separator = MenuItem('', None)
-                _separator.setStyleName('menuSeparator')
-                menubar.addItem(_separator, None)
-            else:
-                menu_name, menu_name_i18n, icon = menu_data
-                menubar.addItem(MenuItem(_item_tpl % (icon, menu_name_i18n), True, menus_dict[menu_name]))
+        self.add(self.menu_bar)
 
-        self.add(menubar)
-
-    #General menu
+    # General menu
     def onWebWidget(self):
         web_panel = panels.WebPanel(self.host, "http://www.goffi.org")
         self.host.addWidget(web_panel)
@@ -262,7 +198,7 @@
         self.menu_settings.removeItem(self.item_params)
 
     def onAvatarUpload(self):
-        body = AvatarUpload()
+        body = file_tools.AvatarUpload()
         _dialog = dialog.GenericDialog("Avatar upload", body, options=['NO_CLOSE'])
         body.setCloseCb(_dialog.close)
         _dialog.setWidth('40%')