Mercurial > libervia-web
comparison browser_side/menu.py @ 336:629c99bbd031
browser + server side: refactored menus:
- getMenus is added to Register class, so it can be used before being logged
- dynamic menus are added to main menu bar
- security limit is used
- menus use i18n
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 04 Feb 2014 16:49:20 +0100 |
parents | 4f221f34bdc7 |
children | 2067d6241927 |
comparison
equal
deleted
inserted
replaced
335:e8c26e24a6c7 | 336:629c99bbd031 |
---|---|
41 from xmlui import XMLUI | 41 from xmlui import XMLUI |
42 import panels | 42 import panels |
43 import dialog | 43 import dialog |
44 from contact_group import ContactGroupEditor | 44 from contact_group import ContactGroupEditor |
45 import re | 45 import re |
46 from sat.core.i18n import _ | |
46 | 47 |
47 | 48 |
48 class MenuCmd: | 49 class MenuCmd: |
49 | 50 |
50 def __init__(self, object, handler): | 51 def __init__(self, object_, handler): |
51 self._object = object | 52 self._object = object_ |
52 self._handler = handler | 53 self._handler = handler |
53 | 54 |
54 def execute(self): | 55 def execute(self): |
55 handler = getattr(self._object, self._handler) | 56 handler = getattr(self._object, self._handler) |
56 handler() | 57 handler() |
58 | |
59 | |
60 class PluginMenuCmd: | |
61 | |
62 def __init__(self, host, action_id): | |
63 self.host = host | |
64 self.action_id = action_id | |
65 | |
66 def execute(self): | |
67 self.host.launchAction(self.action_id, None) | |
57 | 68 |
58 | 69 |
59 class LiberviaMenuBar(MenuBar): | 70 class LiberviaMenuBar(MenuBar): |
60 | 71 |
61 def __init__(self): | 72 def __init__(self): |
136 | 147 |
137 def __init__(self, host): | 148 def __init__(self, host): |
138 self.host = host | 149 self.host = host |
139 SimplePanel.__init__(self) | 150 SimplePanel.__init__(self) |
140 self.setStyleName('menuContainer') | 151 self.setStyleName('menuContainer') |
152 | |
153 def createMenus(self, add_menus): | |
141 _item_tpl = "<img src='media/icons/menu/%s_menu_red.png' />%s" | 154 _item_tpl = "<img src='media/icons/menu/%s_menu_red.png' />%s" |
142 | 155 menus_dict = {} |
143 menu_general = MenuBar(vertical=True) | 156 menus_order = [] |
144 menu_general.addItem("Web widget", MenuCmd(self, "onWebWidget")) | 157 |
145 menu_general.addItem("Disconnect", MenuCmd(self, "onDisconnect")) | 158 def addMenu(menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd): |
146 | 159 """ add a menu to menu_dict """ |
147 menu_contacts = MenuBar(vertical=True) | 160 print "addMenu:",menu_name, menu_name_i18n, item_name_i18n, icon, menu_cmd |
148 menu_contacts.addItem("Add contact", MenuCmd(self, "onAddContact")) | 161 try: |
149 menu_contacts.addItem("Update contact", MenuCmd(self, "onUpdateContact")) | 162 menu_bar = menus_dict[menu_name] |
150 menu_contacts.addItem("Remove contact", MenuCmd(self, "onRemoveContact")) | 163 except KeyError: |
151 menu_contacts.addItem("Manage groups", MenuCmd(self, "onManageContactGroups")) | 164 menu_bar = menus_dict[menu_name] = MenuBar(vertical=True) |
152 | 165 menus_order.append((menu_name, menu_name_i18n, icon)) |
153 menu_group = MenuBar(vertical=True) | 166 menu_bar.addItem(item_name_i18n, menu_cmd) |
154 menu_group.addItem("Discussion", MenuCmd(self, "onJoinRoom")) | 167 |
155 menu_group.addItem("Collective radio", MenuCmd(self, "onCollectiveRadio")) | 168 addMenu("General", _("General"), _("Web widget"), 'home', MenuCmd(self, "onWebWidget")) |
156 | 169 addMenu("General", _("General"), _("Disconnect"), 'home', MenuCmd(self, "onDisconnect")) |
157 menu_games = MenuBar(vertical=True) | 170 addMenu("Contacts", _("Contacts"), _("Add contact"), 'social', MenuCmd(self, "onAddContact")) |
158 menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame")) | 171 addMenu("Contacts", _("Contacts"), _("Update contact"), 'social', MenuCmd(self, "onUpdateContact")) |
159 menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame")) | 172 addMenu("Contacts", _("Contacts"), _("Remove contact"), 'social', MenuCmd(self, "onRemoveContact")) |
160 | 173 addMenu("Contacts", _("Contacts"), _("Manage groups"), 'social', MenuCmd(self, "onManageContactGroups")) |
161 menu_help = MenuBar(vertical=True) | 174 addMenu("Groups", _("Groups"), _("Discussion"), 'social', MenuCmd(self, "onJoinRoom")) |
162 menu_help.addItem("Social contract", MenuCmd(self, "onSocialContract")) | 175 addMenu("Groups", _("Groups"), _("Collective radio"), 'social', MenuCmd(self, "onCollectiveRadio")) |
163 menu_help.addItem("About", MenuCmd(self, "onAbout")) | 176 addMenu("Games", _("Games"), _("Tarot"), 'games', MenuCmd(self, "onTarotGame")) |
164 | 177 addMenu("Games", _("Games"), _("Xiangqi"), 'games', MenuCmd(self, "onXiangqiGame")) |
165 self.menu_settings = MenuBar(vertical=True) | 178 |
166 self.item_params = self.menu_settings.addItem("Parameters", | 179 # additional menus |
167 MenuCmd(self, "onParameters")) | 180 for action_id, type_, path, path_i18n in add_menus: |
181 if not path: | |
182 print "WARNING: skipping menu without path" | |
183 continue | |
184 menu_name = path[0] | |
185 menu_name_i18n = path_i18n[0] | |
186 item_name_i18n = ' | '.join(path_i18n[1:]) | |
187 if not item_name: | |
188 print "WARNING: skipping menu with a path of lenght 1 [%s]" % path[0] | |
189 continue | |
190 addMenu(menu_name, menu_name_i18n, item_name_i18n, 'plugins', PluginMenuCmd(self.host, action_id)) | |
191 | |
192 menus_order.append(None) # we add separator | |
193 | |
194 addMenu("Help", _("Help"), _("Social contract"), 'help', MenuCmd(self, "onSocialContract")) | |
195 addMenu("Help", _("Help"), _("About"), 'help', MenuCmd(self, "onAbout")) | |
196 addMenu("Settings", _("Settings"), _("Parameters"), 'settings', MenuCmd(self, "onParameters")) | |
197 | |
168 # XXX: temporary, will change when a full profile will be managed in SàT | 198 # XXX: temporary, will change when a full profile will be managed in SàT |
169 self.menu_settings.addItem("Upload avatar", MenuCmd(self, "onAvatarUpload")) | 199 addMenu("Settings", _("Settings"), _("Upload avatar"), 'settings', MenuCmd(self, "onAvatarUpload")) |
170 | 200 |
171 menubar = LiberviaMenuBar() | 201 menubar = LiberviaMenuBar() |
172 | 202 |
173 for _name, _icon, _menu in [('General', 'home', menu_general), | 203 for menu_data in menus_order: |
174 ('Contacts', 'social', menu_contacts), | 204 if menu_data is None: |
175 ('Groups', 'social', menu_group), | 205 _separator = MenuItem('', None) |
176 ('Games', 'games', menu_games)]: | 206 _separator.setStyleName('menuSeparator') |
177 menubar.addItem(MenuItem(_item_tpl % (_icon, _name), True, _menu)) | 207 menubar.addItem(_separator, None) |
178 | 208 else: |
179 _separator = MenuItem('', None) | 209 menu_name, menu_name_i18n, icon = menu_data |
180 _separator.setStyleName('menuSeparator') | 210 menubar.addItem(MenuItem(_item_tpl % (icon, menu_name_i18n), True, menus_dict[menu_name])) |
181 menubar.addItem(_separator, None) | |
182 | |
183 for _name, _icon, _menu in [('Help', 'help', menu_help), | |
184 ('Settings', 'settings', self.menu_settings)]: | |
185 menubar.addItem(MenuItem(_item_tpl % (_icon, _name), True, _menu)) | |
186 | 211 |
187 self.add(menubar) | 212 self.add(menubar) |
188 | 213 |
189 #General menu | 214 #General menu |
190 def onWebWidget(self): | 215 def onWebWidget(self): |
333 def gotParams(xmlui): | 358 def gotParams(xmlui): |
334 if not xmlui: | 359 if not xmlui: |
335 return | 360 return |
336 body = XMLUI(self.host, xmlui) | 361 body = XMLUI(self.host, xmlui) |
337 _dialog = dialog.GenericDialog("Parameters", body, options=['NO_CLOSE']) | 362 _dialog = dialog.GenericDialog("Parameters", body, options=['NO_CLOSE']) |
338 body.setCloseCb(_dialog.close) | 363 body.setCloseCb(_dialog.close) |
339 _dialog.setSize('80%', '80%') | 364 _dialog.setSize('80%', '80%') |
340 _dialog.show() | 365 _dialog.show() |
341 self.host.bridge.call('getParamsUI', gotParams) | 366 self.host.bridge.call('getParamsUI', gotParams) |
342 | 367 |
343 def removeItemParams(self): | 368 def removeItemParams(self): |