comparison 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
comparison
equal deleted inserted replaced
493:636b6c477a87 494:5d8632a7bfde
22 log = getLogger(__name__) 22 log = getLogger(__name__)
23 23
24 from sat.core.i18n import _ 24 from sat.core.i18n import _
25 25
26 from pyjamas.ui.SimplePanel import SimplePanel 26 from pyjamas.ui.SimplePanel import SimplePanel
27 from pyjamas.ui.MenuBar import MenuBar
28 from pyjamas.ui.MenuItem import MenuItem
29 from pyjamas.ui.HTML import HTML 27 from pyjamas.ui.HTML import HTML
30 from pyjamas.ui.Frame import Frame 28 from pyjamas.ui.Frame import Frame
31 from pyjamas import Window 29 from pyjamas import Window
32 30
31 from constants import Const as C
33 import jid 32 import jid
34
35 import file_tools 33 import file_tools
36 import xmlui 34 import xmlui
37 import panels 35 import panels
38 import dialog 36 import dialog
39 import contact_group 37 import contact_group
40 38 import base_menu
41 39 from base_menu import MenuCmd
42 class MenuCmd: 40
43 41
44 def __init__(self, object_, handler): 42 class MainMenuBar(base_menu.GenericMenuBar):
45 self._object = object_ 43 """The main menu bar which is displayed on top of the document"""
46 self._handler = handler 44
47 45 ITEM_TPL = "<img src='media/icons/menu/%s_menu_red.png' />%s"
48 def execute(self): 46
49 handler = getattr(self._object, self._handler) 47 def __init__(self, host):
50 handler() 48 base_menu.GenericMenuBar.__init__(self, host, vertical=False)
51 49 self.moved_popup_style = 'menuLastPopup'
52 50
53 class PluginMenuCmd: 51 @classmethod
54 52 def getCategoryHTML(cls, type_, menu_name_i18n):
55 def __init__(self, host, action_id): 53 return cls.ITEM_TPL % (type_, menu_name_i18n)
56 self.host = host 54
57 self.action_id = action_id 55
58 56 class MainMenuPanel(SimplePanel):
59 def execute(self): 57 """Container for the main menu bar"""
60 self.host.launchAction(self.action_id, None)
61
62
63 class LiberviaMenuBar(MenuBar):
64
65 def __init__(self):
66 MenuBar.__init__(self, vertical=False)
67
68 def doItemAction(self, item, fireCommand):
69 MenuBar.doItemAction(self, item, fireCommand)
70 if item == self.items[-1] and self.popup:
71 self.popup.setPopupPosition(Window.getClientWidth() -
72 self.popup.getOffsetWidth() - 22,
73 self.getAbsoluteTop() +
74 self.getOffsetHeight() - 1)
75 self.popup.addStyleName('menuLastPopup')
76
77
78 class AvatarUpload(file_tools.FileUploadPanel):
79 def __init__(self):
80 texts = {'ok_button': 'Upload avatar',
81 '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.',
82 'errback': "Can't open image... did you actually submit an image?",
83 'body_errback': 'Please select another image file.',
84 'callback': "Your new profile picture has been set!"}
85 file_tools.FileUploadPanel.__init__(self, 'upload_avatar', 'avatar_path', 2, texts)
86
87
88 class Menu(SimplePanel):
89 58
90 def __init__(self, host): 59 def __init__(self, host):
91 self.host = host 60 self.host = host
92 SimplePanel.__init__(self) 61 SimplePanel.__init__(self)
93 self.setStyleName('menuContainer') 62 self.setStyleName('menuContainer')
94 63 self.menu_bar = MainMenuBar(self.host)
95 def createMenus(self, add_menus): 64
96 _item_tpl = "<img src='media/icons/menu/%s_menu_red.png' />%s" 65 def addMenu(self, *args):
97 menus_dict = {} 66 self.menu_bar.addMenu(*args)
98 menus_order = [] 67
99 68 def addCachedMenus(self, *args):
100 def addMenu(menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd): 69 self.menu_bar.addCachedMenus(*args)
101 """ add a menu to menu_dict """ 70
102 log.info("addMenu: %s %s %s %s %s" % (menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd)) 71 def createMenus(self):
103 try: 72 self.addMenu("General", _("General"), _("Web widget"), 'home', MenuCmd(self, "onWebWidget"))
104 menu_bar = menus_dict[menu_name] 73 self.addMenu("General", _("General"), _("Disconnect"), 'home', MenuCmd(self, "onDisconnect"))
105 except KeyError: 74 self.addMenu("Contacts", _("Contacts"), None, 'social', None)
106 menu_bar = menus_dict[menu_name] = MenuBar(vertical=True) 75 self.addMenu("Groups", _("Groups"), _("Discussion"), 'social', MenuCmd(self, "onJoinRoom"))
107 menus_order.append((menu_name, menu_name_i18n, icon)) 76 self.addMenu("Groups", _("Groups"), _("Collective radio"), 'social', MenuCmd(self, "onCollectiveRadio"))
108 if item_name_i18n and menu_cmd: 77 self.addMenu("Games", _("Games"), _("Tarot"), 'games', MenuCmd(self, "onTarotGame"))
109 menu_bar.addItem(item_name_i18n, menu_cmd) 78 self.addMenu("Games", _("Games"), _("Xiangqi"), 'games', MenuCmd(self, "onXiangqiGame"))
110
111 addMenu("General", _("General"), _("Web widget"), 'home', MenuCmd(self, "onWebWidget"))
112 addMenu("General", _("General"), _("Disconnect"), 'home', MenuCmd(self, "onDisconnect"))
113 addMenu("Contacts", _("Contacts"), None, 'social', None)
114 addMenu("Groups", _("Groups"), _("Discussion"), 'social', MenuCmd(self, "onJoinRoom"))
115 addMenu("Groups", _("Groups"), _("Collective radio"), 'social', MenuCmd(self, "onCollectiveRadio"))
116 addMenu("Games", _("Games"), _("Tarot"), 'games', MenuCmd(self, "onTarotGame"))
117 addMenu("Games", _("Games"), _("Xiangqi"), 'games', MenuCmd(self, "onXiangqiGame"))
118 79
119 # additional menus 80 # additional menus
120 for action_id, type_, path, path_i18n in add_menus: 81 self.addCachedMenus(C.MENU_GLOBAL)
121 if not path:
122 log.warning("skipping menu without path")
123 continue
124 if len(path) != len(path_i18n):
125 log.error("inconsistency between menu paths")
126 continue
127 menu_name = path[0]
128 menu_name_i18n = path_i18n[0]
129 item_name = path[1:]
130 if not item_name:
131 log.warning("skipping menu with a path of lenght 1 [%s]" % path[0])
132 continue
133 item_name_i18n = ' | '.join(path_i18n[1:])
134 addMenu(menu_name, menu_name_i18n, item_name_i18n, 'plugins', PluginMenuCmd(self.host, action_id))
135 82
136 # menu items that should be displayed after the automatically added ones 83 # menu items that should be displayed after the automatically added ones
137 addMenu("Contacts", _("Contacts"), _("Manage groups"), 'social', MenuCmd(self, "onManageContactGroups")) 84 self.addMenu("Contacts", _("Contacts"), _("Manage groups"), 'social', MenuCmd(self, "onManageContactGroups"))
138 85
139 menus_order.append(None) # we add separator 86 self.menu_bar.addSeparator()
140 87
141 addMenu("Help", _("Help"), _("Social contract"), 'help', MenuCmd(self, "onSocialContract")) 88 self.addMenu("Help", _("Help"), _("Social contract"), 'help', MenuCmd(self, "onSocialContract"))
142 addMenu("Help", _("Help"), _("About"), 'help', MenuCmd(self, "onAbout")) 89 self.addMenu("Help", _("Help"), _("About"), 'help', MenuCmd(self, "onAbout"))
143 addMenu("Settings", _("Settings"), _("Account"), 'settings', MenuCmd(self, "onAccount")) 90 self.addMenu("Settings", _("Settings"), _("Account"), 'settings', MenuCmd(self, "onAccount"))
144 addMenu("Settings", _("Settings"), _("Parameters"), 'settings', MenuCmd(self, "onParameters")) 91 self.addMenu("Settings", _("Settings"), _("Parameters"), 'settings', MenuCmd(self, "onParameters"))
145 92
146 # XXX: temporary, will change when a full profile will be managed in SàT 93 # XXX: temporary, will change when a full profile will be managed in SàT
147 addMenu("Settings", _("Settings"), _("Upload avatar"), 'settings', MenuCmd(self, "onAvatarUpload")) 94 self.addMenu("Settings", _("Settings"), _("Upload avatar"), 'settings', MenuCmd(self, "onAvatarUpload"))
148 95
149 menubar = LiberviaMenuBar() 96 self.add(self.menu_bar)
150 97
151 for menu_data in menus_order: 98 # General menu
152 if menu_data is None:
153 _separator = MenuItem('', None)
154 _separator.setStyleName('menuSeparator')
155 menubar.addItem(_separator, None)
156 else:
157 menu_name, menu_name_i18n, icon = menu_data
158 menubar.addItem(MenuItem(_item_tpl % (icon, menu_name_i18n), True, menus_dict[menu_name]))
159
160 self.add(menubar)
161
162 #General menu
163 def onWebWidget(self): 99 def onWebWidget(self):
164 web_panel = panels.WebPanel(self.host, "http://www.goffi.org") 100 web_panel = panels.WebPanel(self.host, "http://www.goffi.org")
165 self.host.addWidget(web_panel) 101 self.host.addWidget(web_panel)
166 self.host.setSelected(web_panel) 102 self.host.setSelected(web_panel)
167 103
260 def removeItemParams(self): 196 def removeItemParams(self):
261 """Remove the Parameters item from the Settings menu bar.""" 197 """Remove the Parameters item from the Settings menu bar."""
262 self.menu_settings.removeItem(self.item_params) 198 self.menu_settings.removeItem(self.item_params)
263 199
264 def onAvatarUpload(self): 200 def onAvatarUpload(self):
265 body = AvatarUpload() 201 body = file_tools.AvatarUpload()
266 _dialog = dialog.GenericDialog("Avatar upload", body, options=['NO_CLOSE']) 202 _dialog = dialog.GenericDialog("Avatar upload", body, options=['NO_CLOSE'])
267 body.setCloseCb(_dialog.close) 203 body.setCloseCb(_dialog.close)
268 _dialog.setWidth('40%') 204 _dialog.setWidth('40%')
269 _dialog.show() 205 _dialog.show()